problem with merge of 2 sorted linked lists(c)..

Discussion in 'C' started by wantedmit, Apr 6, 2009.

  1. wantedmit

    wantedmit New Member

    Joined:
    Apr 6, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi,im new around..
    im a freshman studying cs..(c language)..
    im sitting on a program for over 6 hours and im sure theres a pretty simple/sleek/fast soultion.
    i have 2 sorted link lists which i need to combine into 1 sorted linked list(without generating a new linked list)in other words i need to merge them.
    what I mean is lets say ive got an input that looks like this:
    first linked list - 1,3,5(1->3->5)
    second linked list-2,4,6(2->4->6)
    i need to change the pointers so that after my input goes through the function i will get an output of (1->2->3->4->5->6).(-> is a pointer for those who didnt get it).
    i need to change the "next" pointer of each node.
    ive got the program running on some of my inputs but its pretty sloppy and doesn't work for most inputs.
    ive searched the web and found some codes that are similiar but not exactly what i need to do..
    i really need help(hope there are pros out here)..
    thanks alot..looking forward for helping here too.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    One thing that can really help is to work out the algorithm by drawing it out on paper. e.g.
    Code:
    (1)->(3)->(5)
    (2)->(4)->(6)
    
    Then you'll need a couple of pointers to point to the current location in each list.
    Each pointer will need advancing down the list depending on which points to a smaller value.

    It's not too hard. Have a go, then post the code when you get stuck.

    > ive searched the web and found some codes that are similiar but not exactly what i need to do..

    Good. If you're interested in learning to program, then you will NEED to work through stuff yourself. Copying other people's examples teaches you ABSOLUTELY NOTHING.
     
  3. wantedmit

    wantedmit New Member

    Joined:
    Apr 6, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    xpiotos..thanks for your advice..believe me ive gone through alot.
    and as a matter of fact i took a paper and did the stuff you recomended.
    ive got the algorithm working for only some inputs(e.g. the example ive written on the paper).
    i tried fixing it up and made a mess.
    im working on this solution for over 6 hours today and almost double the time yesterday.
    i really want to learn but sometimes when you see someones algorithm you can learn alot more than doing something wrong yourself.
    its a new subject for me the linked lists,and you prefer not helping me by giving me a written algorithm then thats ok,but can you give me a clue or a good what to work?because working by an example is really not effiecent(at least on this situation).
    thanks!
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, where are you stuck? Have you got ANY code at all?
    Post it with some notes on what exactly it's doing wrong and where you think the problem might be.
    I'll have a look and see if I can point you in the right direction.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice