Thread: Double linked list

  1. #1
    Unregistered
    Guest

    Double linked list

    Sorry it has no doubt been asked before, but I can't find it in the forum archives and after a day of searching the internet for a solution I need to ask the question. How do I turn the following program fragment into a doubly link list? I ask because I want to print out the list in the order it is entered, but since the lsit adds to the top of the list at the moment it is printed in reverse order. I realise I need to have a prev command in there and that is already in the structure, but i have no idea how to put it into the program and i dont know how to print it either.

    #include<stdlib.h>
    #include<stdio.h>
    #include <string.h>

    struct record {
    char item[15];
    struct record *next, *prev;
    };

    void insert();
    int createroute();

    typedef struct record item_node;

    item_node * new_item;

    item_node * head;
    int createlist()
    {
    char one[15] = "a";
    printf("\nEnter as many items as you wish, one per line");
    printf("\nTo stop entering items, enter a single x\n");
    while (strcmp(one, "x") != 0)
    {
    printf("Enter an item: ");
    gets(one);
    if (strcmp(one, "x") == 0)
    break;
    insert(one);
    }
    return;

    }


    void insert(char *one)
    {
    new_item=malloc(sizeof(item_node));
    strcpy(new_item->item, one);
    new_item->next=head;
    head=new_item;
    return;
    }



    int main()
    {
    head = NULL;
    createlist();
    while(new_item != NULL)
    {
    printf("%s\n", new_item->item);
    new_item = new_item->next;
    }

    }

  2. #2
    Unregistered
    Guest
    I posted an example of my doubly linked list code in a question I had re: user input. see if that might help you out.

  3. #3
    Unregistered
    Guest
    I had a look at that code and tried to implement it, it is quite different from the code I used for my program and I still can't figure out how to get it to work, I'm fairly inexperienced, I really need someone to show me how to do this and I can't find any resources that explain double linked lists with a working example program or with much at all really.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM