【发布时间】:2016-03-30 16:57:27
【问题描述】:
查看底部的最后一个函数 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 先,a->b 第二)OR(a->b 先,b->c 第二) 并不重要。 哪里错了?
【问题讨论】:
-
因为
temp和prev都指向同一个对象。 -
我看到您已将其标记为 c++ 而不是 c。 +1 只选择一个,但是如果您使用的是 c++,为什么您使用 malloc 而不是 new?
-
对于链表来说,首先在纸上完成所有操作,然后在代码中实现这些操作,通常会有很大帮助。
-
@MartinBonner:从他使用的函数和标头来看,我认为他使用的是 C。
标签: c linked-list nodes