C language Single linked list of the establishment, find, add, delete, modify the function to achieve

Source: Internet
Author: User
Tags strcmp

This program is my Learning single linked list of the time to write code, so not complete, I hope that you understand the list of the time to play a little help, and welcome all students to discuss technology, to technology to make friends all over the world. I QQ number: 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) Please type the student's information (including first name, score):/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 does not have the information/n for the student you are looking for");
}
Structstaff*addmessage (Structstaff*head)
{
STRUCTSTAFF*P1,*P2;
P1=head;
P2=head;
P1=len;
printf ("N/a Please enter the information you want to add:/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 Delete student's information, type the student's name:/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 Modify the student's information, type the student's name:");
scanf ("%s", changename);
while (strcmp (changename,p->name)!=0)
{
p=p->next;
}
printf ("/n the student's information is:%s%d", p->name,p->salary);
printf ("/n please retype the student's information including name, Score:");
scanf ("%s%d", p->name,&p->salary);
printf ("/N All students ' information is as follows:/n");
Print (head);

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.