Bse193118 Lab 9 Datastructure
Bse193118 Lab 9 Datastructure
Lab task 9
Code1
#include<iostream>
#include<string>
using namespace std;
struct nodetype
{
string regno, name, city;
int age;
float cgpa;
nodetype* next;
nodetype* previous;
};
class linklist
{
private:
nodetype* head;
nodetype* tail;
public:
linklist(void);
void Insertfront(nodetype&);
void InsertEnd(nodetype&);
void Insertmiddle(nodetype&, string x);
void Deletefront();
void DeleteEnd();
void Deletemiddle(string);
void Displayallelements();
bool isempty(void);
};
linklist::linklist(void)
{
head = NULL;
tail = NULL;
}
bool linklist::isempty(void)
{
if (head == NULL)
return true;
else
return false;
}
void linklist::Displayallelements(void)
{
nodetype* current = head;
int i = 0;
if (isempty())
{
cout << "List is Empty" << endl;
}
else
while (current != NULL)
{
cout << "Student " << i + 1 << endl;
cout << "Name: " << current->name << endl;
cout << "Reg No.: " << current->regno << endl;
cout << "Age: " << current->age << endl;
cout << "CGPA: " << current->cgpa << endl;
cout << "City: " << current->city << endl;
current = current->next;
i++;
cout << endl;
}
}
void linklist::Deletefront()
{
if (!isempty())
{
nodetype* current = head;
head = head->next;
if (head != NULL)
head->previous = NULL;
else
tail = NULL;
delete current;
}
else
cout << "List is empty! Deletion failed" << endl;
}
void linklist::DeleteEnd()
{
if (!isempty())
{
nodetype* temp = tail;
tail = tail->previous;
if (tail != NULL)
tail->next = NULL;
else
head = NULL;
delete temp;
}
else
cout << "List is empty! Deletion failed" << endl;
}
void linklist::Deletemiddle(string x)
{
bool chk = false, xchk = false;
nodetype* current;
current = head;
nodetype* temp = NULL;
if (isempty())
cout << "list is Empty" << endl;
else if (tail->name == x)
{
cout << "Your Not Deleting from Middle" << endl;
}
else if (head->name == x)
{
cout << "Your Not Deleting from Middle" << endl;
}
else
while (1)
{
if (current->next == NULL)
{
xchk = true;
break;
}
else if (current->next->name == x)
{
temp = current->next;
current->next = current->next->next;
current->next->previous = current;
chk = true;
break;
}
current = current->next;
}
if (chk)
delete temp;
else if (xchk)
cout << "No Such Number in Linked List" << endl;
}
class linklist
{
private:
nodetype* head;
nodetype* tail;
public:
linklist(void);
void Insertfront(nodetype &);
void InsertEnd(nodetype &);
void Deletefront();
void DeleteEnd();
void Displayallelements();
bool isempty(void);
};
linklist::linklist(void)
{
head = NULL;
tail = NULL;
}
bool linklist::isempty(void)
{
if (head == NULL)
return true;
else
return false;
}
void linklist::Deletefront()
{
if (!isempty())
{
nodetype* current = head;
head = head->next;
if (head != NULL)
head->previous = NULL;
else
tail = NULL;
delete current;
}
else
cout << "List is empty! Deletion failed" << endl;
}
void linklist::DeleteEnd()
{
if (!isempty())
{
nodetype* temp = tail;
tail = tail->previous;
if (tail != NULL)
tail->next = NULL;
else
head = NULL;
delete temp;
}
else
cout << "List is empty! Deletion failed" << endl;
}
void linklist::Displayallelements(void)
{
nodetype* current = head;
int i = 0;
if (isempty())
{
cout << "List is Empty" << endl;
}
else
while (current != NULL)
{
cout << "Number " << i + 1 << endl;
cout << "Number: " << current->data << endl;
current = current->next;
i++;
cout << endl;
}
}
void MENU()
{
cout << "1) Insert at start of Queue" << endl;
cout << "2) Delete from Start of Queue" << endl;;
cout << "3) Insert at End of Queue" << endl;
cout << "4) Delete from End of Queue" << endl;
cout << "5) Display All Elements" << endl;
}
int main()
{
linklist xyz;
int count = 0;
int choice;
nodetype abc;
while (1)
{
MENU();
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
cout << "Enter Number" << endl;
cin >> abc.data;
xyz.Insertfront(abc);
system("pause");
system("cls");
break;
case 2:
xyz.Deletefront();
system("pause");
system("cls");
break;
case 3:
cout << "Enter Number" << endl;
cin >> abc.data;
xyz.InsertEnd(abc);
system("pause");
system("cls");
break;
case 4:
xyz.DeleteEnd();
system("pause");
system("cls");
break;
case 5:
xyz.Displayallelements();
cout << endl;
system("pause");
system("cls");
}
}
return 0;
}