LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2012, 04:38 AM   #1
fahad.anwar
LQ Newbie
 
Registered: May 2012
Posts: 18

Rep: Reputation: Disabled
Doubly Linked List Insertion Sort in C


Quote:
Hi all

I am trying to insert elements into a doubly linked list in a sorted order. However I keep getting segmentation faults. Please help me figure out where the problem is:

Thanks in advance



struct node *insert_node( struct node *head, int *value )
{
struct node *new_node = NULL;
struct node *cur_node = NULL;
struct node *last_node = NULL;
int found; /* 1 means found a place to insert the new node in, 0 means not*/
new_node = (struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("memory problem\n");
}
new_node->number = *value;
/* If the first element */
if (head == NULL)
{
new_node->next = new_node->prev = NULL;
head = new_node;
}
else if (new_node->number < head->number)
{
new_node->next = head;
head->prev = new_node;
head = new_node;
head->prev = NULL;
}
else
{
cur_node = head;
found = 0;
while (( cur_node != NULL ) && ( found == 0 ))
{
if( new_node->number < cur_node->number )
{
found = 1;
}
else
{
last_node = cur_node;
//cur_node = cur_node->next;
}
}
/* We got the right place to insert our node */
if( found == 1 )
new_node->next = cur_node;
/* Insert at the tail of the list */
else
{
last_node->next = new_node;
new_node->prev = last_node;
cur_node->prev = new_node;
cur_node->next = NULL;
cur_node = new_node;
}
}
return head;
}

thanks

Last edited by fahad.anwar; 07-10-2012 at 04:42 AM. Reason: Indentation
 
Old 07-10-2012, 07:24 PM   #2
visar
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by fahad.anwar View Post
thanks
This is the correct code:

Code:
struct node
{
	int number;
	struct node * next;
	struct node * prev;
};

struct node *insert_node( struct node **head, int *value )
{
	struct node *new_node = NULL;
	struct node *cur_node = NULL;
	int found; /* 1 means found a place to insert the new node in, 0 means not*/
	new_node = (struct node *)malloc(sizeof(struct node));
	if(new_node == NULL)
	{
		printf("memory problem\n");
		exit(1);
	}
	new_node->number = *value;
/* If the first element */
	if (*head == NULL)
	{
		new_node->next = new_node->prev = new_node;
		*head = new_node;
	}
	else if (new_node->number < (*head)->number)
	{
		new_node->next = *head;
		new_node->prev = (*head)->prev;
		(*head)->prev = new_node;
		*head = new_node;
		(*head)->prev->next = *head;
	}
	else
	{
		cur_node = *head;
		found = 0;
		do
		{
			if( new_node->number < cur_node->number )
			{
				found = 1;
				break;
			}
			else
			{
//				last_node = cur_node;
				cur_node = cur_node->next;
			}
		}
		while ( cur_node != *head );
/* We got the right place to insert our node */
		if( found == 1 )
		{
			new_node->next = cur_node;
			new_node->prev = cur_node->prev;
			cur_node->prev->next = new_node;
			cur_node->prev = new_node;
		}
/* Insert at the tail of the list */
		else
		{
			new_node->next = *head;
			new_node->prev = (*head)->prev;
			(*head)->prev->next = new_node;
			(*head)->prev = new_node;
		}
	}
	return *head;
}
 
Old 07-11-2012, 03:36 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list C exvor Programming 14 06-22-2007 06:06 PM
C linked list exvor Programming 4 04-28-2006 05:25 AM
Freeing a Doubly link list !!!! morph_ind Programming 2 04-01-2006 07:20 AM
trying to sort double linked LinkedIndex in c++ nadroj Programming 7 02-02-2006 07:22 PM
c++ doubly linked lists durden2.0 Programming 4 02-25-2004 05:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration