Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // completeLinkedList.cpp : Defines the entry point for the console application.
- /*
- Copyright 2019,
- Auther : Omar M. Omar,
- All rights reserved.
- Start it in 2019-3-27 12:11AM
- End in 2019-3-29 7:41PM
- Last Edition in 2019-4-5 11:14AM
- */
- #include "stdafx.h"
- #include "iostream"
- #include "string"
- using namespace std;
- //All variable
- int choice, i, item, pos, lsize, a[10];
- /*=====================
- SINGLE LINK LIST ===
- =======================
- */
- //Default Struct (Single Link List)
- struct node
- {
- int data;
- node *next;
- };
- //structure for insert char (s_homeWork 5)
- struct nodechar
- {
- char data;
- nodechar *next;
- };
- //structure for insert emp data (s_homeWork 4)
- struct nodeEmp
- {
- string name;
- int age;
- nodeEmp *next;
- };
- //SINGLE LIST CLASS have all labs functions
- class singleList
- {
- public:
- void addEnd(int, node *&, node *&);
- void addBegin(int, node *&, node *&);
- void addByPosition(int, node *&, node *&, int);
- void addByData(int, node *&, node *&, int);
- void Display(node *&);
- void deleteEnd(node *&, node *&);
- void deleteBegin(node *&);
- void deleteByPosition(int, node *&, node *&);
- void deleteByData(int, node *&, node *&);
- }single;
- void labs();
- void singleHomeWork();
- //SINGLE LIST (first home work functions)
- void s_homeWork1();
- void s_increment(node *&);
- void delete_odd(node *&);
- //SINGLE LIST (second home work functions)
- void s_homeWork2();
- void addEndchar(char, nodechar *&, nodechar*&);
- void delete4th(nodechar *&, nodechar *&);
- void split(nodechar *&, nodechar *&);
- void displaychar(nodechar *&head);
- //SINGLE LIST (third home work functions)
- void s_homeWork3();
- void mergelist(node*&,node*&, node*&, node*&, node*&);
- //SINGLE LIST (forth(4th) home work functions)
- void s_homeWork4();
- void displayEmp(nodeEmp *&);
- void createListEmp(nodeEmp*&, nodeEmp*&, int, string);
- //SINGLE LIST (fifth home work functions)
- void s_homeWork5();
- int even_number(node *&);
- /*=========================
- DOUBLE LINK LIST ===
- =======================
- */
- //Default Struct (Double Link List)
- struct doubleNode
- {
- int data;
- doubleNode *next;
- doubleNode *perv;
- };
- //DOUBLE LIST CLASS have all labs functions
- class doubleList
- {
- public:
- void doubleAddEnd(int, doubleNode *&, doubleNode *&);
- void doubleAddBegin(int, doubleNode *&, doubleNode *&);
- void doubleAddByPosition(int, doubleNode *&, doubleNode *&, int);
- void doubleAddByData(int, doubleNode *&, doubleNode *&, int);
- void doubleDisplay(doubleNode *&);
- void doubleDeleteEnd(doubleNode *&, doubleNode *&);
- void doubleDeleteBegin(doubleNode *&);
- void doubleDeleteByPosition(int, doubleNode *&, doubleNode *&);
- void doubleDeleteByData(int, doubleNode *&, doubleNode *&);
- }doubleL;
- void doubleLab();
- void doubleHomeWork();
- //DOUBLE LINKED LIST (first HW)
- void doubleHW1();
- //DOUBLE LINKED LIST (second HW)
- void doubleHW2();
- void swapList(doubleNode *&, doubleNode *&);
- //DOUBLE LINKED LIST (third HW)
- void doubleHW3();
- void duplicate(doubleNode *&);
- //DOUBLE LINKED LIST (forth HW)
- void doubleHW4();
- void deleteAllByKey(doubleNode *&,doubleNode *&,int);
- //main function
- void main()
- {
- node *head, *tail;
- tail = head = NULL;
- do
- {
- cout << "\t\t\t\t->->-> Complete Linked List Program <-<-<-\n\n";
- cout << "\t\t\t\t1 ) Single Linked List\n";
- cout << "\t\t\t\t2 ) Double Linked List\n";
- cout << "\t\t\t\t3 ) Circular Linked List\n";
- cout << "\t\t\t\t4 ) Exit System\n\n";
- cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
- cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
- cin >> choice;
- cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
- switch (choice)
- {
- case 1:
- cout << "\n\t\t\t\t->->-> Welcome to SINGLE linked list <-<-<-\n\n";
- cout << "\t\t\t\t1 ) To see All labs Example\n";
- cout << "\t\t\t\t2 ) To see All home work\n";
- cout << "\t\t\t\t3 ) Back to main menu\n\n";
- cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
- cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
- cin >> choice;
- cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
- switch (choice)
- {
- case 1:
- labs();
- break;
- case 2:
- singleHomeWork();
- break;
- case 3:
- break;
- default:
- break;
- }//End of SINGLE LIST SWITCH
- break;
- case 2:
- cout << "\n\t\t\t\t->->-> Welcome to DOUBLE linked list <-<-<-\n\n";
- cout << "\t\t\t\t1 ) To see All labs Example\n";
- cout << "\t\t\t\t2 ) To see All home work \n";
- cout << "\t\t\t\t3 ) Back to main menu\n\n";
- cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
- cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
- cin >> choice;
- cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
- switch (choice)
- {
- case 1:
- doubleLab();
- break;
- case 2:
- doubleHomeWork();
- break;
- case 3:
- break;
- default:
- break;
- }//End of DOUBLE LIST SWITCH
- break;
- default:
- for (i = 0;i < 40;i++)
- cout << "\n";
- cout << "BYE BYE\n";
- break;
- }//main switch
- } while (choice != 4);
- }//end of main
- //SINGLE LIST (labs function)
- //add End function
- void singleList::addEnd(int newData, node *&head, node *&tail)
- {
- node *newNode = new node;
- newNode->data = newData;
- newNode->next = NULL;
- if (head == NULL)
- {
- head = tail = newNode;
- }
- else
- {
- tail->next = newNode;
- tail = newNode;
- }
- }
- //SINGLE LIST (labs function)
- //add Begin function
- void singleList::addBegin(int newData, node *&head, node *&tail)
- {
- node *newNode = new node;
- newNode->data = newData;
- if (head == NULL)
- {
- newNode->next = NULL;
- head = tail = newNode;
- }
- else
- {
- newNode->next = head;
- head = newNode;
- }
- }
- //SINGLE LIST (labs function)
- //add By position function
- void singleList::addByPosition(int newData, node *&head, node *&tail, int position)
- {
- int positionCounter = 1;
- node *newNode = new node;
- newNode->data = newData;
- if (head == NULL)
- {
- newNode->data = NULL;
- head = tail = newNode;
- }
- node *tempNode = head;
- while (tempNode->next != NULL)
- {
- positionCounter++;
- tempNode = tempNode->next;
- if (positionCounter == position)
- {
- newNode->next = tempNode->next;
- tempNode->next = newNode;
- return;
- }
- }
- cout << "\nPosition " << position << " doesn\'t exist\n\n";
- }
- //SINGLE LIST (labs function)
- //add by Data function
- void singleList::addByData(int newData, node *&head, node *&tail, int searchData)
- {
- node *newNode = new node;
- newNode->data = newData;
- if (head == NULL)
- {
- newNode->data = NULL;
- head = tail = newNode;
- }
- node *tempNode = head;
- while (tempNode->data != searchData)
- {
- if (tempNode->next == NULL)
- {
- cout << "Data ( " << searchData << " ) is not exsist..\n\n";
- return;
- }
- tempNode = tempNode->next;
- }
- newNode->next = tempNode->next;
- tempNode->next = newNode;
- }
- //SINGLE LIST (labs function)
- //Display Function
- void singleList::Display(node *&head)
- {
- if (head == NULL)
- {
- cout << "List is Empty!!\n";
- return;
- }
- node *tempNode;
- tempNode = head;
- cout << "\nThe list contains: " << endl;
- while (tempNode != NULL)
- {
- cout << "---| " << tempNode->data << endl;
- tempNode = tempNode->next;
- }
- cout << "\n\n";
- }
- //SINGLE LIST (labs function)
- //delete End function
- void singleList::deleteEnd(node *&head, node *&tail)
- {
- if (head == NULL)
- {
- cout << "List is empty!!! Deletion is not possible\n";
- return;
- }
- node *tempNode;
- tempNode = head;
- while (tempNode->next != tail)
- {
- if (tempNode->next == NULL)
- {
- head = NULL;
- delete tempNode;
- return;
- }
- tempNode = tempNode->next;
- }
- tail = tempNode;
- tempNode = tempNode->next;
- tail->next = NULL;
- delete tempNode;
- }
- //SINGLE LIST (labs function)
- //Delete begin function
- void singleList::deleteBegin(node *&head)
- {
- if (head == NULL)
- {
- cout << "List is empty!!! Deletion is not possible\n";
- return;
- }
- node *tempNode;
- tempNode = head;
- if (tempNode->next == NULL)
- {
- head = NULL;
- delete tempNode;
- }
- else
- {
- head = head->next;
- delete tempNode;
- }
- }
- //SINGLE LIST (labs function)
- //Delete By position function
- void singleList::deleteByPosition(int position, node *&head, node *&tail)
- {
- int positionCounter = 1;
- if (head == NULL)
- {
- cout << "List is empty!!! Deletion is not possible\n";
- return;
- }
- node *tempNode1, *tempNode2 = new node;
- tempNode1 = head;
- if (head->next == NULL)
- {
- head = NULL;
- delete tempNode1;
- return;
- }
- while (tempNode1 != tail)
- {
- if (positionCounter == position)
- {
- if (tempNode1 == head)
- {
- head = head->next;
- delete tempNode1;
- return;
- }
- else if (tempNode1 == tail)
- {
- tempNode2->next = NULL;
- tail = tempNode2;
- delete tempNode1;
- return;
- }
- else
- {
- tempNode2->next = tempNode1->next;
- delete tempNode1;
- return;
- }
- }
- tempNode2 = tempNode1;
- tempNode1 = tempNode1->next;
- positionCounter++;
- }
- cout << "Given node not found in the list! Deletion not possible\n\n";
- }
- //SINGLE LIST (labs function)
- //Delete By data function
- void singleList::deleteByData(int searchData, node *&head, node *&tail)
- {
- if (head == NULL)
- {
- cout << "List is empty!!! Deletion is not possible\n";
- return;
- }
- node *tempNode1, *tempNode2 = new node;
- tempNode1 = head;
- while (tempNode1->data != searchData)
- {
- tempNode2 = tempNode1;
- tempNode1 = tempNode1->next;
- if (tempNode1 == NULL)
- {
- cout << "The data is not exsist! Deletion is not possible!!!\n\n";
- return;
- }
- }
- if (head->next == NULL && tempNode1 == head)
- {
- head = NULL;
- delete tempNode1;
- return;
- }
- else if (tempNode1 == head)
- {
- head = head->next;
- delete tempNode1;
- return;
- }
- else if (tempNode1 == tail)
- {
- tempNode2->next = NULL;
- tail = tempNode2;
- delete tempNode1;
- return;
- }
- else
- {
- tempNode2->next = tempNode1->next;
- delete tempNode1;
- return;
- }
- }
- //SINGLE LIST (home work function)|| HW 1 || increment function
- void s_increment(node *&head)
- {
- node *temp = head;
- node *prev = new node;
- node *curr = new node;
- curr = head;
- int temppos = 0, pos1 = 3, pos2 = 5;
- if (temp == NULL)
- {
- cout << "Please Enter some data to increment it \n";
- }
- while (curr->next != NULL && temppos != pos1)
- {
- prev = curr;
- curr = curr->next;
- temppos++;
- }
- prev->data += 20;
- cout << "3rd position is increment it : " << prev->data << endl;
- while (curr->next != NULL && temppos != pos2)
- {
- prev = curr;
- curr = curr->next;
- temppos++;
- }
- prev->data += 20;
- cout << "5d position is increment it : " << prev->data << endl;
- while (temp != NULL)
- {
- cout << "data is : " << temp->data << endl;
- temp = temp->next;
- }
- }
- //SINGLE LIST (home work function)|| HW 1 || Delete odd nodes function
- void delete_odd(node *&head) {
- if (head == NULL) {
- /*When given linked list are empty.*/
- printf("\n No element in this linked list.");
- }
- else
- {
- node*temp, *helper;
- helper = temp = head;
- /*Find and remove given linked list node*/
- while (temp) {
- /*Odd nodes*/
- if (temp->data % 2 != 0) {
- /*Remove first node of linked list*/
- if (temp == head) {
- helper = temp;
- /*assign address of next node memory block*/
- head = head->next;
- temp = head;
- /*Before delete node assign that
- next pointer value is NULL*/
- helper->next = NULL;
- /*Free node*/
- delete helper;
- helper = NULL;
- }
- else {
- helper->next = temp->next;
- temp->next = NULL;
- /*Free node*/
- delete temp;
- temp = helper->next;
- }
- }
- else {
- helper = temp;
- /*assign address of next node memory block*/
- temp = temp->next;
- }
- }
- }
- }
- //SINGLE LIST (home work function)|| HW 2 || insert Char in list function
- void addEndchar(char newData, nodechar *&head, nodechar *&tail)
- {
- nodechar *newNode = new nodechar;
- newNode->data = newData;
- newNode->next = NULL;
- if (head == NULL)
- {
- head = tail = newNode;
- }
- else
- {
- tail->next = newNode;
- tail = newNode;
- }
- }
- //SINGLE LIST (home work function)|| HW 2 || Delete 4th nodes function
- void delete4th(nodechar *&head, nodechar *&tail)
- {
- nodechar *temp1, *temp2;
- int i = 1;
- if (head == NULL)
- {
- cout << "The required value does not found in this list\n";
- return;
- }
- else
- {
- temp1 = head;
- temp2 = head;
- }
- while (i != 4)
- {
- i++;
- temp1 = temp1->next;
- }
- while (temp2->next != temp1 && temp2 != temp1)
- {
- temp2 = temp2->next;
- }
- if (temp1 == head) // At the begin
- {
- head = head->next;
- cout << "--| " << temp1->data << " was delete it\n";
- delete temp1;
- }
- else if (temp1->next == NULL || temp1 == tail) // at the end
- {
- temp2->next = NULL;
- tail = temp2;
- cout << "--| " << temp1->data << " was delete it\n";
- delete temp1;
- }
- else
- {
- temp2->next = temp1->next;
- cout << "--| " << temp1->data << " was delete it\n";
- delete temp1;
- }
- }
- //SINGLE LIST (home work function)|| HW 2 || Display char list function
- void displaychar(nodechar *&head)
- {
- nodechar *temp;
- temp = head;
- while (temp != NULL)
- {
- cout << "--| " << temp->data << endl;
- temp = temp->next;
- }
- }
- //SINGLE LIST (home work function)|| HW 2 || Split list function
- void split(nodechar *&head, nodechar *&tail)
- {
- nodechar *temp, *head2, *tail2;
- temp = head;
- if (head == NULL)
- cout << "add some data to split it\n";
- return;
- int count = 1;
- while (count != 5)
- {
- count++;
- temp = temp->next;
- }
- tail = temp;
- head2 = temp->next;
- tail2 = head2;
- while (tail2->next != NULL)
- {
- tail2 = tail2->next;
- }
- displaychar(head);
- cout << endl;
- displaychar(head2);
- }
- //SINGLE LIST (home work function)|| HW 3 || Merge list function
- void mergelist(node *& head,node*& tail, node*& head2, node*& tail2, node*& head3)
- {
- tail->next = head2;
- head2 = tail;
- tail2->next = head3;
- head3 = tail2;
- single.Display(head);
- }
- //SINGLE LIST (home work function)|| HW 4 || Display Emp list function
- void displayEmp(nodeEmp *&head)
- {
- nodeEmp *temp;
- temp = head;
- if (head == NULL)
- cout << "list is empty\n";
- else
- {
- while (temp != NULL)
- {
- cout << "Name is : " << temp->name << endl;
- cout << "Age is : " << temp->age << endl;
- temp = temp->next;
- }
- }
- }
- //SINGLE LIST (home work function)|| HW 4 || Insert Emp list function
- void createListEmp(nodeEmp*& head, nodeEmp*& tail, int age, string name)
- {
- nodeEmp *newNode = new nodeEmp;
- newNode->next = NULL;
- newNode->age = age;
- newNode->name = name;
- if (head == NULL)
- {
- head = tail = newNode;
- }
- else
- {
- tail->next = newNode;
- tail = newNode;
- }
- }
- //SINGLE LIST (home work function)|| HW 5 || Find even number in list function
- int even_number(node *&head)
- {
- int i = 0;
- node *temp;
- temp = head;
- if (head == NULL)
- {
- cout << "List is empty\n";
- return 0;
- }
- else
- {
- while (temp != NULL)
- {
- if (temp->data % 2 == 0)
- {
- a[i] = temp->data;
- i++;
- }
- temp = temp->next;
- }
- }
- return i;
- }
- //SINGLE LIST (Labs function)
- void labs()
- {
- node *head, *tail;
- tail = head = NULL;
- singleList single;
- int choice, item, pos, i, lsize;
- do
- {
- cout << "\n\t\t\t\t->-> All Operation in Single List <-<-\n\n";
- cout << "\t\t\t\t1 - create single link list\n";
- cout << "\t\t\t\t2 - Add item in the End list\n";
- cout << "\t\t\t\t3 - Add item in the Begin list\n";
- cout << "\t\t\t\t4 - Add item By Position in list\n";
- cout << "\t\t\t\t5 - Add item By Data in list \n";
- cout << "\t\t\t\t6 - Display list item\n";
- cout << "\t\t\t\t7- Delete item in the End list\n";
- cout << "\t\t\t\t8 - Delete item in the Begin list\n";
- cout << "\t\t\t\t9 - Delete item By Position list\n";
- cout << "\t\t\t\t10 - Delete item By Data in list\n";
- cout << "\t\t\t\t11 - Exit System\n";
- cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
- cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
- cin >> choice;
- cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
- switch (choice)
- {
- case 1:
- cout << "Enter size of list : ";
- cin >> lsize;
- for (i = 0;i < lsize;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- single.addEnd(item, head, tail);
- }
- break;
- case 2:
- cout << "Enter item : ";
- cin >> item;
- single.addEnd(item, head, tail);
- break;
- case 3:
- cout << "Enter item : ";
- cin >> item;
- single.addBegin(item, head, tail);
- break;
- case 4:
- cout << "Enter the position : ";
- cin >> pos;
- cout << "Enter the item : ";
- cin >> item;
- single.addByPosition(item, head, tail, pos);
- break;
- case 5:
- cout << "Enter the data : ";
- cin >> pos;
- cout << "Enter item : ";
- cin >> item;
- single.addByData(item, head, tail, pos);
- break;
- case 6:
- single.Display(head);
- break;
- case 7:
- single.deleteEnd(head, tail);
- break;
- case 8:
- single.deleteBegin(head);
- break;
- case 9:
- cout << "Enter the position : ";
- cin >> pos;
- single.deleteByPosition(pos, head, tail);
- break;
- case 10:
- cout << "Enter the Data to delete : ";
- cin >> pos;
- single.deleteByData(pos, head, tail);
- break;
- default:
- cout << "\n\n\t\tBye Bye\n\n========================================n\n";
- break;
- }
- } while (choice != 11);
- }//end of lab
- //SINGLE LIST (home work functions)
- void singleHomeWork()
- {
- node *tail, *head;
- tail = head = NULL;
- do
- {
- cout << "\n\t\t\t\t->-> Home Work to test your self <-<-\n\n";
- cout << "\t\t\t\t1- write a segment of code of C++ progrm (using function) for each of the following\n\t\t\t\ta) create a linked list of seven nodes(int)\n"
- "\t\t\t\tb) Increment the value of the 3th and 5th node by 20\n\t\t\t\tc) Delete all the nodes where its data field is odd in linked list\n";
- cout << "\n\t\t\t\t2 - Let (L) be a link list of size (10) char. write a \n\t\t\t\tprogem that execute the following steps in sequence\n\t\t\t\ta) crate this list\n\t\t\t\tb) insert three nodes in end of list"
- "\n\t\t\t\tc) Delete 4th element \n\t\t\t\td)Split L into equal two lists\n";
- cout << "\n\t\t\t\t3 - Merge any three linked list (h1,h2,h3) ,\n\t\t\t\tThese linked list are same type and different size\n";
- cout << "\n\t\t\t\t4 - write a complete program to contruct single linked list of (8) nodes \n\t\t\t\t(Each node contains three elemnt [name,age and pointer]).\n\t\t\t\tthen write function to print the number of persons that ages larger than 20 year\n";
- cout << "\n\t\t\t\t5 - write complete program to construct single linked list of (10) nodes,\n\t\t\t\tthen write function to copy the even values to array of dimension,\n\t\t\t\tprint the array in main fucntion\n";
- cout << "\n\t\t\t\t6 - Exit System\n";
- cout << "\t\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
- cout << "\t\t\t\t\tEnter your Choice ->->-> : ";
- cin >> choice;
- cout << "\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
- switch (choice)
- {
- case 1:
- s_homeWork1();
- break;
- case 2:
- s_homeWork2();
- break;
- case 3:
- s_homeWork3();
- break;
- case 4:
- s_homeWork4();
- break;
- case 5:
- s_homeWork5();
- break;
- default:
- cout << "\n\n\t\tBye Bye\n\n========================================n\n";
- break;
- }
- } while (choice != 6);
- }
- //SINGLE LIST (home work function)|| HW 1
- void s_homeWork1()
- {
- node *head, *tail;
- head = tail = NULL;
- singleList single;
- char choice;
- do
- {
- cout << "a) Create a linked list of seven nodes (int)\n";
- cout << "b) Increment the value of the 3th and 5th node by 20\n";
- cout << "c) Delete all the nodes where its data field is odd in linked list\n";
- cout << "d) Exit System\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 'a':
- for (i = 0;i < 7;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- single.addEnd(item, head, tail);
- }
- break;
- case 'b':
- s_increment(head);
- break;
- case 'c':
- delete_odd(head);
- break;
- default:
- break;
- }
- } while (choice != 'd');
- }
- //SINGLE LIST (home work function)|| HW 2
- void s_homeWork2()
- {
- nodechar *head, *tail;
- head = tail = NULL;
- int i;
- char item;
- char choice;
- do
- {
- cout << "a) Create this list\n";
- cout << "b) Insert three nodes in end of list\n";
- cout << "c) Delete 4th element\n";
- cout << "d) Split the list for two list\n";
- cout << "e) Exit System\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 'a':
- for (i = 0;i < 10;i++)
- {
- cout << "Enter Character : ";
- cin >> item;
- addEndchar(item, head, tail);
- }
- break;
- case 'b':
- for (i = 0;i < 3;i++)
- {
- cout << "Enter Character : ";
- cin >> item;
- addEndchar(item, head, tail);
- }
- break;
- case 'c':
- delete4th(head,tail);
- break;
- case 'd':
- split(head, tail);
- break;
- default:
- break;
- }
- } while (choice != 'e');
- }
- //SINGLE LIST (home work function)|| HW 3
- void s_homeWork3()
- {
- node *head, *tail, *head2, *tail2, *head3, *tail3;
- head = tail = head2 = tail2 = head3 = tail3 = NULL;
- do
- {
- cout << "1) insert data in three list\n";
- cout << "2) Merge all list in one list\n";
- cout << "3) Exit System\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- cout << "\nEnter Data for Fisrt list\n\n";
- for (i = 0;i < 3;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- single.addEnd(item, head, tail);
- }
- cout << "\nEnter Data for Second list\n\n";
- for (i = 0;i < 4;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- single.addEnd(item, head2, tail2);
- }
- cout << "\nEnter Data for Third list\n\n";
- for (i = 0;i < 5;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- single.addEnd(item, head3, tail3);
- }
- break;
- case 2:
- cout << "\n\tAfter Merge All\n";
- mergelist(head,tail, head2, tail2, head3);
- break;
- default:
- break;
- }
- } while (choice != 3);
- }
- //SINGLE LIST (home work function)|| HW 4
- void s_homeWork4()
- {
- nodeEmp *head, *tail;
- tail = head = NULL;
- int age;
- string name;
- do
- {
- cout << "1 - Create list\n";
- cout << "2 - Display list\n";
- cout << "3 - Exit System\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- for (i = 0;i<2;i++)
- {
- cout << "Enter name : ";
- cin >> name;
- cout << "Enter age : ";
- cin >> age;
- createListEmp(head, tail, age, name);
- }
- break;
- case 2:
- displayEmp(head);
- break;
- default:
- break;
- }
- } while (choice != 3);
- }
- //SINGLE LIST (home work function)|| HW 5
- void s_homeWork5()
- {
- node *head, *tail;
- tail = head = NULL;
- int x;
- do
- {
- cout << "1 - create list \n";
- cout << "2 - show the even number\n";
- cout << "3 - exit\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- for (i = 0;i<10;i++)
- {
- cout << "Enter the item : ";
- cin >> item;
- single.addEnd(item, head, tail);
- }
- break;
- case 2:
- x = even_number(head);
- for (i = 0;i<x;i++)
- {
- cout << "even data is : " << a[i] << endl;
- }
- break;
- default:
- break;
- }
- } while (choice != 3);
- }
- /*=====================
- DOUBLE LINK LIST ===
- =======================
- */
- void doubleLab()
- {
- doubleNode *head, *tail;
- tail = head = NULL;
- int choice, item, pos, i, lsize;
- do
- {
- cout << "1 - create double link list\n";
- cout << "2 - Add item in the End list\n";
- cout << "3 - Add item in the Begin list\n";
- cout << "4 - Add item By Position in list\n";
- cout << "5 - Add item By Data in list \n";
- cout << "6 - Display list item\n";
- cout << "7- Delete item in the End list\n";
- cout << "8 - Delete item in the Begin list\n";
- cout << "9 - Delete item By Position list\n";
- cout << "10 - Delete item By Data in list\n";
- cout << "11 - Exit System\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- cout << "Enter size of list : ";
- cin >> lsize;
- for (i = 0;i < lsize;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddBegin(item, head, tail);
- }
- break;
- case 2:
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddEnd(item, head, tail);
- break;
- case 3:
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddBegin(item, head, tail);
- break;
- case 4:
- cout << "Enter the position : ";
- cin >> pos;
- cout << "Enter the item : ";
- cin >> item;
- doubleL.doubleAddByPosition(item, head, tail, pos);
- break;
- case 5:
- cout << "Enter the data : ";
- cin >> pos;
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddByData(item, head, tail, pos);
- break;
- case 6:
- doubleL.doubleDisplay(head);
- break;
- case 7:
- doubleL.doubleDeleteEnd(head, tail);
- break;
- case 8:
- doubleL.doubleDeleteBegin(head);
- break;
- case 9:
- cout << "Enter the position : ";
- cin >> pos;
- doubleL.doubleDeleteByPosition(pos, head, tail);
- break;
- case 10:
- cout << "Enter the Data to delete : ";
- cin >> pos;
- doubleL.doubleDeleteByData(pos, head, tail);
- break;
- default:
- cout << "\n\n\t\tBye Bye\n\n========================================n\n";
- break;
- }
- } while (choice != 11);
- }
- //Lab functions
- void doubleList::doubleAddEnd(int item, doubleNode *&head, doubleNode *&tail)
- {
- doubleNode *newNode = new doubleNode;
- newNode->next = NULL;
- newNode->data = item;
- if (head == NULL)
- {
- newNode->perv = NULL;
- head = tail = newNode;
- }
- else
- {
- newNode->perv = tail;
- tail->next = newNode;
- tail = newNode;
- }
- }
- //Lab functions
- void doubleList::doubleAddBegin(int item, doubleNode *&head, doubleNode *&tail)
- {
- doubleNode *newNode = new doubleNode;
- newNode->perv = NULL;
- newNode->data = item;
- if (head == NULL)
- {
- newNode->next = NULL;
- head = tail = newNode;
- }
- else
- {
- newNode->next = head;
- head->perv = newNode;
- head = newNode;
- }
- }
- //Lab functions
- void doubleList::doubleAddByPosition(int item, doubleNode *&head, doubleNode *&tail, int pos)
- {
- doubleNode *newNode = new doubleNode;
- newNode->next = NULL;
- newNode->data = item;
- doubleNode *temp;
- temp = head;
- int tempos = 1;
- if (head == NULL)
- {
- cout << "\nEmpty list, location not found\n";
- }
- else
- {
- while (tempos != pos)
- {
- if (temp == NULL)
- {
- cout << "The required location not found\n";
- }
- else
- {
- temp = temp->next;
- }
- tempos++;
- }
- }
- if (temp->next == NULL)
- {
- temp->next = newNode;
- newNode->perv = temp;
- newNode->next = NULL;
- tail = newNode;
- }
- else
- {
- newNode->perv = temp;
- newNode->next = temp->next;
- temp->next->perv = newNode;
- temp->next = newNode;
- }
- }
- //lab function
- void doubleList::doubleAddByData(int item, doubleNode *&head, doubleNode *&tail, int value)
- {
- doubleNode *newNode = new doubleNode;
- newNode->next = NULL;
- newNode->data = item;
- doubleNode *temp;
- temp = head;
- if (head == NULL)
- {
- cout << "\nEmpty list, location not found\n";
- }
- else
- {
- while (temp->data != value)
- {
- if (temp == NULL)
- {
- cout << "The required location not found\n";
- }
- else
- {
- temp = temp->next;
- }
- }
- }
- if (temp->next == NULL)
- {
- temp->next = newNode;
- newNode->perv = temp;
- newNode->next = NULL;
- tail = newNode;
- }
- else
- {
- newNode->perv = temp;
- newNode->next = temp->next;
- temp->next->perv = newNode;
- temp->next = newNode;
- }
- }
- //lab function
- void doubleList::doubleDisplay(doubleNode *&head)
- {
- doubleNode *temp = new doubleNode;
- temp = head;
- if (head == NULL)
- {
- cout << "Empty list\n";
- }
- else
- {
- while (temp != NULL)
- {
- cout << "--| " << temp->data << endl;
- temp = temp->next;
- }
- }
- cout << endl;
- }
- //lab function
- void doubleList::doubleDeleteEnd(doubleNode *&head, doubleNode *&tail)
- {
- if (head == NULL)
- {
- cout << "Empty list\n";
- return;
- }
- else
- {
- doubleNode *temp;
- temp = tail;
- if (temp->next == NULL && temp->perv == NULL)
- {
- head = tail = NULL;
- delete temp;
- }
- else
- {
- tail = tail->perv;
- tail->next = NULL;
- delete temp;
- }
- }
- }
- //lab function
- void doubleList::doubleDeleteBegin(doubleNode *&head)
- {
- if (head == NULL)
- {
- cout << "Empty list\n";
- return;
- }
- else
- {
- doubleNode *temp;
- temp = head;
- if (temp->next == NULL)
- {
- head = NULL;
- delete temp;
- }
- else
- {
- head = head->next;
- head->perv = NULL;
- delete temp;
- }
- }
- }
- //lab function
- void doubleList::doubleDeleteByPosition(int pos, doubleNode *&head, doubleNode *&tail)
- {
- doubleNode *temp;
- temp = head;
- int tempos = 1;
- if (head == NULL)
- {
- cout << "Empty list\n";
- return;
- }
- else
- {
- while (tempos != pos)
- {
- if (temp = NULL)
- {
- cout << "The required position not found\n";
- return;
- }
- else
- {
- temp = temp->next;
- }
- tempos++;
- }
- }
- if (temp->next == NULL && temp->perv == NULL)
- {
- head = tail = NULL;
- delete temp;
- }
- else if (temp == head)
- {
- head = head->next;
- head->perv = NULL;
- delete temp;
- }
- else if (temp->next == NULL)
- {
- tail = tail->perv;
- tail->next = NULL;
- delete temp;
- }
- else
- {
- temp->perv->next = temp->next;
- temp->next->perv = temp->perv;
- delete temp;
- }
- }
- //lab function
- void doubleList::doubleDeleteByData(int value, doubleNode *&head, doubleNode *&tail)
- {
- doubleNode *temp;
- temp = head;
- if (head == NULL)
- {
- cout << "Empty list\n";
- return;
- }
- else
- {
- while (temp->data != value)
- {
- if (temp = NULL)
- {
- cout << "The required position not found\n";
- return;
- }
- else
- {
- temp = temp->next;
- }
- }
- }
- if (temp->next == NULL && temp->perv == NULL)
- {
- head = tail = NULL;
- delete temp;
- }
- else if (temp == head)
- {
- head = head->next;
- head->perv = NULL;
- delete temp;
- }
- else if (temp->next == NULL)
- {
- tail = tail->perv;
- tail->next = NULL;
- delete temp;
- }
- else
- {
- temp->perv->next = temp->next;
- temp->next->perv = temp->perv;
- delete temp;
- }
- }
- //DOUBLE LIST (home work function)|| HW 2
- //Swap between two list content
- void swapList(doubleNode *&head, doubleNode *&head2)
- {
- int temp;
- doubleNode *temp1, *temp2,*temp3,*temp4;
- temp1 = temp3 = head;
- temp2 = temp4 = head2;
- if (head == NULL || head2 == NULL)
- {
- cout << "one of list is empty please Enter some data....\n";
- return;
- }
- else
- {
- for (i = 0;i < 5;i++)
- {
- temp = temp1->data;
- temp1->data = temp2->data;
- temp2->data = temp;
- temp1 = temp1->next;
- temp2 = temp2->next;
- }
- cout << "\nfirst list\n";
- doubleL.doubleDisplay(temp3);
- cout << "\nsecond list\n";
- doubleL.doubleDisplay(temp4);
- }
- }
- //DOUBLE LIST (home work function)|| HW 3
- //Remove all duplicate item in list
- void duplicate(doubleNode *&head)
- {
- doubleNode *temp, *temp1,*find,*temp3;
- temp3 = temp=head;
- // pick elements one by one
- while (temp != NULL) {
- temp1 = temp->next;
- while (temp1 != NULL) {
- if (temp->data == temp1->data) {
- find = temp1->next;
- delete temp1;
- temp1 = find;
- temp->next = find;
- find->perv = temp;
- }
- else
- temp1 = temp1->next;
- }
- temp = temp->next;
- }
- doubleL.doubleDisplay(temp3);
- }
- //DOUBLE LIST (home work function)|| HW 4
- //Remove all occurrences by given key
- void deleteAllByKey(doubleNode *&head,doubleNode *&tail,int key)
- {
- doubleNode *temp,*helper,*root;
- temp = helper = root = head;
- if (head == NULL)
- {
- cout << "Empty list\n";
- return;
- }
- else
- {
- while (temp != NULL)
- {
- if (temp->data == key)
- {
- if (temp == root)
- {
- helper = temp;
- temp = temp->next;
- temp->perv = NULL;
- head = temp;
- delete helper;
- }
- else if (temp == tail)
- {
- helper = temp;
- tail = helper->perv;
- tail->next = NULL;
- delete helper;
- temp = NULL;
- }
- else
- {
- helper->next = temp->next;
- temp->next = NULL;
- delete temp;
- temp = helper->next;
- helper->next->perv = helper;
- }
- }
- else
- {
- helper = temp;
- temp = temp->next;
- }
- }
- }
- doubleL.doubleDisplay(head);
- }
- /*==========
- DOUBLE HW ==
- ============*/
- //home work choices function
- void doubleHomeWork()
- {
- do
- {
- cout << "1) Write C++ progem to create n- node double linked list and then prints the contents of list\n";
- cout << "2) Write a program to input 5 nodes in two different int double linked list (H1 and H2), Swap the element of the nodes in list H1 with list H2\n";
- cout << "3) Assume if you have doubly linked list containing n nodes, Write only function to remove duplicate nodes from the list\n";
- cout << "4) Write complete program to construct double linked list of (8) nodes, then delete all occurrences of a given key (x) in doubly linked list\n";
- cout << "5) Exit system\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- doubleHW1();
- break;
- case 2:
- doubleHW2();
- break;
- case 3:
- doubleHW3();
- break;
- case 4:
- doubleHW4();
- break;
- default:
- break;
- }
- } while (choice != 5);
- }
- //DOUBLE LIST (home work function)|| HW 1
- void doubleHW1()
- {
- doubleNode *head, *tail;
- tail = head = NULL;
- do
- {
- cout << "1)Create list\n";
- cout << "2)Display list\n";
- cout << "3)Back\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- cout << "Enter size of list : ";
- cin >> lsize;
- for (i = 0;i < lsize;i++)
- {
- cout << "Enter element : ";
- cin >> item;
- doubleL.doubleAddEnd(item, head, tail);
- }
- break;
- case 2:
- doubleL.doubleDisplay(head);
- break;
- default:
- break;
- }
- } while (choice != 3);
- }
- //DOUBLE LIST (home work function)|| HW 2
- void doubleHW2()
- {
- doubleNode *tail, *head,*tail2,*head2;
- head = tail = head2 = tail2 = NULL;
- do
- {
- cout << "1)Create First list\n";
- cout << "2)Create Second list\n";
- cout << "3)Swap between two list\n";
- cout << "4)Back\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- for (i = 0;i < 5;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddEnd(item, head, tail);
- }
- break;
- case 2:
- for (i = 0;i < 5;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddEnd(item, head2, tail2);
- }
- break;
- case 3:
- swapList(head, head2);
- break;
- default:
- break;
- }
- } while (choice != 4);
- }
- //DOUBLE LIST (home work function)|| HW 3
- void doubleHW3()
- {
- doubleNode *tail, *head;
- head = tail = NULL;
- do
- {
- cout << "1)Create List\n";
- cout << "2)Remove duplicate item\n";
- cout << "3)Back\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- cout << "Enter list size : ";
- cin >> lsize;
- for (i = 0;i < lsize;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddEnd(item, head, tail);
- }
- break;
- case 2:
- duplicate(head);
- break;
- default:
- break;
- }
- } while (choice != 3);
- }
- //DOUBLE LIST (home work function)|| HW 4
- void doubleHW4()
- {
- doubleNode *head, *tail;
- head = tail = NULL;
- int key;
- do
- {
- cout << "1)Create list\n";
- cout << "2)Delete all occurrences of a given key\n";
- cout << "3)Back\n";
- cout << "========================================\n";
- cout << "Enter your Choice : ";
- cin >> choice;
- cout << "========================================\n";
- switch (choice)
- {
- case 1:
- for (i = 0;i < 4;i++)
- {
- cout << "Enter item : ";
- cin >> item;
- doubleL.doubleAddEnd(item, head, tail);
- }
- break;
- case 2:
- cout << "Enter the key : ";
- cin >> key;
- deleteAllByKey(head,tail,key);
- break;
- default:
- break;
- }
- } while (choice != 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment