You are on page 1of 4

# Data Structures & Algorithms / Data Structures

Hamdard University

LAB SESSION 02
DOUBLE LINKED LIST (TWO WAY LIST)
Objectives:
 To understand the basic concept ofdouble linked list.
 To understand the operation performed ondoublelinked list.

Two Way List:
This lab introduces a new list structure, called a two-way list, which can be traversed in two directions:
in the usual forward direction from the beginning of the list to the end, or in the backward direction
from the end of the list to the beginning. Furthermore, a node has immediate access to both the next
node and the preceding node in the list. This means, in particular, that one is able to delete N from the
list without traversing any part of the list.
A two-way list is a linear collection of data elements, called nodes, where each node N is divided into
three parts:
 An information field which contains the data of N
 A pointer field which contains the location of the next node in the list
 A pointer field which contains the location of the preceding node in the list.
struct node{
node* prev;
int item;
node* next;

Data Structures & Algorithms / Data Structures
Hamdard University

The variables head, pn and temp are pointer variables. The purpose of these pointer variables is
that:
 pn is used to hold the address of previous node of the list when a new node is created.
 Temp is used to temporarily hold address of the newly created node.
Create and Insert a Node in Double Linked List
#include<iostream.h>
#include<conio.h>
struct node
{
node *prev;
int data;
node * next;
};
void create_double_list(int d)
{
{
cout<<"New list created"<<endl;
cout<<"Data stored in newly created list is: "<<d<<endl;
}
else
item"<<endl;
}
{
{
cout<<"List is not created. Call create_double_list(intd)to

Data Structures & Algorithms / Data Structures
Hamdard University

void insert_front(int d){
{
cout<<"List is not created. Call create_double_list(int
d)to create a list"<<endl;
}
else
{
cout<<"Item inserted at the begining: "<<d<<endl;
}
}

How to Display the Double Linked List in Backward Direction
void display_rev()
{
node *ptr;
cout<<"Items in reverse order are: "<<endl;
ptr=tail;
//cout<<tail->data<<endl;
while(ptr->prev!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->prev;
}
cout<<ptr->data<<endl;
}

Data Structures & Algorithms / Data Structures
Hamdard University

Lab Exercise:
Instructions:
Make the appropriate functions for the following lab task. Implement the function in main(). Write
the code by hand and take the snapshot of the output screen.
1) Write a program that deletes the first node of a double linked list.
2) Write a program that inputs a number from user. Write the code to generate the following double
linked list: {3,2,5,4,7,6,9,8} Write a function that will search the user inputted number in the given
list and print the message accordingly. Implement the functions in main()
3) Write a program that deletes the odd nodes of the double linked list.
4) Write a program that creates an array which stores 7 integers. Input data in array from user. Now
create a double linked list from this array. Write the contents of double linked list to a text file.
5) Write a program that transforms a single linked list to a double linked list.