Thread: add linked list item to the top

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    add linked list item to the top

    ive been at this for a while now trying to understand the logic of adding an item to the top/beginning of a linked list.

    Code:
    //////////////////////////////////////////
    struct link        //one element of list
    {
        int data;    //data item
        link* next;    // pointer to the next link
    };
    ////////////////////////////////////////////
    void linklist::additem(int d)        //add item
    {
        link* newlink = new link;        //make a new link
        newlink->data = d;                //give it data
        newlink->next = dummy;            //it points to the next link
        dummy = newlink;                //now dummy points to this
    }
    
    void linklist::display()            //display all links
    {
        link* current = dummy;    //set ptr to dummy link
        while( current != NULL )        //quit on last link
        {
            cout << current->data << endl;        //print data
            current = current->next;        //move to next link
        }
    }
    dummy is set to null in the constructor. now, in a previous thread, i got the concept of whats going on here. dummy points to the newest link and when the program outputs the display() it basically has the 'LIFO' output just like a stack. what i want is for it to be 'FIFO' (first in-first out). im not asking for code to fix this, just a hint in the right direction. thanks in advance.

    edit:
    the exercise in my book says to only revise the additem().

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    [HINT]

    Well you would iterate through your linked list until the last node is found. Once you are at the last node you would . . . . . .

    [/HINT]
    Last edited by andyhunter; 02-11-2005 at 10:04 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    I think it needs changing the add function so that the pointers point the other way in the list and keeping hold of the first item in the list

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    I think it needs changing the add function so that the pointers point the other way in the list . . . .
    Geeze, I know how to implement this and you're confusing me.

    Just a quick rant, the OP wants to be pointed in the right direction aka NO CODE unless he specifically asks for it. I am tired of these smrt ..........es coming in posting solutions (that don't work anyway) robbing the OP of the joy of figuring it out.

    That's right, "smrt, I am so smart, smrt"

    By the way wgdb198 this isn't directed at you, just a preemptive strike.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Its cool. I`ve only been doing C++ for a week and I shouldn`t be giving away too much. I`ll get the balance right one day.
    I`m more used to Java.
    I was going to ask a question but looking at the answers on this site it might not be such a great idea.
    Some people seem to spend too much time worrying over whether main should return an int.
    I always do but what's the point if you don't use it for debugging purposes?

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Some people seem to spend too much time worrying over whether main should return an int.
    I always do but what's the point if you don't use it for debugging purposes?
    Oh no, Salem is going to come over and burn down your house and kill your family while you watch!!!!!

    *runs away screaming*"Salem it wasn't me it was the infidel"

    Seriously though, read this FAQ

    BTW try not to hijack threads.

    -Andy
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Some people seem to spend too much time worrying over whether main should return an int.
    Well if you want to learn "dialect-C", which is full of void main, gets(buff), fflush(stdin) and a whole host of other compiler specific nonsense, and then wonder several years later why NONE of those ideas work with your new compiler, then go right ahead - it's not my problem. Of course you could make it my problem by handing over vast amounts of $$$ in return.

    It's all in the detail, and C has so many ways to bite you that you simply cannot affort to be sloppy with it. This means actually learning what the language should do (eg int main), rather than what "my current compiler will let me get away with in this program" (eg void main).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Sorry again (I apologised in the thread I started as well)

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    One solution is to add an additional class member to linklist called 'last'. last will always point to the last link, and first will always point to the first link. Then, you can use last to add newlink's to the chain.

    Go back to that previous thread, and look at the diagram in the upper right.

    In step 3, don't set next equal to first--set it equal to NULL, so it will be NULL every time and stands ready to be assigned the next newlink.

    For step 4, you only want to perform that step for the first newlink, so you need an if statement that can determine when newlink is the first link created. Hint: what will first equal the first time you create a newlink?

    Then, you need to add some steps using 'last'. After you create the newlink, you can use last to access 'next' in the previous link. Then, you can assign the newlink to 'next', which will add newlink to the chain. Once you've used last to access 'next' and point 'next' to the newlink, you can overwrite last, and assign the newlink to it.

    If you are not allowed to add class members, or don't want to, try the solution outlined above anyway, and once you get that working, it should be a simple matter to figure out how to get last without having to store it in a member variable.
    Last edited by 7stud; 02-12-2005 at 10:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. how to add a note to the begining of linked list
    By merixa in forum C++ Programming
    Replies: 8
    Last Post: 10-24-2005, 02:10 AM
  5. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 06:26 PM