Insert, delete of linked list

Source: Internet
Author: User

Structure:

1 struct student{2     int M_val; 3     struct student* M_next; 4 }node,*pnode;

Insert of the linked list:

1 //Trailing Insert2 voidLinkinittail (pnode*pphead)3 {4     intVal;5 Pnode Pnew,ptail;6      while(SCANF ("%d", &val) = =1)7     {8Pnew = (pnode)calloc(1,sizeof(NODE));9Pnew->m_val =Val;Ten         if(*pphead = =NULL) One         { A*pphead =pnew; -Ptail =pnew; -         } the         Else -         { -Ptail->m_next =pnew; -Ptail =pnew; +         } -     } +Ptail =NULL; A } at  - //head insertion without the need to record trailing nodes - voidLinkinithead (pnode*pphead) - { - Pnode pnew; -     intVal; in      while(SCANF ("%d", &val) = =1) -     { toPnew = (pnode)calloc(1,sizeof(NODE)); +Pnew->m_val =Val; -Pnew->m_next = *Pphead; the*pphead =pnew; *     } $ }Panax Notoginseng //Sequential Insertion - voidLinkinitorder (pnode*pphead) the { + Pnode pnew,pprevious,pcurrent; A     intVal; the      while(SCANF ("%d", &val) = =1) +     { -Pnew = (pnode)calloc(1,sizeof(NODE)); $Pnew->m_val =Val; $  -pcurrent = *Pphead; -Pprevious =NULL; the  -          while(Pcurrent && Pcurrent->m_val < pnew->m_val)Wuyi         { thePprevious =pcurrent; -Pcurrent = pcurrent->M_next; Wu         } -         if(Pprevious = =NULL) About         { $Pnew->m_next = *Pphead; -*pphead =pnew; -         } -         Else A         { +Pnew->m_next =pcurrent; thePprevious->m_next =pnew; -         } $     } the}

Link List deletion:

1 voidLinkdelete (pnode* Pphead,intvalue)2 {3Pnode pprevious = NULL, pcurrent = *Pphead;4      while(pcurrent && Pcurrent->m_val! =value)5     {6Pprevious =pcurrent;7Pcurrent = pcurrent->M_next;8     }9     if(Pcurrent = =NULL)Tenprintf"Not exist!"); One     Else A     { -         if(pprevious = = NULL)//Delete the head node. -         { the*pphead = pcurrent->M_next; -              Free(pcurrent); -         } -         Else +         { -Pprevious->m_next = pcurrent->M_next; +              Free(pcurrent); A         } at     } -  -}

Insert, delete of 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.