-
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);
}
-
Well, yes. Your count function will absolutely explode if you pass it a NULL pointer. Guess what the first thing your program does is?
-
Okay but when i comment the count line it still gives the same exception .
-
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.
-
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.
-
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 '='
-
Thanks i found a couple of other mistakes as well apart from that = sign which i was using for comparison.