Archive
C++ Program to implement Generic Classes for maintaining Linked List – Q27
Q27. Program to implement Generic Classes for maintaining Linked List:
Create a Template class that implements a singly-linked list. The linked list class should include following member functions-
push_front( ), push_back( ), push_at_any( ), remove_first( ), remove_last( ) & remove_at _any( ).
There may be a private data member size to keep track of no. of items remaining at any given point of time.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> template <class T> class NODE{ private: T elm, key; NODE *next; public: void addNodeBeg(NODE **); void addNodeEnd(NODE **); void addNodePos(NODE **); void deleteNode(NODE **); void displayList(NODE *); }; template <class T> void NODE<T> :: addNodeBeg(NODE<T> **node){ NODE *temp; cout<<"\n Enter a Value: "; cin>>key; if(*node == NULL){ // list is Empty. *node = new(NODE); (*node)->elm = key; (*node)->next = NULL; } else{ // list is not empty. temp = new(NODE); temp->elm = key; temp->next = *node; *node = temp; } } template <class T> void NODE<T> :: addNodeEnd(NODE<T> **node){ NODE *temp; cout<<"\n Enter a Value: "; cin>>key; if(*node == NULL){ // list is Empty. *node = new(NODE); (*node)->elm = key; (*node)->next = NULL; } else{ // list is not empty. temp = *node; while(temp->next != NULL) temp = temp->next; temp->next = new(NODE); temp = temp->next; temp->elm = key; temp->next = NULL; } } template <class T> void NODE<T> :: addNodePos(NODE<T> **node){ NODE *temp, *ntemp; int pos, i=1; cout<<"\n Enter a Position No.: "; cin>>pos; cout<<"\n Enter a Key: "; cin>>key; if(*node == NULL){ // list is Empty. cout<<"\n List is Empty."; return ; } temp = *node; while(1){ if( (temp->next == NULL) || (i == pos) ) break; temp = temp->next; i++; } ntemp = new(NODE); ntemp->elm = key; ntemp->next = temp->next; temp->next = ntemp; } template <class T> void NODE<T> :: deleteNode(NODE<T> **node){ NODE *temp, *prev; int i=0; cout<<"\n Enter an Element of List to Delete: "; cin>>key; temp = *node; prev = temp; while(1){ if(*node == NULL){ // list is Empty. cout<<"\n List is Empty."; return; } else if(temp->elm == key){ // Element Found. if(i == 0){ // if Element is First Node. (*node) = (*node)->next; cout<<"\n First Node Deleted."; return; } else break; } else if(temp->next == NULL){ cout<<"\n Element not Found."; return; } i++; prev = temp; temp = temp->next; } prev->next = temp->next; cout<<"\n Element Deleted."; } template <class T> void NODE<T> :: displayList(NODE<T> *node){ int i=1; cout<<"\n\t List: "; if(node == NULL) cout<<" List is Empty."; while(node != NULL){ cout<<"\t"<<node->elm; node = node->next; i++; } } int _menu(){ int _ch; cout<<"\n List Type:- \n"; cout<<"\n 1 -> Integer."; cout<<"\n 2 -> Float."; cout<<"\n 3 -> Character."; cout<<"\n Enter your choice: "; cin>>_ch; return (_ch); } void main(){ int ch1, ch2; NODE<int> int_list; NODE<float> float_list; NODE<char> char_list; NODE<int> *int_start = NULL; NODE<float> *float_start = NULL; NODE<char> *char_start = NULL; while(1){ clrscr(); cout<<"\n LINK LIST PROGRAM"; cout<<"\n ~~~~~~~~~~~~~~~~~ \n"; cout<<"\n 1 -> Add Node at Begining."; cout<<"\n 2 -> Add Node at End."; cout<<"\n 3 -> Add Node at Position."; cout<<"\n 4 -> Delete Node."; cout<<"\n 5 -> Display List."; cout<<"\n 6 -> Exit."; cout<<"\n Enter your choice: "; cin>>ch1; switch(ch1){ case 1: // Add node at Begining. ch2 = _menu(); switch(ch2){ case 1: cout<<"\n Enter an Integer Value: "; int_list.addNodeBeg(&int_start); break; case 2: cout<<"\n Enter a Float Value:”; float_list.addNodeBeg(&float_start); break; case 3: cout<<"\n Enter a Character: "; char_list.addNodeBeg(&char_start); break; default: cout<<"\n Wrong Entry. No operation Done."; } break; case 2: // Add node at End. ch2 = _menu(); switch(ch2){ case 1: cout<<"\n Enter an Integer Value: "; int_list.addNodeEnd(&int_start); break; case 2: cout<<"\n Enter a Float Value:"; float_list.addNodeEnd(&float_start); break; case 3: cout<<"\n Enter a Character: "; char_list.addNodeEnd(&char_start); break; default: cout<<"\n Wrong Entry. No operation Done."; } break; case 3: // Add node at Position. ch2 = _menu(); switch(ch2){ case 1: cout<<"\n Enter an Integer Value: "; int_list.addNodePos(&int_start); break; case 2: cout<<"\n Enter a Float Value:"; float_list.addNodePos(&float_start); break; case 3: cout<<"\n Enter a Character: "; char_list.addNodePos(&char_start); break; default: cout<<"\n Wrong Entry. No operation Done."; } break; case 4: // Delete Node. ch2 = _menu(); switch(ch2){ case 1: cout<<"\n Enter an Integer Value: "; int_list.deleteNode(&int_start); break; case 2: cout<<"\n Enter a Float Value:"; float_list.deleteNode(&float_start); break; case 3: cout<<"\n Enter a Character: "; char_list.deleteNode(&char_start); break; default: cout<<"\n Wrong Entry. No operation Done."; } break; case 5: // Display List. cout<<"\n\n Integer List: - "; int_list.displayList(int_start); cout<<"\n\n Float List: - "; float_list.displayList(float_start); cout<<"\n\n Character List: - "; char_list.displayList(char_start); break; default: exit(1); } getch(); } }
Output:
LINK LIST PROGRAM
1 -> Add Node at Begining.
2 -> Add Node at End.
3 -> Add Node at Position.
4 -> Delete Node.
5 -> Display List.
6 -> Exit.
Enter your choice: 5
Integer List: –
List: 3 90 44 45 23 45
Float List: –
List: 90.449997 23.440001 12.34
Character List: –
List: e r