這個程式是我在初學單連結清單的時候編寫出來的代碼,是以還不完整,希望對各位在了解單連結清單的時候起到一點兒的幫助,而且歡迎各位同學讨論技術,以技術廣交天下好友。本人QQ号:648422746
#include
#include
#include
#include
#define len (struct staff *)malloc(sizeof(struct staff))
struct staff
{
char name[10];
int salary;
struct staff *next;
};
struct staff *creat();
void print(struct staff *);
struct staff *find(struct staff *);
struct staff *addmessage(struct staff *);
struct staff *deletemessage(struct staff *);
struct staff *changemessage(struct staff *);
main()
{
struct staff *head,*p;
head=creat();
print(head);
p=find(head);
head=addmessage(head);
head=deletemessage(head);
head=changemessage(head);
}
struct staff *creat()
{
struct staff *head,*p1,*p2;
int n;
n=0;
head=NULL;
p1=len;
printf("/n請鍵入學生的資訊 (包括名字,分數):/n");
scanf("%s%d",p1->name,&p1->salary);
while(p1->salary>0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=len;
scanf("/n%s%d",p1->name,&p1->salary);
}
p2->next=NULL;
return head;
}
void print(struct staff *head)
{
struct staff *p;
p=head;
while(p!=NULL)
{
printf("/n%s%d",p->name,p->salary);
p=p->next;
}
}
struct staff *find(struct staff *head)
{
struct staff *p1;
char findname[10];
p1=head;
printf("/nplease input you find name:/n");
scanf("%s",findname);
while(p1!=NULL)
{
if(strcmp(findname,p1->name)==0)
{
printf("/n%s%d",p1->name,p1->salary);
return p1;
}
else
p1=p1->next;
}
if(p1=NULL)
printf("/n沒有你要查找的學生的資訊/n");
}
struct staff *addmessage(struct staff *head)
{
struct staff *p1,*p2;
p1=head;
p2=head;
p1=len;
printf("/n請輸入你要添加的資訊:/n");
scanf("/n%s%d",p1->name,&p1->salary);
if(head!=NULL)
{
p2=head;
p1->next=p2;
head=p1;
}
else
head=p1;
print(head);
return head;
}
struct staff *deletemessage(struct staff *head)
{
struct staff *p1,*p2;
char deletename[10];
p1=head;
p2=head;
printf("/n删除學生的資訊,請鍵入該學生的姓名:/n");
scanf("%s",deletename);
while(strcmp(deletename,p1->name)!=0)
{
p2=p1;
p1=p1->next;
}
if(strcmp(head->name,p1->name)==0)
{
head=p1->next;
}
else if(strcmp(deletename,p1->name)==0)
{
p2->next=p1->next;
}
free(p1);
print(head);
return head;
}
struct staff *changemessage(struct staff *head)
{
struct staff *p;
char changename[10];
p=head;
printf("/n修改學生的資訊,請鍵入該學生的名字:");
scanf("%s",changename);
while(strcmp(changename,p->name)!=0)
{
p=p->next;
}
printf("/n該學生的資訊為:%s%d",p->name,p->salary);
printf("/n請重新鍵入該學生的資訊包括名字,分數:");
scanf("%s%d",p->name,&p->salary);
printf("/n全部學生的資訊如下:/n");
print(head);
}