关于链表的问题,高手请指教!!!!
先上源码:
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct people)
struct people
{int num;
char name[20];
struct people *next;
};
int n;
struct people *creat(void)//建立链表
{struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people*)malloc(LEN);//开辟一个新单元
scanf("%d%s",&p1->num,&p1->name);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%d%s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}
struct people *del(struct people *head,int num)//删除结点
{struct people *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{ p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
n=n-1;
}
end:
return(head);}
void main()
{
struct people *head1,*head2,*p;
int num;
printf("输入链表a:\n");
head1=creat();
printf("输入链表b:\n");
head2=creat();
loop:for(;head1->num==head2->num;head2=head2->next)
{
num=head1->num;
head1=del(head1,num);
}
if(head1->num!=head2->num)
{
head1=head1->next;
goto loop;
}
printf("处理后的a链表为:\n");//编译能通过,运行到这时就出现程序终止???
p=head1;
if(head1!=NULL)
do
{
printf("%d %s\n",p->num,p->name);
p=p->next;
}
while(p!=NULL);
}
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct people)
struct people
{int num;
char name[20];
struct people *next;
};
int n;
struct people *creat(void)//建立链表
{struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people*)malloc(LEN);//开辟一个新单元
scanf("%d%s",&p1->num,&p1->name);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%d%s",&p1->num,&p1->name);
}
p2->next=NULL;
return(head);
}
struct people *del(struct people *head,int num)//删除结点
{struct people *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{ p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
n=n-1;
}
end:
return(head);}
void main()
{
struct people *head1,*head2,*p;
int num;
printf("输入链表a:\n");
head1=creat();
printf("输入链表b:\n");
head2=creat();
loop:for(;head1->num==head2->num;head2=head2->next)
{
num=head1->num;
head1=del(head1,num);
}
if(head1->num!=head2->num)
{
head1=head1->next;
goto loop;
}
printf("处理后的a链表为:\n");//编译能通过,运行到这时就出现程序终止???
p=head1;
if(head1!=NULL)
do
{
printf("%d %s\n",p->num,p->name);
p=p->next;
}
while(p!=NULL);
}
...全文
相关推荐
用C语言和链表编写的大数相加程序
用C语言和链表编写的大数相加程序,想学链表的不要错过,程序里的不足请多指教
单链表 初始化 创建 输出
单链表 初始化 创建 输出 新手写的 请大家多多指教
Josephus问题C++单循环链表实现
**问题解决步骤**1. **初始化链表**:首先创建一个单循环链表,每个节点代表一个参与者,节点中的数据可以是参与者的编号或其它标识。2.
c++循环链表解决约瑟夫环问题
### 约瑟夫环问题及其C++循环链表实现#### 问题背景与描述约瑟夫环问题源于古罗马历史的一个故事,后来成为计算机科学中的一个经典问题。
C语言加链表编写的大数减法
用C写的大数减法程序,整个程序用的是链表,有些不足之处请指教


