《程序设计基础》上机实验报告
学号:201420224912
姓名:许培鑫
班级:计算机149
日期:2015-5-26
指导教师:成筠
成绩:__________
(1)有两个链表,其结点包括学号、成绩。要求合并两个链表并按学号升序排列。
1、实验内容:
#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student{
int studid;
int score;
struct student *next;
};
struct student *head ;
struct student *head1;
struct student *create()
{
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head=NULL;
printf("请输入学生的学号和成绩:");
scanf("%d %d",&(p1->studid),&(p1->score));
while(p1->studid!=0)
{
if(head==NULL)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入学生的学号和成绩,当学生学号为0时结束输入:");
scanf("%d %d",&(p1->studid),&(p1->score));
}
p2->next=NULL;
return head;
}
struct student *create1()
{
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head1=NULL;
printf("请输入学生的学号和成绩:");
scanf("%d %d",&(p1->studid),&(p1->score));
while(p1->studid!=0)
{
if(head1==NULL)
{
head1=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入学生的学号和成绩,当学生学号为0时结束输入:");
scanf("%d %d",&(p1->studid),&(p1->score));
}
p2->next=NULL;
return head1;
}
void Print(struct student *h)
{
struct student * p;
p=h;
//判断链表是否为空
if(NULL==h)
{
printf("链表为空!\n");
}
else
{
printf("studid score\n");
while(p!=NULL)
{
printf("%d %11d\n",p->studid,p->score);
//指针指向下一个节点
p=p->next;
}
}
}
main()
{
int i,j,count=1,temp;
struct student *p,*p1,*head2;
p=create();
printf("请输入第二个链表\n");
p1=create1();
while(p->next!=NULL)
{
p=p->next;
}
p->next=p1;
printf("合并后的链表是\n");
Print(head);
p1=head;
while(p1->next!=NULL)
{
count=count + 1;
p1=p1->next;
}
for(p=head;p!=NULL;p=p->next)
for(p1=p->next;p1!=NULL;p1=p1->next)
{
if((p->studid)>(p1->studid))
{
temp=p->studid;
p->studid=p1->studid;
p1->studid=temp;
}
}
printf("排序后是\n");
Print(head);
}
1、运行结果:
(2)链表的结点包括学号、姓名。从键盘输入一个学号,如果该学号与链表中某一结点的学号相同,则删去该结点。
2、实验内容:
#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student{
int studid;
char name[20];
struct student *next;
};
struct student *head ;
struct student *create()
{
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head=NULL;
printf("请输入学生的学号和姓名:");
scanf("%d %s",&(p1->studid),p1->name);
while(p1->studid!=0)
{
if(head==NULL)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入学生的学号和姓名,当学生学号为0时结束输入:");
scanf("%d %s",&(p1->studid),(p1->name));
}
p2->next=NULL;
return head;
}
struct student *Delete(struct student * head,int num)
{
struct student *p1;
struct student *p2;
p1=head;
//判断链表是否为空
if(NULL==head)
{
printf("链表为空!\n");
return head;
}
//遍历节点,判断当前节点是不是需要删除的节点及是否为尾节点
//如果找到相应节点,或者已经遍历到尾节点就跳出循环
while(p1->studid!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
//判断是否找到相应节点
if(p1->studid==num)
{
//要删除的节点是不是链表的第一个节点
//如果是,就将头指针指向该节点的后一个节点
//如果不是,就将该节点的前一个节点的指针指向该节点的后一个节点
if(head==p1)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("学号为%d 的节点已删除.\n",num);
}
else
{
printf("链表中没有要删除的元素.\n");
}
return head;
}
void Print(struct student *h)
{
struct student * p;
p=h;
//判断链表是否为空
if(NULL==h)
{
printf("链表为空!\n");
}
else
{
printf("学号 姓名\n");
while(p!=NULL)
{
printf("%d %5s\n",p->studid,p->name);
//指针指向下一个节点
p=p->next;
}
}
}
main()
{
int i,j,count=1,temp;
struct student *p,*p1,*head2;
p=create();
printf("输入你要删除的学号\n");
scanf("%d",&i);
printf("未删除时的链表\n");
Print(head);
Delete(p,i);printf("删除后的链表\n");
Print(head);
}
2、运行结果:
(3)有a、b两个链表,其结点包括学号、姓名。要求从a链表中删去与b链表中有相同学号的结点。
3、实验内容:
#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student{
int studid;
char name[20];
struct student *next;
};
struct student *head ;
struct student *head1;
struct student *create()
{
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head=NULL;
printf("请输入学生的学号和姓名:");
scanf("%d %s",&(p1->studid),(p1->name));
while(p1->studid!=0)
{
if(head==NULL)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入学生的学号和姓名,当学生学号为0时结束输入:");
scanf("%d %s",&(p1->studid),(p1->name));
}
p2->next=NULL;
return head;
}
struct student *create1()
{
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head1=NULL;
printf("请输入学生的学号和姓名:");
scanf("%d %s",&(p1->studid),&(p1->name));
while(p1->studid!=0)
{
if(head1==NULL)
{
head1=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入学生的学号和姓名,当学生学号为0时结束输入:");
scanf("%d %s",&(p1->studid),p1->name);
}
p2->next=NULL;
return head1;
}
struct student *Delete(struct student * head,int num)
{
struct student *p1;
struct student *p2;
p1=head;
if(NULL==head)
{
printf("链表为空!\n");
return head;
}
while(p1->studid!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->studid==num)
{
if(head==p1)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
}
return head;
}
void Print(struct student *h)
{
struct student * p;
p=h;
//判断链表是否为空
if(NULL==h)
{
printf("链表为空!\n");
}
else
{
printf("studid name\n");
while(p!=NULL)
{
printf("%d %11s\n",p->studid,p->name);
//指针指向下一个节点
p=p->next;
}
}
}
main()
{
int i=-1;
struct student *p,*p1,*head2,*p3;
printf("请输入第一个链表\n");
p=create();
printf("请输入第二个链表\n");
p3=create1();
Print(head);
for(;p!=NULL;p=p->next)
for(p1=p3;p1!=NULL;p1=p1->next)
{
if((p->studid)==(p1->studid))
{
i=p->studid;
printf("%3d",i);
}
if(i!=-1)
{
Delete(head,i);
}
}
printf("处理后的链表的\n");
Print(head);
}
3、实验结果:
(4)将一个链表按逆序排列,即链头作链尾,链尾作链头。
4、实验内容
#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student{
int studid;
char name[20];
struct student *next;
};
struct student *head ;
struct student *create()
{
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head=NULL;
printf("请输入学生的学号和姓名:");
scanf("%d %s",&(p1->studid),p1->name);
while(p1->studid!=0)
{
if(head==NULL)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入学生的学号和姓名,当学生学号为0时结束输入:");
scanf("%d %s",&(p1->studid),(p1->name));
}
p2->next=NULL;
return head;
}
struct student *reverse(struct student *p)
{
struct student *p1,*p2,*hea;
p1=p2=hea=p;
while (p!=NULL)
{
p=p->next;
if(p1==hea)
p1->next=NULL;
else
p1->next=p2;
p2=p1;
p1=p;
}
hea=p2;
return hea;
}
void Print(struct student *h)
{
struct student * p;
p=h;
if(NULL==h)
{
printf("链表为空!\n");
}
else
{
printf("学号 姓名\n");
while(p!=NULL)
{
printf("%d %5s\n",p->studid,p->name);
p=p->next;
}
}
}
main()
{
struct student *p,*p1,*head2;
p=create();
printf("原链表\n");
Print(p);
p=reverse(p);
printf("倒序后的链表\n");
Print(p);
}
4、运行结果