Question

In: Computer Science

take the LinkedStack structure from below( don't edit this structure) from below and make a main...

take the LinkedStack structure from below( don't edit this structure) from below and make a main ( dont change the function in the main). Make a main where the user can input the amount of element they want to enter and then they can input the element. example: user choose 2 elements then he puts as$a and fa$f.

StackInterface.h

#pragma once
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file StackInterface.h */

#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE

template<class ItemType>
class StackInterface
{
public:
   /** Sees whether this stack is empty.
   @return True if the stack is empty, or false if not. */
   virtual bool isEmpty() const = 0;

   /** Adds a new entry to the top of this stack.
   @post If the operation was successful, newEntry is at the top of the stack.
   @param newEntry The object to be added as a new entry.
   @return True if the addition is successful or false if not. */
   virtual bool push(const ItemType& newEntry) = 0;

   /** Removes the top of this stack.
   @post If the operation was successful, the top of the stack
   has been removed.
   @return True if the removal is successful or false if not. */
   virtual bool pop() = 0;

   /** Returns the top of this stack.
   @pre The stack is not empty.
   @post The top of the stack has been returned, and
   the stack is unchanged.
   @return The top of the stack. */
   virtual ItemType peek() const = 0;
}; // end StackInterface
#endif

Node.h

#pragma once
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file Node.h
   Listing 4-1 */
#ifndef _NODE
#define _NODE

template<class ItemType>
class Node
{
private:
   ItemType item; // A data item
   Node<ItemType>* next; // Pointer to next node

public:
   Node();
   Node(const ItemType& anItem);
   Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
   void setItem(const ItemType& anItem);
   void setNext(Node<ItemType>* nextNodePtr);
   ItemType getItem() const;
   Node<ItemType>* getNext() const;
}; // end Node

#include "Node.h"
#include <cstddef>

template<class ItemType>
Node<ItemType>::Node() : next(nullptr)
{
} // end default constructor

template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr)
{
} // end constructor

template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) :
   item(anItem), next(nextNodePtr)
{
} // end constructor

template<class ItemType>
void Node<ItemType>::setItem(const ItemType& anItem)
{
   item = anItem;
} // end setItem

template<class ItemType>
void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)
{
   next = nextNodePtr;
} // end setNext

template<class ItemType>
ItemType Node<ItemType>::getItem() const
{
   return item;
} // end getItem

template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const
{
   return next;
} // end getNext
#endif

LinkedStack.h

#pragma once
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** ADT stack: Link-based implementation.
   Listing 7-3.
   @file LinkedStack.h */

#ifndef _LINKED_STACK
#define _LINKED_STACK

#include "StackInterface.h"
#include "Node.h"

template<class ItemType>
class LinkedStack : public StackInterface<ItemType>
{
private:
   Node<ItemType>* topPtr; // Pointer to first node in the chain;
                       // this node contains the stack's top

public:
   // Constructors and destructor:
   LinkedStack(); // Default constructor
   LinkedStack(const LinkedStack<ItemType>& aStack);// Copy constructor
   virtual ~LinkedStack(); // Destructor

// Stack operations:
   bool isEmpty() const;
   bool push(const ItemType& newItem);
   bool pop();
   ItemType peek() const;
}; // end LinkedStack

#include <cassert> // For assert
#include "LinkedStack.h" // Header file

template<class ItemType>
LinkedStack<ItemType>::LinkedStack() : topPtr(nullptr)
{
} // end default constructor

template<class ItemType>
LinkedStack<ItemType>::LinkedStack(const LinkedStack<ItemType>& aStack)
{
   // Point to nodes in original chain
   Node<ItemType>* origChainPtr = aStack.topPtr;

   if (origChainPtr == nullptr)
       topPtr = nullptr; // Original stack is empty
   else
   {
       // Copy first node
       topPtr = new Node<ItemType>();
       topPtr->setItem(origChainPtr->getItem());

       // Point to last node in new chain
       Node<ItemType>* newChainPtr = topPtr;

       // Advance original-chain pointer
       origChainPtr = origChainPtr->getNext();

       // Copy remaining nodes
       while (origChainPtr != nullptr)
       {
           // Get next item from original chain
           ItemType nextItem = origChainPtr->getItem();

           // Create a new node containing the next item
           Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);

           // Link new node to end of new chain
           newChainPtr->setNext(newNodePtr);

           // Advance pointer to new last node
           newChainPtr = newChainPtr->getNext();

           // Advance original-chain pointer
           origChainPtr = origChainPtr->getNext();
       } // end while

       newChainPtr->setNext(nullptr); // Flag end of chain
   } // end if
} // end copy constructor

template<class ItemType>
LinkedStack<ItemType>::~LinkedStack()
{
   // Pop until stack is empty
   while (!isEmpty())
       pop();
} // end destructor

template<class ItemType>
bool LinkedStack<ItemType>::isEmpty() const
{
   return topPtr == nullptr;
} // end isEmpty

template<class ItemType>
bool LinkedStack<ItemType>::push(const ItemType& newItem)
{
   Node<ItemType>* newNodePtr = new Node<ItemType>(newItem, topPtr);
   topPtr = newNodePtr;
   newNodePtr = nullptr;

   return true;
} // end push

template<class ItemType>
bool LinkedStack<ItemType>::pop()
{
   bool result = false;
   if (!isEmpty())
   {
       // Stack is not empty; delete top
       Node<ItemType>* nodeToDeletePtr = topPtr;
       topPtr = topPtr->getNext();

       // Return deleted node to system
       nodeToDeletePtr->setNext(nullptr);
       delete nodeToDeletePtr;
       nodeToDeletePtr = nullptr;

       result = true;
   } // end if

   return result;
} // end pop

template<class ItemType>
ItemType LinkedStack<ItemType>::peek() const
{
   assert(!isEmpty()); // Enforce precondition

   // Stack is not empty; return top
   return topPtr->getItem();
} // end getTop
// End of implementation file.
#endif

Main.cpp

#include <iostream>
#include <string>
#include "LinkedStack.h"
#include "Node.h"
#include "StackInterface.h"

using namespace std;

void backwards(string lang, LinkedStack<char>* aStack)
{
   int i = 0;
   char ch;
   ch = lang[i];
   int l = lang.length();

   while (lang[i] != '$')
   {
       aStack->push(lang[i]);
       i++;
       ch = lang[i];
   }
   // Skip the $
   i++;

   // Match the reverse of s

   bool inLanguage = true; // Assume string is in language
   while (inLanguage && i < l)
   {
       if (!aStack->isEmpty())
       {
           int stackTop = aStack->peek();
           aStack->pop();
           ch = lang[i];
           if (stackTop == ch)
               i++; // Characters match
           else
               inLanguage = false;
       }
       else inLanguage = false;
   }
   if (inLanguage && aStack->isEmpty())

       cout << lang << " is in language." << endl;
   else
       cout << lang << " is not in language." << endl;

}

int main()
{}

Solutions

Expert Solution

PLEASE GIVE THUMBS UP, THANKS

OUTPUT SAMPLE:

CODE :

MAIN.CPP

#include <iostream>
#include <string>
#include "LinkedStack.h"
#include "Node.h"
#include "StackInterface.h"

using namespace std;

void backwards(string lang, LinkedStack<char>* aStack)
{
int i = 0;
char ch;
ch = lang[i];
int l = lang.length();

while (lang[i] != '$')
{
aStack->push(lang[i]);
i++;
ch = lang[i];
}
// Skip the $
i++;

// Match the reverse of s

bool inLanguage = true; // Assume string is in language
while (inLanguage && i < l)
{
if (!aStack->isEmpty())
{
int stackTop = aStack->peek();
aStack->pop();
ch = lang[i];
if (stackTop == ch)
i++; // Characters match
else
inLanguage = false;
}
else inLanguage = false;
}
if (inLanguage && aStack->isEmpty())

cout << lang << " is in language." << endl;
else
cout << lang << " is not in language." << endl;

}

int main()
{
   string lang;
   LinkedStack<char>* aStack=new LinkedStack<char>();
   int n;
   cout<<"Enter no of elements : ";
   cin>>n;
   for(int i=0; i<n; i++)
   {
       cout<<"Enter "<<(i+1)<<" element : ";
       cin>>lang;
       backwards(lang,aStack);
   }
  
}


Related Solutions

C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do...
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result I need to see the state of the stack1 and stack2 after doing the following operation. (stack1 and stack2 need to have individually there own state result) stack1.push(1) stack1.push(2) stack2.push(3) stack2.push(4) stack1.pop() stackTop = stack2.peek() stack1.push(stackTop) stack1.push(5) stack2.pop() stack2.push(6) ArrayStackInterface.h // Created by Frank M. Carrano and Tim Henry. // Copyright...
C++, ArrayStack. Make a main using the following arraystack structure the main most do the following...
C++, ArrayStack. Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result stack1.push(1) stack1.push(2) stack2.push(3) stack2.push(4) stack1.pop() stackTop = stack2.peek() stack1.push(stackTop) stack1.push(5) stack2.pop() stack2.push(6) ArrayStackInterface.h // Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** @file StackInterface.h */ #ifndef _STACK_INTERFACE #define _STACK_INTERFACE template<class ItemType> class StackInterface { public: /** Sees whether this stack is empty. @return...
Edit the given program to produce the following output in c++ mode: - Take in the...
Edit the given program to produce the following output in c++ mode: - Take in the name of a superhero & tell him how many villains he/she has to defeat today - The number of villains is randomly generated and should be a number between 11 and 42. - Use a seed of 7. Hint: Compile the program first before making edits What is your name? Hello Captain America There are 42 villains you need to defeat today Oops! one...
Describe the wrapping structure that is used in the DNA to take it from a double...
Describe the wrapping structure that is used in the DNA to take it from a double strandred helix to a comapct chromosome.
Edit question Record the below transactions including adjustments as + and - entries under the below...
Edit question Record the below transactions including adjustments as + and - entries under the below accounting equation sheet. Cash Accounts Inventory Equipment Accum Prepaid = Accounts Notes Interest Wages Income Bonds Common Retained Receivable Deprec. Insurance Payable Payable Payable Payable Taxes Payable Stock Earnings Prepaid insurance-This year and Next Year 6,300 Sales Revenue for year of 80, 000 (10,000 in cash) and remaining balance charged on account Paid wages during the current period 5,000 AP Bills to supplies of...
in digestive system the main and accessory organs and their main function.? type don't write
in digestive system the main and accessory organs and their main function.? type don't write
Make paragraph of a main idea from one of a scene from the video: Dirt: The...
Make paragraph of a main idea from one of a scene from the video: Dirt: The Movie.
Create an ASP.NET Core MVC solution, edit the main view to contain an HTML form that...
Create an ASP.NET Core MVC solution, edit the main view to contain an HTML form that asks for first name, last name, and email address. The form should also have a submit button, though the submit button doesn't have to do anything yet. looking for an HTML mockup inside an ASP.NET Core MVC Solution.
How does it make sense to vary the position and the velocity independently? Edit: Velocity is...
How does it make sense to vary the position and the velocity independently? Edit: Velocity is the derivative of position, so how can you treat them as independent variables? Doesn't every physics student ask this question when he learns calculus of variations? Does anybody ever answer this question? Ever? If so, please educate me.
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT