1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Doubly Linked List

Discussion in 'C' started by manjotpahwa, Apr 14, 2010.

  1. manjotpahwa

    manjotpahwa New Member

    Joined:
    Apr 14, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I wrote the following code, can anyone tell me why is it not working, I was unable to find a reason...

    //*************************SERIOUS PROBLEM, NOT WORKING******************
    Code:
    //DOUBLY LINKED LIST
    
    #include<stdio.h>
    #include<stdlib.h>
    
    struct dll
    {
    int info;
    struct dll *next;
    struct dll *prev;
    };
    struct dll *head = NULL, *tail = NULL, *temp;
    
    void append();
    void insafter();
    void delete();
    void display();
    
    void main()
    {
    int choice;
    do
    {
    printf("Enter your choice:\n");
    printf("1.Append Node\n2.Insert After a Node\n3.Delete the First Node\n4.Display\n5.Exit");
    scanf("%d", &choice);
    switch(choice)
    	{
    	case 1:
    		append();
    		break;
    	case 2:
    		insafter();
    		break;
    	case 3:
    		delete();
    		break;
    	case 4:
    		display();
    	case 5:
    		exit(1);
    	default:
    	printf("Invalid choice!\n");
    	}
    }while(choice!=4);
    }
    
    void append()
    {
    int x;
    printf("Enter element you wish to push\n");
    scanf("%d", &x);
    temp->info = x;
    if(head==tail && head==NULL)
    	{
    	temp = head;
    	temp->next = temp;
    	temp->prev = NULL;
    	}
    else
    	{
    	tail->next = temp;
    	temp->prev = tail;
    	}
    tail = temp;
    temp->next = NULL;
    }
    
    void insafter() //I assume user inputs this option after he has made a list, no condition check for empty list
    {
    int x, added_item;
    printf("Enter the information of the node after which you want to insert your new node\n");
    scanf("%d", &x);
    temp = head;
    while(temp!=NULL)
    	{
    	while(temp->info!=x)
    	temp= temp->next;
    	}//now we have the node temp after which we insert our node
    printf("Enter item to be added\n");
    scanf("%d", &added_item);
    struct dll *p;
    p->info = added_item;
    p->next = temp->next;
    p->prev = temp;
    temp->next = p;
    }
    
    void delete() //allows deleting only from head
    {
    temp = head;
    if(head==NULL)
    printf("List empty, nothing to delete\n");
    else
    printf("Deleted element: %d", temp->info);
    head = head->next;
    free(temp);
    }
    
    void display()
    {
    temp = head;
    if(head==NULL)
    printf("Doubly Linked List empty\n");
    else
    while(temp!=NULL)
    	{
    	printf("%d", temp->info);
    	temp = temp->next;
    	}
    }
     
    Last edited by a moderator: Apr 14, 2010
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    erros and solution

    Code:
    ............
    scanf("%d", &choice);
    [COLOR=Red]getchar();[/COLOR]//add this for "garbage" collection
    switch(choice){
    ...........
    
    Code:
        case 4:
            display();
            [COLOR=Red]break;[/COLOR]//add this otherwise when it finishes it will go to case 5 and exit.
        case 5:
    

    Code:
    void delete[COLOR=Red]1[/COLOR]();//change the name delete because it exists in c++ compilers
    ............
    void delete[COLOR=Red]1[/COLOR](){
    ........
    }
    
    Code:
    void display(){//i think this look better
        temp = head;
        if(head==NULL){
            printf("Doubly Linked List empty\n");
        }
        else{
            printf("\nNULL ");
            while(temp!=NULL){
               printf("<- %d ->", temp->info);
               temp = temp->next;
           }
           printf("NULL\n");
        }
    }
    
    Code:
    void append(){[COLOR=Red]//do not forget malloc[/COLOR]
        int x;
        printf("Enter element you wish to push\n");
        scanf("%d", &x);
        [COLOR=Red]getchar();[/COLOR]
        [COLOR=Red]temp=(struct dll *)malloc(sizeof(struct dll));[/COLOR]
        temp->info = x;
        if(head==tail && head==NULL){
    [COLOR=Red]       temp->next = NULL;
           temp->prev = NULL;
           head=temp;
           tail = temp;[/COLOR]
        }else{
    [COLOR=Red]       tail->next = temp;
           temp->prev = tail;
           temp->next = NULL;
           tail = temp;[/COLOR]
        } 
    }
    
    
    also since its a double list you want 2 display functions
    a)from head to tail (the one you wrote)
    b)from tail to head

    Code:
    void display[COLOR=Red]2[/COLOR](){//[COLOR=Blue]b)from tail to head[/COLOR]
        [COLOR=Red]temp = tail;[/COLOR]
        if([COLOR=Red]tail[/COLOR]==NULL){
            printf("Doubly Linked List empty\n");
        }
        else{
            printf("\nNULL ");
            while(temp!=NULL){
               printf("<- %d ->", temp->info);
               [COLOR=Red]temp = temp->prev;[/COLOR]
           }
           printf("NULL\n");
        }
    }
    

    with this try to correct the other mistakes you have done.
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    and for choice-->2

    Code:
    void insafter(){ //I assume user inputs this option after he has made a list, no condition check for empty list
        int x, added_item;
        printf("Enter the information of the node after which you want to insert your new node\n");
        scanf("%d", &x);
        [COLOR=Red]getchar();[/COLOR]
        temp = head;
        while(temp!=NULL){
           [COLOR=Red]if (temp->info==x) break;[/COLOR]
            temp= temp->next;
        }//now we have the node temp after which we insert our node
        [COLOR=Red]if (temp==NULL){
            printf("\n the value was not found in the list!\n");
            return;
        }[/COLOR]
        printf("Enter item to be added\n");
        scanf("%d", &added_item);
        [COLOR=Red]getchar();
        struct dll *p;
        p=(struct dll *)malloc(sizeof(struct dll));[/COLOR]
        p->info = added_item;
        p->next = temp->next;
        p->prev = temp;
        temp->next = p;
    }
    
    
     

Share This Page