C Language-linked list

Source: Internet
Author: User

Operation of the linked list #include<stdio.h> #include <malloc.h> #define NULL 0 #define LEN sizeof (struct student) struct student {Long num;float score;struct student *next;};/ /node int n;//store node number struct student *creat ()//create linked list {struct student *head;//head pointer struct student *p1,*p2;n=0;head=null;p1=p2= ( struct student*) malloc (LEN);//Allocate storage space printf ("Input num,score:\n"); scanf ("%ld,%f",&p1->num,&p1-> Score); while (p1->num!=0) {n=n+1;if (n==1)//First node HEAD=P1;ELSE{P2-&GT;NEXT=P1;P2=P1;} p1= (struct student*) malloc (LEN);p rintf ("Input num&&score:\n"); scanf ("%ld,%f", &p1->num,&p1- >score);} P2->next=null;return head;//Returns the head pointer}void print (struct student *head)//linked list output {struct student*p;printf ("\nnow,these%d Records are:\n ", N);p =head;if (head!=null) while (p!=null) {printf ("%ld\t%5.2f\n ", P->num,p->score);p =p-> Next;}} struct student *del (struct student *head,long num)//LINK list delete operation {struct student *p1,*p2;if (head==null)//Empty list {printf ("\ Nlistlink is null\n "); goto end;} P1=head;while (NUM!=P1-&GT;NUM&AMp;&p1->next!=null)//Find the node you want to delete, P1 point to the node you want to delete, P2 point to the previous node of the node you want to delete {P2=p1;p1=p1->next;} if (num==p1->num) {if (P1==head)//The node to be deleted is the first node head=p1->next;elsep2->next=p1->next;printf ("Delete%ld is succeed\n ", num); n=n-1;} elseprintf ("%ld not been found!\n", num); End:return head;} struct student *insert (struct student *head,struct student *stud)//list insert operation {struct Student *p0,*p1,*p2;//p1 holds the last node of the insertion position , p2 holds the previous node of the insertion position p1=head;p0=stud;//the node to be inserted if (head==null)//If the list is empty linked list {head=p0;p0->next=null;} Else{while (P0->num>p1->num && (p1->next!=null))//Find the location to insert {P2=p1;p1=p1->next;} if (p0->num<=p1->num) {if (HEAD==P1)//Insert position as first node, header Head=p0;elsep2->next=p0;p0->next=p1;} else{p1->next=p0;//insertion position is footer, p0->next=null;}} N=n+1;return Head;} void Main () {struct student *head,*stu;long del_num;printf ("Input records:\n"); Head=creat ();//Create linked list print (head);p rintf ("\ninput The Delete number:"), scanf ("%ld", &del_num), while (del_num!=0)//delete node {Head=del (head,del_num);p rint ( Head);p rintf ("\ninpuT the delete number: "); scanf ("%ld ", &del_num);} printf ("\ninput Insert Record:"); stu= (struct student*) malloc (LEN); scanf ("%ld,%f",&stu->num,&stu-> Score), while (stu->num!=0)//Insert Node {head=insert (Head,stu);p rint (head);p rintf ("\ninput Insert Record:"); Stu= ( struct student*) malloc (LEN); scanf ("%ld,%f", &stu->num,&stu->score);}}

C Language-linked list

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.