线性表的链式实现
主要内容:
包含链表的创建、增、删、改、查操作
代码实现:
以下学生信息为例实现代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student *create(struct student *);//创建链表
void name_found(struct student *); //根据姓名进行查找,返回此学生的学号和成绩
void position_found(struct student *); //根据指定的位置可返回相应的学生信息(学号,姓名)
void insert_position(struct student*); //给定一个学生信息,插入到表中指定的位置
void del_position(struct student*); //删除指定位置的学生记录
void statistic_student(struct student*);//统计表中学生个数
struct student
{
char num[14]; //学号
char name[20]; //姓名
int sex; //性别
char tel[14]; //联系电话
char qq[12]; //QQ号
struct student *next;
};
int main()
{
struct student *head=NULL,*p,*p1;
head=create(head);
p=head;
while(p!=NULL)
{
printf("%10s%10d%15s%15s%10s\n",p->name,p->sex,p->num,p->tel,p->qq);//打印链表信息
p=p->next;
}
printf("\n");
printf("根据姓名查找输出赵括的信息\n");
name_found(head);
printf("\n");
printf("输出指定位置信息第二位同学的信息\n");
position_found(head); //输出指定位置的信息
printf("\n");
printf("将刘刚的信息插入到第二位后:\n");
insert_position(head);
printf("\n");
printf("删除第三位同学的信息后:\n");
del_position(head);
printf("\n");
statistic_student(head);
printf("\n");
return 0;
}
struct student *create(struct student * head)
{
struct student *p1,*p2,*p3,*p4;
p1=(struct student *)malloc(sizeof(struct student)); //信息1
strcpy(p1->name,"李思" );
p1->sex=0;
strcpy(p1->num,"2013002");
strcpy(p1->tel,"13910121978");
strcpy(p1->qq,"8796532");
p1->next=NULL;
p2=(struct student *)malloc(sizeof(struct student)); //信息2
strcpy(p2->name,"陈琪");
p2->sex=1;
strcpy(p2->num,"2013003");
strcpy(p2->tel,"13789450012");
strcpy(p2->qq,"3789123");
p2->next=NULL;
p3=(struct student *)malloc(sizeof(struct student)); //信息3
strcpy(p3->name,"王强");
p3->sex=0;
strcpy(p3->num,"2013004");
strcpy(p3->tel,"13634567856");
strcpy(p3->qq,"6543783");
p3->next=NULL;
p4=(struct student *)malloc(sizeof(struct student)); //信息4
strcpy(p4->name,"赵括");
p4->sex=1;
strcpy(p4->num,"2013005");
strcpy(p4->tel,"13534408976");
strcpy(p4->qq,"5679");
p4->next=NULL;
head=p1;
p1->next=p2;
p2->next=p3;
p3->next=p4;
p4->next=NULL;
return head;
}
void name_found(struct student *r) //根据姓名进行查找,返回此学生的学号和成绩
{
struct student *q;
q=r;
char name[20]="赵括";
//scanf("%s",&name);
while(q!=NULL)
{
if(strcmp(q->name,name)==0)
{
printf("%10s%15s%15s\n",q->name,q->num,q->tel);break;
}
q=q->next;
}
}
void position_found(struct student *a) //根据指定的位置可返回相应的学生信息(学号,姓名)
{
struct student *b;
int m=2;
//scanf("%d",&m);
int i=0;
b=a;
while(b!=NULL)
{
i++;
while(i==m)
{
printf("%10s%15s\n",b->name,b->num);break;
}
b=b->next;
}
}
void insert_position(struct student *pa) //给定一个学生信息,插入到表中指定的位置
{
struct student *pb,*pc;
int i=1;
int n=2; //将刘刚的信息插入到第2个
//printf("请输入插入的点");
//scanf("%d",&n);
pb=pa;
while(pb!=NULL&&i<n-1)
{
i++;
pb=pb->next;
}
pc=(struct student *)malloc(sizeof(struct student));
strcpy(pc->name,"刘刚");
pc->sex=1;
strcpy(pc->num,"2013006");
strcpy(pc->tel,"13386543211");
strcpy(pc->qq,"98315");
pc->next=pb->next;
pb->next=pc;
//pb=pa;
while(pb!=NULL)
{
printf("%10s%10d%15s%15s%10s\n",pb->name,pb->sex,pb->num,pb->tel,pb->qq); //打印链表信息
pb=pb->next;
}
}
void del_position(struct student *t)
{
struct student *l,*k;
int i=1;
int n=3;
//printf("请输入想删除的点");
//scanf("%d",&n); //删除第3位同学的信息
l=t;
while(l!=NULL&&i<n-1)
{
l=l->next;
i++;
}
k=l->next;
l->next=k->next;
free(k);
l=t;
while(l!=NULL)
{
printf("%10s%10d%15s%15s%10s\n",l->name,l->sex,l->num,l->tel,l->qq); //打印链表信息
l=l->next;
}
}
void statistic_student(struct student*y) //统计表中学生个数
{
int i=0;
struct student *x;
x=y;
while(x!=NULL)
{
i++;
x=x->next;
}
printf("学生总数为:%d\n",i);
}