User:Afthab017/sandbox

'''Programs related to linked list :

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

using namespace std;
 * 1) include

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

class LinkedList {   Node *start;

public: LinkedList {       start = nullptr; }

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

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 Swap(int &x, int &y) {       int temp = x;        x = y;        y = temp; }

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) {                       Swap(next->data, temp->data); }               }                else {

if (next->data > temp->data) {                       Swap(next->data, temp->data); }               }

next = next->next; }

temp = temp->next; }

DispLL; } };

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'); break; case 2: l.Sort('d'); break; case 0: cout << endl << endl << "Exiting...." << endl; exit(0); break; }   }    return 0; }

2.C++ program to concatenate two linked lists

using namespace std;
 * 1) include

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

class LinkedList {   Node *start; int n;

public: LinkedList {       start = nullptr; }

void CreateLL {       int x;        n++; cout << endl << "Enter element " << n << " : "; cin >> x;       Insert(x);

char ch; cout << "Enter 'y' to continue : "; cin >> ch;

if (ch == 'y') {           CreateLL; }   }

void Insert(int 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; }   }

void DispLL {       Node *node = start; if (node == nullptr) {           cout << endl << endl << "Empty list!" << endl; }       else {           cout << "List : "; while (node != nullptr) {               cout << node->data << " --> "; node = node->next; }           cout << "NULL"; }   }

LinkedList ConCat(LinkedList li) {

LinkedList temp; temp.start = start;

Node *lastNode = temp.start;

while (lastNode->next != nullptr) {           lastNode = lastNode->next; }

lastNode->next = li.start;

return temp; } };

int main {   LinkedList a, b, c;

cout << endl << "Creating List 1 ...." << endl; a.CreateLL; cout << endl << endl << "Creating List 2 ...." << endl; b.CreateLL;

cout << endl << endl << "1ST "; a.DispLL;

cout << endl << endl << "2ND "; b.DispLL;

c = a.ConCat(b);

cout << endl << endl << "The combined "; c.DispLL; return 0; }

'3.C++ program to interchange any two nodes in a list.'

using namespace std;
 * 1) include

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

class LinkedList {   Node *start; int n = 0;

public: LinkedList {       start = nullptr; }

void DispLL {       Node *node = start; cout << "\nList: "; while (node != nullptr) {           cout << node->data << " --> "; node = node->next; }       cout << "NULL" << endl << endl; }

void CreateLL {       int x;        n++; cout<>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;       temp->next = nullptr;

if (start == nullptr) {           start = temp; }       else {           Node *curr = start; while (curr->next != nullptr) {               curr = curr->next; }           curr->next = temp; }   }

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

void Interchange {       int x, y;

cout << "Enter two positions to interchange: "; cin >> x >> y;

if (x > n || y > n || x < 1 || y < 1) {           cout << "\n\nInvalid positions\n\n"; Interchange; return; }

if (x == y)       { cout << "\n\nPositions are same no interchange needed\n\n"; return; }

Node *prevX = NULL, *currX = start; if (x != 1) {           prevX = SearchNode(x - 1); currX = prevX->next; }

Node *prevY = NULL, *currY = start; if (y != 1) {           prevY = SearchNode(y - 1); currY = prevY->next; }

if (currX == NULL || currY == NULL) {           cout << "\n\nNode not found, no swap done\n\n"; return; }

if (prevX != NULL) {           prevX->next = currY; }       else {           start = currY; }

if (prevY != NULL) {           prevY->next = currX; }       else {           start = currX; }

Node *temp = currX->next; currX->next = currY->next; currY->next = temp;

cout << "\n\nNodes swapped\n\n"; DispLL; } };

int main {   LinkedList list; list.CreateLL; list.DispLL; list.Interchange; return 0; }

4.C++ program to interchange n and n+1th nodes in a list.

using namespace std;
 * 1) include

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

class LinkedList { private: Node *start; int n;

public: LinkedList {   start = nullptr; }

void DispLL {   Node *node = start; cout << "\nList: "; while (node != nullptr) {     cout << node->data << " --> "; node = node->next; }   cout << "NULL" << endl << endl; }

void CreateLL {   int x;    n++; cout << endl << "Enter element " << n << " : "; 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;   temp->next = nullptr;

if (start == nullptr) {     start = temp; }   else {     Node *curr = start; while (curr->next != nullptr) {       curr = curr->next; }     curr->next = temp; } }

Node *SearchNode(int pos) {   if (pos < 1 || pos > n)    { return nullptr; }

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

void Interchange {   int x, y;

cout << "Enter position to interchange: "; cin >> x;   y = x + 1;

if (x >= n || x < 1) {     cout << "\n\nInvalid positions\n\n"; Interchange; return; }

Node *prevX = NULL, *currX = start; if (x != 1) {     prevX = SearchNode(x - 1); currX = prevX->next; }

Node *prevY = currX, *currY; currY = prevY->next;

if (prevX != NULL) prevX->next = currY; else start = currY;

prevY->next = currX;

Node *temp = currX->next; currX->next = currY->next; currY->next = temp;

cout << "\n\nNodes swapped\n\n"; DispLL; } };

int main { LinkedList list; list.CreateLL; list.DispLL; list.Interchange; return 0; }

'5.++ program to create and insert an element anywhere in the circular list'

using namespace std;
 * 1) include

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

'6.C++ program to delete an element in the circular list. Menu driven program. (at beginning, end, anywhere).'

using namespace std;
 * 1) include

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

class CircularLinkedList { private: Node *start; int n;

public: CircularLinkedList {       start = nullptr; n = 0; }

void DispDeleted(int x)   { cout << endl << endl << "Node with element " << x << " has been deleted successfully!" << endl; }

void DispLL {       if (start == nullptr) {           cout << endl << endl << "List is empty.....Exiting.." << endl; exit(0); }

Node *node = start; cout << endl << endl << "List: "; 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 DelEnd {

if (start->next == start) {           DispDeleted(start->data); start = nullptr; }       else {           Node *delNode = start, *prev;

while (delNode->next != start) {               prev = delNode; delNode = delNode->next; }

prev->next = start; DispDeleted(delNode->data); delete delNode; }       n--; DispLL; }   void DelBeg {

if (start->next == start) {           DispDeleted(start->data); start = nullptr; }       else {

Node *delNode = start; Node *lastNode = start;

while (lastNode->next != start) {               lastNode = lastNode->next; }           start = delNode->next; lastNode->next = start; DispDeleted(delNode->data); delete delNode; }

n--; DispLL; }   void DelAtPos {

int pos; while (true) {           cout << endl << "Enter position : "; cin >> pos; pos--; if (pos >= n || pos < 0) {               cout << endl << endl << "Invalid postion...Re-Try..!" << endl; }           else {               break; }       }

if (pos == 0) {           DelBeg; }       else {

Node *prev = SearchNode(pos - 1); Node *delNode = prev->next;

prev->next = delNode->next; DispDeleted(delNode->data); delete delNode; DispLL; }   } };

int main {   char ch; CircularLinkedList list; list.CreateLL; list.DispLL;

int choice;

while (true) {

cout << endl << endl; cout << "1.Delete from begning" << endl << "2.Delete from end" << endl << "3.Delete from position" << endl << "0.Exit" << endl << "Enter your choice : "; cin >> choice;

switch (choice) {       case 0: cout << endl << endl << "Exiting...." << endl; break;

case 1: list.DelBeg; break; case 2: list.DelEnd; break; case 3: list.DelAtPos; break;

default: cout << endl << "Invalid choice! " << endl; break; }   }    return 0; }

7.C++ program to Implementation of circular list.

using namespace std;
 * 1) include

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; }   void DispDeleted(int x, int pos) {       cout << endl << endl << "Node with element " << x << " at position " << (pos + 1) << " has been deleted successfully!" << endl; }   void DelAtPos {

int pos; while (true) {           cout << endl << "Enter position : "; cin >> pos; pos--; if (pos >= n || pos < 0) {               cout << endl << endl << "Invalid postion...Re-Try..!" << endl; }           else {               break; }       }

if (pos == 0) {           if (start->next == start) {               DispDeleted(start->data, pos); start = nullptr; }           else {               Node *delNode = start; Node *lastNode = start;

while (lastNode->next != start) {                   lastNode = lastNode->next; }               start = delNode->next; lastNode->next = start; DispDeleted(delNode->data, pos); delete delNode; }       }        else {

Node *prev = SearchNode(pos - 1); Node *delNode = prev->next;

prev->next = delNode->next; DispDeleted(delNode->data, pos); delete delNode; }

DispLL; } };

int main {   int choice; CircularLinkedList list; list.CreateLL; list.DispLL;

while (true) {

cout << endl << endl; cout << "1.Insert" << endl << "2.Delete" << endl << "0.Exit" << endl << "Enter your choice : "; cin >> choice;

switch (choice) {       case 0: cout << endl << endl << "Exiting...." << endl; exit(0); break; case 1: list.InsertAtPos; break; case 2: list.DelAtPos; break;

default: cout << endl << endl << "Invalid choice !" << endl; break; }   }    return 0; }

'8.C++ program to create and count number of nodes in the Doubly linked list.'

using namespace std;
 * 1) include

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

class DLL {

Node *start; Node *end;

public: DLL {       start = nullptr; end = nullptr; }

void CreateLL {       int x;        cout << endl << "Enter element to insert: "; cin >> x;       Insert(x);

char ch; cout << "Enter 'y' to continue : "; cin >> ch;

if (ch == 'y') {           CreateLL; }   }

void Insert(int x)   {

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

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

void DispDLL {

if (start == nullptr) {

cout << endl << endl << "List is empty.." << endl; exit(0); }

Node *temp = start; cout << endl << endl; while (temp != nullptr) {           cout << temp->data << " --> "; temp = temp->next; }       cout << "NULL"; }

int NodeCount {

Node *temp = start; int count = 0;

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

return count; } };

int main {

DLL dll;

dll.CreateLL; dll.DispDLL;

cout << endl << endl << "There are " << dll.NodeCount << " nodes in the list" << endl;

return 0; }

'9.C++ program to insert an element in the Doubly linked list. Menu driven program. (at beginning, end, anywhere).'

using namespace std;
 * 1) include

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

class DLL {

Node *start; Node *end; int count;

public: DLL {       start = nullptr; end = nullptr; count = 0; }

void CreateLL {       int x;        cout << endl << "Enter element to insert: "; cin >> x;       Insert(x);

char ch; cout << "Enter 'y' to continue : "; cin >> ch;

if (ch == 'y') {           CreateLL; }   }

void Insert(int x)   {

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

if (start == nullptr) {           newNode->prev = nullptr; start = newNode; end = newNode; }       else {           end->next = newNode; newNode->prev = end; end = newNode; }       count++; }

void DispDLL {

if (start == nullptr) {

cout << endl << endl << "List is empty.." << endl; exit(0); }

Node *temp = start; cout << endl << endl; while (temp != nullptr) {           cout << temp->data << " --> "; temp = temp->next; }       cout << "NULL"; }

void DispMsg(string msg) {       cout << "\n\nNode inserted at " << msg << endl; }

void InsertBeg {

int x;       cout << "Enter the element to insert : "; cin >> x;

Node *newNode = new Node; newNode->data = x;       newNode->prev = nullptr; newNode->next = start; start = newNode;

count++; DispMsg("begining"); DispDLL; }

void InsertEnd {

int x;       cout << "Enter the element to insert : "; cin >> x;

Node *newNode = new Node; newNode->data = x;       newNode->next = nullptr; newNode->prev = end; end->next = newNode; end = newNode;

count++; DispMsg("end"); DispDLL; }

Node *SearchNode(int pos) {

if (pos >= 1) {           int p = 1; Node *temp = start; while (p != pos) {               temp = temp->next; p++; }

return temp; }       return nullptr; }

void InsertPos {       int x, pos; cout << "Enter the element to insert : "; cin >> x;       while (true) {           cout << "Enter the position to insert : "; cin >> pos; if (pos < 1 || pos > (count + 1)) {               cout << endl << endl << "Invalid position...Re-Try!" << endl; }           else {               break; }       }

Node *temp = SearchNode(pos - 1); Node *newNode = new Node; newNode->data = x;

if (temp == nullptr) {           newNode->prev = nullptr; newNode->next = start; start->prev = newNode; start = newNode; }       else {           newNode->prev = temp; if (temp->next != nullptr) {               newNode->next = temp->next; temp->next->prev = newNode; temp->next = newNode; }           else {               temp->next = newNode; newNode->next = nullptr; end = newNode; }       }

count++; DispMsg("at position " + to_string(pos)); DispDLL; } };

int main {

DLL dll;

dll.CreateLL; dll.DispDLL; int ch; while (true) {

cout << endl << endl; cout << "1.Insert form begining" << endl << "2.Insert from end" << endl << "3.Insert at position" << endl << "0.Exit" << endl << "Enter your choice : ";

cin >> ch;

switch (ch) {       case 1: dll.InsertBeg; break; case 2: dll.InsertEnd; break; case 3: dll.InsertPos; break; case 0: cout << endl << endl << "Exiting..." << endl; exit(0); break;

default: cout << endl << endl << "Invalid choice..!" << endl; }   }    return 0; }