LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 09-23-2010, 05:02 AM   #1
jamesbon
Member
 
Registered: Jun 2010
Posts: 130

Rep: Reputation: 9
program to create a link list is giving an error


I wrote a program to create a link list
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
	struct node *next, *prev;
	int data;
} *start, *prv;
void add_element(int e);
void read_list ();
static int j = 0;
int main()
{
	int i, element, l, choice;
	choice = 1;
	struct node *temp;
	start = NULL;
	prv = start;
	while (choice == 1) {
		printf("What do you want 0 exit 1 to add\n");
		scanf("%d", &choice);
		if (choice == 0)
			break;
		printf("Enter data to be entered\n");
		scanf("%d", &element);
		add_element(element);
               read_list();
	}

}

void add_element(int e)
{
//      printf("\n printing from function %d\n", e);
	struct node *temp;
	if (j == 0) {
		start = (struct node *)malloc(sizeof(struct node));
		start->next = NULL;
		start->data = e;
		//printf("the data entered is %d \n", start->data);
		j++;
	}


		temp = (struct node *)malloc(sizeof(struct node));
		temp->data = e;
		prv->next = temp;
	          temp->next=NULL;
		prv=temp;//since prv is  global pointer if I do not do this step then next time when new memory is allocated prv is still pointing to start or previous pointer what ever the value was so new temp declaration will not have much effect
		j++;

	
}

void read_list()
{
	struct node *temp;
	temp = start;
	while (temp) {
		printf(" %d --> ", temp->data);
		temp = temp->next;
	}
}
but the above program is giving me Segmentation fault at
Code:
		prv->next = temp;
Can any one point me as what mistake have I done?
 
Old 09-23-2010, 05:57 AM   #2
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
You appear to be using the prv pointer before you've set it. On the first pass through add_element() it will still be set to NULL so prv->next can't be used at that point.


There's a lot of better programmers than me hang out here, but the way I'd do it would be to have a

struct node *firstNode and
struct node *lastNode

When you add a new node:
If firstNode is NULL then set it to point to the new entry.
If lastNode is not NULL, set lastNode->next to point to new entry.
set lastNode to point to the new entry.
set lastNode->next to NULL.

Been a long time since I've done anything like this but I think that's about right. (I'm sure someone will correct me if it's not )

Last edited by GazL; 09-23-2010 at 06:01 AM.
 
Old 09-23-2010, 06:43 AM   #3
jamesbon
Member
 
Registered: Jun 2010
Posts: 130

Original Poster
Rep: Reputation: 9
I declared prv and start outside the main loop to be global variables.
I did in main ()
Code:
prv=start;
So my doubt is that is this not going to make sure that when add_list is called prv is pointing to same memory location where
start is pointing.

So what is wrong in main () when I
Code:
prv=start;
and use prv in add_list function ?
 
Old 09-23-2010, 06:54 AM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
when you set prv=start, start=NULL, so prv=NULL.
 
1 members found this post helpful.
Old 09-23-2010, 07:12 AM   #5
jamesbon
Member
 
Registered: Jun 2010
Posts: 130

Original Poster
Rep: Reputation: 9
Ok got your point.
 
  


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
Cannot create new link. Too many open links error sai_sonika Linux - Newbie 3 03-24-2009 05:28 PM
Radius server not starting . giving error rlm_sql (sql): Could not link driver rlm_sq yawe_frek Linux - Server 0 07-09-2007 07:19 PM
An error while trying to create a soft link g_trueblood Slackware 4 10-07-2005 12:17 PM
how to create a link of a program ? anindyanuri Linux - Software 7 06-19-2005 01:11 PM
link list reversl program vijeesh_ep Programming 3 02-27-2005 12:17 AM


All times are GMT -5. The time now is 04:50 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration