Linked list (sort and delete)

Source: Internet
Author: User

#include <cstdio>#include<cstring>#include<algorithm>using namespaceStd;typedefstructlink{intnum; structLink *Next;} Ia;ia*Create (); IA*insert (IA *head, IA *p);voidPrint (IA *head);intMain () {IA* head =NULL; intnum; Head=Create (); scanf ("%d", num);    Delete (head, num); return 0;} IA*Create () {IA*head = NULL, *p; intnum;  while(~SCANF ("%d", &num) &&num >=0) {p= (ia*) malloc (sizeof(IA)); Pnum =num; Head=Insert (head,p);    } Print (head); returnHead;} IA*insert (IA *head,ia *p) {IA*ptr, *PTR1, *ptr2; PTR=p; PTR1= head;//PTR represents the inserted, PTR1 represents the header file, ptr2 transitions    if(Head = =NULL) {Head=p; PNext =NULL; }//The first number is put directly into a    Else    {         while(ptr-> num > ptr1, num && ptr1, next!=NULL) {PTR2= PTR1;//ptr2 Save head pointerPTR1 = Ptr1 Next;//if the size of P is greater than the head pointer then the head pointer goes down        }        if(PTR, num <= ptr1num) {                if(ptr1 = = head)//The head pointer does not move, the instructions are inserted on the lefthead = PTR;//The head pointer is P                ElsePtr2->next = ptr;//If the head pointer moves, then the next point of the head pointer is PPTR--next = PTR1;//at the end, the next point of P is the original head pointer                 }        Else{//Last numberPTR1->next =ptr; PTRNext =NULL; }     }        returnHead; }    voidPrint (IA *head) {IA*p; if(Head = =NULL)return ;  for(p = head; P! = NULL; p = Pnext) printf ("%d", P-num); printf ("\ n");} VOID*delete (IA *head,intnum) {IA*PTR1, *ptr2; if(Head = =NULL)return ;  while(head, num = num) {//if the smallest number is num then the first one is replacedPTR1 =Head; Head= HeadNext;    Free (PTR1); } ptr1= head;//PTR1 Record The current pointer, PTR2 record the next pointerPTR2 = HeadNext;  while(PTR2! = NULL) {//All the time until the end            if(ptr2 num = num) {//if the value of the next pointer is num then the next ptr1 is PTR2, and the PTR2 is covered.PTR1-Next = ptr2Next;            Free (PTR2); }            ElsePTR1 = Ptr2 Next;//Otherwise, then go throughPTR2 = Ptr1Next; } Print (head);}
View Code

Although the comment but still cannot write, must slowly grind Orz

Linked list (sort and delete)

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.