主题:诚问大家链表的问题,请大家帮忙
以下是我写的链表程序,主要的功能是删除某个节点
我有几个问题想请大家帮帮忙:
1,这个程序能否再优化一些
2,我想同时实现删除开头NODE或者删除末端NODE在删除函数中应该如何更改?
3,在删除函数中,我的位置标记是变量F,为什么非得等于2才能正确删除?
4,在创建NODE中的WHILE(1)是什么意思?我尝试过其他的值也能通过(唯一除了0),比如我把WHILE(1)改成WHILE(5)甚至WHILE(-1)都可以,是怎么回事?
纯为自己把数据结构弄得一清二楚,如哪位朋友有时间,恳求回答.
typedef struct node
{
int info;
struct node *link;
}n;
void create(n *start)
{
n *p;
int item;
char ch;
while(1)
{
printf("\n\nEnter the element: ");
scanf("%d",&item);
p->info=item;
printf("\n\nContinune?(y/n)");
fflush(stdin);
ch=getchar();
if(ch=='y')
{
start=(n*)malloc(sizeof
(n));
p->link=start;
p=start;
}
else
{
p->link='\0';
return;
}
}
}
void display(n *p)
{
while(p!=NULL)
{
printf("%d",p->info);
printf(" -> ");
p=p->link;
}
printf("NULL");
}
void dele(n *p)
{
int f=2,c,pos,item;
n *ga,*p3;
printf("\n\nDo you want delete from the position or
item?\n\n");
printf("position:1 item:2 \n\n Please choice(1/2):
");
scanf("%d",&c);
if(c==1)
{
printf("\n\nEnter the position:");
scanf("%d",&pos);
while(p!=NULL)
{
if(f==pos)
{
p3=p->link;
p->link=p3->link;
p3->link=NULL;
ga=(n*)malloc(sizeof(n));
p3=ga;
break;
}
else
{
p=p->link;
f++;
}
if(p==NULL)
break;
}
}
if(c==2)
{
printf("\n\nEnter the item:");
scanf("%d",&item);
while(p!=NULL)
{
if(p->info==item)
{
p3=p->link;
p->link=NULL;
ga=(n*)malloc(sizeof(n));
p3=ga;
break;
}
else
{
p=p->link;
f++;
}
if(p==NULL)
break;
}
}
}
void main()
{
n *start;
int item,pos;
start=(n*)malloc(sizeof(n));
clrscr();
printf("Creating a singly linked list: \n\n");
create(start);
printf("\n\nThe linked list created
is:\n\n");
display(start);
printf("\n\nEnter a item to search: ");
scanf("%d",&item);
search(start,item);
printf("\n\nEnter the position of new node
to be inserted: ");
scanf("%d",&pos);
printf("\nThe oringnal linked list is:\n\n");
display(start);
insert(start,pos);
printf("\n\nThe linked list after inserted
is:\n\n");
display(start);
printf("\n\nDelete the node from the new
linked list:\n\n");
dele(start);
printf("\n\nThe linked list after deleted
is:\n\n");
display(start);
getch();
}
我有几个问题想请大家帮帮忙:
1,这个程序能否再优化一些
2,我想同时实现删除开头NODE或者删除末端NODE在删除函数中应该如何更改?
3,在删除函数中,我的位置标记是变量F,为什么非得等于2才能正确删除?
4,在创建NODE中的WHILE(1)是什么意思?我尝试过其他的值也能通过(唯一除了0),比如我把WHILE(1)改成WHILE(5)甚至WHILE(-1)都可以,是怎么回事?
纯为自己把数据结构弄得一清二楚,如哪位朋友有时间,恳求回答.
typedef struct node
{
int info;
struct node *link;
}n;
void create(n *start)
{
n *p;
int item;
char ch;
while(1)
{
printf("\n\nEnter the element: ");
scanf("%d",&item);
p->info=item;
printf("\n\nContinune?(y/n)");
fflush(stdin);
ch=getchar();
if(ch=='y')
{
start=(n*)malloc(sizeof
(n));
p->link=start;
p=start;
}
else
{
p->link='\0';
return;
}
}
}
void display(n *p)
{
while(p!=NULL)
{
printf("%d",p->info);
printf(" -> ");
p=p->link;
}
printf("NULL");
}
void dele(n *p)
{
int f=2,c,pos,item;
n *ga,*p3;
printf("\n\nDo you want delete from the position or
item?\n\n");
printf("position:1 item:2 \n\n Please choice(1/2):
");
scanf("%d",&c);
if(c==1)
{
printf("\n\nEnter the position:");
scanf("%d",&pos);
while(p!=NULL)
{
if(f==pos)
{
p3=p->link;
p->link=p3->link;
p3->link=NULL;
ga=(n*)malloc(sizeof(n));
p3=ga;
break;
}
else
{
p=p->link;
f++;
}
if(p==NULL)
break;
}
}
if(c==2)
{
printf("\n\nEnter the item:");
scanf("%d",&item);
while(p!=NULL)
{
if(p->info==item)
{
p3=p->link;
p->link=NULL;
ga=(n*)malloc(sizeof(n));
p3=ga;
break;
}
else
{
p=p->link;
f++;
}
if(p==NULL)
break;
}
}
}
void main()
{
n *start;
int item,pos;
start=(n*)malloc(sizeof(n));
clrscr();
printf("Creating a singly linked list: \n\n");
create(start);
printf("\n\nThe linked list created
is:\n\n");
display(start);
printf("\n\nEnter a item to search: ");
scanf("%d",&item);
search(start,item);
printf("\n\nEnter the position of new node
to be inserted: ");
scanf("%d",&pos);
printf("\nThe oringnal linked list is:\n\n");
display(start);
insert(start,pos);
printf("\n\nThe linked list after inserted
is:\n\n");
display(start);
printf("\n\nDelete the node from the new
linked list:\n\n");
dele(start);
printf("\n\nThe linked list after deleted
is:\n\n");
display(start);
getch();
}