C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-09-2009, 07:31 PM   #1
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
linked list error....

Hi ,

I was just trying to understand linked list using this program but when compiled it does not give any error but on running the same it gives unhandled exception. The compiler i am using is the visual c++ 2009.

[insert]
Code:
#include <stdio.h>
#include <stdlib.h>

struct node{
	int data;
	struct node *link;
};

void addatend(struct node **, int);
void addatbeg(struct node**, int);
void addatloc(struct node*, int , int);
void display(struct node *);
void count(struct node *);

int main(void){

	struct node *p;
	p = NULL;

	//display (p);
	count (p);
	addatend(&p, 15);
	addatend(&p, 12);
	addatend(&p, 23);

	display (p);
	count (p);
	addatbeg(&p, 54);
	addatbeg(&p, 78);
	addatbeg(&p, 35);

	display (p);
	count (p);
	addatloc(p, 2, 32);
	addatloc(p, 5, 36);
	display (p);
	count (p);

	return 0;
}

void addatend (struct node **q, int num){

	struct node *temp, *r;

	if(*q = NULL){

		temp = malloc(sizeof(struct node));
		temp -> data = num;
		temp -> link = NULL;
		*q = temp;
	}

	else{

		temp = *q;
		while(temp -> link != NULL)
			temp = temp ->link;

		r = malloc(sizeof(struct node));
		r ->data = num;
		r ->link = NULL;
		temp -> link = r;
	}
}


void addatbeg(struct node **q, int num){

	struct node *temp;

	temp = malloc(sizeof(struct node));
	temp ->data = num;
	temp ->link = *q;

	*q= temp;
}

void addatloc(struct node*q, int loc, int num){

	struct node *temp, *r;
	int i;

	temp = q;
	for(i = 0; i< loc ; i++){

		temp = temp ->link;
	}

	r = malloc(sizeof(struct node));
	r ->data = num;
	r ->link = temp->link;
	temp ->link = r;
}

void display (struct node *q){

	int i = 0;
	struct node *temp;

	temp = q;
	while (temp ->link != NULL){
		printf("\nThe data at loc %d is %d", i , temp ->data);
		i++;
	}
}

void count (struct node *q){

	struct node *temp;
	int count = 0;

	temp = q;
	while (temp ->link != NULL){
		++count;
	}

	printf("\n%dThe total no. of elements in linked list is ", count);
}
roaan is offline   Reply With Quote
Old 07-09-2009, 07:45 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Well, yes. Your count function will absolutely explode if you pass it a NULL pointer. Guess what the first thing your program does is?
tabstop is offline   Reply With Quote
Old 07-09-2009, 07:51 PM   #3
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
Okay but when i comment the count line it still gives the same exception .
roaan is offline   Reply With Quote
Old 07-09-2009, 07:59 PM   #4
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
p needs to be set to something valid before it can be used. Commenting out *p = NULL, doesn't set p to something valid. Each function that takes a struct node* should have as its first line
if (thePointer == NULL) return;
That is called defensive programming.

Also, look at addatloc. If loc is bigger than your list, your going to go off the end of the list in your for loop. Examine the value of each parameter to make sure its valid for the state your program is in. If the parameter value is not valid, the program needs to handle the situation.
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP

Last edited by Zlatko; 07-09-2009 at 08:07 PM.
Zlatko is offline   Reply With Quote
Old 07-09-2009, 08:27 PM   #5
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
I added all the check conditions but the error seems to come from the line

[insert]
Code:
		temp = *q;                                                // else condition of addatend function
		while(temp -> link != NULL)
			temp = temp ->link;
In the addatend function i have defined the condition if i am adding the element for the first time but it seems to escape that if condition and jumps to the else part directly.
roaan is offline   Reply With Quote
Old 07-09-2009, 08:28 PM   #6
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
About the previous post, I didn't realize which line you commented out.
OK, here's what I want you to do. Your using visual studio, so make sure your project is the active one and press the F5 key. You'll get your access violation. Look at the stack trace. It says that addatend was called from line 22 in main. Basically, the program failed at the first addatend. Where is the access violation? Its in the while loop in the 'else' section of the addatend function. Why is it there when you're running the first addatend? Shouldn't it be in the *q is NULL section? It should, and I don't want to torture you with optical illusions, so I'll tell you that you have a single '=' in if (*q = NULL) when you want a double '='
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP
Zlatko is offline   Reply With Quote
Old 07-09-2009, 08:56 PM   #7
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
Thanks i found a couple of other mistakes as well apart from that = sign which i was using for comparison.
roaan is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do you resolve this error? -EquinoX- C Programming 32 11-05-2008 04:35 PM
Errors including <windows.h> jw232 Windows Programming 4 07-29-2008 01:29 PM
Quantum Random Bit Generator shawnt C++ Programming 62 06-18-2008 10:17 AM
error: template with C linkage michaels-r C++ Programming 3 05-17-2006 08:11 AM
load gif into program willc0de4food Windows Programming 14 01-11-2006 10:43 AM


All times are GMT -6. The time now is 05:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22