当前位置:文档之家› C语言程序设计 实验11.2_单向链表

C语言程序设计 实验11.2_单向链表

实验11.2 单向链表
序号 题目名称 题目满分 题目得分 题目编号
1 调试示例error11_3.cpp
2 编程题
3 编程题
4 编程题
5 编程题
6 编程题
1 调试示例error11_3.cpp
/*----程序填空,不要改变与输入输出有关的语句。输入若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束,用单向链表组织这些学生信息后,再按顺序输出。
输入: 输出:
1 zhang 78 1 zhang 78
2 wang 80 2 wang 80
3 li 75 3 li 75
4 zhao 85 4 zhao 85
0
-----*/
#include
#include
#include
struct stud_node{
int num;
char name[20];
int score;
struct stud_node *next;
};
void main()
{
struct stud_node *head,*tail, *p;
int num, score;
char name[20];
int size = sizeof(struct stud_node);

head=tail=NULL;
scanf("%d",&num);
while(num != 0){
scanf("%s%d",name,&score); /* ??*/
p=(struct stud_node*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}

for(p=head; p!=NULL; p=p->next)
printf("%d %s %d\n", p->num,p->name,p->score);
}

2 编程题
/*----程序填空,不要改变与输入输出有关的语句。输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。提示:
定义函数struct stud_node *Creat_Stu_Doc()完成创建链表
定义函数struct stud_node *DeleteDoc(struct stud_node *head,int min_score)将分数低于min_score的结点删除
定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表
输入:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
输出:
2 wang 80
4 zhao 85
-----*/
#include
#include
#include
struct stud_node{
int num;
char name[20];
int score;
struct stud_node *next; /*定义结构类型变量 stud_node为全局变量*/
};
struct stud_node *Creat_Stu_Doc();
struct stud_node *DeleteDoc(struct stud_node *head,int min_score);
void Ptrint_Stu_Doc(struct stud_node *head);
void main()
{
struct stud_node *head;
int min_score;

head=Creat_Stu_Doc();
scanf("%d",&min_score);
head=DeleteDoc(head,min_score);
Ptrint_Stu_Doc(head);
}
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *head,*tail, *p;
int num, score;
char name[20];
int size = sizeof(struct stud_node);

head=tail=NULL;
scanf("%d",&num);
while(num != 0){
scanf("%s%d",name,&score);
p=(struct stud_node*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
return head;
}
struct stud_node *D

eleteDoc(struct stud_node *head,int min_score)
{
struct stud_node*ptr1,*ptr2;

while(head!=NULL&&head->scoreptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL){
if(ptr2->scoreptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
return head;
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node*ptr;

if(head==NULL){
printf("\nNo Records\n");
return;
}
for(ptr=head; ptr; ptr=ptr->next)
printf("%d %s %d\n", ptr->num,ptr->name,ptr->score);
}
3 编程题
/*-----程序填空,不要改变与输入输出有关的语句。输入若干个正整数(输入-1为结束标志),要求按输入数据的逆序建立一个链表并输出。提示:
定义函数struct stud_node *Creat_Stu_Doc()完成按输入数据的逆序创建链表。
输入:
1 2 3 4 5 6 7 -1
输出:
7 6 5 4 3 2 1
----*/
#include
#include
#include
struct stud_node{
int num;
struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc();
void Ptrint_Stu_Doc(struct stud_node *head);
void main()
{
struct stud_node *head;

head=Creat_Stu_Doc();
Ptrint_Stu_Doc(head);
}

struct stud_node *Creat_Stu_Doc()
{
struct stud_node *head,*p;
int num;
int size=sizeof(struct stud_node);

head=NULL;
scanf("%d",&num);
while(num!=-1){
p=(struct stud_node *)malloc(size);
p->num=num;
p->next=head;
head=p;
scanf("%d",&num);
}
return head;
}

void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node *ptr;
if(head==NULL){
printf("No Records\n");
return;
}
for(ptr=head;ptr;ptr=ptr->next)
printf("%d ",ptr->num);
printf("\n");
}

4 编程题
/*---程序填空,不要改变与输入输出有关的语句。输入若干个正整数(输入-1为结束标志),并建立一个单向链表,将其中的偶数值结点删除后输出。定义函数struct stud_node *DeleteDoc(struct stud_node *head)删除链表中偶数值结点。
输入:
1 2 3 4 5 6 7 -1
输出:
1 3 5 7------*/
#include
#include
struct stud_node{
int num;
struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc();
struct stud_node *DeleteDoc(struct stud_node *head);
void Ptrint_Stu_Doc(struct stud_node *head);
void main()
{
struct stud_node *head;

head=Creat_Stu_Doc();
head=DeleteDoc(head);
Ptrint_Stu_Doc(head);
}
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *head,*tail,*p;
int num;
int size=sizeof(struct stud_node);

head=tail=NULL;
scanf("%d",&num);
while(num!=-1){
p=(struct stud_node *)malloc(size);
p->num=num;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
return head;
}
struct stud_node *DeleteDoc(struct stud_node *head)
{
struct stud_node*ptr1,*ptr2;

while(head!=NULL&&head->num%2==0){
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL){
if(ptr2->num%2==0){
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
return head;
}

void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node *ptr;
if(head==NULL){
printf("No Records\n");
return;
}
for(ptr=head;ptr;ptr=ptr->next)
printf("%d ",ptr->num);
printf("\n");
}

5 编程题

/*----程序填空,不要改变与输入输出有关的语句。有2个已按升序排序的单向链表,头指针分别为list1、list2,链表中每一结点的数据域是一个整数。请编写一个函数,把2个链表拼成1个链表并对新链表升序排列后,返回新链表。编写主程序验证实现。提示:
定义函数struct stud_node *Creat_Stu_Doc()分别创建两个链表;
定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表;
定义函数struct stud_node *InserDoc(struct stud_node *list1,struct stud_node *list2)完成按升序将链表list2的结点依次插入链表list1中。
输入:
1 3 5 7 -1
2 4 6 -1
输出:
1 2 3 4 5 6 7
-----*/
#include
#include
struct stud_node{
int num;
struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc();
void Ptrint_Stu_Doc(struct stud_node *head);
struct stud_node *InserDoc(struct stud_node *list1,struct stud_node *list2);
void main()
{
struct stud_node *list1,*list2;

list1=Creat_Stu_Doc();
list2=Creat_Stu_Doc();
list1=InserDoc(list1,list2);
Ptrint_Stu_Doc(list1);
}
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *head,*tail,*p;
int num;
int size=sizeof(struct stud_node);

head=tail=NULL;
scanf("%d",&num);
while(num!=-1){
p=(struct stud_node *)malloc(size);
p->num=num;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
return head;
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node *ptr;

if(head==NULL){
printf("No Records\n");
return;
}
for(ptr=head;ptr;ptr=ptr->next)
printf("%d ",ptr->num);
printf("\n");
}
struct stud_node *InserDoc(struct stud_node *list1,struct stud_node *list2)
{
struct stud_node *ptr,*ptr1,*ptr2;

ptr2=list1;
ptr=list2;
while(list2!=NULL){
if(list1==NULL){
list1=ptr;
/* list1->next=NULL; ?*/
}
else{
while((ptr->num>ptr2->num)&&(ptr2->next!=NULL)){
ptr1=ptr2;
ptr2=ptr2->next;
}
if(ptr->num<=ptr2->num){
list2=list2->next;
if(list1==ptr2){
list1=ptr;
ptr1=ptr2;
}
else
ptr1->next=ptr;
ptr->next=ptr2;
ptr=list2;
ptr1=ptr1->next;
}
else{
ptr2->next=ptr;
break;
}
}
}
return list1;
}

6 编程题

/*-----程序填空,不要改变与输入输出有关

的语句。有一个单向链表,头指针为L,结点的数据域是一个整数,将链表L中奇数值的结点重新组成一个新的链表NEW,并输出新建链表。
输入:
1 2 3 4 5 6 7 -1
输出:
1 3 5 7
----*/
#include
#include
#include
struct stud_node{
int num;
struct stud_node *next;
};
void Ptrint_Stu_Doc(struct stud_node *head);
void main()
{
struct stud_node *L,*tail1,*tail2,*p1,*p2,*NEW;
int num;
int size=sizeof(struct stud_node);

L=tail1=NULL;
scanf("%d",&num);
while(num!=-1){ /*---新建链表L--*/
p1=(struct stud_node *)malloc(size);
p1->num=num;
p1->next=NULL;
if(L==NULL)
L=p1;
else
tail1->next=p1;
tail1=p1;
scanf("%d",&num);
}

NEW=tail2=NULL;
p1=L;
while(p1!=NULL){
if(p1->num%2==0&&p1!=NULL){ /*--删除链表L中的偶数值---*/
if(p1->next!=NULL){
p1=p1->next;
continue;
}
else
break;
}
if(p1==NULL) break; /*----将链表L中奇数值的结点重新组成一个新的链表NEW--*/
p2=(struct stud_node *)malloc(size);
p2->num=p1->num;
p2->next=NULL;
if(NEW==NULL)
NEW=p2;
else
tail2->next=p2;
tail2=p2;
p1=p1->next;
}
tail2->next=NULL;
Ptrint_Stu_Doc(NEW);
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node *ptr;
if(head==NULL){
printf("No Records\n");
return;
}
for(ptr=head;ptr;ptr=ptr->next)
printf("%d ",ptr->num);
printf("\n");
}




相关主题
文本预览
相关文档 最新文档