Singly-linked list

This is a discussion on Singly-linked list within the C++ Programming forums, part of the General Programming Boards category; I want to add an element to the list before the selected one. could anyone fix my code? Because it ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Singly-linked list

    I want to add an element to the list before the selected one. could anyone fix my code? Because it doesn't work for me.

    Code:
    List *get(int n)
    {
     List *pcurr = h;
     for (int i = 0; i < n; i++) pcurr = pcurr->n;
     return pcurr;
    }
    
    
    void add(int n, int x)
    {
     List *pnew = new List;
     List *pcurr = get(n);
     pnew->x = x;
     if (h == NULL)
     {
      h = pnew;
      pnew->n = pnew;
     }
     else
     {
      pnew->n = pcurr;
      pcurr->n->n = pnew;
      if (n == 0) h = pnew;
     }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Can you explain me what your code do?
    I fear that you just copy the code from somewhere else, which is ok if you say so

    //also it seems that h is a global variable
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    Well, I just pasted a part of my function. Here's the rest:

    Code:
    struct List
    {
     int x;
     List *n;
     List *p;
    };
    
    
    List *h;
    
    
    void init()
    {
     h = NULL;
    }
    Function 'get' gets an index of the particular element. Now I want to create a function that would add an element before the one returned from get, however it's quite problematic for me. (it doesn't work properly)

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Perform the whole operation in a piece of paper. Then think of the steps you followed in the paper and transform them to an algorithm. Be aware of not losing your pointers!!

    Moreover, you see here the code I use for a simple linked list. It has comments for every line!
    //it's in c, but you will get the idea with the pointers.

    Also mind that in C++ we use classes instead of structs.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    if (h == NULL)
    {
        h = pnew;
        pnew->n = pnew;
    }
    That code looks a bit odd. Although it could be your intention, this creates a circular linked-list which does not seem to be what you truly want. Do you mean to set the next/previous pointers to NULL here? I'm guessing that the n/p pointers mean next/previous. The rest of your code does not appear to deal with the p/previous pointer as it should and this would make the given code sample a doubly linked list and not a singly linked list.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    You simply cannot do it the way you are trying to do it.
    To insert before an item you need a pointer to the item before that, which your 'get' function will not give you. Find another way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Singly Linked List C++
    By luckyali444 in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2011, 11:30 PM
  2. singly linked list
    By Annonymous in forum C Programming
    Replies: 22
    Last Post: 09-10-2011, 05:50 PM
  3. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  4. Singly Linked List
    By devarishi in forum C Programming
    Replies: 4
    Last Post: 12-23-2008, 11:00 PM
  5. need help with singly linked-list
    By vearns in forum C++ Programming
    Replies: 20
    Last Post: 04-09-2008, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21