0% found this document useful (0 votes)
12 views

Doubly Linked List

The document describes a C program to implement a doubly linked list with the following functions: 1. Create the list 2. Display the list forward and backward 3. Delete a node from the last position 4. Insert a node in any middle position It provides details on the structure of a node in a doubly linked list and algorithms for performing insertion, deletion from the end, and displaying the list. The output shows an example run of the program menu allowing the user to insert, insert at a location, delete from the end, and display the doubly linked list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Doubly Linked List

The document describes a C program to implement a doubly linked list with the following functions: 1. Create the list 2. Display the list forward and backward 3. Delete a node from the last position 4. Insert a node in any middle position It provides details on the structure of a node in a doubly linked list and algorithms for performing insertion, deletion from the end, and displaying the list. The output shows an example run of the program menu allowing the user to insert, insert at a location, delete from the end, and display the doubly linked list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment no: 6 Exp no:6

Date: Signature:

Problem statement:1.Write a program in c to implement doubly linked list

Include thee following functions in your program

1.create the list.

2.Display the list in forward and backward manner

3.delete a node from last position

4.Insert a node in any middle position

Statement: Doubly linked list


Doubly linked list is a complex type of linked list in which a node contains a pointer to the
previous as well as the next node in the sequence. Therefore, in a doubly linked list, a
node consists of three parts: node data, pointer to the next node in sequence (next
pointer) , pointer to the previous node (previous pointer). A sample node in a doubly
linked list is shown in the figure.

A doubly linked list containing three nodes having numbers from 1 to 3 in their data part,
is shown in the following image.
in C, structure of a node in doubly linked list can be given as :

1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. }

The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.

In a singly linked list, we could traverse only in one direction, because each node contains
address of the next node and it doesn't have any record of its previous nodes. However, doubly
linked list overcome this limitation of singly linked list. Due to the fact that, each node of the list
contains the address of its previous node, we can find all the details about the previous node as
well by using the previous address stored inside the previous part of each node.

ALGORITHM

In the main function use switch case to make the program menu driven.

. Insertion 1. Create a new node

allocate memory for newNode assign the data to newNode.

New node

2. Set prev and next pointers of new node point next of newNode to the first node of
the doubly linked list point prev to null
Reorganize the pointers (changes are denoted by purple arrows)

3. Make new node as head node

Point prev of the first node to newNode (now the previous head is the second node)

Point head to newNode

.Insertion at particular place

Step 1: IF PTR = NULL

Write OVERFLOW

Go to Step 15

[END OF IF]

Step 2: SET NEW_NODE = PTR

Step 3: SET PTR = PTR -> NEXT

Step 4: SET NEW_NODE -> DATA = VAL

Step 5: SET TEMP = START

Step 6: SET I = 0

Step 7: REPEAT 8 to 10 until I<="" li="">

Step 8: SET TEMP = TEMP -> NEXT

STEP 9: IF TEMP = NULL

STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"

GOTO STEP 15

[END OF IF]

[END OF LOOP]

Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT


Step 12: SET NEW_NODE -> PREV = TEMP

Step 13 : SET TEMP -> NEXT = NEW_NODE

Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE

Step 15: EXIT

.Deletion from end

Step 1: IF HEAD = NULL

Write UNDERFLOW

Go to Step 7

[END OF IF]

Step 2: SET TEMP = HEAD

Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL

Step 4: SET TEMP = TEMP->NEXT

[END OF LOOP]

Step 5: SET TEMP ->PREV-> NEXT = NULL

Step 6: FREE TEMP

Step 7: EXIT

.Display

1.Define a new node 'current' that will point to the head.

2.Print current. data till current points to null.

3.Current will point to the next node in the list in each iteration.

PROGRAM BODY

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

struct node *prev; struct node *next;

int data;

};

struct node *head; void insertion (); void insertion_specified(); void deletion_last();
void display();

void main ()

{ int choice =0; while(choice != 5)

printf("\n*********Main Menu*********\n");

printf("\nChoose one option from the following list ...\n");


printf("\n===============================================\n");
printf("\n1.Insert \n2.Insert at any place anywhere\n2.Insert at any random
place\n3.Delete from last\n4.Show\n5.Exit\n"); printf("\nEnter your choice?\n");
scanf("\n%d",&choice); switch(choice)

{ case 1: insertion break; case 2: insertion_specified();

break; case 3: deletion_last(); break; case 4: display(); case 9: exit(0); break; default:

printf("Please enter valid choice..");

void insertion()

{
struct node *ptr,*temp;

int item;

ptr = (struct node *) malloc(sizeof(struct node)); if(ptr == NULL)

printf("\nOVERFLOW");

} else

printf("\nEnter value"); scanf("%d",&item); ptr->data=item; if(head == NULL)

ptr->next = NULL; ptr->prev = NULL;

head = ptr;

} else

temp = head; while(temp->next!=NULL)

temp = temp->next;

temp->next = ptr; ptr ->prev=temp;

ptr->next = NULL;

printf("\nnode inserted\n");
}

void insertion_specified()

struct node *ptr,*temp;

int item,loc,i;

ptr = (struct node *)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\n OVERFLOW");

} else

temp=head; printf("Enter the location"); scanf("%d",&loc);

for(i=0;i<loc;i++)

temp = temp->next; if(temp == NULL)

printf("\n There are less than %d elements", loc); return;

printf("Enter value"); scanf("%d",&item); ptr->data = item; ptr->next = temp->next;


ptr -> prev = temp; temp->next = ptr;

temp->next->prev=ptr;
printf("\nnode inserted\n");

void deletion_last()

struct node *ptr; if(head == NULL)

printf("\n UNDERFLOW");

else if(head->next == NULL)

head = NULL; free(head);

printf("\nnode deleted\n");

} else

ptr = head;

if(ptr->next != NULL)

ptr = ptr -> next;

ptr -> prev -> next = NULL; free(ptr); printf("\nnode deleted\n");

}
}

void display()

struct node *ptr; printf("\n printing values...\n"); ptr = head; while(ptr != NULL)

printf("%d\n",ptr->data);

ptr=ptr->next;

OUTPUT

********Main Menu*********

Choose one option from the following list ... *********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert

2.Insert at any random location

3.Delete from last

4.Display

5.Exit

Enter your choice? 1

Enter Item value12 node inserted

*********Main Menu*********
Choose one option from the following list ...

===============================================

1.Insert

2.Insert at any random location

3.Delete from last

4.Display

5.Exit

Enter your choice? 2

Enter the location 2 enter value 22

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert

2.Insert at any random location

3.Delete from last

4.Display

5.Exit

Enter your choice? 3

Node deleted

*********Main Menu*********

Choose one option from the following list ...


===============================================

1.Insert

2.Insert at any random location

3.Delete from last

4.Display

5.Exit

Enter your choice? 4

12

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert

2.Insert at any random location

3.Delete from last

4.Display

5.Exit

Enter your choice?

DISCUSSION

Doubly Linked List is a variation of Linked list in which navigation is possible in both
ways, either forward and backward easily as compared to Single Linked List.
Following are the important terms to understand the concept of doubly linked list.

Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.

Prev − Each link of a linked list contains a link to the previous link called Prev.

LinkedList − A Linked List contains the connection link to the first link called First
and to the last link called Last.

Basic Operations

Following are the basic operations supported by a list.

Insertion − Adds an element at the beginning of the list.

Deletion − Deletes an element at the beginning of the list.

Insert Last − Adds an element at the end of the list.

Delete Last − Deletes an element from the end of the list.

Insert After − Adds an element after an item of the list.

Delete − Deletes an element from the list using the key.

Display forward − Displays the complete list in a forward manner.

Display backward − Displays the complete list in a backward manner.

You might also like