0% found this document useful (0 votes)
3 views18 pages

03 134241 017 132709114925 24032025 082035pm

This document is a lab journal for a Data Structures and Algorithms course at Bahria University, detailing the implementation of doubly linked lists. It covers the objectives, tools required, and provides a comprehensive explanation of operations such as insertion, deletion, and traversal of doubly linked lists, along with a sample C++ program. Additionally, it includes a task for creating an airline ticket reservation system using linked lists.

Uploaded by

hammadmehmood464
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

03 134241 017 132709114925 24032025 082035pm

This document is a lab journal for a Data Structures and Algorithms course at Bahria University, detailing the implementation of doubly linked lists. It covers the objectives, tools required, and provides a comprehensive explanation of operations such as insertion, deletion, and traversal of doubly linked lists, along with a sample C++ program. Additionally, it includes a task for creating an airline ticket reservation system using linked lists.

Uploaded by

hammadmehmood464
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 07
(Spring 2025)

Course: Data Structure and Algo. Lab


Course Code: CSL-221 Max Marks: 30
Faculty’s Name: Rizwan Khalid

Name: Hammad Mehmood Enroll No: 03-134241-017

Implementation Doubly link list with all operations

Lab 7: Implementation of Doubly


Linked Lists
Objectives
Understanding of:
● Implementing of doubly linked lists
● Design Scenario

Tools Required
● PC with Windows 7 Professional
● Visual Studio 2010

Doubly Linked Lists:


In singly linked list, we can move/traverse only in one single direction because each node has the
address of the next node only. Suppose we are in the middle of the linked list and we want the address
of previous node then we don’t have any option other than repeating the traversing from the beginning
node.
To overcome this drawback, a doubly linked list can be used. In this, there are two pointers. One of
these pointers points to the next node and the other points to the previous node.
The structure for a doubly linked list node can be declared as:
{
struct node *prev_node;
int info;
struct node *next_node;
};

prev_node would contain a pointer to the address of the previous node and next_node would
point the next node in the list. Hence, we can move in both the directions.
Traversing
Traversal of a doubly linked list is similar to that of a singly linked list. We have to first check for a
condition: whether the linked list is empty or not. This helps to set the start pointer at a proper location.
After that we access each node till end.
Insertion
A new node can be inserted very easily in a doubly linked list. We just need to set the pointers
prev_node and next_node carefully interlinking the prev_node and the next_node node with
the appropriate pointers.

If you are inserting a Node n2 between Node n1 and n3, you should set the pointer prev_node of n2
to n1 and pointer next_node of n2 to n3.

Insertion in a doubly linked list can be done in multiple ways:

1. Insertion in between the nodes.


2. Insertion at the beginning.
3. Insertion in an empty list.
4. Insertion at the end of the list.
Deletion
A node can be deleted very easily in a doubly linked list. We just need to set the pointers
prev_node and next_node logically to the nodes.
Deletion of the nodes can be done in the following ways:

1. Delete at the end.


2. Delete the first node.
3. Delete in between the nodes.

Reverse a Doubly Linked List


Assume we have four nodes n1, n2, n3 and n4
Steps to reverse
1. Pointer begin points to the last node n4.
2. Since, the n4 is the first node now, its prev_node pointer must be NULL.
3. Node n1 is the last node, hence its next_node must be NULL.
4. Pointer next_node of n4 points to n3, next_node of n3 points to n2 and next_node of n2 points
to n1.
5. Pointer prev_node of n1 points to n2, prev_node of n2 points to n3 and prev_node of n3 points
to n4.

//Program 1 - To Implement Complete Functionality of Doubly Linked List.


#include<iostream>
#include<stdlib.h>

using namespace std;

struct node
{
struct node *prev_node;
int info;
struct node *next_node;
};

struct node *create_list(struct node *begin);

//Function To Display Link List


void display(struct node *begin);

// Function To Empty List


struct node *addtoemptylist(struct node *begin,int data_element);

//Function To Insert At Beginning Of Link List


struct node *addatbeglist(struct node *begin,int data_element);

//Function To Insert At End Of Link List


struct node *addatendlist(struct node *begin,int data_element);
//Function To Insert After any Specific Location in Link List
struct node *addafterlist(struct node *begin,int data_element,int
item_pos);

//Function To Insert Before any Specific Location in Link List


struct node *addbeforelist(struct node *begin,int data_element,int
item_pos);

//Function To DELETE any Node from Link List


struct node *deletenode(struct node *begin,int data_element);

//Function To Reverse the contents of The Link List


struct node *reverselist(struct node *begin);

//Main Body
int main()
{
int option, data_element, item_pos;
struct node *begin=NULL;
while(1)
{
cout<<"\n1.Create A New Doubly Linked List\n";
cout<<"2.Display the Doubly Linked List\n";
cout<<"3.Add to an Empty Doubly Linked List\n";
cout<<"4.Add at Starting of the Doubly Linked List\n";
cout<<"5.Add at Ending\n";
cout<<"6.Add After a Node\n";
cout<<"7.Add Before a Node\n";
cout<<"8.Delete a Node\n";
cout<<"9.Reverse the Doubly Linked List\n";
cout<<"10.Exit\n";
cout<<"Enter your option : ";
cin>>option;

switch(option)
{
case 1:
begin=create_list(begin);
break;
case 2:
display(begin);
break;
case 3:
cout<<"Enter the element:";
cin>>data_element;
begin=addtoemptylist(begin,data_element);
break;
case 4:
cout<<"Enter the element:";
cin>>data_element;
begin=addatbeglist(begin,data_element);
break;
case 5:
cout<<"Enter the element:";
cin>>data_element;
begin=addatendlist(begin,data_element);
break;
case 6:
cout<<"Enter the element:";
cin>>data_element;
cout<<"Enter the element after which to insert : ";
cin>>item_pos;
begin=addafterlist(begin,data_element,item_pos);
break;
case 7:
cout<<"Enter the element: ";
cin>>data_element;
cout<<"Enter the element before which to insert : ";
cin>>item_pos;
begin=addbeforelist(begin,data_element,item_pos);
break;
case 8:
cout<<"Enter the element to be Deleted : ";
cin>>data_element;
begin=deletenode(begin,data_element);
break;
case 9:
begin=reverselist(begin);
break;
case 10:
exit(1);
default:
cout<<"Wrong option\n";
}
}

return 0;
}

// Funtion To Create List


struct node *create_list(struct node *begin)
{
int i,n,data_element;
cout<<"Enter the number of nodes : ";
cin>>n;
begin=NULL;
if(n==0)
return begin;
cout<<"Enter the element: ";
cin>>data_element;
begin=addtoemptylist(begin,data_element);

for(i=2;i<=n;i++)
{
cout<<"Enter the element to be inserted : ";
cin>>data_element;
begin=addatendlist(begin,data_element);
}
return begin;
}

void display(struct node *begin)


{
struct node *p;
if(begin==NULL)
{
cout<<"List is empty\n";
return;
}
p=begin;
cout<<"List is :\n";
while(p!=NULL)
{
cout<<p->info<<" ";
p=p->next_node;
}
cout<<"\n";
}

struct node *addtoemptylist(struct node *begin,int data_element)


{
struct node *temp;
temp=new struct node;
temp->info=data_element;
temp->prev_node=NULL;
temp->next_node=NULL;
begin=temp;
return begin;
}
struct node *addatbeglist(struct node *begin,int data_element)
{
if(begin==NULL)
{
cout<<"List is empty\n";
return begin;
}

struct node *temp;


temp = new struct node;
temp->info=data_element;
temp->prev_node=NULL;
temp->next_node=begin;
begin->prev_node=temp;
begin=temp;
return begin;
}

struct node *addatendlist(struct node *begin,int data_element)


{
if(begin==NULL)
{
cout<<"List is empty\n";
return begin;
}

struct node *temp,*p;


temp=new struct node;
temp->info=data_element;
p=begin;
while(p->next_node!=NULL)
p=p->next_node;
p->next_node=temp;
temp->next_node=NULL;
temp->prev_node=p;
return begin;
}

struct node *addafterlist(struct node *begin,int data_element,int


item_pos)
{
struct node *temp,*p;
temp=new struct node;
temp->info=data_element;
p=begin;
while(p!=NULL)
{
if(p->info==item_pos)
{
temp->prev_node=p;
temp->next_node=p->next_node;
if(p->next_node!=NULL)
p->next_node->prev_node=temp;
p->next_node=temp;
return begin;
}
p=p->next_node;
}
cout<<item_pos<<" not present in the list\n\n";
return begin;
}

struct node *addbeforelist(struct node *begin,int data_element,int


item_pos)
{
struct node *temp,*q;
if(begin==NULL )
{
cout<<"List is empty\n";
return begin;
}
if(begin->info==item_pos)
{
temp = new struct node;
temp->info=data_element;
temp->prev_node=NULL;
temp->next_node=begin;
begin->prev_node=temp;
begin=temp;
return begin;
}
q=begin;
while(q!=NULL)
{
if(q->info==item_pos)
{
temp=new struct node;
temp->info=data_element;
temp->prev_node=q->prev_node;
temp->next_node = q;
q->prev_node->next_node=temp;
q->prev_node=temp;
return begin;
}
q=q->next_node;
}
cout<<item_pos<<" not present in the list\n";
return begin;
}

struct node *deletenode(struct node *begin,int data_element)


{
struct node *temp;
if(begin==NULL)
{
cout<<"List is empty\n";
return begin;
}
if(begin->next_node==NULL)
if(begin->info==data_element)
{
temp=begin;
begin=NULL;
delete(temp);
return begin;
}
else
{
cout<<"Element "<<data_element<<" not found\n";
return begin;
}

if(begin->info==data_element)
{
temp=begin;
begin=begin->next_node;
begin->prev_node=NULL;
delete(temp);
return begin;
}
temp=begin->next_node;
while(temp->next_node!=NULL )
{
if(temp->info==data_element)
{
temp->prev_node->next_node=temp->next_node;
temp->next_node->prev_node=temp->prev_node;
delete(temp);
return begin;
}
temp=temp->next_node;
}

if(temp->info==data_element)
{
temp->prev_node->next_node=NULL;
delete(temp);
return begin;
}
cout<<"Element "<<data_element<<" not found\n";
return begin;
}

struct node *reverselist(struct node *begin)


{
if(begin==NULL)
{
cout<<"List is empty\n";
return begin;
}

struct node *p1,*p2;


p1=begin;
p2=p1->next_node;
p1->next_node=NULL;
p1->prev_node=p2;
while(p2!=NULL)
{
p2->prev_node=p2->next_node;
p2->next_node=p1;
p1=p2;
p2=p2->prev_node;
}
begin=p1;
cout<<"List reverselistd\n";
return begin;
}
OUTPUT: IMPLEMENT THIS CODE TO SEE OUTPUT

TASKS
Task #.1: Estimated Time: 75 Mins.
Write a simple airline ticket reservation system program. The program should display a menu with
following options: Reserve a ticket, cancel a reservation, check whether a ticket is reserved for a
particular person and display the passengers. The information is maintained alphabetized link list of
names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a
fuller version, place no limit on the number of flights. Create a link list of flights with each node including
a pointer to a link list of passengers.

#include <iostream>
using namespace std;

struct Passenger
{
string name;
Passenger *prev;
Passenger *next;

Passenger(string n)
{
name = n;
prev = next = nullptr;
}
};

struct Flight
{
int flightNumber;
Passenger *passengerHead;
Flight *next;

Flight(int num)
{
flightNumber = num;
passengerHead = nullptr;
next = nullptr;
}
};

class Ticket
{
private:
Flight *flighthead;

public:
Ticket()
{
flighthead = NULL;
}

Flight *findflight(int num)


{
Flight *temp = flighthead;
while (temp != NULL)
{
if (temp->flightNumber == num)
{
return temp;
}
temp = temp->next;
}
return NULL;
}

void addflight(int num)


{
if (findflight(num) != NULL)
{
cout << "Flight# " << num << " already exists!" << endl;
return;
}
Flight *newflightnode = new Flight(num);
newflightnode->next = flighthead;
flighthead = newflightnode;
cout << "Flight# " << num << " added successfully!" << endl;
}

void reserveticket(int num, string name)


{
Flight *flight = findflight(num);
if (flight == NULL)
{
cout << "Flight# " << num << " not found!" << endl;
return;
}

Passenger *newpassenger = new Passenger(name);


Passenger *curr = flight->passengerHead;
Passenger *prev = NULL;

while (curr != nullptr && curr->name < name)


{
prev = curr;
curr = curr->next;
}

if (curr != nullptr && curr->name == name)


{
cout << "Passenger " << name << " already has a reservation!" <<
endl;
delete newpassenger;
return;
}

newpassenger->next = curr;
newpassenger->prev = prev;

if (prev != NULL)
prev->next = newpassenger;
else
flight->passengerHead = newpassenger;

if (curr != nullptr)
curr->prev = newpassenger;

cout << "Ticket reserved for " << name << " on Flight " << num << "." <<
endl;
}

void cancelreservation(int num, string name)


{
Flight *flight = findflight(num);
if (flight == NULL)
{
cout << "Flight# " << num << " not found!" << endl;
return;
}
Passenger *curr = flight->passengerHead;
while (curr != NULL && curr->name < name)
{
curr = curr->next;
}
if (curr == NULL || curr->name != name)
{
cout << "Reservation not found for " << name << "!" << endl;
return;
}

if (curr->prev == NULL)
{
flight->passengerHead = curr->next;
}
else
curr->prev->next = curr->next;

if (curr->next != nullptr)
curr->next->prev = curr->prev;

delete curr;
cout << "Reservation cancelled successfully for " << name << "!" << endl;
}

void checkreservation(int num, string name)


{
Flight *flight = findflight(num);
if (flight == NULL)
{
cout << "Flight# " << num << " not found!" << endl;
return;
}

Passenger *finder = flight->passengerHead;


while (finder != NULL)
{
if (finder->name == name)
{
cout << name << " has a reservation on Flight# " << num << "." <<
endl;
return;
}
finder = finder->next;
}
cout << "No reservation found for " << name << " on Flight# " << num <<
"." << endl;
}

void display()
{

Flight *temp = flighthead;


Passenger *temp2 = temp->passengerHead;
if (temp == NULL)
{
cout << "No flight avaialable" << endl;
return;
}
if (temp2 == NULL)
{
cout << "No passengers.";
}

while (temp != NULL)


{
cout << "Flight# " << temp->flightNumber << " ";

while (temp2 != NULL)


{
cout << "Name: " << temp2->name << endl;
temp2 = temp2->next;
}

temp = temp->next;
}
}
};

int main()
{
int choice;
int flightNumber;
string name;
Ticket F1;
while (true)
{
cout << "\nMenu:" << endl;
cout << "1. Add Flight" << endl;
cout << "2. Reserve Ticket" << endl;
cout << "3. Cancel Reservation" << endl;
cout << "4. Check Reservation" << endl;
cout << "5. Display Passengers" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();

switch (choice)
{
case 1:
cout << "Enter flight number: ";
cin >> flightNumber;
F1.addflight(flightNumber);
break;
case 2:
cout << "Enter flight number: ";
cin >> flightNumber;
cin.ignore();
cout << "Enter passenger name: ";
getline(cin, name);
F1.reserveticket(flightNumber, name);
break;
case 3:
cout << "Enter flight number: ";
cin >> flightNumber;
cin.ignore();
cout << "Enter passenger name to cancel: ";
getline(cin, name);
F1.cancelreservation(flightNumber, name);
break;
case 4:
cout << "Enter flight number: ";
cin >> flightNumber;
cin.ignore();
cout << "Enter passenger name to check: ";
getline(cin, name);
F1.checkreservation(flightNumber, name);
break;
case 5:
F1.display();
break;
case 6:
cout << "Exiting program!!!" << endl;
return 0;
default:
cout << "Invalid Entry" << endl;
break;
}
}
return 0;
}

You might also like