Single linked list (build, insert, delete, sort, reverse, print)

Source: Internet
Author: User

#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<curses.h>using namespaceStd;typedefstructstudent{intdata; structStudent *Next;} Node;node* Creat (void) {node*head,*p,*s; intx,cycle=1; Head= (node*) malloc (sizeof(node)); P=Head;  while(cycle) {printf ("\nplease input The data (0 means stop):"); scanf ("%d",&x); if(x) {s= (node *) malloc (sizeof(node)); S->data=x; printf ("\n%d",s->data); P->next=s; P=s; }        Elsecycle=0;//input X=0,then cycle=0,the for cycle are over;} head=head->next;//remove Headerp->next=NULL;returnhead;}intLength (node *head)//measuring the length of list{    intn=0; Node*p; P=Head;  while(p) {p=p->Next; N++; }    returnN;}intPrint (node *head)//Print the list{node*p; intn=0, i=1; N=Length (head); printf ("\nnow.these%d Records is:", N); P=Head; if(!head)return-1;  while(p) {printf ("\ n%d",p->data); P=p->Next; }}node* DEL (node *head,intNum//Delete the node whose data = = num{node*p1,*P2; P1=Head;  while(Num!=p1->data && p1->next!=NULL) {P2=P1; P1=p1->Next; }    if(num = = p1->data) {        if(P1 = =head) {Head=p1->Next;        Free (p1); }        ElseP2->next=p1->Next; }    Elseprintf ("\n%d could not been found", num); returnHead;} Node* Insert (Node *head,intnum) {Node*p0,*p1,*P2; P1=Head; P0= (node *) malloc (sizeof(node)); P0->data=num;  while(P0->data > P1->data && p1->next! =NULL) {P2=P1; P1=p1->Next; }    if(P0->data <= p1->data) {        if(Head = =p1) {P0->next=P1; Head=P0; }        Else{p0->next=P1; P2->next=P0; }    }    Else{P1->next=P0; P0->next=NULL; }    returnHead;} Node* Sort (Node *head) {Node*p,*p2,*P3; intn,temp; N=Length (head); if(Head==null | | head->next==NULL)returnHead; P=Head;  for(intj=1; j<n;++j) {P=Head;  for(intI=0; i<n-j;++i) {if(P->data > p->next->data) {Temp= p->data; P->data = p->next->data; P->next->data =temp; } P= p->Next; }    }    returnHead;} Node* Reverse (Node *head) {Node*p1,*p2,*P3; if(Head==null | | head->next==NULL)returnHead; P1=Head; P2= p1->Next;  while(p2) {P3= p2->Next; P2->next =P1; P1=P2; P2=P3; } head->next =NULL; Head=P1; returnhead;}intMain (intargcChar**argv) {Node*Head; intNum,n;  while(1) {printf ("\ n----------------"); printf ("\n******menu******|"); printf ("\n0:exit |"); printf ("\n1:cerat List |"); printf ("\n2:delete a Node |"); printf ("\n3:insert a Node |"); printf ("\n4:print List |"); printf ("\n5:sort List |"); printf ("\n6:reverse List |"); printf ("\ n----------------"); printf ("\ Chose your number listed above:"); scanf ("%d",&num); Switch(num) { Case 0:return 0;  Case 1: Head=creat (); Break;  Case 2: printf ("\nchose the number you want delete:"); scanf ("%d",&N); Head=del (head,n);  Break;  Case 3: printf ("\nchose the number you want insert:"); scanf ("%d",&N); Head=Insert (head,n);  Break;  Case 4: Print (head); Break;  Case 5: Head=sort (head); Break;  Case 6: Head=reverse (head); Break; }    }}

Single linked list (build, insert, delete, sort, reverse, print)

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.