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

DSA Lab 09

The document contains source code for a C++ program that implements a doubly linked list. The program allows the user to insert nodes at the end of the list, delete nodes from the end, search for a node by value, display all nodes, and check if the list is empty. The main function contains a menu that calls functions for these linked list operations on a doubly linked list with nodes that contain data and next/previous node pointers.

Uploaded by

Atif Jalal
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)
73 views

DSA Lab 09

The document contains source code for a C++ program that implements a doubly linked list. The program allows the user to insert nodes at the end of the list, delete nodes from the end, search for a node by value, display all nodes, and check if the list is empty. The main function contains a menu that calls functions for these linked list operations on a doubly linked list with nodes that contain data and next/previous node pointers.

Uploaded by

Atif Jalal
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/ 5

EXERCISE 1

SOURCE CODE:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 09 Date: 14 July, 2020

#include <iostream>
using namespace std;

struct node
{
int num;
node *next;
node *prev;
};

node *head = NULL;

void insertAtEnd(int n);


void deleteFromEnd();
void display();
bool isListEmpty();
int search(int);

void main()
{
int a = 0, b;
cout << "\n ------------------ DOUBLY LINK LIST BASED LIST ---------" << endl;
cout << " 1. Insert at end \n 2. Delete from end \n 3. Search " << endl;
cout << " 4. Display \n 5. Is List Empty? \n 6. Exit" << endl;
cout << " -------------------------------------------------------" << endl;
while (a != 6)
{
cout << "\n Enter your choice : ";
cin >> a;
switch (a)
{
case 1:
{
cout << " Enter number : ";
cin >> b;
insertAtEnd(b);
break;
}
case 2:
{
deleteFromEnd();
break;
}
case 3:
{
cout << " Enter number : ";
cin >> b;
if (search(b) == -1)
cout << "\n Value doesn't exist." << endl;
else
{
cout << "\n Value exists in node " << search(b) + 1 << "." << endl;
}
break;
Atif Jalal
02-235191-027
BS (IT)-3A Lab 09 Date: 14 July, 2020
}
case 4:
{
display();
break;
}
case 5:
{
if (isListEmpty())
{
cout << "\n List is Empty" << endl;
}
else
{
cout << "\n List is not Empty" << endl;
}
break;
}
case 6:
break;
default:
cout << "\n Select from the given (1-6) options." << endl;
}
}
system("pause");
}

void insertAtEnd(int n)
{
node *obj = new node;
obj->num = n;
obj->next = NULL;
obj->prev = NULL;
if (head == NULL)
{
head = obj;
}
else
{
node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = obj;
obj->prev = temp;
}
}

void deleteFromEnd()
{
if (head == NULL)
{
cout << "\n List Empty";
Atif Jalal
02-235191-027
BS (IT)-3A Lab 09 Date: 14 July, 2020
}
else if (head->next == NULL)
{
delete head;
}
else
{
node *temp = head;
while (temp->next->next != NULL)
{
temp = temp->next;
}
delete (temp->next);
temp->next = NULL;
}
}

void display()
{
int a = 0;
if (head == NULL)
{
cout << "\n List Empty";
}
else
{
node *temp = head;
while (temp->next != NULL)
{
cout << " Num " << a + 1 << " : " << temp->num << endl;
temp = temp->next;
a++;
}
cout << " Num " << a + 1 << " : " << temp->num << endl;
}
}

bool isListEmpty()
{
if (head == NULL)
{
return true;
}
else
return false;
}

int search(int n)
{
int a = 0;
if (head == NULL)
{
cout << "\n List Empty";
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 09 Date: 14 July, 2020
else
{
node *temp = head;
while (temp->next != NULL)
{
if (n == temp->num)
{
return a;
}
else
{
temp = temp->next;
a++;
}
}
if (temp->num == n)
{
return a;
}
else
{
return a = -1;
}
}
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy