linked list error....

This is a discussion on linked list error.... within the C Programming forums, part of the General Programming Boards category; Hi , I was just trying to understand linked list using this program but when compiled it does not give ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    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);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, yes. Your count function will absolutely explode if you pass it a NULL pointer. Guess what the first thing your program does is?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay but when i comment the count line it still gives the same exception .

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    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.
    Last edited by Zlatko; 07-09-2009 at 08:07 PM.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    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.

  6. #6
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    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 '='

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Thanks i found a couple of other mistakes as well apart from that = sign which i was using for comparison.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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