Thread: Linked List Concept-ish

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    49

    Linked List Concept-ish

    I am having trouble adding a second node to a doubly linked list. I define my list as follows:

    Code:
    typedef struct Node {
      char command[256];
      int length;
      struct Node *prev;
      struct Node *next;
    } userInput;
    
    typedef struct q {
      userInput *head;
      userInput *tail;
    } inputQ;
    
    inputQ input = {NULL, NULL};
    userInput *newNode;
    For the 1st node, I do this:

    Code:
    input.head = newNode;
    input.tail = newNode;
    newNode->prev = NULL;
    newNode->next = NULL;
    And, for the 2nd node, I do this:

    Code:
    newNode->prev = input.tail;
    input.tail->next = newNode;
    input.tail = newNode;
    Now, when I iterate back through my list (I want to start at the tail and work my way towards the head), I can only ever get the 1st node to print, then the 2nd node is garbage, which means, to me, that I've linked something wrong. Can anyone please tell me where?

    Thank you all so much!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I see no obvious problem here, maybe your iteration is done incorrectly?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I think he/she don't allocate memory every time for a new node and therefore the only valid node will allways overwriten.But to check this, we should see the complete source code.
    Other have classes, we are class

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    49
    I don't want to share all the source code, because this is for a project in my Operating Systems class.

    The up-history I'm trying to implement is extra credit, but she has a clear policy of no source code online.

    So, the logic is fine then?

    I use a sys_alloc_mem function that our professor gave to us when giving us the kernel and other necessary components to boot our tiny OS which I call every time like this:

    Code:
    if((newNode = (userInput *)sys_alloc_mem(sizeof(userInput))) == NULL)
    {
        return NULL; //no memory
    }
    When I hit the up arrow key after typing in one command, I get that command back just fine. But, if I enter a new command, then try to hit the up arrow twice, it will print the first thing. Then, it will print a capital S and a white diamond with a black ? inside it which makes me think it is null.

    Is the above not right for allocating memory?

    Edit: It returns NULL, because the function it's in returns a userInput ptr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Sorting Linked Lists (code not concept)
    By Newbie Magic in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2004, 08:57 AM
  5. linked list - basic concept.
    By Vber in forum C Programming
    Replies: 4
    Last Post: 04-11-2003, 02:30 PM