结构体成员可以 包含指向本身的指针;
这为我们创建链表提供了基础;
以下代码包括了我能想到的链表基本功能
1:建立一个男生链表和女生链表,包含姓名和年龄
2:查看链表成员
3:二个链表相连
4:成员按年龄大小排序
5:寻找指定年龄最相近的成员
6:删除成员
7:添加成员
如果你看懂了以下的代码,链表基本就学会了
ps:程序运行了一遍,但肯定会有逻辑上的漏洞,没有做太多调试;
使用方法:先按1 创造男生链表
再按2 创建女生链表 //2 查看男生链表 4 查看女生链表 能用了
再按5 使二个链表合起来 //6 查看新链表 能用了
再按7 使其按年龄排序 // 8 查看年龄链表可以用了 9 查找年龄相近成员 11 删除成员 12 添加成员 能用了
按10可以退出程序
源代码:
#include<stdlib.h>
#include<stdio.h>#include<string.h>
#include<math.h>
typedef struct man //结构体定义
{
char name[20];
int age;
struct man *p;
}m;
void Lookup(m *a,int b,int c) //寻找年龄最相近的成员函数
{
int las=0,nex=0,ran=0,i=0,bol=0,bo=0,boo=0,j=0,biao1=0,biao2=0;
m *d,*head;
d=a;
las=abs((a->age)-b);
a=a->p;
for(i=1;i<c;i++)
{
nex=abs((a->age)-b);
if(las<nex)
{
bol=1;
if(1==bo)
{
for(j=0;j<=boo;j++)
{
printf("name:%sage:%d\n",d->name,d->age);
d=d->p;
}
}
else
printf("name:%sage:%d\n",d->name,d->age);
}
else if(las==nex)
{ biao1++;
bo=1;
boo=boo+1;
a=a->p;
}
else if(i==i-1)
{
if(1==bo)
{
for(i=0;i<=boo;i++)
{
printf("name:%sage:%d\n",d->name,d->age);
d=d->p;
}
}
else
printf("name:%sage:%d\n",d->name,d->age);
goto A;
}
else if (las>nex )
{
bo=0;
boo=0;
d=a;
las=nex;
a=a->p;
biao2++;
}
if(1==bol)
goto A;
}
for(i=1;i<c;i++)
d=d->p;
printf("name:%sage:%d\n",d->name,d->age);
A: sleep(1);
}
void Add(m *a,m *b ,int c) //添加成员的函数
{
int i;
m *d;
d=b;
for(i=1;i<c;i++)
{
d=d->p;
}
d->p=a;
}
void Cpy(m *a,m *b ,int c) //拷贝一个新链表的函数
{
int i=0;
m *head;
strcpy((a->name),(b->name));
a->age=b->age;
for(i=1;i<c;i++)
{
head=(m *)malloc(sizeof(m));
a->p=head;
a=head;
b=b->p;
strcpy((a->name),(b->name));
a->age=b->age;
}
}
int Del(m *a,m *b,int c) //删除成员函数
{
m *head,*tail;
head=b;tail=b;
int i,k=0;
static ax=0;
tail=tail->p;
for(i=1;i<c;i++)
{
if(strcmp(tail->name,a->name))
{ head=tail;
tail=tail->p;
}
else
{
head->p=tail->p;
k=1;
free(tail);
ax++;
}
}
if (k==0)
{
printf("NOT FIND!");
return 0;
}
else
{
printf("SUCCESS!");
return ax;
}
}
void Age(m *a,m *b,int c) //按年龄排序的函数
{
int i=0,j=0,sum=0;
m *d;
char e[20];
d=a;
for(i=0;i<(c-1);i++)
{
a=d;b=d;
for(j=0;j<(c-1);j++)
{
b=b->p;
if((a->age)>(b->age))
{
sum=a->age;
a->age=b->age;
b->age=sum;
strcpy(e,a->name);
strcpy(a->name,b->name);
strcpy(b->name,e);
}
a=a->p;
}
}
}
void Creat(m *b) //创建链表
{
m *head;
head=(m *)malloc(sizeof(m));
printf("please enter name:");
fgets(head->name,20,stdin);
fgets(head->name,20,stdin);
printf("please enter age:");
scanf("%d",&(head->age));
b->p=head;
}
void Enter(m *ep,int ea) //查看链表
{
int i=0;
for(i=0;i<ea;i++)
{
printf("student name:%s",ep->name);
printf("student age:%d\n",ep->age);
ep=ep->p;
}
}
int main()
{
int sele=0 ;
int i,number,nm=0,nw=0,nstu=0;
m *pmhead,*pmtail,no1={"boys form\n",0}; // 功能列表
pmhead=&no1;pmtail=&no1;
m *pwhead,*pwtail,no2={"gils form\n,0"};
pwhead=&no2;pwtail=&no2;
m *pstuhead,*pstutail,no3={"student form\n",0};
pstuhead=&no3;pstutail=&no3;
m *pagehead,*pagetail,no4={"age form\n",0};
pagehead=&no4;pstutail=&no4;
while(1) //无限循环
{
printf("this is a stdudent form.you can select these options\n");
printf("1 creat new boys form\n2 creat new girl form\n3 see boys form\n\
4 see girls form\n5 link to one\n6 see the one\n7\
the new table by age\n8 see the new table\n9 seek near age\n10 quit\n\
11 Delete member\n12 Add member\n");
printf("please enter:");
scanf("%d",&sele);
if (sele>12 || sele<1) //如果输入数字超过功能序列 提示出错
{
printf ("error!");
return 1;
}
else if ( 1==sele )
{
printf("please enter you went craet man number:");
scanf("%d",&number);
if(number>0)
{
printf("please enter name:");
fgets(no1.name,20,stdin);
fgets(no1.name,20,stdin);
printf("please enter age:");
scanf("%d",&(no1.age));
nm++;
for(i=1;i<number;i++)
{
Creat(pmtail);
pmtail=pmtail->p;
pmtail->p=pmhead;
nm++;
}
}
}
else if ( 3==sele )
{
printf("this is boys form:\n");
pmtail=pmhead;
Enter(pmtail,nm);
}
else if ( 2==sele)
{
printf("please enter you went craet girls number:");
scanf("%d",&number);
if(number>0)
{
printf("please enter name:");
fgets(no2.name,20,stdin);
fgets(no2.name,20,stdin);
printf("please enter age:");
scanf("%d",&(no2.age));
nw++;
for(i=1;i<number;i++)
{
Creat(pwtail);
pwtail=pwtail->p;
pwtail->p=pwhead;
nw++;
}
}
}
else if (4==sele)
{
printf("this is girlss form:\n");
pwtail=pwhead;
Enter(pwtail,nw);
}
else if (5==sele)
{
pstuhead=pmhead;
pstutail=pmhead;
for (i=1;i<nm;i++)
pstutail=pstutail->p;
pstutail->p=pwhead;
pstutail=pwhead;
for (i=1;i<nw;i++)
pstutail=pstutail->p;
pstutail->p=pmhead;
}
else if (6==sele)
{
printf("this is student form:\n");
pstutail=pmhead;
nstu=nw+nm;
Enter(pstutail,nstu);
}
else if (7==sele)
{
pagetail=&no4;
pstutail=&no1;
nstu=nw+nm;
Cpy(pagetail,pstutail,nstu);
pagetail=&no4;
pstutail=&no1;
Age(pagetail,pagehead,nstu);
}
else if (8==sele)
{
printf("this form sort by age:\n");
pagetail=&no4;
nstu=nw+nm;
Enter(pagetail,nstu);
}
else if (9==sele)
{
int slage;
printf("enter age:\n");
scanf("%d",&slage);
pagetail=&no4;
nstu=nw+nm;
Lookup(pagetail,slage,nstu);
}
else if (11==sele)
{
m del,*pdelhead,*pdeltail;
printf("please enter delect member name:");
fgets(del.name,20,stdin);
fgets(del.name,20,stdin);
pagetail=&no4;
nstu=nw+nm;
pdeltail=&del;
pdelhead=&del;
nw=nw-Del(pdeltail,pagetail,nstu);
}
else if (12==sele)
{ nstu=nw+nm;
m addm,*paddhead,*paddtail;
printf("please enter add member name:");
fgets(addm.name,20,stdin);
fgets(addm.name,20,stdin);
printf ("please enter add member age:");
scanf("%d",&addm.age);
paddhead=&addm;paddtail=&addm;
Add(paddtail,pagetail,nstu);
nw++;
}
else if (10 == sele)
return 0;
printf ("wait...\n");
sleep(2); //延时程序,觉得慢可以删掉
}
return 0;
}
运行展示图