這裏是我的鏈接列表包含一串鑰匙版本,持有內容的字符串表示:不知道爲什麼我的代碼沒有添加和打印我的鏈接列表?
struct node{
char key[10];
char content;
struct node *next;
};
struct node *head=(struct node *) NULL;
struct node *tail=(struct node *) NULL;
struct node * initinode(char *key, char content)
{
struct node *ptr;
ptr = (struct node *) calloc(1, sizeof(struct node));
if(ptr == NULL)
return (struct node *) NULL;
else {
strcpy(ptr->key, key);
ptr->content = content;
return ptr;
}
}
void printnode(struct node *ptr)
{
printf("Key ->%s\n", ptr->key);
printf("Contents ->%d\n", ptr->content);
}
void printlist(struct node *ptr)
{
while(ptr != NULL)
{
printnode(ptr);
ptr = ptr->next;
}
}
void add(struct node *new)
{
if(head == NULL)
head = new;
tail->next = new;
tail->next = NULL;
tail= new;
}
struct node * searchname(struct node *ptr, char *key)
{
while(strcmp(key, ptr->key) != 0) {
ptr = ptr->next;
if(ptr == NULL)
break;
}
return ptr;
}
//-----------------------------add to the list number of files and print list
int file_count = 0;
DIR * dirp;
struct dirent * entry;
dirp = opendir(cwd);
while ((entry = readdir(dirp)) != NULL)
{
if (entry->d_type == DT_REG) { /* If the entry is a regular file */
file_count++;
}
}
printf("%d \n",file_count);
char file=(char)file_count;
closedir(dirp);
ptr=initinode(files, file);
add(ptr);
printlist(head);
//-----------------------------------casting
在addtion到這個問題,我想不同的數據類型在它的字符串表示形式添加到我的名單。我想嘗試將它轉換爲字符串,但似乎我用於此的方法不適用於其他方法。如果你建議將一個無效的dataype潛入列表中,請詳細解釋。
謝謝
關鍵字請修正對這個職位的格式。這是不可能讀的。 – 2013-02-25 23:51:07
好的確定對不起,這個即時通訊新的到這個網站 – 2013-02-26 00:01:51