Thread: double linked list

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    8

    double linked list

    Hello gurus!

    I'm new in C and the following problem drives me crazy.

    I am trying to implement a double linked list.

    In main() I call the function as:

    Code:
    dlistInsertLast (&l, moveWhite[0], prevPos, nexPos);
    in DEBUG mode I can see that prevPos and nexPos are pointers with value prevPos='a1' and nexPos='a7'

    in function dlistInsertLast I assign the values in order to create my node etc...
    Code:
    void dlistInsertLast (dlist * lp, char p, char* ppos, char* npos)
    /* Copy the data */
       m->piece = p;
       strncpy(m->prevPos, ppos, sizeof(m->prevPos));           //prevPos is a char array since it is not assignable we use strncpy
       strncpy(m->nexPos, npos, sizeof(m->nexPos));             //to copy the data between the two
    in DEBUG mode ppos='a1' and npos='a7'

    now when I print my double linked list using function
    Code:
    void printDlist(dlist *lp)
    i get the following
    Previous Position : a1a7
    Next Position : a7

    which tells me that m->prevPos gets the value of both ppos and npos.

    Any help about why that happens?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    In the future, please provide a more complete code sample (all relevant code including type/struct and variable declarations), otherwise we can only guess. That being said, here's my guess:

    strncpy does not guarantee null termination of your strings. I would bet that you declared m->prevPos to be a char array of size 2, not leaving room for the null. Remember, any string in C needs enough room for all the characters you want to store, plus 1 for the terminating null. Declare prevPos and nexPos to be of size 3.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Here's a good doubly linked list implementation: http://www.snippets.24bytes.com/2010/06/double-linked-list.html

    tin askisi 2 kaneis?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    anduril462 THANKS A TON! IT WORKS!

    That was the issue... and imagine I was thinking that I had more room that needed (as at the end I got printed 4 letters instead of 2).

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    thanks for the link

    thn teleiwsa! douleyan ola ektos apo ayto pou to eixa afhsei sta global declarations kai oute eixa skeftei na to eleg3w!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mnml View Post
    Here's a good doubly linked list implementation: 24 Bytes: Double Linked List
    tin askisi 2 kaneis?
    I would not call that "good". I would call it rather amatuerish...
    • The code snippets have void main().
    • They use the oudated includes such as iostream.h.
    • They're missing "using namespace std;"
    • Some snippets frequently and redundantly use "this->".
    • Many of them have either no indentation or poor indentation (damn there's no barf smiley).
    • The sort routines have cout statements inside them.
    • Some snippets are very poorly implemented such as the very naieve "Is Prime Number".

    I also dislike the lack of whitespace in the coding style used there.
    Last edited by iMalc; 11-19-2011 at 08:45 PM.
    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"

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Quote Originally Posted by marlaFC View Post
    I'm new in C
    As for the rest, it is implemented in C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Double Linked List
    By ch4 in forum C Programming
    Replies: 10
    Last Post: 06-21-2007, 01:06 AM
  3. What is the use of Double Linked List?
    By Yin in forum C++ Programming
    Replies: 13
    Last Post: 04-17-2002, 05:20 AM
  4. What is double-linked list?
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 10:31 PM
  5. Double linked list
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-24-2001, 08:07 PM