Thread: Appending in Doubly Liked list

  1. #1
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16

    Appending in Doubly Liked list

    Been battling this program for the last week...
    The problem I am facing is that it will clear the list if I try to call this function again.
    Example:
    Code:
    Output:
    Calling append function
    Adding 3 to rear...
    [3]
    Calling it again...
    Adding 50 to rear
    [50] 
    Instead of being [3 50]
    Same thing happens if I try inserting in the front after inserting in rear... Then, try inserting in rear again. It will clear the list.

    This is it right now...:

    Code:
    void append(List currentList, int value)
    { 
        Node newNode = createNode(value, NULL, currentList->head);
        Node currentNode = currentList->head; 
        ++currentList->size; //increase size
          if(currentList->head) == NULL){
             currentList->head = newNode;
             printf("added at beginning\n");
          }
          else 
          {
            //else tranverses through the list to the node, then inserts next to it. 
            node *current = head;
            while (currentNode != NULL ) { 
                if(current->head == NULL)
                {
                    currentList->head = newNode;
                    printf("added later\n");
                    break; 
                }
                currentNode = currentList->head; 
                         };
           }
    }
    Any advice or see any problems with it?
    Full code of implementation of ADT is: C code - 218 lines - codepad
    (Can provide header files, Test file, and Node.c if requested)
    Please let me know if I am not being clear

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your code seems strange.
    You need to provide the other files.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It would be best if you could trim down the program to a simple, yet complete program that compiles and exhibits the problem you're seeing.

    It appears you're typedef'ing pointers, which is often not recommended as this tends to make the code more difficult to follow and get right.

    There also appears to be different types in use. For instance, line 3 of the code you posted declares a variable of type "Node", whereas line 13 declares a variable of type "node". The former implies a typedef'd pointer, whereas the latter does not.

  4. #4
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    List test: C code - 89 lines - codepad
    Node.c:C code - 43 lines - codepad
    Node.h:C code - 62 lines - codepad

    And Sorry that was error about line 13... Meant to remove that when posting... Had it on ssh removed when compiled but forgot to remove it on JGRASP (where I pasted it from).

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have not posted "list.h", so we can't compile what you've provided.

    There may be more code here than you can reasonably expect someone to pick through and test for you.

    I suggest either one of these two options:

    1. Run your code through a debugger and see if you can trace the cause of the problem yourself (recommended); or
    2. Provide a simplified version of the code that exhibits the problem, as I originally suggested: Short, Self Contained, Correct Example

    I will also once again recommend you do not typedef pointers.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If you want someone to help you you need to make that as easy as possible.
    In this case you need to:
    * Compile your program with a decent warning level (e.g., with gcc use -W -Wall).
    (Actually, there are outright syntax errors in the code you've posted.)
    * Run it to check that what you say is happening is still happening.
    * Zip all the necessary files into a zip file.
    * Post that somewhere.
    Last edited by algorism; 03-03-2016 at 08:34 AM.

  7. #7
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Sorry I thought I did...
    List H:
    C code - 71 lines - codepad
    Sorry!!!
    I am 98% sure that the problem is within the append function as I just posted that short post of it.
    As my prepend function works smoothly.
    I understand what you're saying about typedef pointers. My professor did in two of his functions in example of singly linked list that he had us edit to be doubly linked list. (My program)

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Nope. Your code contains way too many errors, especially basic syntax errors (missing semi-colons, wrong number of parenthesis, etc) and undeclared types.

    If you really expect people to look over your entire program spanning multiple sources (which is generally unlikely), then at the very least you should ensure it compiles.

    I tried to do you a favor by re-creating your project in an attempt to help find the problem, but all it has done is waste my time.

    I am now done with this thread. Best of luck to you.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For the regulars, I don't know if this is a cross-post or just the same "boiler-plate" code, but be aware: Doubly-Linked Circular List in C- Insert at Position/ Removal / Print Functions - Stack Overflow

  10. #10
    University Student
    Join Date
    Sep 2015
    Location
    United States
    Posts
    16
    Uh....
    No it doesn't... Unless I posted the wrong code...
    I can show you on my SSH server where it compiles perfectly fine.
    Just two warnings.
    That have to do with a %d declartion.
    But I thank you for the effort.
    THanks for links.
    And that... I know the problem lies within that function.
    Just wanted to understand why it clears the list and does not change head to not being NULL after I append it once.
    Sorry for any confusion.DOne with thi thread
    Last edited by marchmadness; 03-03-2016 at 10:02 AM.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    main.c|43|warning: implicit declaration of function 'append_Helper'|
    main.c|44|error: expected ';' before 'printf'|
    Code:
    // line 43:
    append_Helper(newList)
    After adding the semi-colon:

    Code:
    main.c||In function 'main':|
    main.c|43|warning: implicit declaration of function 'append_Helper'|
    list.c||In function 'append':|
    list.c|43|error: expected expression before '==' token|
    list.c|43|error: expected statement before ')' token|
    list.c|47|error: 'else' without a previous 'if'|
    list.c|50|error: 'node' undeclared (first use in this function)|
    list.c|50|error: (Each undeclared identifier is reported only once|
    list.c|50|error: for each function it appears in.)|
    list.c|50|error: 'current' undeclared (first use in this function)|
    list.c|50|error: 'head' undeclared (first use in this function)|
    ||=== Build finished: 8 errors, 1 warnings ===|
    Stopped there.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marchmadness
    No it doesn't... Unless I posted the wrong code...
    I can show you on my SSH server where it compiles perfectly fine.
    Just two warnings.
    That have to do with a %d declartion.
    Supposing that you posted code different from what you actually compiled... did you compile at a high warning level? Furthermore, warnings that have to do with format specifiers does not mean "compiles perfectly fine". I would reserve "compiles perfectly fine" for compilation at the highest warning levels with no warnings, or warnings that are deemed spurious after expert examination. Warnings that have to do with format specifiers typically should not be ignored.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked list
    By msky in forum C Programming
    Replies: 0
    Last Post: 11-11-2011, 04:25 PM
  2. Appending a list!
    By GCNDoug in forum C++ Programming
    Replies: 14
    Last Post: 10-06-2008, 02:19 PM
  3. Appending new file to a list
    By dopejack in forum C Programming
    Replies: 3
    Last Post: 08-07-2006, 12:28 PM
  4. Doubly-Linked List
    By jgs in forum C Programming
    Replies: 7
    Last Post: 04-18-2005, 01:39 PM
  5. Doubly Linked List.. please Help!!
    By ProgrammingDlux in forum C++ Programming
    Replies: 8
    Last Post: 10-24-2004, 08:27 PM

Tags for this Thread