C語言單鏈表的建立,尋找,添加,刪除,修改功能實現

來源:互聯網
上載者:User

    這個程式是我在初學單鏈表的時候編寫出來的代碼,所以還不完整,希望對各位在理解單鏈表的時候起到一點兒的協助,而且歡迎各位同學討論技術,以技術廣交天下好友。本人QQ號:648422746

 

 

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#definelen(structstaff*)malloc(sizeof(structstaff))
structstaff
{
 charname[10];
 intsalary;
 structstaff*next;
};
structstaff*creat();
voidprint(structstaff*);
structstaff*find(structstaff*);
structstaff*addmessage(structstaff*);
structstaff*deletemessage(structstaff*);
structstaff*changemessage(structstaff*);
main()
{
 structstaff*head,*p; 
 head=creat();
 print(head);
 p=find(head);
 head=addmessage(head);
 head=deletemessage(head);
 head=changemessage(head);
}

structstaff*creat()
{
 structstaff*head,*p1,*p2;
 intn;
 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;
 returnhead;
}
voidprint(structstaff*head)
{
 structstaff*p;
 p=head;

while(p!=NULL)
 {
  printf("/n%s%d",p->name,p->salary);
  p=p->next;
   }
}
structstaff*find(structstaff*head)
{
 structstaff*p1;
 charfindname[10];
 p1=head;
 printf("/npleaseinputyoufindname:/n");
 scanf("%s",findname);
  while(p1!=NULL)
  {
   if(strcmp(findname,p1->name)==0)
   {
    printf("/n%s%d",p1->name,p1->salary);
    returnp1; 
   }
      else
    p1=p1->next;
  }
   if(p1=NULL)
  printf("/n沒有你要尋找的學生的資訊/n");
}
structstaff*addmessage(structstaff*head)
{
 structstaff*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);
 returnhead;
}
structstaff*deletemessage(structstaff*head)
{
 structstaff*p1,*p2;
 chardeletename[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;
 }
    elseif(strcmp(deletename,p1->name)==0)
 {
         p2->next=p1->next;
 }
 free(p1);
    print(head);
 returnhead;
}
structstaff*changemessage(structstaff*head)
{
 structstaff*p;
 charchangename[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);

}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.