-implement the Stack ADT using the linked list approach *Most of code is complet
ID: 3903205 • Letter: #
Question
-implement the Stack ADT using the linked list approach
*Most of code is completed. Please fix the code under "StackLinked<DataType>::StackLinked(int maxNumber)", "StackLinked<DataType>::StackLinked(const StackLinked& other)", "StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)", and "bool StackLinked<DataType>::isFull() const".
#include "StackLinked.h"
template <typename DataType>
StackLinked<DataType>::StackLinked(int maxNumber)
{
StackNode* top = 0;
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
StackNode* otherTemp;
StackNode* thisPre;
while (otherTemp == NULL);
{
thisPre = new StackNode
}
}
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
}
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
if (isFull())
{
throw logic_error("Stack is full.");
}
else
top = new StackNode(newDataItem, top);
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
if (isEmpty())
{
throw logic_error("Stack is empty.");
}
else
{
StackNode* temp = top;
top = top->next;
DataType t = temp->dataItem;
delete temp;
return t;
}
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
StackNode* next;
while (top != NULL)
{
delete top;
top = NULL;
}
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
if (top == NULL)
{
//return top == 0;
return true;
}
else
{
return false;
}
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
return false;
}
template <typename DataType>
void StackLinked<DataType>::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;
}
}
Explanation / Answer
Implementation of Stack ADT using the linked list approach
#include<iostream>
using namespace std;
class node
{
public:
class node *next;
int data;
};
class stack : public node
{
node *start;
int tos;
public:
stack()
{
tos=-1;
}
void push(int num)
{
if (tos < 0 )
{
start =new node;
start->next=NULL;
start->data=num;
tos ++;
}
else
{
node *temp,*temp1;
temp=start;
if(tos >= 4)
{
cout <<"stack is overflow";
return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=num;
}
}
void display()
{
node *temp;
temp=start;
if (tos < 0)
{
cout <<" stack is underflow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=start;
if( tos < 0 )
{
cout <<"stack is underflow";
return;
}
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
int main()
{
stack s1;
int ch;
while(1)
{
cout <<" 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<" Enter an element";
cin >> ch;
s1.push(ch);
break;
case 2: s1.pop();
break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT
1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:1 Enter an element23 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:3 23 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:1 Enter an element24 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:3 23 24 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:2 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:3 23 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:1 Enter an element23 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:3 23 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:1 Enter an element24 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:3 23 24 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:2 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:3 23 1.PUSH 2.POP 3.DISPLAY 4.EXIT enter ur choice:2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.