#include "stdio.h"
#include "malloc.h"
#include "string.h"
static int stu_count=0;
struct student{
int age;
char num[20];
char name[20];
char sex[12];
student *next;
};
static int SIZE = sizeof(student);
void main()
{
student *head=NULL;
static student *tail=NULL;
int n=0,i=0;
student* search(student *head,char*strNum);
student* create(int n,student* taill);
void print(student *head);
void insert(student* head,student *tail);
void del(student* head,char* strNum);
printf("input the number of records n: ");
scanf("%d",&n);
printf("/n");
head=create(n,tail);
command:;
printf("Operation order OutLine:/n");
printf("order 0:退出程序/n");
printf("order 1:显示链表数据/n");
printf("order 2:插入新数据/n");
printf("order 3:查询数据(按编号)/n");
printf("order 4:删除数据记录/n");
printf("order 5:查看记录总数/n");
printf("input order: ");
scanf("%d",&i);
while(1)
{
switch(i)
{
case 0:
{
printf("Progarm ending............../n");
//exit(0);
goto exitprogramm;
break;
}
case 1:
{
printf("The data of this linktabel follows:/n");
print(head);
//pause(5);
goto command;
break;
}
case 2:
{
insert(head,tail);
goto command;
break;
}
case 3:
{
char tempStr[20]={'/0'};
printf("input your code number:/n");
scanf("%s",tempStr);
search(head,tempStr);
goto command;
break;
}
case 4:
{
char tempStr[20]={'/0'};
printf("input your code number:/n");
scanf("%s",tempStr);
del(head,tempStr);
goto command;
printf("Now the total number of records is %d",stu_count);
break;
}
case 5:
{
printf("记录总数为:%d/n",stu_count);
goto command;
break;
}
}
exitprogramm:;
}
}
student* search(student *headd,char*strNum)
{ int count=stu_count;
student *temp=headd;
while(count>1)
{
if(strcmp(temp->num,strNum)!=0)
{
temp=temp->next;
}
else
{
printf("num %s does exit!!",strNum);
printf("num=%s/nname=%s/nsex=%sage=%d/n",temp->num,temp->name ,temp->sex ,temp->age );
return temp;
}
count--;
}
printf("Noone's codenum is %s!!!",strNum);
return NULL;
}
student* create(int n,student* taill)//按编号查询,编号不能重复
{
int i;
student *headd=(student*)malloc(SIZE),*tempNew=NULL,*tempTail=NULL;
if(headd==NULL)
{
printf("No memory exits!!!");
return NULL;
}
stu_count++;
headd->next = NULL;
//head=headd;
taill=headd;
tempNew=headd;
//head=NULL;
printf("please input the first record(num,name,sex,age,isolated by blankspace or tab):");
scanf("%s%s%s%d",tempNew->num,tempNew->name,tempNew->sex,&(tempNew->age));
taill->next=tempNew;
tempNew->next=NULL;
for(i=2;i<=n;i++)
{
if((tempNew=(student*)malloc(SIZE))!=NULL)
stu_count++;
else
{
printf("Error,Memory not Enough!!!/n");
printf("Error record number:%d!!!!/n",i);
//exit(1);
return NULL;
}
printf("please input the %dth record (num,name,sex,age,isolated by blankspace or tab)::",i);
scanf("%s%s%s%d",tempNew->num,tempNew->name,tempNew->sex,&(tempNew->age));
taill->next=tempNew;
tempNew->next=NULL;
taill=tempNew;
}
return headd;
}
void print(student *head)
{
student *p=head;
printf("the total count of student is %d/n",stu_count);
printf("Now ,These %d records are:/n",stu_count);
printf("编号/t/t姓名/t/t性别/t/t年龄/n");
while(p)
{
printf("%s/t/t%s/t/t%s/t/t%d/n",p->num,p->name,p->sex,p->age);
p=p->next;
}
}
void insert(student* headd,student *taill)
{
int i=stu_count;
student *tempTail=taill;
if((tempTail->next=(student*)malloc(SIZE))!=NULL)
stu_count++;
else
{
printf("Error,Memory not Enough!!!");
//exit(1);
goto error;
}
gooninsert:;
printf("/nplease insert as follows(codenum,name,sex,age):/n");
scanf("%s%s%s%d",tempTail->num,tempTail->name,tempTail->sex,&(tempTail->age));
if(search(headd,tempTail->num)!=NULL)
{
printf("An %s already exists!! Please insert an unrecorded code number./n",tempTail->num);
printf("Will you go on?yes or no?");
char tempstr[10]={'/0'};
scanf("%s",tempstr);
if(strcmp(tempstr,"yes")==0)
goto gooninsert;
else
{
free(tempTail->next);
tempTail->next=NULL;
return;
}
}
tempTail=tempTail->next;
tempTail->next = NULL;
error:;
}
void del(student* head,char* strNum)//
{
student *temp=head,*temp1;
char number[20]={'/0'};
int n=stu_count;
while(n>1)
{
if(strcmp(temp->num,strNum)!=0)
{
temp=temp->next;
temp1=temp;
}
else
{
temp1->next=temp->next;
free(temp);
}
n--;
}
}