自己写的链表的操作:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
#define N 5
#include<stdlib.h>
struct student
{long num;
char name[10];
float score;
struct student *next;
};
int n;
struct student *creat(void)
{struct student *p,*q;
int i=0;
n=1;
q=(struct student *)malloc(LEN);
q->num=100;
q->next=NULL;
for(i=1;i<=N;i++)
{p=(struct student *)malloc(LEN);
scanf("%ld,%f,%s",&p->num,&p->score,p->name);
p->next=q->next;
q->next=p;
n++;
}
return(p);
}
void print(struct student *q)
{struct student *p;
printf("there are %d student in the list ,they are:\n",(n-1));
p=q;
if(q!=NULL)
do{
printf("%ld %.2f %s\n",p->num,p->score,p->name);
p=p->next;
}while(p!=NULL);
}
struct student *insert(struct student *q,int i)
{struct student *p,*s;
int j=0;
p=q;
while(p&&j<(i-1)) {p=p->next;j++;}
if(!p||j>i-1) printf("wrong,input again");
else
{ printf("input the student's message which you want to insert:\n");
s=(struct student *)malloc(LEN);
scanf("%ld,%f,%s",&s->num,&s->score,s->name);
s->next=p->next;
p->next=s;
n=n+1;
}
return(q);
}
struct student *search(struct student *q,int i)
{struct student *p;
int j=1;
p=q;
while(p&&j<i)
{p=p->next;
j++;
}
if(!p||j>i)
printf("error!");
else
{printf("the number of %d student's message are:\n",i);
printf("%ld,%.2f,%s\n",p->num,p->score,p->name);}
}
struct student *deleted(struct student *L,int i)
{struct student *p,*q;
int j=0;
p=L;
while(p->next&&j<(i-1))
{p=p->next;
j++;}
if(!(p->next)||j>(i-1)) printf("error!");
q=p->next;
p->next=q->next;
printf("the num of %ld studnet was deleted:\n",q->num);
n=n-1;
}
int main()
{struct student *head,*stu;
int i,j;
printf("**************\n");
printf("**************\n");
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
printf(" ");
for(j=1;j<=(2*i-1);j++)
printf("*");
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=4-i;j++)
printf(" ");
for(j=1;j<=(2*i-1);j++)
printf("*");
printf("\n");
}
printf("**************\n");
printf("**************\n");
printf("input the student's message:num,score,name(end with 0,0,0)\n");
head=creat();
print(head);
printf("\n\n\n");
printf(" 1-charu\n");
printf(" 2-shanchu\n");
printf(" 3-chazhao\n");
printf(" 4-dayin\n");
printf("input a number(1-4) to make a choose\n");
scanf("%d",&i);
switch(i)
{ case 1:printf("input before which studnet you want to insert:\n");
scanf("%d",&i);
while(i)
{head=insert(head,i);
print(head);
printf("input before which studnet you want to insert:\n");
scanf("%d",&i);
} break;
case 2:printf("which number of the student do you want to deleted:\n");
scanf("%d",&i);
while(i)
{head=deleted(head,i);
print(head);
printf("which number of the stuent do you want to deleted:\n");
scanf("%d",&i);
}
break;
case 3:printf("which number of the student do you want to search:\n");
scanf("%d",&i);
while(i)
{head=search(head,i);
print(head);
printf("which of the studnet do you want to search:\n");
scanf("%d",&i);
} break;
case 4:print(head); break;
default :printf("the number you input are wrong:\n");
}
system("pause");
return 0;
}