implementation of singly linked list

Upload: sukhchain-singh-chheena

Post on 18-Jul-2015

62 views

Category:

Documents


0 download

TRANSCRIPT

  • 5/16/2018 Implementation of Singly Linked List

    1/9

    HOME LlIIIJX LOGIN

    Wednesday, Augusl26, 2009Implementation of Singly Linked ListA Linked l ist isa chain of structs or records called Nodes. Each node has at least too tweetmembers, one of which points to the next Node inthe list and the other holds thedata. These are defined as Single Linked Lists because they can only point to thenext Node inthe list but not to the previous.

    retweet

    01.02.03.04.05.

    int Datajstruct Node"Next;

    }"Head;

    viewplain printstruct Node{

    We use above structure for a Node in our example. Variable Data holds the data in the Nodewhi le pointer of type struct Node Next holds the address to the next Node inthe l is t. Head is apointer of type struct Nodewhich acts as the Headto the list.

    Initially we set 'Head' as NULL which means list isempty.

    I viewplain print ?01. //Set HEADas NULL02. Head=NULL;Let us see Insertion Functions ..addBeg, addEnd, addAt.

    viewplain print ?01. II Adding a Nodeat the Beginning of the List02.03. void addBeg(int num)04. {05. struct Node"temp;06 .07. temp=(struct Node")malloc(sizeof(struct Node;08 . temp->Data = num;09.10. if (Head == NULL)11. {12 . //List is Empty13. Head=temp;14. Head->Next=NULL;15.16. else17. {18. temp->Next=Head;19. Head=temp;20.21 .

    D A T A

    We create 'temp' node and save the Data part.

    If Head is NULL it means l ist isempty. So we set temp node as the Head of the list and set theNext as NULL. Else we set the Next intemp node as Head and reassign Head with temp.

    SEARCH THIS BLOGSearch

    FOLLOW ME ON TWITTER!

    LABELSBloggernnouncement

    C Comic Computer Organizat ionFacts

    Hacking Hardware How toIndia Unux Memo!),

    Open Source

    GCC

    Security Signals Windowsin T in

    Projecthostingandintegrationin thecloud.Subversio

    GitC O L l A B N E l : Icodesio~

    BLOG ARCHIVE~ 2010 (8),. 2009 (37)~ December(2)~ November (2)~ October (2)~ September (5),. August (3 )Implementation of Singly Linked ListSignals in Linux- Standard Signals

  • 5/16/2018 Implementation of Singly Linked List

    2/9

    01 .02.03.04 .05.06.07 .08 .09 .10 .11.12.13 .14.15.16 .17 .18 .19.20.21.22.23.24.25.26.27.28.29.

    view plain print ?//Adding a Node at the end of the listv oi d a dd En d( in t n um ){ s tr uc t N od e ' te mp l, ' te mp 2;

    t em pl = (s tr uc t N od e ' )m a ll oc (s iz e of (s tr uc t N od e ;templ->Data=num;II Copying the Hea d location into another node.temp2=Head;i f( He ad = = N UL L){

    II If List is empty we create First Node.Head=templ;Head->Next=NULL;else{

    I I Traverse down to end of the list.w hi le (t e mp 2- >N ex t ! = N UL L)temp2=temp2->Next;II Append at the end of the list.templ->Next=NULL;temp2- >Next=templ;

    t~n:'ip2H'!l i ld

    ~_NULL

    Signals in Linux- Basics~ July(1 )~ May(2)~ April (1)~ March (6)~ February (9)~ January(4)

    ~ 2008 (39)~ 2007 (6)

    AdChoices It >c. c++. C#courses Aptech50 lakh studentstrained already 22years of expertiseWJ II W .A p te c h- Ed u c at io n . c om

    Online CodingMade EasyTry CodeitRightOnlinetoday and Take a free45 minute web demoWNN.codeitrightonline.com

    Are You a Fresher?Join PlacementOriented 4 Month ITCourse Designed forFresh GradsNIIT .carnllT -Training

    CompellentStorage CenterWith Fluid DataTechnology to SlashStorage Cost Upto80%. Know More!WNN.Dell.com/CompellentLeast Known C++KeywordsLearn these rare C++Keywords Surpriseyourself!WNN.thejaywalker.net

    POPULAR POSTSImplementation of Singly Linked ListCache Memory- Direct Mapped Cache

    Cache Memory- Set Associative Mapped CacheTinTin E-Book collectionCache Memory- FullyAssociative Mapped Cacheiptables - Rate-limit incoming connectionsSingly Linked List in CTin Tin EBook Collection - Part2Using GOOGLE as proxyto hide IP-AdressTin Tin EBook Collection - Part3

    REALTIME TWITTER UPDATES

  • 5/16/2018 Implementation of Singly Linked List

    3/9

    view plain print ?01. II Adding a new Node at specified position02 .03. void addAt(int num, int loc)04. {05. int i;96. struct Node *tempJ *prev_ptrJ *cur _ptrj07 .0S. cur_ptr=Head;09 .10. if(loc > (lengthO+l) II loc

  • 5/16/2018 Implementation of Singly Linked List

    4/9

    01. II Deleting a node from List depending upon the data in the node.02.03. int delNodeData(int num)04. {

    view plain print ?

    95. struct Node *prev_ptr, *cur_ptrj06.07. cur_ptr=Head;0S.09. while(cur_ptr != NULL)10. {11. if(cur _ptr->Data == num)12. {13. if(cur _ptr==Head)14. {15. Head=cur _ptr->Next;16. free(cur_ptr);17. return 0;lS .19.20.21.22.23.24.25.26.27.2S.29.30.31.32.33. printfC\nElement %d is not found in the t.Lst ", num);34. return 1j

    else{

    prev_ptr->Next=cur _ptr->Next;free(cur_ptr);return Bj

    else{

    prev_ptr=cur _ptr;cur _ptr=cur _ptr->Next;

    35.

    If the value to be deleted isthe head of the l ist .

    If the value to be deleted is notthe head of the list.

    I:Llr_p'tr

    d . : : u r . _ p t r

    ~~~"ULL

    Similarly if we want to delete a node based on its location inthe l is t.

  • 5/16/2018 Implementation of Singly Linked List

    5/9

    01. /I Deleting a node from List depending upon the location in the list.02.03. int delNodeLoc(int loc)04. {

    view plain print ?

    95. struct Node *prev_ptr, *cur_ptrj06. int i;07.0S. cur_ptr=Head;09.10. if(loc > (lengthO) II loc Nextjfree(cur_ptr);return Bj

    else{

    for(i=l; iNext;

    prev_ptr->Next=cur _ptr->Next;free(cur_ptr);

    36.

    Displaying a list.view pla in print ?

    01. /I Displaying list contents02.03. void displayO04. {05. struct Node cur_ptr;06.07. cur_ptr=Head;0S.09. if(cur _ptr==NULL)10. {11. printf("\nList is Empty");12.13. else14. {15. printf(" Elements in the List: );16. /ltraverse the entire linked list17. ..hile(cur _ptrl =NULL)lS. {19. printf(" -> %d ",cur _ptr->Data);20. cur _ptr=cur _ptr->Next;21. }22. printf("\n");23.24.

    Reversing a list.view pla in print ?

    01. /lReversesing a Linked List02.03. void reverseO04. {as. struct Node *prev_ptr, *cur _ptr, *tempj06.07. cur_ptr=Head;0S. prev_ptr=NULL;09.10. ..hile(cur_ptr != NULL)11. {12. temp=prev_ptr;13. prev_ptr=cur _ptr;14.15. cur_ptr=cur_ptr->Next;16. prev_ptr->Next=temp;17.lS.19. Head=prev_ptr;20.

    For complete source code - Checkout Singly Linked List in C

  • 5/16/2018 Implementation of Singly Linked List

    6/9

    L A BE LS : C , D A TA S TR U CT UR E S

    32 comments:m anil kumar said.. .

    wry good and s impleOctober 29. 2009 9:13 AM

    Anonymous said ...This program helped me better understanding Linked Lis ts .. Thank you wry much.Noverrber 11, 20094:24 PM

    Anonymous said ...Thanks for posting an example.Decerrber 16, 200911:18AM

    Anonymous said ...d li lagi sya mo' run ay. ..January 18.201012:23 PM

    ~ Siva said ...Helps me really friendFebruary 2, 2010 11:29AM

    amar said.. .wry good example to understand the singly l ink l ist conceptFebruary 23, 2010 5:30 PM

    Anonymous said ...thank u wnmrmnyy much . .. .. post l ike these simple programs withcomments . .. .April 15, 2010 7:01 PM

    Anonymous said ...Thanks a lo t fo r posting the code. Very clear and clean code. Good Job.May 9.2010 12:01 AM

    Anonymous said ...i t's useful for ewryone ...wry great amazing ... .. .. .August 22, 2010 4:33 PM

    Anonymous said ...thanks a lot posting this example ... .. .. ..August 22, 2010 4:34 PM

    Anonymous said ...Thanks fo r posting these codes. It he lped me a lot to understand Linked List. Good job! Godbless. =)August 29, 2010 8:47 AM

  • 5/16/2018 Implementation of Singly Linked List

    7/9

    Amy said . ..it really wry he lp 4 my subject thanxSepterrber 6, 2010 11:14 AM

    m Rajiv Chandel said ...Thank youSepterrber 7, 2010 3:11 R>1

    m Rajiv Chandel said ...Thank youSepterrber 7, 2010 3:11 R>1

    Anonymous said ...easy to understand step wiseSepterrber 15, 2010 11:18AM

    m dileep said ...it he lped me a lot... thank u wry muchNoverrber 14, 2010 6:46 AM

    m dileep said ...it he lped me a lot... thank u wry muchNoverrber 14, 2010 6:46 AM

    dileep said ...

    Noverrber 14, 2010 6:47 AMsimply easy to understand ... . ty:)

    m joenayjoe said.. .

    February 12, 2011 12:47 AM

    wry good explanation and indeed WrIJ helful! ! i bookmarked your blog.. thanks for yourcontribution

    Anonymous said ...really helpful. . . thank you so muchMarch 17, 2011 12:31 AM

    Mayur said ...Great!!!March 17, 201111:44AM

    Anonymous said ...I was totally confused about linked lists and the set of codes set it out all clear to me in a wrysimp le manner. Kudos to the person for his effo rts to bring out things in a lucid manner to al l.Apr il 9,2011 6:02 R>1

    Anonymous said ...Wow! Thanks a lo t :-)Apr il 20, 2011 12:08 R>1

  • 5/16/2018 Implementation of Singly Linked List

    8/9

    m riddsaid ...grS post mate . .. really good job!April 28. 2011 8:08 AM

    Mahendra Gupta said.. .superb!!!!!!!!!!!!simple and easier to understandApr il 30, 2011 11:32AM

    Anonymous said ...While most of it works I dont get why the while loop to find the end of the list has become anightmare. I ts become some inf in ite loop i cant fix.f lley 5, 2011 3:23 RIl l

    m priyanka said ...simple and easy to understand ... .. ..f lley 5, 2011 4:43 RIl l

    Sneha said.. .really really effectiw codingAugust 20, 2011 9:25 AM

    islam said.. .i understand l inks wry easi ly

    islamAugust 20, 2011 5:22 RIl l

    Anonymous said ...wry helpful l in understanding the basics of l inked lists ... .. . thanks alaI. .. .August 28, 2011 10:10 RIl l

    Anonymous said ...excellent..n wry helpfuL!!Septerrber 2, 2011 10:33 RIl l

    Anonymous said ...wry helpfulSepterrber6,20111:17R111

    Post a CommentL in ks t o t his p o stCreate a Link

    Newer Post Home Older Post

  • 5/16/2018 Implementation of Singly Linked List

    9/9