doubly linked list

This is a discussion on doubly linked list within the C Programming forums, part of the General Programming Boards category; Halo all, I was making a simple doubly linked list but while diplaying it I got this run time error- ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    doubly linked list

    Halo all,
    I was making a simple doubly linked list but while diplaying it I got this run time error-
    Unhandled exception at 0x004115ec in example.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
    Here is my code. I'm not able to get where I'm doing wrong.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
    	int info;
    	struct node *right;
    	struct node *left;
    }*head=NULL,*tail=NULL,*p=NULL;
    void make_list(void)
    {
    	int elts,count;
    	struct node *temp;
    	printf("How many elements do you want to enter\n");
    	scanf("%d",&elts);
    	for(count=0;count<elts;count++)
    	{
    		p=malloc(sizeof(struct node));
    		printf("Enter the %d element",count+1);
    		scanf("%d",&(p->info));
    		if(head==NULL)
    		{
    			head=p;
    			head->left=NULL;
    			head->right=NULL;
    			tail=head;
    		}
    		else
    		{
    			temp=tail->right;
    			tail->right=p->left;
    			p->left=temp;
    			p->right=NULL;
    			tail=p;
    		}
    	}
    }
    void display(void)
    {
    	for(p=head;p!=tail;p=p->right)
    		printf("%d->",p->info);
    }
    int main(void)
    {
    	int ch;
    	while(1)
    	{
    		printf("\nWhat do you wanna do\n1.Make a list\n2.display the list\n3.exit");
    		scanf("%d",&ch);
    		switch(ch)
    		{
    		case 1:make_list();
    			break;
    		case 2:display();
    			break;
    		case 3:exit(0);
    		}
    	}
    }
    I've debugged the code but to no avail.
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    it would be better to split it in more functions, to succeed, try to implement first function, which is simple and just adds one element to empty list, or creates just first element, then test it, then if it doesnt have errors make it more advanced...its better than to write more code and then trying to find where an error is... check i created doubly circularly linked list here: Circularly-Doubly Linked List implementation

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    But I just want to know what's wrong with my code? I also have other people's code but correction in what I've made will be highly appreciable.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    tail->right=p->left;
    p->left = temp;
    Ooooooooooops. (Can't assign p->left to some other variable before p->left has been assigned!)

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks tabstop. Again you solved my problem. I knew I was making a blunder somewhere. Thanks once again.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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