User:Chechu-20067

Write a C++ program to Delete an element anywhere from the linear linked list. Menu driven

program (at the beginning, end and after an element). #include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node(int value)

{

data = value;

next = nullptr;

}

};

class linkedlist

{

public:

Node *head;

linkedlist

{

head = nullptr;

}

void create(int value)

{

Node *newnode = new Node(value);

if (head == nullptr)

{

head = newnode;

}

else

{

Node *current = head;

while (current->next != nullptr)

current = current->next;

current->next = newnode;

}

}

void deletebeg

{

Node *ptr;

if (head == nullptr)

{cout<<"There is no node or empty"<next;

delete ptr;

}

cout<<"Element sucessfully deleted from beginning"<next == nullptr)

{

ptr = head;

head = nullptr;

delete ptr;

}

else

{

loc = head;

ptr = head->next;

while (ptr->next != nullptr)

{

loc = ptr;

ptr = ptr->next;

}

loc->next = nullptr;

delete ptr;

}

}

cout << "Element successfully deleted from the end" << endl;

}

void deletespec

{

Node *ptr, *loc;

int temp;

cout << "Enter the element you want to delete: ";

cin >> temp;

ptr = head;

if (head == nullptr)

{

cout << "Empty list" << endl;

return;

}

else

{

if (temp == ptr->data)

{

head = ptr->next;

delete ptr;

cout << "Element deleted successfully" << endl;

return;

}

loc = ptr;

while (ptr != nullptr)

{

if (ptr->data == temp)

{

loc->next = ptr->next;

delete ptr;

cout << "Element deleted successfully" << endl;

return;

}

loc = ptr;

ptr = ptr->next;

}

cout << "Element not found" << endl;

}

}

void display

{if(head!=nullptr)

{

Node *current = head;

while (current != nullptr)

{

cout << current->data << "->";

current = current->next;

}

cout << "Nullptr" << endl;

}

else

cout<<"The list is Empty"<> a;

list.create(a);

cout << "Do you want to add one more element? enter 1:" << endl;

cin >> y;

} while (y == 1);

int ch, item, loc;

do

{

cout << "1.Delete at beginning" << endl

<< "2.Delete at end" << endl

<< "3.Delete at specific position" << endl

<< "4.Display" << endl

<< "5.Exit" << endl;

cout << "Enter your choice" << endl;

cin >> ch;

switch (ch)

{

case 1:

list.deletebeg;

break;

case 2:

list.deleteend;

break;

case 3:

list.deletespec;

break;

case 4:

cout << "Elements of the list are:" << endl;

list.display;

break;

case 5:

cout << "Exiting....";

exit(0);

break;

}

} while (ch != 5);

return 0;

}

//27.corrected(1) Write a C++ program to Insert an element anywhere in the linear linked list. Menu driven program and use recursive function (at the beginning, end and after an element)


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Linkedlist

{

public:

Node *head;

Linkedlist

{

head=nullptr;

}

void create(int value)

{

int ch;

Node *newnode =new Node(value);

if(head==nullptr)

{

head=newnode;

}

else

{

Node *current= head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

}

cout<<"\n do you want to add one more element(enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the element you want to insert: ";

cin>>value;

create(value);

}

else

return;

}

void insertbeginning(int n)

{

int ch;

Node *ptr=new Node;

ptr->data=n;

if(head==nullptr)

ptr->next=nullptr;

else

{

ptr->next=head;

head=ptr;

}

cout<<"\n do you want to insert another element in the beginning(enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the value to be inserted: ";

cin>>n;

insertbeginning(n);

}

else

return;

}

void insertend(int n)

{

int ch;

Node *ptr=new Node;

Node *temp;

ptr->data=n;

ptr->next=nullptr;

if(head==nullptr)

head=ptr;

else

{

temp=head;

while(temp->next!=nullptr)

temp=temp->next;

temp->next=ptr;

}

cout<<"\n do you want to insert another element at the end (enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the element you want insert : ";

cin>>n;

insertend(n);

}

else

return;

}

void insertspecific(int n,int item)

{   int ch;

Node *loc,*temp=head;

loc=nullptr;

while(temp!=nullptr)

{

if(temp->data==item)

{

loc=temp;

break;

}

else

temp=temp->next;

}

if(loc==nullptr)

cout<<"\n element not found ";

else

{

Node *ptr=new Node;

ptr->data=n;

ptr->next=loc->next;

loc->next=ptr;

cout<<"\n the linked list is : ";

display;

}

cout<<"\n do you want to insert another element at a specific position (enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the element you want insert : ";

cin>>n;

cout<<"\n after which element you want to insert :";

cin>>item;

insertspecific(n,item);

}

else

return;

}

void display

{

Node *current=head;

if(current==nullptr)

{

cout<<"\n empty list!! "<data<<"->";

current=current->next;

}

cout<<"nullptr"<>a;

list.create(a);

cout<<"\n the linked list is: ";

list.display;

do

{

cout<<"\n...menu..."<>ch;

switch(ch)

{

case 1:cout<<"\n enter the element you want insert at      beginning: ";

cin>>a;

list.insertbeginning(a);

cout<<"\n the linked list is: ";

list.display;

break;

case 2:cout<<"\n enter the element you want to insert at    end: ";

cin>>a;

list.insertend(a);

cout<<"\n the linked list is: ";

list.display;

break;

case  3:cout<<"\n enter the new element you wnt to insert:    ";

cin>>a;

cout<<"\n after which element you want to insert:";

cin>>l;

list.insertspecific(a,l);

break;

case 0:cout<<"\n exiting....";

break;

default:cout<<"\n invalid choice .enter a valid choice";

}

}while(ch!=0);

return 0;

} using namespace std;
 * 1) include
 * 1) include
 * 1) include

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice;

switch (choice) {       case 1:#include using namespace std;

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice;

switch (choice) {       case 1: char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch; using namespace std;
 * 1) include

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }#include using namespace std;

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice; using namespace std;
 * 1) include

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice;

switch (choice) {       case 1: char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch;

if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.d#include using namespace std;

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }#include using namespace std;

struct Node {   int data; Node *next; };

class LinkedList {   Node *start; int count;

public: LinkedList {       start = nullptr; count = 0; }

void Create {

insert; char ch; cout << "Do you want to insert another element : "; cin >> ch; if (ch == 'y') {           Create; }       return; }

void insertPos {       int x;        cout << "Enter the value to insert : "; cin >> x;

int pos; while (true) {           cout << "Enter the position to insert : "; cin >> pos;

if (pos < 1 || pos > (count + 1)) {               cout << "\n\nInvalid position !"; }           else {               break; }       }

Node *prevNode = SearchNodePos(pos); Node *newNode = new Node; newNode->data = x;

if (prevNode == nullptr) {           newNode->next = start; start = newNode; }       else {           newNode->next = prevNode->next; prevNode->next = newNode; }

cout << "\n\nNew node inserted to position " << pos; count++; display; }

Node *SearchNodePos(int pos) {       Node *curr = start, *prev = nullptr; for (int i = 1; i < pos; i++) {           prev = curr; curr = curr->next; }       return prev; }

void insert {       int value;

cout << "Enter the value to insert: "; cin >> value;

Node *newNode = new Node; newNode->data = value; newNode->next = nullptr;

if (start == nullptr) {           start = newNode; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice;

switch (choice) {       case 1: char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch;

if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.display; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }       else {

Node *temp = start;

while (temp->next != nullptr) {               temp = temp->next; }           temp->next = newNode; }       count++; }

void display {       Node *temp = start; if (temp == nullptr) {           cout << "\n\nEmpty List!" << endl; }       else {           cout << "\n\nList: "; while (temp != nullptr) {               cout << temp->data << " -> "; temp = temp->next; }           cout << "NULL" << endl; }   }

Node *SearchNode(int x)   { Node *temp = start; Node *prev = nullptr;

while (temp != nullptr) {           if (temp->data == x)            { return prev; }           prev = temp; temp = temp->next; }

cout << "\n\nElement not found!..Re-Try..!" << endl; return nullptr; }

void deleteNode(int value) {

if (start == nullptr) {           cout << "\n\nEmpty List!" << endl; return; }

Node *preNode = SearchNode(value);

if (preNode == nullptr && start->data == value) {

Node *del = start; start = start->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       else if (preNode != nullptr) {           Node *del = preNode->next; preNode->next = del->next; cout << "\n\nNode with element " << del->data << " has been deleted successfully!" << endl; delete del; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice;

switch (choice) {       case 1: char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch;

if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.display; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }isplay; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }       switch (choice) {       case 1: char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch;

if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.display; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }       display; } };

int main {   LinkedList list; int choice, value;

list.Create; list.display;

while (true) {       cout << "\n\n1. Insert into list\n" << "2. Delete from list\n" << "0. Exit\n" << "Enter your choice: "; cin >> choice;

switch (choice) {       case 1: char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch;

if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.display; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }               if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.display; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }           char ch; list.insertPos; while (true) {

cout << "\n\nDo you want to insert another element : "; cin >> ch;

if (ch == 'y') {                   list.insertPos; }               else {                   break; }           }            list.display; break; case 2: cout << "Enter the value to delete: "; cin >> value; list.deleteNode(value); break; case 0: cout << "\n\nExiting..." << endl; exit(0); default: cout << "Invalid choice. Try again." << endl; }   }

return 0; }

31 /*Write a program to arrange the list based on the

ascending or descending order of the

information field (linked list,

dynamic memory allocation techniques).*/

//Swapping nodes...!


 * 1) include

using namespace std;

struct Node

{

int data;

Node *next;

};

class LinkedList

{

Node *start;

int n;

public:

LinkedList

{

start = nullptr;

n = 0;

}

void CreateLL

{

int x;

cout << endl

<< "Enter the number to insert: ";

cin >> x;

Node *newNode = new Node;

newNode->data = x;

newNode->next = nullptr;

if (start == nullptr)

{

start = newNode;

}

else

{

Node *temp = start;

while (temp->next != nullptr)

{

temp = temp->next;

}

temp->next = newNode;

}

n++;

}

void DispLL

{

Node *node = start;

if (node == nullptr)

{

cout << endl

<< endl

<< "Empty list!" << endl;

}

else

{

cout << endl

<< "List : ";

while (node != nullptr)

{

cout << node->data << "\t";

node = node->next;

}

}

}

void Sort(char order)

{

Node *temp = start;

while (temp != nullptr)

{

Node *next = temp->next;

while (next != nullptr)

{

if (order == 'a')

{

if (next->data < temp->data)

{

Interchange(next, temp);

temp = next;

}

}

else

{

if (next->data > temp->data)

{

Interchange(next, temp);

temp = next;

}

}

next = next->next;

}

temp = temp->next;

}

}

Node *SearchPrevNode(Node *curr)

{

Node *temp = start;

if (curr == start)

{

return nullptr;

}

while (temp->next != curr)

{

temp = temp->next;

}

return temp;

}

void Interchange(Node *node1, Node *node2)

{

Node *prevX = SearchPrevNode(node1), *currX = node1;

Node *prevY = SearchPrevNode(node2), *currY = node2;

if (prevX != nullptr)

prevX->next = currY;

else

start = currY;

if (prevY != nullptr)

prevY->next = currX;

else

start = currX;

Node *temp = currX->next;

currX->next = currY->next;

currY->next = temp;

}

};

int main

{

LinkedList l;

char ch;

do

{

l.CreateLL;

l.DispLL;

cout << endl

<< endl

<< "Do you want to insert another number (enter 'y' for continue): ";

cin >> ch;

} while (ch == 'y');

int cho;

while (true)

{

cout << endl

<< endl

<< "1.Arrange in acending order" << endl

<< "2.Arrange in decending order" << endl

<< "0.Exit" << endl

<< "Enter your choice : ";

cin >> cho;

switch (cho)

{

case 1:

l.Sort('a');

l.DispLL;

break;

case 2:

l.Sort('d');

l.DispLL;

break;

case 0:

cout << endl

<< endl

<< "Exiting...." << endl;

exit(0);

break;

}

}

return 0;

}

35 /*Write a C++ program to create and

insert an element anywhere in the circular list.*/


 * 1) include

using namespace std;

struct Node

{

int data;

Node *next;

};

class CircularLinkedList

{

private:

Node *start;

int n;

public:

CircularLinkedList

{

start = nullptr;

n = 0;

}

void DispLL

{

if (start == nullptr)

{

cout << "List is empty." << endl;

return;

}

Node *node = start;

cout << "\nList: ";

do

{

cout << node->data;

node = node->next;

if (node != start)

{

cout << " --> ";

}

} while (node != start);

}

void CreateLL

{

int x;

cout << endl

<< "Enter element " << (n + 1) << " : ";

cin >> x;

Insert(x);

char ch;

cout << "Enter 'y' to continue : ";

cin >> ch;

if (ch == 'y')

{

CreateLL;

}

}

void Insert(int x)

{

Node *temp = new Node;

temp->data = x;

if (start == nullptr)

{

start = temp;

temp->next = start;

}

else

{

Node *curr = start;

while (curr->next != start)

{

curr = curr->next;

}

curr->next = temp;

temp->next = start;

}

n++;

}

Node *SearchNode(int pos)

{

Node *curr = start;

for (int i = 0; i < pos; i++)

{

curr = curr->next;

}

return curr;

}

void InsertAtPos

{

int pos, x;

cout << endl

<< "Enter the element to insert : ";

cin >> x;

while (true)

{

cout << endl

<< "Enter the position you want to enter : ";

cin >> pos;

pos--;

if (pos < 0 || pos > n)

{

cout << endl

<< endl

<< "Invalid position...Re-Enter!" << endl;

}

else

{

break;

}

}

if (pos == 0)

{

Node *newNode = new Node;

Node *temp = start;

while (temp->next != start)

{

temp = temp->next;

}

temp->next = newNode;

newNode->next = start;

start = newNode;

newNode->data = x;

}

else

{

Node *newNode = new Node;

Node *prevNode = SearchNode(pos - 1);

newNode->data = x;

newNode->next = prevNode->next;

prevNode->next = newNode;

}

n++;

cout << endl

<< endl

<< "Element (" << x << ") has been inserted at position (" << (pos + 1) << ")" << endl;

DispLL;

}

};

int main

{

char ch;

CircularLinkedList list;

list.CreateLL;

list.DispLL;

do

{

list.InsertAtPos;

cout << endl

<< endl

<< "Insert another element ? ('y' to continue) : ";

cin >> ch;

if (ch != 'y')

{

break;

}

} while (true);

return 0;

}

//44.(not completed) Write a C++ program to Implementation of Insertion and deletion of nodes in the binary

//search trees.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *lchild;

Node *rchild;

Node{}

Node(int value)

{

data=value;

lchild=nullptr;

rchild=nullptr;

}

};

class BST

{

public:

Node *root;

BST

{

root=nullptr;

}

void create(int value)

{

Node *newnode = new Node(value);

if(root==nullptr)

{

root=newnode;

}

else

{

Node *ptr=root,*ptr1;

while(ptr!=nullptr)

{

if(newnode->datadata)

{

ptr1=ptr;

ptr=ptr->lchild;

}

else if(newnode->data>ptr->data)

{

ptr1=ptr;

ptr=ptr->rchild;

}

else if(newnode->data==ptr->data)

{

cout<<"\n item already exist";

return;

}

}

if(ptr==nullptr)

{

if(ptr1->datadata)

ptr1->rchild=newnode;

else

ptr1->lchild=newnode;

}

}

}

void display

{

if(root==nullptr)

{

cout<<"\n binary search tree is empty";

return;

}

else

{

Inorder(root);

}

}

void Inorder(Node *node)

{

if(node!=nullptr)

{

Inorder(node->lchild);

cout<data<<" ";

Inorder(node->rchild);

}

}

void deleteN(int item)

{

Node *ptr=root,*ptr1;

int f=0,ch;

if(root==nullptr)

{

cout<<"\n binary search tree is empty.deletion is not possible";

return;

}

while(ptr!=nullptr)

{

if(itemdata)

{

ptr1=ptr;

ptr=ptr->lchild;

}

else if(item>ptr->data)

{

ptr1=ptr;

ptr=ptr->rchild;

}

else if(item==ptr->data)

{

f=1;

}

}

if(f==0)

{

cout<<"\n "<lchild==nullptr) && (ptr->rchild==nullptr))

ch=1;

else if((ptr->lchild!=nullptr) && (ptr->rchild!=nullptr))

ch=2;

else

ch=3;

switch(ch)

{

case 1: if(ptr1->lchild==ptr)

ptr1->lchild=nullptr;

else

ptr1->rchild=nullptr;

cout<<"\n node with element "<<item<<" deleted";

delete ptr;

break;

case 2:

}

}

};

29 //write a c++ program to delete an element anywhere from linear linked list .menu driven program.

//(at the beginning,end and after an element).


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node(int value)

{

data =value;

next=nullptr;

}

};

class Linkedlist

{

Node *head;

public:

Linkedlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

head=newnode;

else

{

Node *temp=head;

while(temp->next!=nullptr)

temp=temp->next;

temp->next=newnode;

}

}

void display

{

if(head==nullptr)

{

cout<<"\n list is empty";

return;

}

else

{

cout<<"\n the linked list is: ";

Node *temp=head;

while(temp!=nullptr)

{

cout<data<<"->";

temp=temp->next;

}

cout<<"nullptr"<next==nullptr)

{

cout<<"\n only one node left.deletion after an element is not possible";

return;

}

else

{

while(ptr!=nullptr)

{

if(ptr->data==item)

{

loc=ptr;

if(loc->next==nullptr)

{

cout<<"\n no node after "<next;

}

if(loc==nullptr)

cout<<"\n item not found"<next;

loc->next=temp->next;

delete temp;

display;

}

}

}

void deletebeg

{

Node *ptr;

if(head==nullptr)

{

cout<<"\n the list is empty"<next;

delete ptr;

}

display;

}

void deleteEnd

{

Node *ptr,*loc;

if(head==nullptr)

{

cout<<"\n the list is empty no more deletion is possible";

exit(0);

}

else

{

if(head->next==nullptr)

{

ptr=head;

head=nullptr;

delete ptr;

}

else

{

loc=head;

ptr=head->next;

while(ptr->next!=nullptr)

{

loc=ptr;

ptr=ptr->next;

}

loc->next=nullptr;

delete ptr;

}

display;

}

}

};

int main

{

Linkedlist list;

int item,n,ch;

do

{

cout<<"\n enter the number you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>ch;

}while(ch==1);

list.display;

do

{

cout<<"\n...menu..."<<endl

<<"1.delete from beginning"<<endl

<<"2.delete from end"<<endl

<<"3.delete after an element"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:list.deletebeg;

break;

case 2:list.deleteEnd;

break;

case 3:cout<<"\n after which element you want to delete: ";

cin>>item;

list.deleteSpecific(item);

break;

case 0:cout<<"\n exiting..."<<endl;

break;

default:cout<<"\n invalid choice.";

}

}while(ch!=0);

return 0;

}

//48. Write a C++ program to Implementation of circular doubly linked list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node(int value)

{

data=value;

next=nullptr;

prev=nullptr;

}

};

class CDLL

{

private:

Node *head;

public:

CDLL

{

head=nullptr;

}

void create(int value)

{

Node *newnode=new Node(value);

if(!head)

{

head=newnode;

head->next=head;

head->prev=head;

}

else

{

Node *lastnode=head->prev;

lastnode->next=newnode;

newnode->prev=lastnode;

newnode->next=head;

head->prev=newnode;

}

}

void display

{

if(!head)

{

cout<<"list is empty";

return;

}

Node *current=head;

cout<<"\n the linked list is: ";

do

{

cout<<current->data<<"->";

current=current->next;

}while(current!=head);

cout<<"nullptr"<<endl;

}

void deleteSpecific

{

int p;

if(!head)

{

cout<<"\n list is empty.deletion is not possible";

return;

}

cout<<"\n enter the position where you want to delete element: ";

cin>>p;

if(p==1)

{

if(head->next==head)

{

Node *temp=head;

head=nullptr;

delete temp;

}

else

{

Node *lastnode=head->prev;

head->next->prev=lastnode;

lastnode->next=head->next;

Node *temp=head;

head=head->next;

delete temp;

}

}

else

{

int currentposition=1;

Node *current=head;

while(currentposition <p && current->next!=head)

{

currentposition++;

current=current->next;

}

if(current->next==head && currentposition<p)

{

cout<<"\n invalid position.position exceed the limit";

return;

}

else

{

Node *temp=current;

current->prev->next=current->next;

current->next->prev=current->prev;

if(current->next==head)

{

head->prev=current->prev;

}

delete temp;

}

}

display;

}

void inserAtSpecific(int n,int p,int countN)

{

if(((p!=countN)&&(p>countN+1))||(p<=0))

{

cout<<"\n invalid position"<<endl;

return;

}

if(p==1)

{

Node *newnode=new Node(n);

if(head==nullptr)

{

head=newnode;

head->next=head->prev=head;

}

else

{

Node *lastNode=head->prev;

newnode->prev=lastNode;

head->prev=newnode;

newnode->next=head;

lastNode->next=newnode;

head=newnode;

}

}

else if(p==countN+1)

{

Node *newnode=new Node(n);

newnode->next=head;

Node *lastNode=head->prev;

lastNode->next=newnode;

newnode->prev=lastNode;

head->prev=newnode;

}

else

{

int currentposition=1;

Node *current=head;

while(currentposition<p-1 && current->next!=head)

{

current=current->next;

currentposition++;

}

Node *newnode = new Node(n);

newnode->next=current->next;

current->next=newnode;

newnode->prev=current;

}

display;

}

int countNode

{

if(head==nullptr)

{

return 0;

}

else

{

Node *current=head;

int countN=1;

while(current->next!=head)

{

countN++;

current=current->next;

}

return countN;

}

}

};

int main

{

CDLL list;

int n,y,ch,p,c;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to continue(enter 1 to continue): ";

cin>>y;

}while(y==1);

list.display;

do

{

cout<<"\n ....menu..."<<endl

<<"1. insert an element"<<endl

<<"2. delete an element"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1: cout<<"\n enter the element you want to insert: ";

cin>>n;

cout<<"\n enter the position where you want to insert element : ";

cin>>p;

c=list.countNode;

list.inserAtSpecific(n,p,c);

break;

case 2: list.deleteSpecific;

break;

case 0: cout<<"\n exiting..."<<endl;

break;

default: cout<<"\n invalid choce"<<endl;

}

}while(ch!=0);

return 0;

}

//47. Write a C++ program to delete an element in the circular Doubly linked list. Menu driven program.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node(int value)

{

data=value;

next=nullptr;

prev=nullptr;

}

};

class CDLL

{

private:

Node *head;

public:

CDLL

{

head=nullptr;

}

void create(int value)

{

Node *newnode=new Node(value);

if(!head)

{

head=newnode;

head->next=head;

head->prev=head;

}

else

{

Node *lastnode=head->prev;

lastnode->next=newnode;

newnode->prev=lastnode;

newnode->next=head;

head->prev=newnode;

}

}

void display

{

if(!head)

{

cout<<"list is empty";

return;

}

Node *current=head;

cout<<"\n the linked list is: ";

do

{

cout<<current->data<<"->";

current=current->next;

}while(current!=head);

cout<<"nullptr"<<endl;

}

void deleteEnd

{

if(!head)

{

cout<<"\n list is empty. deletion is not possible";

return;

}

else

{

if(head->next==head)

{

Node *temp=head;

head=nullptr;

delete temp;

}

else

{

Node *lastnode=head->prev;

lastnode->prev->next=head;

head->prev=lastnode->prev;

delete lastnode;

}

display;

}

}

void deleteBeg

{

if(!head)

{

cout<<"\n list is empty";

return;

}

if(head->next==head)

{

Node *temp=head;

head=nullptr;

delete temp;

}

else

{

Node *lastnode=head->prev;

head->next->prev=lastnode;

lastnode->next=head->next;

Node *temp=head;

head=head->next;

delete temp;

}

display;

}

void deleteSpecific(int p)

{

if(!head)

{

cout<<"\n list is empty.deletion is not possible";

return;

}

if(p==1)

{

if(head->next==head)

{

Node *temp=head;

head=nullptr;

delete temp;

}

else

{

Node *lastnode=head->prev;

head->next->prev=lastnode;

lastnode->next=head->next;

Node *temp=head;

head=head->next;

delete temp;

}

}

else

{

int currentposition=1;

Node *current=head;

while(currentposition <p && current->next!=head)

{

currentposition++;

current=current->next;

}

if(current->next==head && currentposition<p)

{

cout<<"\n invalid position.position exceed the limit";

return;

}

else

{

Node *temp=current;

current->prev->next=current->next;

current->next->prev=current->prev;

if(current->next==head)

{

head->prev=current->prev;

}

delete temp;

}

}

display;

}

};

int main

{

CDLL list;

int n,y,ch,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to continue(enter 1 to continue): ";

cin>>y;

}while(y==1);

list.display;

do

{

cout<<"\n ....menu..."<<endl

<<"1. delete at beg"<<endl

<<"2. delete at end"<<endl

<<"3. delete a specific position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1: list.deleteBeg;

break;

case 2: list.deleteEnd;

break;

case 3:  cout<<"\n enter the position where you want to delete element: ";

cin>>p;

if(p>0)

list.deleteSpecific(p);

else

cout<<"\n invalid position";

break;

case 0: cout<<"\n exiting..."<<endl;

break;

default: cout<<"\n invalid choce"<<endl;

}

}while(ch!=0);

return 0;

}

//46. Write a C++ program to insert an element in the circular doubly linked list. Menu driven

//program. (at beginning, end, anywhere).


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node(int value)

{

data=value;

next=nullptr;

prev=nullptr;

}

};

class CDLL

{

private:

Node *head;

public:

CDLL

{

head=nullptr;

}

void create(int value)

{

Node *newnode=new Node(value);

if(!head)

{

head=newnode;

head->next=head;

head->prev=head;

}

else

{

Node *lastnode=head->prev;

lastnode->next=newnode;

newnode->prev=lastnode;

newnode->next=head;

head->prev=newnode;

}

}

void display

{

if(!head)

{

cout<<"list is empty";

return;

}

Node *current=head;

do

{

cout<<current->data<<"->";

current=current->next;

}while(current!=head);

cout<<"nullptr"<<endl;

}

void insertAtBeg(int value)

{

Node *newnode=new Node(value);

if(!head)

{

head=newnode;

head->next=head->prev=newnode;

}

else

{

Node *lastnode=head->prev;

newnode->next=head;

newnode->prev=lastnode;

lastnode->next=head->prev=newnode;

head=newnode;

}

}

int countNode

{

if(head==nullptr)

{

cout<<"\n list is empty";

return 0;

}

else

{

Node *current=head;

int countN=1;

while(current->next!=head)

{

countN++;

current=current->next;

}

return countN;

}

}

void inserAtSpecific(int n,int p,int countN)

{

if(((p!=countN)&&(p>countN+1))||(p<=0))

{

cout<<"\n invalid position"<<endl;

return;

}

if(p==1)

{

Node *newnode=new Node(n);

Node *lastNode=head->prev;

newnode->prev=lastNode;

head->prev=newnode;

newnode->next=head;

lastNode->next=newnode;

head=newnode;

}

else if(p==countN+1)

{

Node *newnode=new Node(n);

newnode->next=head;

Node *lastNode=head->prev;

lastNode->next=newnode;

newnode->prev=lastNode;

head->prev=newnode;

}

else

{

int currentposition=1;

Node *current=head;

while(currentposition<p-1 && current->next!=head)

{

current=current->next;

currentposition++;

}

Node *newnode = new Node(n);

newnode->next=current->next;

current->next=newnode;

newnode->prev=current;

}

cout<<"\n the list after insertion: ";

display;

}

};

int main

{

CDLL list;

int n,y,ch,c,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to continue(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the circularly doubly linked list is: ";

list.display;

do

{

cout<<"\n ....menu..."<<endl

<<"1. insert at beg"<<endl

<<"2. insert at end"<<endl

<<"3. insert at specific position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1: cout<<"\n enter the element to insert: ";

cin>>n;

list.insertAtBeg(n);

cout<<"\n list after insertion: ";

list.display;

break;

case 2: cout<<"\n enter the element to insert: ";

cin>>n;

list.create(n);

cout<<"\n list after insertion: ";

list.display;

break;

case 3:  c=list.countNode;

cout<<"\n enter the position where you want to insert element: ";

cin>>p;

if(p>0)

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.inserAtSpecific(n,p,c);

}

else

{

cout<<"\n invalid position";

}

break;

case 0: cout<<"\n exiting..."<<endl;

break;

default: cout<<"\n invalid choce"<<endl;

}

}while(ch!=0);

return 0;

}

//45. Write a C++ program to create circular doubly linked list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node(int value)

{

data=value;

next=nullptr;

prev=nullptr;

}

};

class CDLL

{

private:

Node *head;

public:

CDLL

{

head=nullptr;

}

void create(int value)

{

Node *newnode=new Node(value);

if(!head)

{

head=newnode;

head->next=head;

head->prev=head;

}

else

{

Node *lastnode=head->prev;

lastnode->next=newnode;

newnode->prev=lastnode;

newnode->next=head;

head->prev=newnode;

}

}

void display

{

if(!head)

{

cout<<"list is empty";

return;

}

Node *current=head;

do

{

cout<<current->data<<"->";

current=current->next;

}while(current!=head);

cout<<"nullptr"<<endl;

}

};

int main

{

CDLL list;

int n,y;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to continue(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the circularly doubly linked list is: ";

list.display;

return 0;

}

//43.(not completed) Write a C++ program to Creation of binary search trees. Tree traversals. Recursive function

//for traversals.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *lchild;

Node *rchild;

Node{}

Node(int value)

{

data=value;

lchild=nullptr;

rchild=nullptr;

}

};

class BST

{

public:

Node *root;

BST

{

root=nullptr;

}

void create(int value)

{

Node *newnode = new Node(value);

if(root==nullptr)

{

root=newnode;

}

else

{

Node *ptr=root,*ptr1;

while(ptr!=nullptr)

{

if(newnode->data<ptr->data)

{

ptr1=ptr;

ptr=ptr->lchild;

}

else if(newnode->data>ptr->data)

{

ptr1=ptr;

ptr=ptr->rchild;

}

else if(newnode->data==ptr->data)

{

cout<<"\n item already exist";

return;

}

}

if(ptr==nullptr)

{

if(ptr1->data<newnode->data)

ptr1->rchild=newnode;

else

ptr1->lchild=newnode;

}

}

}

void display

{

cout<<"\n InOrder traversal: ";

Inorder(root);

cout<<"\n PreOrder traversal: ";

Preorder(root);

cout<<"\n PostOrder traversal: ";

Postorder(root);

}

void Inorder(Node *node)

{

if(node!=nullptr)

{

Inorder(node->lchild);

cout<<node->data<<" ";

Inorder(node->rchild);

}

}

void Preorder(Node *node)

{

if(node!=nullptr)

{

cout<<node->data<<" ";

Preorder(node->lchild);

Preorder(node->rchild);

}

}

void Postorder(Node *node)

{

if(node!=nullptr)

{

Postorder(node->lchild);

Postorder(node->rchild);

cout<<node->data<<" ";

}

}

};

int main

{

int n,y;

BST bst;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

bst.create(n);

cout<<"\n do you want to add another element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n The binary search tree : ";

bst.display;

return 0;

}

//42. Write a C++ program to Implementation of queue using linked list. Menu driven program.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Queue

{

public:

Node *front,*rear;

Queue

{

front=nullptr;

rear=nullptr;

}

void insert(int value)

{

Node *newnode =new Node(value);

if(front==nullptr)

{

front=newnode;

rear=newnode;

}

else

{

rear->next=newnode;

newnode->next=nullptr;

rear=newnode;

}

}

void Delete

{

if(front==nullptr)

{

cout<<"\n queue is empty";

return;

}

else

{

Node *temp;

temp=front;

front=front ->next;

cout<<"\n"<<temp->data<< "deleted from queue";

delete temp;

if(front==nullptr)

{

cout<<"\n the queue is empty ";

return;

}

else

{

cout<<"\n the queue after deletion is: ";

display;

}

}

}

void display

{

Node *current=front;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

};

int main

{

Queue list;

int n,y,ch;

do

{

cout<<"\n enter the element you want insert into queue: ";

cin>>n;

list.insert(n);

cout<<"\n do you want to add one more element int queue(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the queue is : ";

list.display;

do

{

cout<<"\n ...menu..."<<endl

<<"1.insert element"<<endl

<<"2.delete element"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1: cout<<"\n enter the element you want to insert:    ";

cin>>n;

list.insert(n);

cout<<"\n the queue after insertion is: ";

list.display;

break;

case 2:list.Delete;

break;

case 0:cout<<"\n exiting...";

break;

default:cout<<"\n invalid choice ";

}

}while(ch!=0);

return 0;

}

//41. Write a C++ program to Implementation of stack using of linked list. Menu driven program.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Stack

{

public:

Node *top,*head;

Stack

{

top=nullptr;

head=nullptr;

}

void insert(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

{

top=newnode;

head=top;

}

else

{

top->next=newnode;

newnode->next=nullptr;

top=newnode;

}

}

void Delete

{

if(head==nullptr)

{

cout<<"\n stack is empty";

return;

}

else

{

Node *current=head;

Node *temp;

if(head->next==nullptr)

{

temp=head;

head=nullptr;

cout<<"\n"<<temp->data<<" deleted from stack";

delete temp;

cout<<"\n the stack is empty";

return;

}

else

{

while(current->next!=top)

{

current=current->next;

}

current->next=nullptr;

temp=top;

top=current;

cout<<"\n"<<temp->data<<" deleted from stack";

delete temp;

cout<<"\n the stack  after deletion is: ";

display;

}

}

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

};

int main

{

Stack list;

int n,y,ch;

do

{

cout<<"\n enter the element you want insert into stack: ";

cin>>n;

list.insert(n);

cout<<"\n do you want to add one more element into stack(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the stack is : ";

list.display;

do

{

cout<<"\n ...menu..."<<endl

<<"1.insert element"<<endl

<<"2.delete element"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1: cout<<"\n enter the element you want to insert:    ";

cin>>n;

list.insert(n);

cout<<"\n the stack after insertion is: ";

list.display;

break;

case 2:list.Delete;

break;

case 0:cout<<"\n exiting...";

break;

default:cout<<"\n invalid choice ";

}

}while(ch!=0);

return 0;

}

//40. Write a C++ program to delete an element in the Doubly linked list. Menu driven program. (at beginning, end, anywhere).


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node {}

Node(int value)

{

data=value;

prev=nullptr;

next=nullptr;

}

};

class DLL

{

private:

Node *head;

Node *tail;

public:

DLL

{

head=nullptr;

tail=nullptr;

}

void insertelement(int value)

{

Node *newnode =new Node(value);

if(!head)

{

head=tail=newnode;

}

else

{

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

}

}

void display

{

if(head==nullptr)

{

cout<<"list is empty";

return;

}

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"End"<<endl;

}

void deleteAtBeg

{

if(!head)

{

cout<<"\n list is empty";

return;

}

Node *toDelete=head;

head=head->next;

if(head)

head->prev=nullptr;

else

tail=nullptr;

delete toDelete;

display;

}

void deleteAtEnd

{

if(!tail)

{

cout<<"\n list is empty";

return;

}

Node *todelete=tail;

tail=tail->prev;

if(tail)

tail->next=nullptr;

else

head=nullptr;

delete todelete;

display;

}

void delete_position(int position)

{

if(!head)

{

cout<<"\n list is empty";

return;

}

if(position==1)

{

Node *todelete=head;

head=head->next;

if(head)

head->prev=nullptr;

else

tail=nullptr;

delete todelete;

display;

}

else

{

int currentposition=1;

Node *current=head;

while(current!=nullptr && currentposition<position)

{

current=current->next;

currentposition++;

}

if(current==nullptr)

{

cout<<"\n position exceeds the length of the list"<<endl;

return;

}

Node *todelete=current;

current->prev->next=current->next;

if(current->next)

current->next->prev=current->prev;

else

tail=current->prev;

delete todelete;

display;

}

}

};

int main

{

DLL list;

int n,y=0,ch,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.insertelement(n);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the doubly linked list is: ";

list.display;

do

{

cout<<"\n...menu.."<<endl

<<"1. delete at beginning"<<endl

<<"2. delete at end"<<endl

<<"3.delete at position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:

list.deleteAtBeg;

break;

case 2:

list.deleteAtEnd;

break;

case 3:cout<<"\n enter the position to delete: ";

cin>>p;

if(p>0)

{

list.delete_position(p);

}

else

cout<<"\n invalid postion"<<endl;

break;

case 0:cout<<"\n exiting....";

break;

default:cout<<"\n invalid choice";

}

}while(ch!=0);

return 0;

}

//39. Write a C++ program to insert an element in the Doubly linked list.

// Menu driven program. (at beginning, end, anywhere).


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node {}

Node(int value)

{

data=value;

prev=nullptr;

next=nullptr;

}

};

class DLL

{

private:

Node *head;

Node *tail;

public:

DLL

{

head=nullptr;

tail=nullptr;

}

void insertelement(int value)

{

Node *newnode =new Node(value);

if(!head)

{

head=tail=newnode;

}

else

{

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

}

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"End"<<endl;

}

void insertatP(int value, int position)

{

Node* newNode = new Node(value);

if (position == 1)

{

if (!head)

{

head = tail = newNode;

}

else

{

newNode->next = head;

head->prev = newNode;

head = newNode;

}

}

else

{

int currentPosition = 1;

Node* current = head;

while (current != nullptr && currentPosition < position - 1)

{

current = current->next;

currentPosition++;

}

if (current == nullptr)

{

cout << "Position exceeds the length of the list." << endl;

return;

}

else

{

newNode->next = current->next;

if (current->next != nullptr)

{

current->next->prev = newNode;

}

current->next = newNode;

newNode->prev = current;

}

}

cout<<"\n the doubly linked list is : ";

display;

}

void insertAtBeg(int value)

{

Node *newnode=new Node(value);

if(!head)

{

head=tail=newnode;

}

else

{

newnode->next=head;

head->prev=newnode;

head=newnode;

}

cout<<"\n the linked list is : ";

display;

}

};

int main

{

DLL list;

int n,y=0,ch,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.insertelement(n);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the doubly linked list is: ";

list.display;

do

{

cout<<"\n...menu.."<<endl

<<"1. insert at beginning"<<endl

<<"2. insert at end"<<endl

<<"3.insert at position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:cout<<"\n enter the element to insert: ";

cin>>n;

list.insertAtBeg(n);

break;

case 2:cout<<"\n enter the element to insert: ";

cin>>n;

list.insertelement(n);

cout<<"\n the list is: ";

list.display;

break;

case 3:cout<<"\n enter the position to insert: ";

cin>>p;

if(p>0)

{

cout<<"\n enter the element to insert: ";

cin>>n;

list.insertatP(n,p);

}

else

cout<<"\n invalid postion"<<endl;

break;

case 0:cout<<"\n exiting....";

break;

default:cout<<"\n invalid choice";

}

}while(ch!=0);

return 0;

}

//38. Write a C++ program to create and count number of nodes in the Doubly linked list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *prev;

Node *next;

Node {}

Node(int value)

{

data=value;

prev=nullptr;

next=nullptr;

}

};

class DLL

{

private:

Node *head;

Node *tail;

public:

DLL

{

head=nullptr;

tail=nullptr;

}

void insertelement(int value)

{

Node *newnode =new Node(value);

if(!head)

{

head=tail=newnode;

}

else

{

tail->next=newnode;

newnode->prev=tail;

tail=newnode;

}

}

int countNodes

{

int count=0;

Node *current=head;

while(current!=nullptr)

{

count++;

current=current->next;

}

return count;

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"End"<<endl;

}

};

int main

{

DLL list;

int a,y=0;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>a;

list.insertelement(a);

cout<<"\n do you want ton add one more element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the doubly linked list is: ";

list.display;

int nodecount=list.countNodes;

cout<<"\n number of  nodes created : "<<nodecount<<endl;

return 0;

}

//37. Write a C++ program to Implementation of circular list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Circularlist

{

public:

Node *head;

Circularlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

{

head=newnode;

head->next=head;

}

else

{

Node *current=head;

while(current->next!=head)

{

current=current->next;

}

current->next=newnode;

newnode->next=head;

}

}

void display

{

if(head==nullptr)

{

cout<<"\n list is empty";

return;

}

else

{

Node *current=head;

do

{

cout<<current->data<<"->";

current=current->next;

} while(current!=head);

cout<<"nullptr"<<endl;

}

}

void deleteAtSpecific(int position)

{

if(head==nullptr)

{

cout<<"\n the circular linked list is empty.nothing to delete";

return;

}

Node *current=head;

Node *prev=nullptr;

int currentposition=1;

while(currentposition<position && current->next!=head)

{

prev=current;

current=current->next;

currentposition++;

}

if(currentposition==position)

{

if(prev==nullptr)

{

Node *lastnode=head;

while(lastnode->next!=head)

lastnode=lastnode->next;

if(current->next!=head)

{

head=current->next;

lastnode->next=head;

}

else

head=nullptr;

}

else

prev->next=current->next;

cout<<current->data<< " deleted"<<endl;

delete current;

display;

}

else

{

cout<<"\n invalid position.nothing to delete"<<endl;

return;

}

}

int countNodes

{

int countnode=1;

if(head==nullptr)

{

return 0;

}

else

{

Node * current=head;

while(current->next!=head)

{

countnode+=1;

current=current->next;

}

}

return countnode;

}

void insertspecific(int n,int p,int countnode )

{

Node *ptr;

if(((p!=countnode)&&(p>countnode+1)) ||( p<=0))

{

cout<<"\n invalid position";

return;

}

else

{

if(p==1)

{

ptr=new Node(n);

if(head==nullptr)

{

head=ptr;

head->next=head;

}

else

{

ptr->next=head;

Node *temp=head;

while(temp->next!=head)

{

temp=temp->next;

}

temp->next=ptr;

head=ptr;

}

}

else if(p==countnode+1)

{

ptr=new Node(n);

Node *temp=head;

while(temp->next!=head)

temp=temp->next;

ptr->next=head;

temp->next=ptr;

}

else

{

Node *temp=head;

int currp=1;

while((currp< p-1)&&(temp->next!=head))

{

currp++;

temp=temp->next;

}

ptr=new Node(n);

ptr->next=temp->next;

temp->next=ptr;

}

cout<<"\n the linked list is: ";

display;

}

}

};

int main

{

Circularlist list;

int ch,a,item,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>a;

list.create(a);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>ch;

}while(ch==1);

cout<<"\n the linked lisyt is: ";

list.display;

int countN;

do

{

cout<<"\n...menu..."<<endl

<<"1.insert a node"<<endl

<<"2.delete a node"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:countN=list.countNodes;

cout<<"\n enter the element you want to insert:   ";

cin>>item;

cout<<"\n enter the position you want to insert: ";

cin>>p;

list.insertspecific(item,p,countN);

break;

case 2:countN=list.countNodes;

if(countN==0)

cout<<"\n nothing to delete.list is empty";

else

{

cout<<"\n enter the position you want to delete:    ";

cin>>p;

list.deleteAtSpecific(p);

}

break;

case 0:cout<<"\n exiting...";

break;

default:cout<<"\n invalid choice";

}

}while(ch!=0);

return 0;

}

//36. Write a C++ program to delete an element in the circular list. Menu driven program. (at beginning, end, anywhere).


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Circularlist

{

public:

Node *head;

Circularlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

{

head=newnode;

head->next=head;

}

else

{

Node *current=head;

while(current->next!=head)

{

current=current->next;

}

current->next=newnode;

newnode->next=head;

}

}

void display

{

cout<<endl;

Node *current=head;

do

{

cout<<current->data<<"->";

current=current->next;

} while(current!=head);

cout<<"nullptr"<<endl;

}

void deletebeg

{

if(head==nullptr)

{

cout<<"\n the linked list is empty.nothing to delete"<<endl;

return;

}

else

{

Node *temp=head;

Node *lastnode =head;

while(lastnode->next!=head)

{

lastnode=lastnode->next;

}

if(head==lastnode)

{      cout<<temp->data<<" deleted ";

delete temp;

head=nullptr;

cout<<"\n list is empty";

return;

}

head=head->next;

lastnode->next=head;

cout<<temp->data<<" deleted ";

delete temp;

display;

}

}

void deleteEnd

{

if(head==nullptr)

{

cout<<"\n the circular linked list is empty.nothing to delete";

return;

}

else

{

Node *current=head;

Node *prev=nullptr;

while(current->next!=head)

{

prev=current;

current=current->next;

}

if(prev==nullptr)

{

head=nullptr;

cout<<current->data<<" deleted ";

delete current;

cout<<"\n list is empty";

return;

}

else

{

prev->next=head;

cout<<current->data<<" deleted ";

delete current;

display;

}

}

}

void deleteAtSpecific(int position)

{

if(head==nullptr)

{

cout<<"\n the circular linked list is empty.nothing to delete";

return;

}

Node *current=head;

Node *prev=nullptr;

int currentposition=1;

while(currentposition<position && current->next!=head)

{

prev=current;

current=current->next;

currentposition++;

}

if(currentposition==position)

{

if(current->next==head && prev==nullptr)

{

head=nullptr;

cout<<current->data<<" deleted ";

delete current;

cout<<"\n list is empty"<<endl;

return;

}

else if(prev==nullptr)

{

Node *lastnode=head;

while(lastnode->next!=head)

lastnode=lastnode->next;

head=current->next;

lastnode->next=head;

}

else

prev->next=current->next;

cout<<current->data<<" deleted";

delete current;

display;

}

else

{

cout<<"\n invalid position.nothing to delete"<<endl;

return;

}

}

};

int main

{

Circularlist list;

int n,y,ch,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want insert another element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the circular linked list is: ";

list.display;

do

{

cout<<"\n....menu..."<<endl

<<"1.delete at beginning"<<endl

<<"2.delete at end"<<endl

<<"3.delete at specific position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:list.deletebeg;

break;

case 2:list.deleteEnd;

break;

case 3:if(list.head==nullptr)

{

cout<<"\n list is empty .deletion is not possible";

}

else

{

cout<<"\n enter the position you want to delete: "    ;

cin>>p;

list.deleteAtSpecific(p);

}

break;

case 0 :cout<<"\n exiting....";

break;

default:cout<<"\n invalid choice "<<endl;

}

}while(ch!=0);

return 0;

}

//35. Write a C++ program to create and insert an element anywhere in the circular list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Circularlist

{

public:

Node *head;

int count;

Circularlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

{

head=newnode;

head->next=nullptr;

}

else

{

Node *current=head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

newnode->next=head;

}

}

void display

{

if(head==nullptr)

{

cout<<"\n the list is empty";

return;

}

else

{cout<<"\n the circular linked list is: ";

Node *current=head;

do

{

cout<<current->data<<"->";

current=current->next;

} while(current!=head);

cout<<"nullptr"<<endl;

}

}

int countnodes

{

count=0;

Node *current=head;

while(current!=nullptr)

{

count++;

current=current->next;

}

return count;

}

void insertbeg(int value)

{

Node *newnode=new Node(value);

if(head==nullptr)

{

head=newnode;

newnode->next=newnode;

}

else

{

Node *temp=head;

while(temp->next!=head)

{

temp=temp->next;

}

temp->next=newnode;

newnode->next=head;

head=newnode;

}

display;

}

void insertend(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

{

head=newnode;

newnode->next=newnode;

}

else

{

Node *temp=head;

while(temp->next!=head)

{

temp=temp->next;

}

temp->next=newnode;

newnode->next=head;

}

display;

}

void insertspecific(int n,int p )

{   int ch;

Node *ptr,*loc=head;

int temp=p-1,k;

Node *prev=nullptr;

for(k=0;k<temp;k++)

{

prev=loc;

loc=loc->next;

if(loc==nullptr)

{

if(k+1==temp)

{

ptr=new Node(n);

prev->next=ptr;

ptr->next=head;

display;

return;

}

else

{

cout<<"\n insertion is not possible at the specified position";

return;

}

}

}

if(loc==head)

{

ptr=new Node(n);

ptr->next=loc;

head=ptr;

Node *temp=head;

while(temp->next!=head)

{

temp=temp->next;

}

temp->next=ptr;

display;

}

else

{

ptr=new Node(n);

ptr->next=loc;

prev->next=ptr;

display;

}

}

};

int main

{

Circularlist list;

int ch,a,item,p;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>a;

list.create(a);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>ch;

}while(ch==1);

list.display;

int count =list.countnodes;

do

{

cout<<"\n....menu..."<<endl

<<"1.insert at beginning"<<endl

<<"2.insert at end"<<endl

<<"3.insert at position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{   case 1:cout<<"\n enter the element you want to insert         at beginning: ";

cin>>a;

list.insertbeg(a);

break;

case 2:cout<<"\n enter the element you want to insert         at end: ";

cin>>a;

list.insertend(a);

break;

case 3:cout<<"\n enter the element to insert: ";

cin>>item;

top:cout<<"\n the position you want to insert element: ";

cin>>p;

if(p==0)

{

cout<<"\n invalid position ";

goto top;

}

list.insertspecific(item,p);

break;

case 0: cout<<"\n exiting...";

break;

default:cout<<"\n invalid cchoice..";

}

}while(ch!=0);

return 0;

}

//34. Write a C++ program to interchange n and n+1th nodes in a list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

}

};

class Linkedlist

{

public:

Node *head;

int count;

Linkedlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

head=newnode;

else

{

Node *current=head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

}

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

void countnodes

{

count=0;

Node *current=head;

while(current!=nullptr)

{

++count;

current=current->next;

}

}

void interchange

{

int n;

top:cout<<"\n enter the position of the node : ";

cin>>n;

if(n<1||n>count)

{

cout<<"\n invalid position ";

goto top;

}

Node *prevn=nullptr,*currn=head;

for(int i=0;i<n-1;i++)

{

prevn=currn;

currn=currn->next;

}

if(currn->next==nullptr)

{

cout<<"\n last node, interchanging is not possible";

exit(0);

}

else

{

Node *currm=currn->next;

if(prevn==nullptr)

head=currm;

else

prevn->next=currm;

currn->next=currm->next;

currm->next=currn;

cout<<"\n after interchanging the l8inked list is: ";

display;

}

}

};

int main

{

Linkedlist list;

int n,y;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the linked list is: ";

list.display;

list.countnodes;

list.interchange;

return 0;

}

//33. Write a C++ program to interchange any two nodes in a list.


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Linkedlist

{

public:

Node *head;

int count;

Linkedlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode =new Node(value);

if(head==nullptr)

head=newnode;

else

{

Node *current=head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

}

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

void countnodes

{

count=0;

Node *current=head;

while(current!=nullptr)

{

count++;

current=current->next;

}

}

Node *search(int x)

{

Node *loc=head,*prev=nullptr;

for(int i=0;i<x-1;i++)

{

prev=loc;

loc=loc->next;

}

return prev;

}

void interchange

{

int n,m;

if(count==1)

{

cout<<"\n interchanging is not possible .only one element in list";

exit(0);

}

top: cout<<"\n enter the position of nodes you want to interchange: ";

cin>>n>>m;

if((n<=0||n>count) || (m<=0||m>count))

{

cout<<"\n invalid positions";

goto top;

}

else if(n==m)

{

cout<<"\n same positions cannot interchange";

exit(0);

}

Node *prevn,*currn;

if(n==1)

{

prevn=nullptr;

currn=head;

}

else

{

prevn=search(n);

currn=prevn->next;

}

Node *prevm,*currm;

if(m==1)

{

prevm=nullptr;

currm=head;

}

else

{

prevm=search(m);

currm=prevm->next;

}

if(prevn==nullptr)

{  Node *temp;

head=currm;

prevm->next=currn;

temp=currn->next;

currn->next=currm->next;

currm->next=temp;

}

else if(prevm==nullptr)

{

Node *temp;

head=currn;

prevn->next=currm;

temp=currm->next;

currm->next=currn->next;

currn->next=temp;

}

else

{   Node *temp;

prevn->next=currm;

temp=currm->next;

currm->next=currn->next;

prevm->next=currn;

currn->next=temp;

}

cout<<"\n after interchanging list is: ";

display;

}

};

int main

{

Linkedlist list;

int n,y;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to add one more element(enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the linked list is: ";

list.display;

list.countnodes;

list.interchange;

return 0;

}

//32. Write a C++ program to concatenate two linked lists


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Linkedlist

{

public:

Node *head;

Linkedlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode=new Node(value);

if(head==nullptr)

{

head=newnode;

}

else

{

Node *current=head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

}

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

Linkedlist concatinate(Linkedlist list2)

{

Linkedlist list4;

Node *current;

list4.head=head;

current=list4.head;

while(current->next!=nullptr)

{

current=current->next;

}

current->next=list2.head;

return list4;

}

};

int main

{

Linkedlist list1,list2,list3;

int a1,a2,y;

cout<<"\n creating list 1 ";

do

{

cout<<"\n enter the element you want to insert: ";

cin>>a1;

list1.create(a1);

cout<<"\n do you want to add one more element (enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n creating list 2 ";

do

{

cout<<"\n enter the element you want to insert: ";

cin>>a2;

list2.create(a2);

cout<<"\n do you want to add one more element (enter 1 to continue): ";

cin>>y;

}while(y==1);

cout<<"\n the linked list 1: ";

list1.display;

cout<<"\n the linked list 2: ";

list2.display;

list3=list1.concatinate(list2);

cout<<"\n the merged list is: ";

list3.display;

return 0;

}

/31. Write a program to arrange the list based on the ascending or descending order of the information field (linked list, dynamic memory allocation techniques).


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Linkedlist

{

public:

Node *head;

Linkedlist

{

head=nullptr;

}

void create(int value)

{

Node *newnode=new Node(value);

if(head==nullptr)

{

head=newnode;

}

else

{

Node *current=head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

}

}

void display

{

Node *current=head;

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

void Swap(int &x,int &y)

{

int temp;

temp=x;

x=y;

y=temp;

}

void Ascending

{

Node *temp1=head,*temp2;

while(temp1!=nullptr)

{

temp2=temp1->next;

while(temp2!=nullptr)

{

if(temp1->data > temp2->data)

{

Swap(temp1->data,temp2->data);

}

temp2=temp2->next;

}

temp1=temp1->next;

}

}

void Descending

{

Node *temp1=head,*temp2;

while(temp1!=nullptr)

{

temp2=temp1->next;

while(temp2!=nullptr)

{

if(temp1->data<temp2->data)

{

Swap(temp1->data,temp2->data);

}

temp2=temp2->next;

}

temp1=temp1->next;

}

}

};

int main

{

Linkedlist list;

int n,ch,y;

do

{

cout<<"\n enter the element you want to insert: ";

cin>>n;

list.create(n);

cout<<"\n do you want to add one more element(enter 1 to continue) : ";

cin>>y;

}while(y==1);

cout<<"\n the linked list is: ";

list.display;

do

{

cout<<"...menu..."<<endl

<<"1.ascending order"<<endl

<<"2.descending order"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:list.Ascending;

cout<<"\n the sorted list in ascending order is: ";

list.display;

break;

case 2: list.Descending;

cout<<"the sorted list in descending order is: ";

list.display;

break;

case 0:cout<<"\n exiting...";

break;

default:cout<<"\n oinvalid choice";

}

}while(ch!=0);

return 0;

}

//27.corrected(1) Write a C++ program to Insert an element anywhere in the linear linked list. Menu driven program and use recursive function (at the beginning, end and after an element)


 * 1) include

using namespace std;

class Node

{

public:

int data;

Node *next;

Node{}

Node(int value)

{

data=value;

next=nullptr;

}

};

class Linkedlist

{

public:

Node *head;

Linkedlist

{

head=nullptr;

}

void create(int value)

{

int ch;

Node *newnode =new Node(value);

if(head==nullptr)

{

head=newnode;

}

else

{

Node *current= head;

while(current->next!=nullptr)

current=current->next;

current->next=newnode;

}

cout<<"\n do you want to add one more element(enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the element you want to insert: ";

cin>>value;

create(value);

}

else

return;

}

void insertbeginning(int n)

{

int ch;

Node *ptr=new Node;

ptr->data=n;

if(head==nullptr)

ptr->next=nullptr;

else

{

ptr->next=head;

head=ptr;

}

cout<<"\n do you want to insert another element in the beginning(enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the value to be inserted: ";

cin>>n;

insertbeginning(n);

}

else

return;

}

void insertend(int n)

{

int ch;

Node *ptr=new Node;

Node *temp;

ptr->data=n;

ptr->next=nullptr;

if(head==nullptr)

head=ptr;

else

{

temp=head;

while(temp->next!=nullptr)

temp=temp->next;

temp->next=ptr;

}

cout<<"\n do you want to insert another element at the end (enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the element you want insert : ";

cin>>n;

insertend(n);

}

else

return;

}

void insertspecific(int n,int item)

{   int ch;

Node *loc,*temp=head;

loc=nullptr;

while(temp!=nullptr)

{

if(temp->data==item)

{

loc=temp;

break;

}

else

temp=temp->next;

}

if(loc==nullptr)

cout<<"\n element not found ";

else

{

Node *ptr=new Node;

ptr->data=n;

ptr->next=loc->next;

loc->next=ptr;

cout<<"\n the linked list is : ";

display;

}

cout<<"\n do you want to insert another element at a specific position (enter 1): ";

cin>>ch;

if(ch==1)

{

cout<<"\n enter the element you want insert : ";

cin>>n;

cout<<"\n after which element you want to insert :";

cin>>item;

insertspecific(n,item);

}

else

return;

}

void display

{

Node *current=head;

if(current==nullptr)

{

cout<<"\n empty list!! "<<endl;

}

else

{

while(current!=nullptr)

{

cout<<current->data<<"->";

current=current->next;

}

cout<<"nullptr"<<endl;

}

}

};

int main

{

Linkedlist list;

int a,l;

int ch;

cout<<"\n enter the value to be inserted: ";

cin>>a;

list.create(a);

cout<<"\n the linked list is: ";

list.display;

do

{

cout<<"\n...menu..."<<endl

<<"1.insert at beginning"<<endl

<<"2.insert at end"<<endl

<<"3.insert at specific position"<<endl

<<"0.exit"<<endl

<<"enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:cout<<"\n enter the element you want insert at      beginning: ";

cin>>a;

list.insertbeginning(a);

cout<<"\n the linked list is: ";

list.display;

break;

case 2:cout<<"\n enter the element you want to insert at    end: ";

cin>>a;

list.insertend(a);

cout<<"\n the linked list is: ";

list.display;

break;

case  3:cout<<"\n enter the new element you wnt to insert:    ";

cin>>a;

cout<<"\n after which element you want to insert:";

cin>>l;

list.insertspecific(a,l);

break;

case 0:cout<<"\n exiting....";

break;

default:cout<<"\n invalid choice .enter a valid choice";

}

}while(ch!=0);

return 0;

}


 * 1) include

using namespace std;

class Node {

public:

int data;

Node* next;

Node(int value) : data(value), next(nullptr) {}

};

class LinkedList {

private:

Node* head;

public:

void insert(int data) {

Node* newNode = new Node(data);

if (head == nullptr) {

head = newNode;

} else {

Node* current = head;

while (current->next != nullptr) {

current = current->next;

}

current->next = newNode;

}

}

int countNodes {

int count = 0;

Node* current = head;

while (current != nullptr) {

count++;

current = current->next;

}

return count;

}

void display

{

Node* current = head;

while (current != nullptr) {

cout << current->data << " ";

current = current->next;

}

cout <<endl;

}

};

int main {

LinkedList myList;

int a,ch;

do

{cout<<"Enter the value to be inserted"<<endl;

cin>>a;

myList.insert(a);

cout<<"Do you want to add another element to the list entere 1 :"<<endl;

cin>>ch;

}while(ch==1);

cout<<"The linked list is:"<<endl;

myList.display;

int nodeCount = myList.countNodes;

cout << "Number of nodes in the linked list: " << nodeCount <<endl;

return 0;

}

25. Write a C++ program to Insert an element after a particular element in the linear linked list                #include

using namespace std;

struct node {

int data;

node* next;

};

class list {

private:

node* start;

public:

list {

start = nullptr;

}

void create_node(int n) {

node* temp, * r;

if (start == nullptr) {

temp = new node;

temp->data = n;

temp->next = nullptr;

start = temp;

}

else {

temp = start;

while (temp->next != nullptr) {

temp = temp->next;

}

r = new node;

r->data = n;

r->next = nullptr;

temp->next = r;

}

}

void display {

node* p;

if (start == nullptr)

cout << "\nThe list is empty" << endl;

else {

p = start;

while (p != nullptr) {

cout << "\n" << p->data;

p = p->next;

}

}

}

void insertspec(int item, int n) {

if (n <= 0) {

cout << "Invalid position. Position should be greater than 0." << endl;

return;

}

node* ptr = new node;

ptr->data = item;

ptr->next = nullptr;

if (n == 1) {

ptr->next = start;

start = ptr;

return;

}

node* temp = start;

for (int k = 1; k < n - 1 && temp != nullptr; k++) {

temp = temp->next;

}

if (temp == nullptr) {

cout << "Position is out of range. Inserting at the end of the list." << endl;

temp = start;

while (temp->next != nullptr) {

temp = temp->next;

}

}

ptr->next = temp->next;

temp->next = ptr;

}

};

int main {

int item, n;

list obj;

cout << "Enter the limit of a list: ";

cin >> n;

for (int i = 0; i < n; i++) {

int data;

cout << "Enter data for item " << i + 1 << ": ";

cin >> data;

obj.create_node(data);

}

cout << "\nList before inserting:" << endl;

obj.display;

cout << "\nEnter the item to be inserted: ";

cin >> item;

cout << "Enter the position where you want to insert: ";

cin >> n;

obj.insertspec(item, n);

cout << "\nList after inserting:" << endl;

obj.display;

return 0;

}

//7. Write a program to add 2 sparse Matrices. Check all validation


 * 1) include


 * 1) define max 10

using namespace std;

class Sparse

{

int pc, da, pb, c[max][3], z, s;

public:

void sparseAddition(int A[][3], int B[][3])

{

da = pb = pc = 1;

while ((da <= A[0][2]) && (pb <= B[0][2]))

{

if (A[da][0] == B[pb][0])

{

if (A[da][1] == B[pb][1])

{

s = A[da][2] + B[pb][2];

if (s != 0)

{

c[pc][0] = A[da][0];

c[pc][1] = A[da][1];

c[pc][2] = s;

pb++;

da++;

pc++;

}

else

{

da++;

pb++;

}

}

else

{

if (A[da][1] < B[pb][1]) // Use '<' for correct order

{

c[pc][0] = A[da][0];

c[pc][1] = A[da][1];

c[pc][2] = A[da][2];

da++;

pc++;

}

else

{

c[pc][0] = B[pb][0];

c[pc][1] = B[pb][1];

c[pc][2] = B[pb][2];

pb++;

pc++;

}

}

}

else

{

if (A[da][0] < B[pb][0]) // Use '<' for correct order

{

c[pc][0] = A[da][0];

c[pc][1] = A[da][1];

c[pc][2] = A[da][2];

da++;

pc++;

}

else

{

c[pc][0] = B[pb][0];

c[pc][1] = B[pb][1];

c[pc][2] = B[pb][2];

pb++;

pc++;

}

}

}

while (pb <= B[0][2])

{

c[pc][0] = B[pb][0];

c[pc][1] = B[pb][1];

c[pc][2] = B[pb][2];

pb++;

pc++;

}

while (da <= A[0][2])

{

c[pc][0] = A[da][0];

c[pc][1] = A[da][1];

c[pc][2] = A[da][2];

da++;

pc++;

}

z = pc - 1;

c[0][0] = A[0][0];

c[0][1] = A[0][1];

c[0][2] = pc - 1;

}

void displaysum

{

for (int i = 0; i <= z; i++)

{

for (int j = 0; j < 3; j++)

cout << c[i][j] << "\t\t ";

cout << endl;

}

}

void display(int A[][3])

{

for (int i = 0; i <= A[0][2]; i++)

{

for (int j = 0; j < 3; j++)

cout << A[i][j] << "\t\t";

cout << endl;

}

}

};

int main

{

Sparse s1, s2, s3;

int A[max][3], B[max][3], j, s, i;

top:

cout << "enter the row size, column size and non zero elements size of matrix A: ";

cin >> A[0][0] >> A[0][1] >> A[0][2];

if ((A[0][0] <= max) && (A[0][1] <= max) && (A[0][0] != 0) && (A[0][1] != 0) && (A[0][2] != 0))

{

top1:

cout << "\n enter the row size, column size and non zero elements size of matrix B: ";

cin >> B[0][0] >> B[0][1] >> B[0][2];

if ((B[0][0] <= max) && (B[0][1] <= max) && (B[0][0] != 0) && (B[0][1] != 0) && (B[0][2] != 0))

{

if((A[0][2]<(A[0][0]*A[0][1])/2)&&(B[0][2]<(B[0][0]*B[0][1])/2))

{

if ((A[0][0] == B[0][0]) && (A[0][1] == B[0][1]))

{

cout << "\n Enter rows, columns and non-zero elements of matrix A: \n";

for (i = 1; i <= A[0][2]; i++)

{ cout<<"\n enter the row of "<<i<<" non zero element: ";

cin >> A[i][0];

do

{

if((A[i][0]<0)||(A[i][0]>(A[0][0]-1)))

{

cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<<A[0][0]-1<<"): ";

cin>>A[i][0];

}

}while((A[i][0]<0)||(A[i][0]>(A[0][0]-1)));

cout<<"\n enter the column of "<<i<< " non zero element: ";

cin>>A[i][1];

do

{

if((A[i][1]<0)||(A[i][1]>(A[0][1]-1)))

{

cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<<A[0][1]-1<<")";

cin>>A[i][1];

}

}while((A[i][1]<0)||(A[i][1]>(A[0][1]-1)));

cout<<"\n enter the "<<i<<" non zero element: ";

cin>>A[i][2];

do

{

if(A[i][2]==0)

{

cout<<"\n enter a number other than zero: ";

cin>>A[i][2];

}

}while(A[i][2]==0);

}

cout<<endl;

cout << "\n Enter rows, columns and non-zero elements of matrix B: \n";

for (i = 1; i <= B[0][2]; i++)

{

cout<<"\n enter the row of "<<i<<" non zero element: ";

cin >> B[i][0];

do

{

if((B[i][0]<0)||(B[i][0]>(B[0][0]-1)))

{

cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<<A[0][0]-1<<"): ";

cin>>B[i][0];

}

}while((B[i][0]<0)||(B[i][0]>(B[0][0]-1)));

cout<<"\n enter the column of "<<i<< " non zero element: ";

cin>>B[i][1];

do

{

if((B[i][1]<0)||(B[i][1]>(B[0][1]-1)))

{

cout<<"\n invalid entry..!!.enter a valid entry(between 0-"<<B[0][1]-1<<")";

cin>>B[i][1];

}

}while((B[i][1]<0)||(B[i][1]>(B[0][1]-1)));

cout<<"\n enter the "<<1<<" non zero element: ";

cin>>B[i][2];

do

{

if(B[i][2]==0)

{

cout<<"\n enter a number other than zero: ";

cin>>B[i][2];

}

}while(B[i][2]==0);

}

cout << "\n matrix A: \n";

s1.display(A);

cout << "\n matrix B:\n";

s2.display(B);

s3.sparseAddition(A, B);

cout << "\n sum of two sparse matrices is:\n";

s3.displaysum;

}

else

cout << "\n rows and columns of two matrices are not equal. Addition is not possible";

}

else

cout<<"\n not a sparse matrix.sparse matrix addition is not possible";

}

else

{

cout << "\n invalid entry...!!.( enter size of row and column between(1-10)and non zero element greater than 0 )\n";

goto top1;

}

}

else

{

cout << "\n invalid entry...!!.(enter size of row and column between(1-10)and non zero element greater than 0) \n";

goto top;

}

return 0;

}

24. Write a C++ program to Insert an element at the beginning of the linear linked list. #include

using namespace std;

struct node {

int data;

node* next;

};

class list {

private:

node* start;

public:

list {

start = nullptr;

}

void create_node(int n)

{

node* temp, * r;

if (start == nullptr) {

temp = new node;

temp->data = n;

temp->next = nullptr;

start = temp;

}

else {

temp = start;

while (temp->next != nullptr) {

temp = temp->next;

}

r = new node;

r->data = n;

r->next = nullptr;

temp->next = r;

}

}

void display {

node* p;

if (start == nullptr)

cout << "\nThe list is empty" << endl;

else {

p = start;

while (p != nullptr) {

cout << "\n" << p->data;

p = p->next;

}

}

}

void insertbeginning(int n) {

node* ptr = new node;

ptr->data = n;

ptr->next = start;

start = ptr;

}

};

int main {

int item, n;

list obj;

cout << "Enter the limit of the list" << endl;

cin >> n;

for(int i = 0; i < n; i++) {

int data;

cout << "Enter data for item " << i + 1 << ": ";

cin >> data;

obj.create_node(data);

}

cout << "\nList before inserting" << endl;

obj.display;

cout << "\nEnter the item to be inserted at the beginning" << endl;

cin >> item;

obj.insertbeginning(item);

cout << "\nList after inserting" << endl;

obj.display;

return 0;

}

23. Write a C++ program to create linear linked list and print the same. (do not enter the size of

the list in advance dynamic concept).                                                                                                            #include

using namespace std;

struct node {

int data;

node* next;

};

class list {

private:

node* start;

public:

list {

start = nullptr;

}

void create_node(int n) {

node* temp, * r;

if (start == nullptr) {

temp = new node;

temp->data = n;

temp->next = nullptr;

start = temp;

}

else {

temp = start;

while (temp->next != nullptr) {

temp = temp->next;

}

r = new node;

r->data = n;

r->next = nullptr;

temp->next = r;

}

}

void display {

node* p;

if (start == nullptr)

cout << "\nThe list is empty" << endl;

else {

p = start;

while (p != nullptr) {

cout << "\n" << p->data;

p = p->next;

}

}

}

};

int main {

int n;

char c;

list obj;

do {

cout << "Enter the number to be inserted: ";

cin >> n;

obj.create_node(n);

cout << "Do you want to continue (y/n): ";

cin >> c;

} while (c == 'y' || c == 'Y');

cout << "\nThe list is:" << endl;

obj.display;

return 0;

}

22. Write a program to Implementation of circular queue using array. menu driven program. #include

using namespace std;


 * 1) define size 5

class circular_Queue {

int front, rear;

int cq[size];

public:

circular_Queue {

front = -1;

rear = -1;

}

void insert(int num) {

if (front == (rear + 1) % size) {

cout << "Queue is full" << endl;

return;

} else {

if (front == -1)

front = 0;

rear = (rear + 1) % size;

cq[rear] = num;

}

}

int remove {

int num;

if (front == -1) {

cout << "Queue is empty" << endl;

return -1;

} else {

num = cq[front];

if (front == rear)

front = rear = -1;

else

front = (front + 1) % size;

}

return num;

}

void display {

int i;

if (front == -1) {

cout << "Queue is empty" << endl;

return;

} else {

cout << "The status of the queue" << endl;

if (front <= rear) {

for (i = front; i <= rear; i++)

cout << cq[i] << " ";

} else

{

for (i = front; i < size; i++)

cout << cq[i] << " ";

for (i = 0; i <= rear; i++)

cout << cq[i] << " ";

}

cout << endl;

}

}

};

int main {

int choice, n;

circular_Queue obj;

cout << "\nEnter the size of the Queue: ";

cin >> n;

if (n > 0 && n <= size) {

cout << "Enter " << n << " elements into the queue" << endl;

for (int i = 0; i < n; i++) {

int num;

cin >> num;

obj.insert(num);

}

do {

cout << "1. Insert" << endl;

cout << "2. Delete" << endl;

cout << "3. Display" << endl;

cout << "4. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: {

int num;

cout << "Enter the number: ";

cin >> num;

obj.insert(num);

break;

}

case 2: {

int removedItem = obj.remove;

if (removedItem != -1) {

cout << "Deleted item = " << removedItem << endl;

}

break;

}

case 3:

obj.display;

break;

case 4:

cout << "Exiting...." << endl;

return 0;

default:

cout << "Invalid choice entry" << endl;

break;

}

} while (choice != 4);

} else {

cout << "Invalid size for the queue" << endl;

}

return 0;

}

//21. Write a program to Implementation of queue using arras (linear queue) menu driven program.


 * 1) include


 * 1) define size 5

using namespace std;

class Queue

{

int queue[size],front =0,rear=0,x;

public:

void readdata(int n)

{

x=n;

for(rear=front;rear<x;rear++)

{

cin>>queue[rear];

}

rear=rear-1;

}

void insert

{

int item;

if(rear>=size-1)

{

cout<<"\n queue is full.insertion is not possible";

}

else

{

cout<<"\n enter the item to be inserted: ";

cin>>item;

rear=rear+1;

x=x+1;

queue[rear]=item;

cout<<"\n new queue after insertion: ";

display;

}

}

void Delete

{

int item;

if((front <0)||(front >rear))

{

cout<<"\n queue is empty ";

exit(0);

}

else

{

item=queue[front];

front =front+1;

cout<<"\n "<<item <<" deleted from queue";

if(front>rear)

cout<<"\n queue is empty.deletion is not possible";

else

{

cout<<"\n after deletion queue elements are: ";

display;

}

}

}

void display

{

for(rear=front;rear<x;rear++)

cout<<queue[rear]<<" ";

rear-=1;

}

};

int main

{

Queue q1;

int n,ch;

top:cout<<"\n enter the number of elements in queue: ";

cin>>n;

if((n<=size)&&(n>0))

{

cout<<"\n enter the elements of queue: ";

q1.readdata(n);

cout<<"\n queue elements are: ";

q1.display;

do

{

cout<<"\n 1.insert an element";

cout<<"\n 2.delete an item";

cout<<"\n 3.display";

cout<<"\n 0.exit";

cout<<"\n enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:q1.insert;

break;

case 2:q1.Delete;

break;

case 3: cout<<"\n the queue elements are: ";

q1.display;

break;

case 0: cout<<"\n exiting ";

break;

default:cout<<"\n invalid choice";

}

}while(ch!=0);

}

else

{

cout<<"\n invalid size entry.enter size between 1-5";

goto top;

}

return 0;

}

//20. Write a program to insert more than one element into a queue.


 * 1) include


 * 1) define size 5

using namespace std;

class Queue

{

private:

int queue[size],front=0,rear=-1,x;

public:

void readdata(int n)

{

x=n;

for(rear=0;rear<x;rear++)

{

cin>>queue[rear];

}

rear=rear-1;

}

int insert

{

int item;

if(rear>=size-1)

{

cout<<"\n queue is full.insertion is not possible";

exit(0);

}

else

{

cout<<"\n enter the item to be inserted: ";

cin>>item;

rear=rear+1;

x=x+1;

queue[rear]=item;

cout<<"\n new queue after insertion: ";

display;

}

return rear;

}

void display

{

for(rear=0;rear<x;rear++)

cout<<queue[rear]<<" ";

rear-=1;

}

};

int main

{

Queue q1;

int n,ch,r;

top:cout<<"\n enter the number of elements in queue: ";

cin>>n;

if((n<=size)&&(n>0))

{

cout<<"\n enter the elements of queue: ";

q1.readdata(n);

cout<<"\n queue elements are: ";

q1.display;

do

{

cout<<"\n 1.insert an element";

cout<<"\n 2.display";

cout<<"\n 0.exit";

cout<<"\n enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:r=q1.insert;

if(r>=size-1)

{

cout<<"\n queue is full no more insertion is possible";

exit(0);

}

break;

case 2: cout<<"\n the queue elements are: ";

q1.display;

break;

case 0: cout<<"\n exiting ";

break;

default:cout<<"\n invalid choice";

}

}while(ch!=0);

}

else

{

cout<<"\n invalid size entry.enter size between 1-5";

goto top;

}

return 0;

}

29.

Hi # include


 * 1) include

using namespace std;

class Stack

{

char data[100];

int top;

public:

Stack

{

top = -1;

}

void push(char c)

{

data[++top] = c;

}

char pop

{

if (!isEmpty)

{

return data[top--];

}

return '\0';

}

char peek

{

if (!isEmpty)

{

return data[top];

}

return '\0';

}

bool isEmpty

{

return top == -1;

}

};

class InfixToPostfix

{

string infix;

string postfix;

int getPrecedence(char c)

{

if (c == '^')

return 3;

else if (c == '*' || c == '/')

return 2;

else if (c == '+' || c == '-')

return 1;

else

return 0;

}

public:

InfixToPostfix

{

infix = "";

postfix = "";

}

void readInfix

{

cout << "Enter an infix expression: ";

getline(cin, infix);

}

void displayInfix

{

cout << "Infix expression: " << infix << endl;

}

string convert

{

Stack operatorStack;

for (int i = 0; i < infix.length; i++)

{

char c = infix[i];

if (isalnum(c))

{

postfix += c;

postfix += ' ';

}

else if (c == '(')

{

operatorStack.push(c);

}

else if (c == ')')

{

while (!operatorStack.isEmpty && operatorStack.peek != '(')

{

postfix += operatorStack.pop;

postfix += ' ';

}

if (!operatorStack.isEmpty && operatorStack.peek == '(')

{

operatorStack.pop;

}

}

else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')

{

while (!operatorStack.isEmpty && getPrecedence(c) <= getPrecedence(operatorStack.peek))

{

postfix += operatorStack.pop;

postfix += ' ';

}

operatorStack.push(c);

}

}

while (!operatorStack.isEmpty)

{

postfix += operatorStack.pop;

postfix += ' ';

}

return postfix;

}

};

int main

{

InfixToPostfix converter;

converter.readInfix;

converter.displayInfix;

string postfixExpression = converter.convert;

cout << "Postfix expression: " << postfixExpression << endl;

return 0;

} using namespace std;
 * 1) include
 * 2) include
 * 3) include

class Stack {   int s[20]; int top;

public: Stack {       top = -1; }

void push(int value) {       if (top < 19) {           s[++top] = value; }   }

int pop {       if (top >= 0) {           return s[top--]; }       else {           return -1; }   } };

class Postfix {   string p;    Stack stack;

public: void ReadP {       cout << endl << "Enter the postfix expression : "; getline(cin, p); }

void Disp {       cout << endl << "The postfix expression is : " << p << endl; }

int eval {       for (int j = 0; j < p.length; j++) {           if (isdigit(p[j])) {               int num = 0; while (isdigit(p[j])) {                   num = num * 10 + (p[j] - 48); j++; }               j--; stack.push(num); }           else if (p[j] == '+' || p[j] == '-' || p[j] == '*' || p[j] == '/' || p[j] == '^') {               int operand2 = stack.pop; int operand1 = stack.pop;

switch (p[j]) {               case '+': stack.push(operand1 + operand2); break; case '-': stack.push(operand1 - operand2); break; case '*': stack.push(operand1 * operand2); break; case '/': stack.push(operand1 / operand2); break; case '^': stack.push(pow(operand1, operand2)); break; }           }        }

return stack.pop; } };

int main {   Postfix postfix; postfix.ReadP; postfix.Disp; int result = postfix.eval; cout << endl << "The result of the postfix expression is : " << result << endl; return 0; }