-
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;
}
}
-
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.
-
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.
-