Question: help me to fix my error in c++ : Linked List with capital Letter Please help me to write two function in main() int. Just
help me to fix my error in c++ : Linked List with capital Letter
Please help me to write two function in main() int.
Just make sur is run!
Example two function in below:
APEND_CHOICE
INSERT_CHOICE
void Appendediteam (DynamicStack &stack) { string item; // Get an item to push onto the stack. cin.ignore(); cout << " Enter an item: "; getline(cin, item); stack.Append(item); }
void Appendediteam (DynamicStack &stack) { string item; // Get an item to push onto the stack. cin.ignore(); cout << " Enter an postion at : "; getline(cin, item); stack.Insert(item); }
//***************************************************************************
#pragma once #ifndef DYNAMICSTACK_H #define DYNAMICSTACK_H #include using namespace std;
// Stack template template class DynamicStack { private: // Structure for the stach nodes StackNode { T value; // Value in the node StackNode *next; // Pointer to next node };
StackNode *top; // Pointer to the stack top
public: // Stack operations void push(char); void pop(char &); bool isEmpty();
void createnode(char &); int insert_pos(char, int); void append(char); int delete_pos(int); int search(char); void reverse(); void display() const; bool isuppercase(); //check whether entered char is uppercase or not bool isEmpty(); //Constructor DynamicStack() { top= NULL; } // Destructor ~DynamicStack() { / free all the memory space of nodes struct node *ptr = start; //ptr for iterator while (ptr != nullptr) { //iterate unitl list is empty start = start->next; free(ptr); // free the current node ptr = start; } } }; #endif
//DaynmicStack.cpp
#include // For cout #include #include #include #include "DynamicStack.h" using namespace std;
//****************************************************** // isuppercase to check a charecter is upercase or low * // value will have ascii range of 65-90 * //****************************************************** template bool DynamicStack::isuppercase(T &) {
//Upper case letters will have ascii range of 65-90 if ( T >= 65 && T <= 90) { //If it is upper case letter. return true; } return false; // letter is not an uppercase on } //****************************************************** // creating Node value and if false exit * // EXIT_FAILURE after check * //****************************************************** template void DynamicStack::createnode(char &) { stack node *temp, *s; if (!isuppercase(T)) //If it is not an uppercase letter exit(EXIT_FAILURE); // exit from the program. temp = new(struct node); if (temp == NULL) // if memory is not allocated { cout << "Memory not allocated " << endl; return temp; } else { temp->data = value; temp->next = NULL; return temp; //return the created node } } //*************************************************** // Destructor * //*************************************************** template DynamicStack::~DynamicStack() { StackNode *nodePtr, *nextNode;
// Position nodePtr at the start of the stack. nodePtr = start ;
// Traverse the list deleting each node. while (nodePtr != NULL) { nextNode = nodePtr->next; delete nodePtr; nodePtr = nextNode; } } //****************************************************** template void DynamicStack::append(char T) { struct node *temp, *s; temp = create_node(T); s = start; // If list is empty, make the new node as start. if (start == NULL) start = temp; else { // traverse till end and insert the new node. while (s->next != NULL) { s = s->next; } temp->next = NULL; s->next = temp; } cout << "Element Inserted at last" << endl; } //************************************************** template int DynamicStack::insert_pos(char T, int pos) { int counter = 0; struct node *temp, *s, *ptr = NULL; temp = create_node(value); int i; s = top; if (pos == 0) //If it is first position { // Make new node as start of the list temp->next = top; top = temp; return 0; } else { while (s != NULL) { // traverse till we got the appropriate position ptr = s; s = s->next; counter++; if (pos == counter) { // if we reach the postion to insert ptr->next = temp; temp->next = s; return 0; } } } return -1; // Not able to insert. So failure. } //************************************************************* template int DynamicStack::delete_pos(int pos) { int i, counter = 0; if (start == NULL) { cout << "List is empty so cant delete" << endl; return -1; } struct node *s, *ptr = NULL; s = top; if (pos == 0) //If we have to delete first node { top = s->next; // Move the start to next node. } else { while (s != NULL) // traverse till we find the appropriate position { if (pos == counter) { // if we find the position ptr->next = s->next; free(s); return 0; } ptr = s; // Holding the previous node at the time of deletion counter ++; s = s->next; } return -1; // Not able to delete any node. Suitable position not found. } free(s); // free the node which has to be deleted. return 0; }*/ template //*************************************************** template int DynamicStack::search(char T) { int pos = -1; bool flag = false; if (start == NULL) { cout << "List is empty" << endl; return -1; } struct node *s; s = start; while (s != NULL) // traverse the list till we reach end or till we get a match { pos++; if (s->data == T) // match found { return pos; // return position } s = s->next; } return -1; // No match found. } template void DynamicStack::reverse() { struct node *ptr1, *ptr2, *ptr3;
if (start == NULL) // If list is empty nothing to reverse { cout << "List is empty" << endl; return; } if (start->next == NULL) // If list has one element, no need to reverse { return; } ptr1 = start; //1st Node ptr2 = ptr1->next; //2nd node ptr3 = ptr2->next; //3rd node ptr1->next = NULL; // make first node next to null. ptr2->next = ptr1; // Reverse 1st and 2nd node while (ptr3 != NULL) // Continue this till the end { ptr1 = ptr2; ptr2 = ptr3; ptr3 = ptr3->next; ptr2->next = ptr1; } start = ptr2; } template void DynamicStack::display()const
{ struct node *temp; if (start == NULL) { cout << "The List is Empty" << endl; return; } temp = start; cout << "Elements of list are: " << endl; while (temp != NULL) // Till the last element of list { cout << temp->data << " | "; temp = temp->next; } cout << " " << endl; }
template bool DynamicStack::isEmpty() { bool status;
if (!top) status = true; else status = false;
return status; }
// main.cpp
#include #include #include #include #include "DynamicStack.cpp" using namespace std;
// Constants for the menu choices const int APEND_CHOICE = 1, INSERT_CHOICE = 2, DISPLAY_CHOICE =3, DISPLAY_NUMBERNODE = 4, Display_MAXNODE =5, QUIT_CHOICE = 6;
// Function prototypes void menu(int &); void getStackSize(int &); void AppendIteam(DynamicStack &); void InsertIteam(DynamicStack&); void DisplayIteam(DynamicStack &); void displaynumNod(DynamicStack&); void displayMaxNod(DynamicStack&); int main() { int choice; // To hold a menu choice
// Create the stack. DynamicStack stack;
do { // Get the user's menu choice. menu(choice);
// Perform the user's choice. if (choice != QUIT_CHOICE) { switch (choice) { case APEND_CHOICE: AppendIteam(stack); break; case INSERT_CHOICE: InsertIteam(stack); break; case DISPLAY_CHOICE DisplayIteam(stack); break; } } } while (choice != QUIT_CHOICE);
return 0; }
//************************************************ // The menu function displays the menu and gets * void menu(int &choice) {
// Display the menu and get the user's choice. cout << "Operations on character singly linked list " << APEND_CHOICE << " - Appended an item onto the stack " << INSERT_CHOICE << " - Insert an item in the stack " << DISPLAY_CHOICE <<"This is all Iteam: " << DISPLAY_NUMBERNODE <<"Number of Node : " << Display_MAXNODE <<"This is Max Node: " << QUIT_CHOICE << " - Quit the program " << "Enter your choice: "; cin >> choice;
// Validate the choice while (choice < APEND_CHOICE || choice > QUIT_CHOICE) { cout << "Enter a valid choice: "; cin >> choice; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
