GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Oct-2006, 05:32
akanksha akanksha is offline
New Member
 
Join Date: Sep 2006
Posts: 6
akanksha is on a distinguished road

Doubly linked list


Hi,
Below is simple program of doubly linked list.I know the concept,as to how it works but i am unable to make program.Here is a program picked up from a book.
CPP / C++ / C Code:
/* This program segment creates a doubly linked list then prints in forward and backword */

#include <stdio.h>
#include <malloc.h>

void main(void)
{
	int i;

	struct ListEntry {
		int number;
		struct ListEntry *next;
		struct ListEntry *previous;
	} start, *node;

	start.next = NULL;  /* Empty list */
	start.previous = NULL;
	node = &start;      /* Point to the start of the list */

	for (i = 1; i <= 10; i++)
	{
		node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));
		node->next->previous = node;
		node = node->next;
		node->number = 50+i;
		node->next = NULL;
	}

	/* Display the list */

	node = start.next;

	do {
		printf("%d ", node->number);
		node = node->next;
	} while (node->next);  /* Show 60 only one time */


	do {
		printf("%d ", node->number);
		node = node->previous;
	} while (node->previous);
}


I have not understood this line-
node->next->previous = node;
And also in the display part,the first do while loop is ok, but how does the second do while loop knows that it has to turn back.I mean this very line-
node = node->previous;
has to be before the second do while loop.
Can anyone tell.
  #2  
Old 05-Oct-2006, 11:14
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Doubly linked list


Quote:
Originally Posted by akanksha
I have not understood this line-
node->next->previous = node;
Get the next node in the link (node->next) and point that node back to the current node. Assume

node1 -> node2 -> nodeX -> node3 -> node4

If current node is nodeX, point node3's previous pointer to nodeX


Quote:
Originally Posted by akanksha
And also in the display part,the first do while loop is ok, but how does the second do while loop knows that it has to turn back.I mean this very line-
node = node->previous;
has to be before the second do while loop.
Nope. At the start of the second loop node is pointing to the final node. So it starts at the end and prints the list backwards.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #3  
Old 06-Oct-2006, 02:43
akanksha akanksha is offline
New Member
 
Join Date: Sep 2006
Posts: 6
akanksha is on a distinguished road

Re: Doubly linked list


Thanx very much Waltp for the explaning the code.I am able to make the program but facing a little prob in insertion of the element at the end in the single linked list and double,.Below is a program that does so in a single linked list.
In the insertion function if I do so

node->next=new;
instead of
previous->next=new;

then the program does not insert the element at the end.Why an extra element is required to first traverse the list till the end and with the help of that element insertion is done? The same problem is with double linked list also.The rule says that node->next=new is to be done.


CPP / C++ / C Code:
/*  INSERT A NODE INTO A SIMPLE LINKED LIST AT THE END OF THE LIST */
# include <stdio.h>
# include <malloc.h>
# include<conio.h>

struct link
{
	char info[20];
	struct link *next;
};

int i,number;
struct link *start, *previous, *new;

void create_list(struct link *node)
{
	char ch;
	start->next = NULL;  /* Empty list */

	start->next=node;      /* Point to the start of the list */
	i = 0;
	while(ch != 'n')
	{
		printf("\n Input the node: %d: ", (i+1));
		gets(node->info);
		fflush(stdin);

		node->next = NULL;

		printf("\n Input choice n for break: ");
		ch = getchar();
		fflush(stdin);
			i++;
		if(ch!='n')
		{
		node->next = (struct link* ) malloc(sizeof(struct link));
		node = node->next;
		}
	}
}

void insertion(struct link *node)
{
	node = start->next;
	previous = start;

	while(node)
	{
		node = node->next;
		previous= previous->next;
	}
	if(node == NULL)
	{
		new= (struct link* ) malloc(sizeof(struct link));
		new->next=NULL;
		previous->next=new;

		printf("\n Input the last node value: ");
		gets(new->info);
		fflush(stdin);
	}
}

void display(struct link *node)
{
	node = start->next;

	while (node)
	{
		printf("\n 0x%x", node);
		printf(" %s", node->info);
		node = node->next;
	}
}

void main()
{
	struct link *node = (struct link *) malloc(sizeof(struct link));
clrscr();
	create_list(node);
printf("\n Created list is as follows:\n");
	display(node);
	insertion(node);
printf("\n After Inserting a node list is as follows:\n");
	display(node);
	getch();
}
  #4  
Old 06-Oct-2006, 11:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Doubly linked list


Quote:
Originally Posted by akanksha
Thanx very much Waltp for the explaning the code.I am able to make the program but facing a little prob in insertion of the element at the end in the single linked list and double,.Below is a program that does so in a single linked list.
Oh oh.... Lots of dangerous code in here!!!!
See this and this!


Quote:
Originally Posted by akanksha
In the insertion function if I do so

node->next=new;
instead of
previous->next=new;

then the program does not insert the element at the end.Why an extra element is required to first traverse the list till the end and with the help of that element insertion is done? The same problem is with double linked list also.The rule says that node->next=new is to be done.
So don't change the code. When you change the code, you change the way the program behaves -- and it doesn't do the right thing.

When you add a node at the end, the last node->next points to NULL. So when you move the pointers normally they link up fine. You should not have to process anything special except at the head node.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
linked list error message Krandygrl00 CPP / C++ Forum 4 22-Jun-2005 14:13
search linked list itsmecathys CPP / C++ Forum 20 18-Apr-2005 01:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:27.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.