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
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;...
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.          ...
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);...
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include...
IN C++ format, Fill in the missing functions, using the concept of operator overloading. code: #include <string> #include <ostream> using namespace std; class Book { private:     string title;     string author;     unsigned isbn;     double price; public: /**Constructors*/    Book();    Book(string t, string a, unsigned i, double p);     /**Access Functions*/     string get_title();     string get_author();     unsigned get_isbn();     double get_price();     /**Manipulation Procedures*/    void set_title(string t);     void set_author(string a);     void set_isbn(unsigned...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT