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

Bse193118 Lab 9 Datastructure

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)
9 views18 pages

Bse193118 Lab 9 Datastructure

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

Bse193118

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;
}

void linklist::InsertEnd(nodetype& abc)


{
nodetype* nnode = new nodetype;
nnode->name = abc.name;
nnode->regno = abc.regno;
nnode->age = abc.age;
nnode->cgpa = abc.cgpa;
nnode->city = abc.city;
nnode->next = NULL;
nnode->previous = NULL;
if (isempty())
{
head = nnode;
tail = nnode;
}
else
{
tail->next = nnode;
nnode->previous = tail;
tail = nnode;
}
}
void linklist::Insertfront(nodetype& abc)
{
nodetype* nnode = new nodetype;
nnode->name = abc.name;
nnode->regno = abc.regno;
nnode->age = abc.age;
nnode->cgpa = abc.cgpa;
nnode->city = abc.city;
nnode->next = NULL;
nnode->previous = NULL;
if (isempty())
{
head = nnode;
tail = nnode;
}
else
{
head->previous = nnode;
nnode->next = head;
head = nnode;
}
}

void linklist::Insertmiddle(nodetype &abc, string x)


{
nodetype* nnode = new nodetype;
nnode->name = abc.name;
nnode->regno = abc.regno;
nnode->age = abc.age;
nnode->cgpa = abc.cgpa;
nnode->city = abc.city;
nnode->next = NULL;
nnode->previous = NULL;
nodetype* current = head;
if (isempty())
{
head = nnode;
tail = nnode;
}
else if (x == tail->name)
cout << "Your Not Entering Data in Middle" << endl;
else
{
while (current->name != x)
current = current->next;
current->next->previous = nnode;
nnode->next = current->next;
nnode->previous = current;
current->next = nnode;
}
}
void MENU()
{
cout << "1) Insert at Front" << endl;
cout << "2) Insert at Rear" << endl;
cout << "3) Insert In Middle After" << endl;
cout << "4) Delete from Front" << endl;
cout << "5) Delete from Rear" << endl;
cout << "6) Delete from Middle" << endl;
cout << "7) Display all Data" << endl;
}
int main()
{
linklist xyz;
string name;
int count = 0;
int choice;
nodetype abc;
while (1)
{
MENU();
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
cout << "Enter Name" << endl;
getline(cin, abc.name);
cout << "Enter RegNo." << endl;
getline(cin, abc.regno);
cout << "Enter Age" << endl;
cin >> abc.age;
cout << "Enter CGPA" << endl;
cin >> abc.cgpa;
cin.ignore();
cout << "Enter City" << endl;
getline(cin, abc.city);
xyz.Insertfront(abc);
system("pause");
system("cls");
break;
case 2:
cout << "Enter Name" << endl;
getline(cin, abc.name);
cout << "Enter RegNo." << endl;
getline(cin, abc.regno);
cout << "Enter Age" << endl;
cin >> abc.age;
cout << "Enter CGPA" << endl;
cin >> abc.cgpa;
cin.ignore();
cout << "Enter City" << endl;
getline(cin, abc.city);
xyz.InsertEnd(abc);
system("pause");
system("cls");
break;
case 3:
cout << "Enter Name of Student To Whom After
you want to Enter a New Students" << endl;
getline(cin, name);
cout << "Enter Name" << endl;
getline(cin, abc.name);
cout << "Enter RegNo." << endl;
getline(cin, abc.regno);
cout << "Enter Age" << endl;
cin >> abc.age;
cout << "Enter CGPA" << endl;
cin >> abc.cgpa;
cin.ignore();
cout << "Enter City" << endl;
getline(cin, abc.city);
xyz.Insertmiddle(abc,name);
system("pause");
system("cls");
break;
case 4:
xyz.Deletefront();
system("pause");
system("cls");
break;
case 5:
xyz.DeleteEnd();
system("pause");
system("cls");
break;
case 6:
cout << "Enter Name of Student who you want
to Delete" << endl;
getline(cin, name);
xyz.Deletemiddle(name);
system("pause");
system("cls");
break;
case 7:
xyz.Displayallelements();
cout << endl;
system("pause");
system("cls");
break;
default:
cout << "Invalid Choice" << endl;
}
}
return 0;
}
Code 2
#include<iostream>
#include<string>
using namespace std;
struct nodetype
{
int data;
nodetype* next;
nodetype* previous;
};

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::InsertEnd(nodetype &abc)


{
nodetype* nnode = new nodetype;
nnode->data = abc.data;
nnode->next = NULL;
nnode->previous = NULL;
if (isempty())
{
head = nnode;
tail = nnode;
}
else
{
tail->next = nnode;
nnode->previous = tail;
tail = nnode;
}
}
void linklist::Insertfront(nodetype &abc)
{
nodetype* nnode = new nodetype;
nnode->data = abc.data;
nnode->next = NULL;
nnode->previous = NULL;
if (isempty())
{
head = nnode;
tail = nnode;
}
else
{
head->previous = nnode;
nnode->next = head;
head = nnode;
}
}

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;
}

You might also like