链式存储结构

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
printf("输入要查找的学号:\n");
scanf("%ld",&x);
}
head=creat(stu,3);
printf("输入要删除的学号:\n");
long nu;
scanf("%ld",&nu);
while(nu!=0)
{head=del(head,nu);
print(head);
printf("输入要删除的学号:\n");
p1=p1->next;}
printf("优秀学生%d",n);
return 0;
}
int main()
{ struct Student stu[3];
intቤተ መጻሕፍቲ ባይዱi;
printf("enter the data:\n");
for(i=0;i<3;i++)
scanf("%ld %s %c %d %ld",&stu[i].num,&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
struct Student *head;
head=creat(stu,3);
print(head);
printf("输入要查找的学号:\n");
long x;
scanf("%ld",&x);
while(x!=0)
{head=find(head,x);
printf("%ld %s %c %d %ld\n",head->num,head->name,head->sex,head->age,head->score);
}
return 0;
}
2.#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{long num;
char name[20];
char sex;
int age;
long score;
struct Student *next;
(2)定义学生结构体数组,数组元素为结构体类型,成员有学号、姓名、性别、年龄,总成绩。
(3)定义链表的创建、插入、删除、查找和输出函数,主函数里分别调用以上函数,其中创建、插入和删除函数函数类型是指针类型,函数返回值是链表起始地址。在调用以上函数前,主函数里要先完成学生结构体数组的数据输入(注意:要求按学生的学号顺序输入)。
{head=p0;
p0->next=NULL;
}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{if(head=p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
struct Student *head;
head=creat(stu,3);
print(head);
int search (struct Student *head);
search(head);
return 0;
}
总结:
Malloc返回的指针必须转换为struct student类型数据的指针
实验七链式存储结构
一、实验目的
1.掌握链表结点定义的存储结构;
2.掌握使用指针访问结构体数组元素;
3.掌握数组名作为函数参数。
二、实验内容及要求
1、建立学生链表,每个结点包括:学号、姓名、性别、年龄,总成绩,并在链表上实现插入、删除、查找和输出操作;
要求:
(1)创建的链表为动态链表。(知识点:利用动态内存分配的有关函数,如malloc,free函数)
scanf("%ld",&nu);
}
printf("输入插入的数据:\n");
struct Student *stude;
stude=(struct Student *)malloc(LEN);
scanf("%ld %s %c %d %ld",&stude->num,&stude->name,&stude->sex,&stude->age,&stude->score);
p1=p2=(struct Student *)malloc(LEN);
head=NULL;
p1=p;
while(n<x)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
p1=p+n;
}
p2->next=NULL;
while(stude->num!=0)
{head=insert(head,stude);
print(head);
printf("输入插入的数据:\n");
stude=(struct Student*)malloc(LEN);
scanf("%ld %s %c %d %ld",&stude->num,&stude->name,&stude->sex,&stude->age,&stude->score);
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
p1=p+n;
}
p2->next=NULL;
return(head);
}
void print(struct Student *head)
{struct Student *p;
printf("\nNow,These records are:\n");
p=head;
if(head!=NULL)
do
{printf("%ld %s %c %d %ld\n",p->num,p->name,p->sex,p->age,p->score);
p=p->next;
}while(p!=NULL);
return(head);}
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;
p1=p1->next;}
if(num==p1->num)
{if(head==p1)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%d\n",num);
while(num!=p1->num&&p1->next!=NULL)
p1=p1->next;
if(num==p1->num)
return(p1);
}
struct Student *creat(struct Student *p,int x)
{int n=0;
struct Student *p1,*p2,*head;
}
else
{p1->next=p0;
p0->next=NULL;
}
}
return(head);
}
struct Student *del(struct Student *head,long num)
{
struct Student *p1,*p2;
p1=head;
if(head==NULL)
{printf("\n list null\n");
(4)链表的创建,函数参数有两个,分别表示指向结构体数组的指针和数组元素个数。(注意:主函数调用链表的创建函数,实参分别为结构体数组名和数组元素个数)。
(5)链表插入即插入一个新生的结点,要求按学生的学号顺序插入。(注意:函数参数有两个,分别表示链表头指针和待插入结点的地址,按照插入的位置不同分情况插入。允许插入多个结点,直到输入要插入的学号为0)。
};
struct Student *creat(struct Student *p,int x)
{int n=0;
struct Student *p1,*p2,*head;
p1=p2=(struct Student *)malloc(LEN);
head=NULL;
p1=p;
while(n<x)
{n=n+1;
(知识点:链表的遍历,工作指针的移动)
(1)#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{long num;
char name[20];
char sex;
int age;
long score;
(8)链表输出要求将链表中各结点的数据依次输出。(注意:函数参数即为链表头指针)。
2、查找并计算已知一个链表中数据域值为一个指定值范围(如总成绩是良好等)的结点个数;
要求:
(1)在上一题的基础上,再定义一个统计函数。
(2)定义一个工作指针,初始值为链表起始地址。
(3)让工作指针对链表进行遍历,检查当前结点是否在指定值范围内,如是则计数。最后输出符合要求的结点个数。
p=p->next;
}while(p!=NULL);
}
struct Student *insert(struct Student *head,struct Student *stud)
{struct Student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
return(head);
}
void print(struct Student *head)
{struct Student *p;
printf("\nNow,These records are:\n");
p=head;
if(head!=NULL)
do
{printf("%ld %s %c %d %ld\n",p->num,p->name,p->sex,p->age,p->score);
}
int search(struct Student*head)
{struct Student *p1;
p1=head;
if(head==NULL)
{printf("\n list null\n");
return(0);}
int n=0;
while(p1!=NULL)
{if(p1->score>60) n=n+1;
struct Student *next;
};
struct Student *find(struct Student *head,long num)
{struct Student *p1;
p1=head;
if(head==NULL)
{printf("\n list null\n");
return(head);}
}
else
printf("%ld not been found",num);
return(head);
}
int main()
{ struct Student stu[3];
int i;
printf("enter the data:\n");
for(i=0;i<3;i++)
scanf("%ld %s %c %d %ld",&stu[i].num,&stu[i].name,&stu[i].sex,&stu[i].age,&stu[i].score);
(6)链表删除即删除一个指定的结点,要求按指定学号删除指定的结点。(注意:函数参数有两个,分别表示链表头指针和要删除的学号,按照删除的位置不同分情况删除,还要考虑链表是空表和找不到要删除的结点情况。允许删除多个结点,直到输入要删除的学号为0)。
(7)链表查找要求按学生的学号进行查找。(注意:函数参数有两个,分别表示链表头指针和要查找的学号。找到该结点,输出结点全部信息。找不到该结点,输出信息提示。允许多次查找,直到输入要查找的学号为0)。
相关文档
最新文档