Question

Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empt

Create a C++ program. Include comment, input and output file.

STACK IMPLEMENTATION USING AN link list

IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS.

WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS.

OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>
using namespace std;


template <typename T>
class Stack {
private:
    T stackArray[100];
    int top;
public:
      Stack() {
        top = -1;
    }

    void push(T value) {
        ++top;
        stackArray[top] = value;
    }
bool isFullStack( ){

    if(top == 100)
        return true ;
    else return false ;


}

    void display() {
        for(int i = 0; i <= top ; ++i) {
            cout << stackArray[i] << " ";
        }
        cout << endl;
    }
    T pop() {

        // check if the stack is not empty
        if(isEmptyStack()) {

            cout << "The stack is empty so you cannot pop element " << endl;
        }
        else {
        T retVal = stackArray[top];
        --top;
        return retVal;
    }}

    T top_element() {

        if( top != -1 )
        return stackArray[top];
        else
            cout<< "Stack is empty "<< endl;

    }

    int count() {
        return top + 1;
    }


    bool isEmptyStack() {
        if (top == -1) {
            return true;
        }
        else {
            return false;
        }
    }


};

int main()
{
    Stack<double> data_double;
   cout<< "when performing pop() operation on the empty stack : "<< endl ;
    
    data_double.pop();

    data_double.push(15);
    data_double.push(25);
    data_double.push(226);
    data_double.push(32);
    data_double.push(5);
    data_double.push(52);
    data_double.push(92);
    cout<< " Display all elements of the stack:" << endl ;

    data_double.display() ;
    cout << "Top element is :" << data_double.top_element() << endl;
     data_double.pop();
    cout<<"Stack contents after single POP : " <<endl ;
    data_double.display();

    cout << "Top element is( after one pop()  :" << data_double.top_element() << endl;

cout<< " The number of elements in the stack :  " << data_double.count()<< endl ;

    cout<< "Is stack full ?  " ;
    data_double.isFullStack() ?  cout<< "yes " : cout<< "NO ";


    return 0;
}


when performing pop) operation on the empty stack The stack is empty so you cannot pop element Display all elements of the st

Add a comment
Know the answer?
Add Answer to:
Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ functions using linked list A templated stack class that stores type T with the following...

    C++ functions using linked list A templated stack class that stores type T with the following public functions: template <class T> class Stack{ - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item at the top of the stack. };

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //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...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT