Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ASSIGNMENT

SUBJECT : DATA STRUCTURES


TASK BY : MADAM SHAISTA RASHID

GROUP MEMBERS
AILA MAHMOOD (4213-FBAS/BSSE/F21A)
EISHA INAM (4249-FBAS/BSSE/F21A)
JAWERIA AMIR (4195-FBAS/BSSE/F21A)

DOUBLY LINKED LISTS


List.h
#include <iostream>
#include "string"
using namespace std;

class Node
{
public:
string data; //data of node
Node* next; //address of next node
Node* prev; //address of previous node
};

class DoublyLinkedList
{
Node* first; //points towards first node
Node* last; //points towards last node

public:
DoublyLinkedList()
{
first = 0;
last = 0;
}

void InsertItem(string);
void InsertatStart();
void InsertatEnd();
void Display();
int CountNodes();
void DeleteFirstNode();
void DeleteLastNode();
void DeleteItem(string);
bool FindValue();
string MinimumValue();
void FirstCh(char);

~DoublyLinkedList()
{
Node* temp = first;
while (temp != 0)
{
first = first->next;
delete temp;
temp = first;
}
last = 0;
}
};

List.cpp
#include "List.h"
#include "string"
#include <iostream>
using namespace std;

void DoublyLinkedList::InsertItem(string value)


{
Node* temp = first;
Node* temp1 = new Node;
temp1->data = value;
temp1->next = 0;
temp1->prev = 0;

if (first == 0)
{
first = temp1;
last = temp1;
}
else if (value < first->data)
{
temp1->next = first;
first->prev = temp1; //temp->prev = temp1;
first = temp1;
}
else
{
while (temp != 0 && temp->data < value)
{
temp = temp->next;
}

if (temp == 0)
{
temp1->prev = last;
last->next = temp1;
last = temp1;
}
else
{
temp1->prev = temp->prev;
temp->prev->next = temp1;
temp->prev = temp1;
temp1->next = temp;
}
}
cout << "Value added" << endl;
}
void DoublyLinkedList::InsertatStart()
{
string n;
cout << "Enter value to add : ";
cin >> n;

Node* temp;
temp = new Node;
temp->data = n;
temp->next = 0;
temp->prev = 0;

if (first == 0)
{
first = temp;
last = temp;
}
else
{
first->prev = temp;
temp->next = first;
first = temp;
}
cout << "Value added" << endl;
}

void DoublyLinkedList::InsertatEnd()
{
Node* last = first;
string n;
cout << "Enter value to add : ";
cin >> n;

Node* temp; //pointer for every new node


temp = new Node;
temp->data = n; //data stores value of n
temp->next = 0; //last next becomes null
temp->prev = 0;

if (first == 0)
{
first = temp;
last = temp;
}
else
{
while (last->next != 0)
{
last = last->next;
}

last->next = temp;
temp->prev = last;
last = temp;

cout << "Value added" << endl;


}

void DoublyLinkedList::Display()
{
Node* temp;
if (first == 0)
cout << "List is empty" << endl;
else
{
temp = first;
while (temp != 0)
{
cout << temp->data << endl;
temp = temp->next;
}

}
}
int DoublyLinkedList::CountNodes()
{
Node* temp = first; //initialization
int count = 0;

while (temp != 0)
{
count++;
temp = temp->next;
}
return count;
}
void DoublyLinkedList::DeleteFirstNode()
{
if (first == 0)
cout << "List is empty" << endl;
else
{
Node* temp = first;
first = first->next;
delete temp;

if (first == 0)
last = 0;
}
cout << "Value deleted" << endl;
}
void DoublyLinkedList::DeleteLastNode()
{
Node* temp = first;

if (first == 0) //0 node


cout << "List is empty" << endl;
else if (first == last) //1 node
{
delete first;
first = 0;
last = 0;
cout << "Value deleted" << endl;
}
else //more than 1 nodes
{
while (temp->next != last)
{
temp = temp->next;
}
last = last->prev;
temp = temp->next;
last->next = 0;
delete temp;
cout << "Value deleted" << endl;
}
}

void DoublyLinkedList::DeleteItem(string value)


{
Node* temp = first;
bool found = false;

if (first == 0) //0 node


cout << "List is empty" << endl;
while (temp != 0 && found == false) //found
{
if (temp->data == value)
{
found = true;
}
else
{
temp = temp->next;
}
}
if (found == false)
{
cout << "Value not found" << endl;
}
else if (temp == first)
{
DeleteFirstNode();
}
else if (temp == last)
{
DeleteLastNode();
}
else
{
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
delete temp;
cout << "Value deleted" << endl;
}

}
bool DoublyLinkedList::FindValue()
{
Node* temp = first;
string value;
cout << "Enter value : ";
cin >> value;
bool found = false;

while (temp != 0 && found == false)


{
if (temp->data == value)
{
found = true;
return true; //return found;
}
temp = temp->next;
}
if (found == false)
{
return false;
}
}
string DoublyLinkedList::MinimumValue()
{
Node* temp = first;
Node* minNode = first;

string min;
if (first == 0)
cout << "List is empty" << endl;
else
{
min = first->data;
while (temp != 0)
{
if (temp->data < min)
{
min = temp->data;
}
temp = temp->next;
}
return min;

}
}
void DoublyLinkedList::FirstCh(char c)
{
Node* temp = first;
bool found = false;

while (temp != 0)
{
if (temp->data[0] == c)
{
found = true;
cout << temp->data << endl;
}
temp = temp->next;
}
if (found == false)
{
cout << "No value from given character" << endl;
}

Source.cpp
#include "List.h"
#include <iostream>
#include "string"
using namespace std;
int main()
{
int choice, n;
char i;
DoublyLinkedList l;

cout << "Enter 1 to insert an item" << endl;


cout << "Enter 2 to insert value at start of the list" << endl;
cout << "Enter 3 to insert value at end of the list" << endl;
cout << "Enter 4 to display the list" << endl;
cout << "Enter 5 to count nodes" << endl;
cout << "Enter 6 to delete first node" << endl;
cout << "Enter 7 to delete last node" << endl;
cout << "Enter 8 to delete any value" << endl;
cout << "Enter 9 to find value in the list" << endl;
cout << "Enter 10 to find the minimum value" << endl;
cout << "Enter 11 to display all node values beginning with given character" << endl;

do
{
cout << "Enter 1-11 : ";
cin >> choice;

if (choice == 1)
{
cout << "INSERT AN ITEM" << endl;
string n;
cout << "Enter value : ";
cin >> n;
l.InsertItem(n);
}
else if (choice == 2)
{
cout << "INSERT AT START" << endl;
l.InsertatStart();
}
else if (choice == 3)
{
cout << "INSERT AT END" << endl;
l.InsertatEnd();
}
else if (choice == 4)
{
cout << "DISPLAY" << endl;
l.Display();
}
else if (choice == 5)
{
cout << "COUNT NODES" << endl;
cout << l.CountNodes() << endl;
}
else if (choice == 6)
{
cout << "DELETE FIRST ITEM" << endl;
l.DeleteFirstNode();
}
else if (choice == 7)
{
cout << "DELETE LAST ITEM" << endl;
l.DeleteLastNode();
}
else if (choice == 8)
{
cout << "DELETE AN ITEM" << endl;
string n;
cout << "Enter value : ";
cin >> n;
l.DeleteItem(n);
}
else if (choice == 9)
{
cout << "FIND AN ITEM" << endl;
if (l.FindValue() == 1)
cout << "Value found" << endl;
else
cout << "Value not found" << endl;
}
else if (choice == 10)
{
cout << "MINIMUM VALUE" << endl;
cout << l.MinimumValue() << endl;
}
else if (choice == 11)
{
cout << "ITEMS FROM GIVEN CHARACTER" << endl;
char c;
cout << "Enter first character : ";
cin >> c;
l.FirstCh(c);
}

cout << "Want to enter more? Enter (y/n) : ";


cin >> i;
} while (i == 'y');

Output

You might also like