//包含了文件頭#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;
}