我的主要是:
void main()
{
int flag = 1;
LinkedList *list = NULL;
list = makeList();
while (flag) {
add_last(list, makeNode(nextKey(), "uninitialized"));
printf("please enter 0 to stop any other number to go on \n");
scanf("%d",&flag);
}
printKeys(list);
}
我有 2 个定义节点和列表的结构:
typedef struct item{
int data;
char *charData;
struct item *next;
}item;
typedef struct{
struct item *head;
}LinkedList;
我通过函数创建列表:
LinkedList *makeList(){
LinkedList *head = (LinkedList*)malloc(sizeof(LinkedList));
return head;
}
以及函数的节点:
item *makeNode(int key, char* data){
item *newItem = (item*)malloc(sizeof(item));
if (newItem != NULL) {
newItem->data = key;
newItem->next = NULL;
}
return newItem;
}
现在我需要编写 2 个函数,第一个用于在列表末尾添加新项目,第二个用于打印列表。
签署我的第一个函数是:
void add_last(LinkedList *list, item *newItem){
}
第二个是:
void printKeys(LinkedList *list){
}
我是“C”世界的新手,我不知道该怎么做。 我不明白如何访问该列表。
谢谢...
最佳答案
printKeys
函数应迭代节点,直到找到 next
为 null
的一个节点。通过这样做,应该打印 key
字段。 add_last
函数也许应该迭代,直到找到最后一个节点,然后将最后一个节点的 next
字段设置为 newItem
。
关于c - 如何在链接列表中添加和打印项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22638213/