【发布时间】:2021-01-18 15:25:12
【问题描述】:
我无法在单循环链表中插入和显示,它只插入第一个元素,与显示功能相同
void create() {
struct node *temp , *p;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data :");
scanf("%d",&(temp->data));
temp->next = NULL;
if(root == NULL) {
root = p = temp;
}
else {
while(p->next!=root) {
p->next = temp;
p = temp;
p->next = root;
}
}
}
void display() {
struct node *temp;
if(root == NULL) {
printf("List is Empty\n");
}
else {
temp = root;
while(temp->next!=root) {
printf("%d",temp->data);
temp = temp->next;
}
printf("%d",temp->next);
}
}
【问题讨论】:
-
等10分钟我来解决这个问题
-
在
create中,p永远不会在root != NULL获得值。不幸的是,p仅在这种情况下使用,具有未定义的值。 -
你能放下你的代码吗?这样我就可以完全理解了
标签: c data-structures linked-list