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