/大家好我是右丞,喜欢和大家一起学习和进步的不怎么的普通人!/
今天也请继续加油!
实验六、单向链表程序设计(p115–p121)
一、实验目的
(1)掌握单向链表的概念和建立方法。
(2)掌握单向链表的基本操作。
二、实验内容
完成C语言程序设计实验与习题指导11.2内容。
1.源程序:
2.调试例子:
3.结果如图:
三、实验程序
1、调试示例
建立学生信息链表:输入若干个学生的信息(学号、姓名、成绩),当输入学号为0
时结束,用单向链表组织这些学生信息后,再按顺序输出。(自建链表)
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
char Name[5];
int Sum;
struct Student *Prept;
}STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == 0)
break;
scanf("%s%d", &Pt->Name, &Pt->Sum);
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
}
return head;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d %s %d\n",p->Num,p->Name,p->Sum);
p = p->Prept;
}
}
int main()
{
STU *head;
printf("studentnum name sum\n");
head = CreatNode();
print(head);
return 0;
}
2.调试例子:
3.结果如图:
2、基础编程题
(1)单向链表建立:输入若干个学生的信息(学号、姓名、成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
char Name[5];
int Sum;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == 0)
break;
scanf("%s%d", &Pt->Name, &Pt->Sum);
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
}
return head;
}
void FindNode(STU *head,int Foundation)
{
int n=1;
STU *p=head->Prept;
while(p!=NULL)
{
if(p->Sum>=Foundation)
{
printf("Student%d:%d %s %d\n",n,p->Num,p->Name,p->Sum);
n++;
p=p->Prept;
}
else
{
p=p->Prept;
}
}
if(n==1)
{
printf("Sorry,Noone");
}
}
int main()
{
int Foundation;
STU *head;
printf("studentnum name sum\n");
head = CreatNode();
printf("What is your want to find?\n");
scanf("%d",&Foundation);
FindNode(head,Foundation);
return 0;
}
2.调试例子:
3.结果如图:
(2)逆序数据建立链表:输入若干个正整数(输人-1为结束标志),要求按输人数据的逆序建立一个链表,并输出。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt;
STU *end;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
end = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == -1)
break;
Pt->Prept=end->Prept;
end->Prept=Pt;
}
return end;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d\n",p->Num);
p = p->Prept;
}
}
int main()
{
int Foundation;
STU *head;
printf("what is your num?\n");
head = CreatNode();
print(head);
return 0;
}
2.调试例子:1 2 3 4
3.结果如图:
(3)删除单向链表偶数节点:输入若干个正整数(输人-1为结束标志),建立一个单向链表,将其中的偶数节点删除后输出。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == -1)
break;
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
}
return head;
}
STU *ChangeNode(STU *L)
{
STU *pnext,*head,*Pt;
int n=1;
Pt=L;
pnext=L;
head=L;
while(pnext->Prept!=NULL)
{
Pt=pnext->Prept;
if(n%2==0)
{
pnext->Prept=Pt->Prept;
free(Pt);
}
pnext=pnext->Prept;
n++;
}
return head;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d\n",p->Num);
p = p->Prept;
}
}
int main()
{
int Foundation;
STU *L,*NEW;
printf("what is your num?\n");
L = CreatNode();
printf("They will be changed\n");
NEW = ChangeNode(L);
print(NEW);
return 0;
}
2.调试例子:1 2 3
3.结果如图:
(4)链表拼接: 输人若干个正整数 (输入-1为结束标志)建立两个已按升序排序的单向链表,头指针分别为list l、list 2, 把两个链表拼成一个链表,并输出新链表信息。要求自定义函数,实现将两个链表拼成一个链表,并返回拼组后的新链表。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == -1)
break;
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
}
return head;
}
STU *Conext(STU *list1,STU *list2)
{
STU *pnext;
pnext=list1;
while(pnext->Prept!=NULL)
{
pnext=pnext->Prept;
}
pnext->Prept=list2->Prept;
return list1;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d\n",p->Num);
p = p->Prept;
}
}
int main()
{
int Foundation;
STU *list1,*list2,*List;
printf("what is your first num?\n");
list1 = CreatNode();
printf("what is your second num?\n");
list2 = CreatNode();
List=Conext(list1,list2);
print(List);
return 0;
}
2.调试例子:123 -1 456 -1
3.结果如图:
(5)奇数值结点链表:输入若干个整数(输入-1为结束标志)建立一个单向链表,头指针为L,将链表L中奇数值的结点重新组成一个新的链表NEW,并输出新链表的信息。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == -1)
break;
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
}
return head;
}
STU *ChangeNode(STU *L)
{
int n=1;
STU *pnext1,*pnext2,*Pt,*head;
pnext1=L;
pnext2=L;
head=pnext2;
while(pnext1->Prept!=NULL)
{
if(n%2==0&&n!=1)
{
Pt=(STU *)malloc(sizeof(STU));
Pt->Num=pnext1->Num;
pnext2->Prept=Pt;
pnext2=Pt;
}
pnext1=pnext1->Prept;
n++;
}
pnext2->Prept=NULL;
return head;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d\n",p->Num);
p = p->Prept;
}
}
int main()
{
int Foundation;
STU *L,*NEW;
printf("what is your num?\n");
L = CreatNode();
printf("They will be changed\n");
NEW = ChangeNode(L);
print(NEW);
return 0;
}
2.调试例子:1 2 3 4 5 6 -1
3.结果如图:
3、改错题
统计专业人数:输入若干个学生的学号(共7位,其中第2、3位是专业号),以“#”作为输入结束标志,将其生成一个链表,统计链表中专业为计算机(编号为02)的学生人数。
1.源程序:
#include
#include
#include
typedef struct Student
{
int num[7];
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
int i=0;
char t;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
for(i=0;i<7;i++)
{
scanf("%d",&Pt->num[i]);
}
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
t=getchar();
if(t=='#')
break;
}
return head;
}
int FindNode(STU *head)
{
int Num=0;
STU *p=head;
while(p->Prept!=NULL)
{
if(p->num[1]=='0'&&p->num[2]=='2')
Num++;
p=p->Prept;
}
return Num;
}
int main()
{
int Foundation=0;
STU *head;
printf("studentnum name sum\n");
head = CreatNode();
printf("What is your want to find?\n");
Foundation=FindNode(head);
printf("%d",Foundation);
return 0;
}
2.调试例子:
3.结果如图:
4、拓展编程题
(1)删除结点:输入若干个整数(输入-1为结束标志)建立一个单向链表,再输入一个整数m,删除链表中值为m的所有结点。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt,*Pnext;
STU *head;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
head = Pt;
Pnext = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == -1)
break;
Pnext->Prept = Pt;
Pt->Prept = NULL;
Pnext = Pt;
}
return head;
}
STU *CancelNode(STU *L,int m)
{
STU *pnext,*head,*Pt;
Pt=L;
pnext=L;
head=L;
printf("New node!!!\n");
while(pnext->Prept!=NULL)
{
Pt=pnext->Prept;
if(pnext->Prept->Num==m)
{
pnext->Prept=Pt->Prept;
free(Pt);
}
pnext=pnext->Prept;
}
return head;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d\n",p->Num);
p = p->Prept;
}
}
int main()
{
int Foundation;
int m;
STU *L,*NEW;
printf("what is your num?\n");
L = CreatNode();
printf("what is node you want to cancel?\n");
scanf("%d",&m);
NEW = CancelNode(L,m);
print(NEW);
return 0;
}
2.调试例子:1 2 3 4 5 6
3.结果如图:
(2)链表逆置:输入若干个整数(输入-1为结束志)建立一个单向链表,再将链表逆置后输出,即表头置为表尾,表尾为表头。试编写相应程序。
1.源程序:
#include
#include
#include
typedef struct Student
{
int Num;
struct Student *Prept;
} STU;
STU *CreatNode()
{
STU *Pt;
STU *end;
Pt = (STU*)malloc(sizeof(STU));
Pt->Prept = NULL;
end = Pt;
while(1)
{
Pt = (STU*)malloc(sizeof(STU));
scanf("%d", &Pt->Num);
if(Pt->Num == -1)
break;
Pt->Prept=end->Prept;
end->Prept=Pt;
}
return end;
}
void print(STU *head)
{
STU *p;
p = head->Prept;
while(p->Num!=0)
{
printf("%d\n",p->Num);
p = p->Prept;
}
}
int main()
{
int Foundation;
STU *head;
printf("what is your num?\n");
head = CreatNode();
print(head);
return 0;
}