/*在TC中运行好好的,win-tc也可以,就是DEV C++编译后一堆看不懂的提示,还望哪位懂DEV C++的解释下~*/
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
struct stu
{ int num;
char name[20];
char tel[20];
struct stu *next;
};
struct stu *creat(int n)
{
struct stu *head,*p1,*pd;
int i;
for(i=0;i<n;i++)
{
p1=(struct stu*) malloc(sizeof (struct stu));
p1->num=i+1;
printf("No.%d\n",i+1);
printf("Input name\n");
scanf("%s",p1->name);
printf("Input telephone number\n");
scanf("%s",p1->tel);
if(i==0)
head=pd=p1;
else
pd->next=p1;
pd=p1;
pd->next=0;
}
return head;
}
void print(struct stu *head)
{
printf("num\tname\t\t\ttel.number\t\n");
while(head!=NULL)
{
printf("%d\t%s\t\t\t%s\t\n",head->num,head->name,head->tel);
head=head->next;
}
}
struct stu *del(struct stu *head)
{
struct stu *p,*pf;
int n;
if(head==NULL)
{ printf("no list!"); goto end; }
printf("input position\n");
scanf("%d",&n);
p=head;
while(p->num!=n&&p->next!=NULL)
{ pf=p; p=p->next;}
if(p->num==n)
{
if(p==head) head=head->next;
else pf->next=p->next;
free(p);
}
else
printf("There is no this number!\n");
end: return head;
}
void search(struct stu *head)
{
char name[20];
int flag=0;
printf("Input name\n");
scanf("%s",name);
while(head!=NULL)
{
if(strcmp(name,head->name)==0)
{flag=1; break;}
else
head=head->next;
}
if(flag==1)
{printf("num\tname\t\t\ttel.number\n");
printf("%d\t%s\t\t\t%s\n",head->num,head->name,head->tel);
}
else
printf("not found!\n");
}
struct stu *add(struct stu *head)
{
struct stu *pnew,*p,*pb;
pnew=(struct stu*) malloc(sizeof (struct stu));
printf("input posision:");
scanf("%d",&pnew->num);
printf("input name:");
scanf("%s",pnew->name);
printf("input telnumber:");
scanf("%s",pnew->tel);
if(head==NULL)
{head=pnew; head->next=NULL;}
else
p=pb=head;
while(p!=NULL)
{ if(p->num==pnew->num)
{pb->next=pnew;
pnew->next=p;
while(p!=NULL)
{(p->num)=(p->num)+1; p=p->next;}
break;
}
else
if(p->num>pnew->num)
{pb->next=pnew;
pnew->next=p;
break;
}
pb=p;
p=p->next;
}
if(p==NULL)
{pb->next=pnew; pnew->next=NULL;}
return head;
}
void main()
{
int i;
struct stu *head=NULL;
while(1)
{
printf("********Welcome!********\n");
printf("press 1......creat\n");
printf("press 2......add\n");
printf("press 3......search\n");
printf("press 4......print\n");
printf("press 5......delete\n");
printf("press 6......exit\n");
printf("***********************\n");
scanf("%d",&i);
switch(i)
{
case 1 :printf("how many?\n");
scanf("%d",&i);
head=creat(i);
break;
case 2 : head=add(head); break;
case 3 : search(head); break;
case 4 : print(head); break;
case 5 : head=del(head); break;
case 6 : exit(0); break;
}
printf("\nFinished! Press any key...\n");
getch();
}
}