Question

- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...

- implement the Stack ADT using the linked list approach. Use C++ program language

#include "StackLinked.h"

template
StackLinked::StackLinked (int maxNumber)
{
}

template
StackLinked::StackLinked(const StackLinked& other)
{
}

template
StackLinked& StackLinked::operator=(const StackLinked& other)
{
}

template
StackLinked::~StackLinked()
{
   clear();
}

template
void StackLinked::push(const DataType& newDataItem) throw (logic_error)
{
  
}

template
DataType StackLinked::pop() throw (logic_error)
{
}

template
void StackLinked::clear()
{
   StackNode* t;
   while ( top != NULL)
   {
       t = top;
       top = top->next;
       delete t;
   }
}

template
bool StackLinked::isEmpty() const
{
return false;
}

template
bool StackLinked::isFull() const
{
   return false;
}

template
void StackLinked::showStructure() const
{
if( isEmpty() )
{
   cout << "Empty stack" << endl;
}
else
{
cout << "Top\t";
   for (StackNode* temp = top; temp != 0; temp = temp->next) {
   if( temp == top ) {
       cout << "[" << temp->dataItem << "]\t";
   }
   else {
       cout << temp->dataItem << "\t";
   }
   }
cout << "Bottom" << endl;
}

}

// Class declaration for the array implementation of the Stack ADT

//Stack.h

//
//--------------------------------------------------------------------

#ifndef STACKARRAY_H
#define STACKARRAY_H

#include
#include

using namespace std;

#include "Stack.h"

template
class StackLinked : public Stack {

public:

StackLinked(int maxNumber = Stack::MAX_STACK_SIZE);
StackLinked(const StackLinked& other);
StackLinked& operator=(const StackLinked& other);
~StackLinked();

void push(const DataType& newDataItem) throw (logic_error);
DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;
bool isFull() const;

void showStructure() const;

private:

class StackNode {
public:
   StackNode(const DataType& nodeData, StackNode* nextPtr)
   {
       dataItem = nodeData;
       next = nextPtr;
   }

   DataType dataItem;
   StackNode* next;
};

StackNode* top;
};

#endif       //#ifndef STACKARRAY_H

Homework Answers

Answer #1

#include<iostream.h>

#include<conio.h>

struct node

{

int data;

node *link;

};

class stack

{

private:

struct node *top;

public:

stack();

void push(int n);

void pop();

void display();

};

stack::stack()

{

top=NULL;

}

void stack::push(int n)

{

node *tmp;

tmp= new node;

if(tmp==NULL)

cout<<"\nStack is empty";

else

{

tmp->data=n;

tmp->link=top;

top=tmp;

}

}

void stack::pop()

{

if(top==NULL)

cout<<"\nStack is empty";

else

{

node *tmp;

int n;

tmp=top;

n=tmp->data;

top=top->link;

delete tmp;

}

}

void stack::display()

{

node *tmp;

tmp=top;

if(tmp==NULL)

cout<<"\nStack is empty";

else

{

cout<<"\nThe elements in the stack are:";

while(tmp!=NULL)

{

cout<<tmp->data;

tmp=tmp->link;

}

}

}

void main()

{

clrscr();

stack s;

int x,ch;

do

{

cout<<"\n1.Push";

cout<<"\n2.Pop";

cout<<"\n3.Display";

cout<<"\n4.Exit";

cout<<"\nEnter the choice";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\nEnter the element to be pushed";

cin>>x;

s.push(x);

cout<<"\nThe element is inserted";

break;

case 2:

s.pop();

break;

case 3:

s.display();

case 4:

break;

}

}while(ch!=4);

getch();

}

Know the answer?
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
Similar Questions
Used the next seudoucode pseudocode and implement in the next code below run in c ++...
Used the next seudoucode pseudocode and implement in the next code below run in c ++ this code What is the output of the following pseudocode, where num1 , num2 , and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " " << num3 << endl ____________________________________________________________________________________ a// Created by Frank M....
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
tacks. Trace the C++ program below showing all output in the order that it is displayed....
tacks. Trace the C++ program below showing all output in the order that it is displayed. If anything happens that makes it impossible to accomplish an operation or the results of so doing are unpredictable, describe what happens and abort the program at that point. Assume the Stack class is fully defined in an appropriate header and implementation file. The effect of the functions (methods) is as follows: constructor - creates an empty stack empty - returns true if the...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly linked node, with an instance variables data and next. public class LinkedStack<T> implements Stack<T> { private Node<T> head;    public LinkedStack() { head = new Node<T>(); } // pops the top item off the stack // returning the popped value // throw an EmptyStackException if there are no // items in the stack public T pop() { // write your implementation below } }
Complete the following C program to solve the parenthesis matching problem using a stack. We studied...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied the problem and the algorithm in class. Given a sequence of chars (symbols), check if each “(”, “{”, or “[” is paired with a matching “)”, “}”, or “[”. For example, correct: ( )(( )){([( )])}     correct: (( )(( ))){([( )])}   incorrect: )(( )){([( )])}     incorrect: ({[ ])}     incorrect: (   #include <stdio.h> #include <stdlib.h> #define size 100 void push(char *s, int* top, char element);...