Professional Documents
Culture Documents
DS Doubly Linked List Assignment - Removed PDF
DS Doubly Linked List Assignment - Removed PDF
GROUP MEMBERS
AILA MAHMOOD (4213-FBAS/BSSE/F21A)
EISHA INAM (4249-FBAS/BSSE/F21A)
JAWERIA AMIR (4195-FBAS/BSSE/F21A)
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;
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;
if (first == 0)
{
first = temp;
last = temp;
}
else
{
while (last->next != 0)
{
last = last->next;
}
last->next = temp;
temp->prev = last;
last = temp;
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;
}
bool DoublyLinkedList::FindValue()
{
Node* temp = first;
string value;
cout << "Enter value : ";
cin >> value;
bool found = false;
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;
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);
}
Output