//*************************SERIOUS PROBLEM, NOT WORKING******************

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

Can anyone tell me why my code is not working? Its a doubly linked list...

Edited 6 Years Ago by Nick Evan: Changed snippet -&gt; thread. Added tags

Serious alright, you completely missed these posts.

Announcement: Please use BB Code and Inlinecode tags

Announcement: We only give homework help to those who show effort

Hi,

I modified your code and make it work. But, this is not optimized code.
Please refer some data structure books and change your code.
I added malloc function to create node.

Please let me know if you need any more help.

Thanks
Raju.

//DOUBLY LINKED LIST

#include<stdio.h>
#include<stdlib.h>

typedef struct __dll {
    int info;
    struct __dll *next;
    struct __dll *prev;
} dll;

dll *head = NULL, *tail = NULL, *temp;

void append();
void insafter();
void delete();
void display();

int main(void)
{
    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();
                break;
            case 5:
                exit(1);
            default:
            printf("Invalid choice!\n");
        }
    }while(choice != 5);

    return 1;
}

void append() {
    int x;
    printf("Enter element you wish to push\n");
    scanf("%d", &x);
    temp = (dll *)malloc (sizeof(dll));
    if(temp == NULL) {
        printf("Error: Malloc error \n");
        return;
    }

    temp->info = x;
    temp->prev = NULL;
    temp->next = NULL;
    if( (head == tail) && (head == NULL) ) {
        head = tail = temp;
    }
    else if(tail != NULL ) {
        temp->prev = tail;
        tail->next = temp;
        tail = temp;
    }
    else {
        printf("Error in List \n");
    }

}

//I assume user inputs this option after he has made a list, no condition check for empty list
void insafter() {
    int x, added_item;
    dll *new_node;
    printf("Enter Node after insert your new node\n");
    scanf("%d", &x);
    temp = head;
    while(temp != NULL) {
        if(temp->info != x)
            temp = temp->next;
        else
            break;
    }//now we have the node temp after which we insert our node
    if(temp == NULL) {
        printf("Error: Node does not exist in List \n");
        return;
    }
    printf("Enter item to be added\n");
    scanf("%d", &added_item);
    new_node = (dll *)malloc (sizeof(dll));
    if(temp == NULL) {
        printf("Error: Malloc error \n");
        return;
    }

    new_node->info = added_item;
    new_node->prev = temp;
    new_node->next = temp->next;
    temp->next = new_node;
    if(temp == tail) {
        tail = new_node;
    }
}

//allows deleting only from head
void delete() {
    temp = head;
    if(head == NULL)
        printf("List empty, nothing to delete\n");
    else
        printf("Deleted element: %d \n", temp->info);

    if(head == tail) {
        head = tail = NULL;
    }
    else {
        head = head->next;
    }
    free(temp);
}

void display( ) {
    temp = head;
    if(head == NULL)
        printf("Doubly Linked List empty\n");
    else {
        printf("\nList Nodes: ");
        while(temp != NULL) {
            printf("%d ", temp->info);
            temp = temp->next;
        }
        printf("\n\n");
    }
}

Edited 3 Years Ago by pritaeas: Fixed formatting

Comments
(1) don't give away homework answers

rule #1: don't give away homework answers
rule #2: when giving away homework answers, at least use code tags
rule #3: see rule #1

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.