Linked List operations

Source: Internet
Author: User

I tried the linked list.

It's easy. Two ordered linked lists are combined into one ordered linked list.

 

It's very simple, but I cannot write it out. Today I have a good review of some operations on the linked list.

 

Typedef struct lnode {<br/> int data; <br/> struct lnode * pnext; <br/>} lnode, * linklist; </P> <p> bool listinsert (linklist & L, int I, int e) <br/> {<br/> lnode * P = L; <br/> Int J = 0; <br/> while (P & J <i-1) <br/>{< br/> P = p-> pnext; <br/> J ++; <br/>}< br/> lnode * q = (lnode *) malloc (sizeof (lnode )); <br/> If (q = NULL) <br/>{< br/> printf ("Memory Allocation Error! /N "); <br/> return false; <br/>}< br/> q-> DATA = E; <br/> q-> pnext = p-> pnext; <br/> P-> pnext = Q; </P> <p> return true; <br/>}</P> <p> bool listdelete (linklist & L, int I) <br/>{< br/> Int J = 0; <br/> lnode * P = L; <br/> lnode * q = NULL; <br/> while (j <i-1) <br/>{< br/> P = p-> pnext; <br/> If (P = NULL) // exceeds the limit <br/>{< br/> return false; <br/>}</P> <p> q = p-> pnext; <br/> P-> pnext = Q-> pnext; </P> <p> free (Q); </P> <p> r Eturn true; <br/>}</P> <p> // header node not included <br/> int listlength (linklist & L) <br/> {<br/> lnode * P = L; <br/> int I = 0; <br/> while (p-> pnext) <br/>{< br/> I ++; <br/> P = p-> pnext; <br/>}</P> <p> return I; <br/>}</P> <p> // The deletion efficiency is too low <br/> bool listdestory (linklist & L) <br/>{< br/> int nlen = listlength (l); <br/> for (INT I = nlen; I> 0; I --) <br/>{< br/> listdelete (L, I); <br/>}< br/> // release the header node <br/> free (L ); <br/> return true; <br/>} </P> <p> // combine two ordered lists into an ordered list <br/> void mergelist (linklist & la, linklist & Lb, linklist & lc) <br/> {<br/> lnode * pA = La-> pnext; <br/> lnode * pb = LB-> pnext; <br/> lnode * Pc = NULL; <br/> lc = pc = La; <br/> while (PA & Pb) <br/>{< br/> If (Pa-> data <= Pb-> data) <br/>{< br/> PC-> pnext = PA; <br/> PC-> pnext; <br/> pa-> pnext; <br/>}< br/> else <br/>{< br/> PC-> pnext = Pb; <br/> Pc = pc-> pnext; <br/> Pb = Pb -> Pnext; <br/>}</P> <p> PC-> pnext = pa? PA: Pb; <br/> free (LB); // release the LB header node. </P> <p >}</P> <p> // print out <br/> void listprint (linklist & L) <br/>{< br/> lnode * P = L-> pnext; <br/> while (P) <br/>{< br/> printf ("% d/T", p-> data); <br/> P = p-> pnext; <br/>}</P> <p> printf ("/N"); <br/>}</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> linklist LA = (linklist) malloc (sizeof (lnode); <br/> linklist lB = (linklist) malloc (sizeof (lnode); <br/> linklist lc = NULL; <br/> La-> pnext = NULL; <br/> LB-> pnext = NULL; </P> <p> for (INT I = 1; I <11; I ++) <br/> {<br/> listinsert (La, I, I ); <br/> listinsert (LB, I, I); <br/>}< br/> listprint (LA); </P> <p> mergelist (La, LB, lc); <br/> listprint (LC); <br/> listdestory (LC); <br/> getchar (); <br/> return 0; <br/>}< br/>

 

You can study it later.

These things are very clear in a small research.

If you don't need it for a long time, you can't remember anything. Remember to be despised by others: even linked list operations won't happen. I am very depressed: Don't insult my IQ.

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.