//包含了文件头#include "cstdilb"能在VC中编译成功,没有的话就只能在tc2.0中编译成功 //该程序只是我学习过程中自己练习的,各位观众不要见笑,对出学c的同学应该会有所帮助 #include "stdio.h"
#include "conio.h"
#include "string.h"
#include "cstdlib"
#define Null 0
struct node
{ int member;
int number;
char name[10];
char sex[5];
struct node *next;
};
struct node *creatlink() /*CREAT LINK*/
{
int x;
struct node *head,*q,*p;
head=q=Null;
printf ("Enter data (if the member's vale<0 the program end)\n");
printf ("Plaese enter a x's vale: ");
scanf ("%d",&x);
printf ("\n");
p=(struct node*)malloc(sizeof(struct node));
if (!p)exit(0);
p->member=x; if (x<0)
{
printf ("The program is end haha!!\n");
exit(0);
}
printf ("Plaese enter a number's vale: ");
scanf ("%d",&p->number);
printf ("\n");
p->next=Null;
head=p;
q=p;
printf ("Plaese enter a x's vale: " );
scanf ("%d",&x);
printf ("\n");
while (x>=0)
{
p=(struct node*)malloc(sizeof(struct node));
if (!p)exit(0);
p->member=x;
printf ("Plaese enter a number's vale: ");
scanf ("%d",&p->number);
printf ("\n");
q->next=p;
p->next=Null;
q=p;
printf ("Plaese enter a x's vale: ");
scanf ("%d",&x);
printf ("\n");
if (x<0)
printf ("The program is end haha!!\n"); }
return(head); }
void writename(struct node *h) /*WRITE NAME*/
{
struct node *search;
search=h;
while(search!=Null) /*当search指针不是空时*/
{
printf ("--------------------------------------------\n");
printf ("Plaese enter a name: ");
scanf ("%s",search->name);
printf("\n");
search=search->next;
}
}
void writesex(struct node *h) /*WRITE SEX*/
{ struct node *search;
search=h;
while (search!=Null) /*当search指针不是空时*/
{
printf ("----------------------------------------\n");
printf ("Plaese enter a sex to this man or woman: ");
scanf ("%s",search->sex);
printf ("\n");
search=search->next;
}
}
void putlink(struct node *h) /*PUT LINK*/
{
struct node *search;
search=h;
while (search)
{
printf ("----------------------------------------------------------------");
printf("\n");
printf("%d,%d,%s,%s",search->member,search->number,&search->name,&search->sex);
printf ("\n");
search=search->next;
}
} int mm(int n) /*递归调用*/
{
if (n<1||n>3) /*如果n的值大于3或小于1就是错误的输入*/
{
printf ("You make a error chooes !\n");
printf ("Make a right chooes : ");
scanf ("%d",&n);
mm(n);
printf ("\n"); }
return(n);
}
void insertele(struct node *h) /*INSRET ELEMENT*/
{
struct node *search,*befores,*newele;
int m,s;
search=befores=h;
newele=(struct node *)malloc(sizeof(struct node)); /*开辟一个新接点空间*/
printf ("--------------------------------------------\n");
if (!newele) /*判断是否分配成功*/
{
printf ("The newelement'mallocing falled ! \n");
exit(0);
}
else
{
printf ("--------------------------------------------\n");
printf ("Please enter new data to the newelement \n");
scanf ("%d,%d,%s,%s",&newele->member,&newele->number,&newele->name,&newele->sex); /*输入新链表的数据*/
}
printf ("-------------------------------------------------\n");
printf ("Please choose a way to insert your new elsement :\n(* if you choose 1,you can enter 1,2 enter 2........)\n1 is the member way,2 is the number way,3 is the name way\n");
printf ("Please enter way's vale(you only can enter a int vale>0and<4:" );
scanf ("%d",&m);
s=mm(m); /*递归调用*/ printf ("\n-----------------------------------------------\n");
switch(s)
{
case 1: /*按序列号插入*/
{
while(search!=Null&&(search->member<=newele->member))
{
befores=search;
search=search->next;
}
befores->next=newele;
newele->next=search; };
break;
case 2: /*按数值大小插入*/
{
while(search!=Null&&(search->number<=newele->number))
{
befores=search;
search=search->next;
}
befores->next=newele;
newele->next=search;
};
break;
case 3: /*按姓名插入*/
{
while(search!=Null&&(strcmp(search->name,newele->name)<0))
{
befores=search;
search=search->next;
}
befores->next=newele;
newele->next=search;
};
break; }
printf ("%d,%d,%s,%s\n",newele->member,newele->number,&newele->name,&newele->sex);
}
struct node *delelink(struct node *h) /*DELETE LINK*/
{
struct node *search,*befores;
int member;
printf ("This program's funcation is to delete a member which you want \n");
printf ("Plaese enter a member which you want to delete : ");
scanf ("%d",&member);
printf ("\n");
search=h;
if ((h->member==member)&&h)
{
h=search->next;
free(search); }
while ((search->member!=member)&&search)
{
befores=search;
search=search->next; }
if ((search->member==member)&&search)
{
befores->next=search->next;
free(search);
} return(h); /*把头指针传给putlink函数,此函数从头指针开始输出数据*/
}
int main()
{
struct node *H,*h;
printf (" Now,let's creat a link ,hahaha!!!\n\n");
H=creatlink(); /*建立一个链表并输入每个元素的编号以及一个属于该元素的一个数据*/
writename(H) ; /*输入每个元素的姓名*/
writesex(H); /*输入每个元素的性别*/
printf ("--------------------------------------------------------------------------\n");
printf (" Now,let's put this link out !!\n\n");
putlink(H); /*打印原始链表*/
printf("---------------------------------------------------------------------------\n");
printf ("Now let's insert a element !\n");
printf ("--------------------------------------------------------------------------\n");
insertele(H); /*插入一个新的元素*/
printf ("Now let's put out this new link !\n");
putlink(H);
printf ("---------------------------------------------------------------------------\n");
printf("Now,let's delete a member!!!\n\n");
h=delelink(H); /*删除一个元素*/
printf("\n");
printf ("--------------------------------------------------------------------------\n"); printf ("put out the link again!\n");
putlink(h); /*打印删除一个元素后的链表*/
printf ("\n------------------------------------------------------------------------\n the program is end !\n press any key to continue !!\n\n\n\n\n\n\n\n\n");
getch();
return 0;
}