Thread: C programming - Doubly linked list ! How to fix the error in displaying?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Cebu City, Philippines
    Posts
    1

    C programming - Doubly linked list ! How to fix the error in displaying?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
    
    typedef struct node
    {
    char *str;
    node *next;
    node *prev;
    
    };
    
    node *head = NULL;
    node *tail = NULL;
    
    void traverse();
    void process(char *a);
    
    void main()
    {
    char a[10];
    int index;
    
    int count=4;
    
    for(index=0;index<count;index++)
    {
    printf("\nEnter char%d: ",index+1);
    scanf("%s",a);
    process(a);
    }
    
    traverse();
    
    }
    void process(char *a)
    {
    node *tempN = (node*)malloc(sizeof(node));
    node *temp = (node*)malloc(sizeof(node));
    temp=head;
    
    if(temp==NULL)
    {
    tempN->str=a;
    tempN->next=NULL;
    tempN->prev=NULL;
    head=tempN;
    tail=head;
    }
    if(temp!=NULL)
    {
    temp=tail;
    tempN->str=a;
    tempN->next=NULL;
    tempN->prev=temp;
    temp->next=tempN;
    tail=tempN;
    }
    
    
    }
    void traverse()
    {
    node *temp;
    temp=head;
    
    while(temp != NULL)
    {
    printf("%s\n",temp->str);
    temp=temp->next;
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. You need to learn how to indent code.
    SourceForge.net: Indentation - cpwiki

    2. main returns an int, not void
    SourceForge.net: Void main - cpwiki

    3. You need to make an actual copy of the string (using malloc and strcpy).
    Just assigning a pointer is NOT a true copy of the string.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Fix your indentation, if your code is hard to read, it's easy to make mistakes, and hard to find or fix them. If it was simply a copy-paste error when posting, please edit your post to fix it, it helps us easily spot your problems.

    Also, in the future, please give more details. What is the "error in displaying"? Provide the input you gave the program, the output you expect to see and the output you actually see. Again, it helps us recreate and fix your problem.

    Next, ditch that crappy old Turbo C compiler, and get something modern. If you need it for school, I still suggest you get a better compiler for home (GCC, Code::Blocks/MinGW, Pelles C are all free and modern), use that to write your programs, and only use Turbo C to make sure your teacher can compile and run them. Here's a list of problems:

    • Don't #include conio.h. It's non-standard, and you aren't using any of it's features.
    • Global variables are bad (link). There are times when they're okay, but this is not such a time. Declare them in main and pass them as needed to your functions.
    • It's int main(void), and return an int at the end, usually 0 for success (see here for why it matters).
    • Your struct/typedef statement is wrong, so it wont even compile. The way you have it, the typedef isn't processed until the end of the struct definition, so when it reads the lines with next and prev, it doesn't know what type 'node' is. next and prev should be declared 'struct node' or you need to forward declare node by separating out the typedef and putting it on a line above the struct definition (see below).
    • Don't cast the return value of malloc. It is not required by modern compilers, and can actually hide errors. See here.


    Declaring your struct using one of the two methods below (they are 100% equivalent, it's really a question of which you prefer)
    Code:
    // Alternative 1, put the typedef above the node
    typedef struct node      node;
    struct node {
        char *str;
        node *next;
        node *prev;
    };
    // Alternative 2, use the struct name (not the typedef'ed name) for the members
    typedef struct node {
        char *str;
        struct node *next;
        struct node *prev;
    } node;
    Also, process() and traverse() are horrible function names. Pick names that describes what the function does, like insert and print.

    Your "displaying" problem, if we're talking about the same one, is it only prints the last node 4 times. That's because when you create and insert a node in the list, you simply point s to a. Thus, when you read the last string into a, every node in the list points to the same array in main(), and thus prints the same contents. You need to allocate space for a copy of the string (e.g. malloc -- remember +1 byte for the null terminator) and copy the string (strcpy).

    You have a memory leak in your insert function, you allocate 2 nodes, but you only need one. You actually malloc memory, store the address in temp, and on the very next line, overwrite that address by doing temp=head;. Just node *temp = head; would be fine.

    Lastly, make sure you free all your memory before you exit.
    Last edited by anduril462; 09-15-2013 at 01:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming - Doubly linked list
    By pghpens91 in forum C Programming
    Replies: 5
    Last Post: 07-12-2012, 07:56 PM
  2. Doubly linked list compile error
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2009, 12:43 AM
  3. doubly linked list
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 07-21-2009, 09:32 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. doubly linked list error.
    By noob2c in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2003, 10:49 PM

Tags for this Thread