l链表的综合操作
2007-04-15 11:45
351 查看
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
long score;
struct student *next;
};
main(){
int j,listnum,delnum,n;
int num1=1000,score1=2000;
struct student *head,*p1,*p2;
/*生成链表。。。*/
printf("/nput in the length of the list: ");
scanf("%d",&listnum);
for(n=1;n<=listnum;n++){
p1=(struct student*)malloc(LEN);
p1->num=num1++; p1->score=score1++ ;
if(n==1) head=p2=p1;
else {p2->next=p1;p2=p1;}
}
p2->next=NULL;
printf("head: %0x,last:%0x/n",head,p1);/*链表生成结束*/
/*输出链表*/
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n,p1=p1->next){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
}
/*删除一个节点*/
p1=head;
n=1;
do{
printf("/ndelete:put in the right order:from %d to %d/n",1,listnum);
scanf("%d",&j);
}while(j<1||j>listnum);/*从 1到listnum 有效*/
while(n<j){/*查找第j个元素,当n等于j时,跳出循环*/
p2=p1;p1=p1->next;++n;
}
if(j==1){
head=p1->next;
printf("the num:%ld,the score:%ld/n",p1->num,p1->score);
}
else{
p2->next=p1->next;
printf("/nyou have delete : num:%ld, score:%ld/n",p1->num,p1->score);
free(*p1);
}
--listnum;/*删除节点结束*/
/*输出节点*/
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
p1=p1->next;
}
/*插入一个节点*/
do{
printf("/n insert: put in the right order: from %d to %d/n",1,listnum+1);
scanf("%d",&j);
}while(j<1||j>listnum+1);/*从 1到listnum+1 有效*/
p1=head;
n=1;
while(n<j){p2=p1;p1=p1->next;n++;}
p1=(struct student*)malloc(LEN);
p1->num=num1++;p1->score=score1++;
if(j==1)
{p1->next=head;head=p1;}
else
{p1->next=p2->next;p2->next=p1;}
++listnum;/*插入节点结束*/
/*输出链表*/
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n ){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
p1=p1->next;
}
scanf("%d",&j);
}
#define LEN sizeof(struct student)
struct student
{
long num;
long score;
struct student *next;
};
main(){
int j,listnum,delnum,n;
int num1=1000,score1=2000;
struct student *head,*p1,*p2;
/*生成链表。。。*/
printf("/nput in the length of the list: ");
scanf("%d",&listnum);
for(n=1;n<=listnum;n++){
p1=(struct student*)malloc(LEN);
p1->num=num1++; p1->score=score1++ ;
if(n==1) head=p2=p1;
else {p2->next=p1;p2=p1;}
}
p2->next=NULL;
printf("head: %0x,last:%0x/n",head,p1);/*链表生成结束*/
/*输出链表*/
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n,p1=p1->next){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
}
/*删除一个节点*/
p1=head;
n=1;
do{
printf("/ndelete:put in the right order:from %d to %d/n",1,listnum);
scanf("%d",&j);
}while(j<1||j>listnum);/*从 1到listnum 有效*/
while(n<j){/*查找第j个元素,当n等于j时,跳出循环*/
p2=p1;p1=p1->next;++n;
}
if(j==1){
head=p1->next;
printf("the num:%ld,the score:%ld/n",p1->num,p1->score);
}
else{
p2->next=p1->next;
printf("/nyou have delete : num:%ld, score:%ld/n",p1->num,p1->score);
free(*p1);
}
--listnum;/*删除节点结束*/
/*输出节点*/
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
p1=p1->next;
}
/*插入一个节点*/
do{
printf("/n insert: put in the right order: from %d to %d/n",1,listnum+1);
scanf("%d",&j);
}while(j<1||j>listnum+1);/*从 1到listnum+1 有效*/
p1=head;
n=1;
while(n<j){p2=p1;p1=p1->next;n++;}
p1=(struct student*)malloc(LEN);
p1->num=num1++;p1->score=score1++;
if(j==1)
{p1->next=head;head=p1;}
else
{p1->next=p2->next;p2->next=p1;}
++listnum;/*插入节点结束*/
/*输出链表*/
p1=head;
printf("/nnow the new list is:/n");
for(n=1;n<=listnum;++n ){
printf("/nn=%d,num=%ld,score=%ld/n",n,p1->num,p1->score);
p1=p1->next;
}
scanf("%d",&j);
}
相关文章推荐
- 单链表综合操作
- 链表的综合操作(重新整理,模块一点)
- 关于链表的综合操作
- 对链表的综合操作
- 链表操作综合练习(创建、拆分、插入、删除、合并、打印、逆置)
- 对单向链表的综合操作
- 单链表综合操作
- P305_1111链表的综合操作,建表、…
- 【nginx】双向队列(ngx_queue.h)的合并队列(链表) ngx_queue_add操作
- 双向链表的删除操作
- 链表简单操作
- 16.单向链表的一些基本操作实现(链表反转,链表有环无环判断,链表冒泡排序,链表快速排序)
- [转载] 单链表的相关操作
- 链表的各种操作
- 数据结构上机测试2-2:单链表操作B
- Java单链表增删改查反转基本操作
- 利用application,cookies,sessino以及文件文件操作制作计数器和投票的综合实例(按学习进程更新)
- 中易云串口服务器对接物联网综合管控云平台—易云系统操作演示
- 单链表的基本操作
- 链表的一些操作---面试必备