查看底部的最后一个函数 i_at()。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define nl printf("\n")
#define pf printf
#define si(a) scanf("%d",&a)
#define sc(a) scanf("%c",&a);
struct node{
int val;
struct node *next;
};
struct node *first,*last,*damn,*temp,*prev,*newnode;
void i_beg();
void i_end();
void display();
void del();
void i_at();
struct node *create(int);
main()
{
int val,i,j; char key;val=1;
while(val<6 && val>0){
pf("\n1.Start \t2.End \t3.Display\t4.Delete\t5.Insert\t6.Exit\n------------------------------------------------\n");
si(val);
if(val==6)exit(0);
else{
switch(val){
case 1: i_beg(); break;
case 2: i_end(); break;
case 3: display();break;
case 4: del(); break;
case 5: i_at();break;
case 6: exit(0);break;
}
}
}
}
struct node * create(int val){
newnode = (struct node *)malloc(sizeof( struct node));
if(newnode == NULL)
{
pf("Oops! You have gone out of memory\n");
return 0;
}
else
{
newnode -> val = val;
newnode -> next = NULL;
return newnode;
}
}
void i_beg(){
int x;
//if(first -> val == last -> val && first == NULL) pf("Only one node exists!!!\n ");
pf("Enter a value to insert\t");
si(x); newnode = create(x);
if(first == last && first == NULL)
{
first = last = newnode;
first -> next = NULL;
last -> next = NULL;
pf("\n %d inserted at first\n",x);
}
else
{
temp = first;
first = newnode;
first -> next = temp;
pf("\n %d inserted at first\n",x);
}
}
void i_end(){
int x;
//if(first -> val == last -> val && first == NULL) pf("Only one node exists!!!\n ");
pf("Enter a value to insert\t");
si(x); newnode = create(x);
if(first == last && first == NULL)
{
first = last = newnode;
first -> next = NULL;
last -> next = NULL;
pf("\n %d inserted at last\n",x);
}
else
{
last -> next = newnode;
last = newnode;
last -> next = NULL;
pf("\n %d inserted at last\n",x);
}
}
void display(){
prev = first;
while(prev != NULL)
{
pf("%d -> ",prev -> val);
prev = prev -> next;
}pf(" NULL");
nl;
}
void del(){
pf("Enter the position to delete\t");
int i,pos;si(pos);
for(prev = first,i=1 ; i<pos-1 ; prev=prev->next,i++);// not i< pos but i<pos-1
temp = prev -> next;
prev -> next = temp->next;
}
void i_at(){
pf("Enter the position to add after this position\t");
int i,pos;si(pos);
for(prev = first,i=1 ; i<pos ; prev=prev->next,i++);
pf("Enter a value to insert\t");
si(i); newnode = create(i);
temp = prev;
// order of these statements is important
newnode -> next = temp -> next;
prev -> next = newnode;
pf("%d inserted after position %d\n",i,pos);
}
如果我把这些行写成
prev -> next = newnode;
newnode -> next = temp -> next;
那么 display() 函数出错了。但是如果我将这两行的顺序颠倒为
newnode -> next = temp -> next;
prev -> next = newnode;
然后代码正在运行。这只是将新节点链接到链表的问题。那么,如果我把代码中显示的那两行的顺序交换了,为什么代码是错误的?
例如:要将 b 连接到 a&c,连接 (b->c first, a->b second)OR(a->b first, b->c second) 无关紧要。
错误在哪里?
最佳答案
当你这样做时:
temp = prev;
您没有复制结构,您只是将名称
prev
与名称 temp
别名化,因为两者都是指针。所以当你这样做时:
prev -> next = newnode;
newnode -> next = temp -> next;
您正在更改
next
和 prev
的字段 temp
的值,第二条语句不正确。在另一种情况下,更新顺序是正确的。
关于c - 链表 - 在中间插入,链接新节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34449537/