Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3794754 • Letter: #

Question

- 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 ";
   for (StackNode* temp = top; temp != 0; temp = temp->next) {
   if( temp == top ) {
       cout << "[" << temp->dataItem << "] ";
   }
   else {
       cout << temp->dataItem << " ";
   }
   }
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

Explanation / Answer

#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<<" Stack is empty";

else

{

tmp->data=n;

tmp->link=top;

top=tmp;

}

}

void stack::pop()

{

if(top==NULL)

cout<<" Stack 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<<" Stack is empty";

else

{

cout<<" The elements in the stack are:";

while(tmp!=NULL)

{

cout<<tmp->data;

tmp=tmp->link;

}

}

}

void main()

{

clrscr();

stack s;

int x,ch;

do

{

cout<<" 1.Push";

cout<<" 2.Pop";

cout<<" 3.Display";

cout<<" 4.Exit";

cout<<" Enter the choice";

cin>>ch;

switch(ch)

{

case 1:

cout<<" Enter the element to be pushed";

cin>>x;

s.push(x);

cout<<" The element is inserted";

break;

case 2:

s.pop();

break;

case 3:

s.display();

case 4:

break;

}

}while(ch!=4);

getch();

}