Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Doubly Linked List (http://www.go4expert.com/forums/doubly-linked-list-t21759/)

manjotpahwa 14Apr2010 20:02

Doubly Linked List
 
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;
        }
}


virxen 15Apr2010 00:45

Re: Doubly Linked List
 
erros and solution

Code:

............
scanf("%d", &choice);
getchar();//add this for "garbage" collection
switch(choice){
...........

Code:

    case 4:
        display();
        break;//add this otherwise when it finishes it will go to case 5 and exit.
    case 5:


Code:

void delete1();//change the name delete because it exists in c++ compilers
............
void delete1(){
........
}

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(){//do not forget malloc
    int x;
    printf("Enter element you wish to push\n");
    scanf("%d", &x);
    getchar();
    temp=(struct dll *)malloc(sizeof(struct dll));
    temp->info = x;
    if(head==tail && head==NULL){
      temp->next = NULL;
      temp->prev = NULL;
      head=temp;
      tail = temp;

    }else{
      tail->next = temp;
      temp->prev = tail;
      temp->next = NULL;
      tail = temp;

    }
}

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 display2(){//b)from tail to head
    temp = tail;
    if(tail==NULL){
        printf("Doubly Linked List empty\n");
    }
    else{
        printf("\nNULL ");
        while(temp!=NULL){
          printf("<- %d ->", temp->info);
          temp = temp->prev;
      }
      printf("NULL\n");
    }
}


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

virxen 15Apr2010 00:59

Re: Doubly Linked List
 
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);
    getchar();
    temp = head;
    while(temp!=NULL){
      if (temp->info==x) break;
        temp= temp->next;
    }//now we have the node temp after which we insert our node
    if (temp==NULL){
        printf("\n the value was not found in the list!\n");
        return;
    }

    printf("Enter item to be added\n");
    scanf("%d", &added_item);
    getchar();
    struct dll *p;
    p=(struct dll *)malloc(sizeof(struct dll));

    p->info = added_item;
    p->next = temp->next;
    p->prev = temp;
    temp->next = p;
}



All times are GMT +5.5. The time now is 04:06.