所以我认为发生了什么是我的头节点不断被覆盖,但我不知道为什么。如果我删除while循环,并且只是放入这样的东西,它的工作就好了。虽然循环覆盖C链接列表的头节点在C
head = addItem(head, "item one");
head = addBack(head, "item two");
print(head);
下面是代码,因为它是现在,下面我将包含头函数与函数。请注意,我所有的菜单项目都没有在循环中,一直在坚持让链接列表正确一段时间。预先感谢您提供任何提示或建议。
主营:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "functions.h"
int menu();
void print(node *head);
node* addItem(node *head, char *item);
node* addBack(node *head, char *item);
int main()
{
int selection;
char *item;
node* head = NULL;
char junk;
// Run the menu, and do something based on the selection
do {
selection = menu();
// If they choose a number that's not between 1 and 3, or 0
if (selection > 3) {
printf("Please select a valid option\n");
}
// If they enter 1, add an item to the list
else if (selection == 1) {
printf("Enter your list item: ");
// scanf leftover characters so fgets will work
scanf("%c", &junk);
fgets(item, 100, stdin);
if (head == NULL) {
head = addItem(head, item);
}
else if (head != NULL) {
addBack(head, item);
}
}
else if (selection == 3) {
// Print remaining items
print(head);
}
} while (selection != 0);
return 0;
}
编辑:忘了补充在头文件的功能。
struct node
{
char *item;
struct node *next;
};
typedef struct node node;
// Menu of choices, returns selection
int menu()
{
int selection;
printf("\nChoose an option:\n1: Enter a list item \
\n2: Delete a list item\n3: Print remaining items \
\n0: Quit\n\n");
scanf("%d", &selection);
return selection;
}
node* addItem(node *head, char *item)
{
node *tmp;
tmp = malloc(sizeof(node));
tmp->item = item;
tmp->next = head;
head = tmp;
return head;
}
node* addBack(node *head, char *item)
{
node *tmp, *p;
tmp = malloc(sizeof(node));
tmp->item = item;
p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = tmp;
tmp->next = NULL;
return head;
}
void print(node* head)
{
node *tmp;
tmp = head;
if (tmp == NULL) {
printf("Add an item first, list is empty\n");
exit(0);
}
while(tmp != NULL)
{
printf("%s\n ", tmp->item);
tmp = tmp->next;
}
}
愚蠢的问题:如果选择== 2? – clusterdude