- implement the Stack ADT (50 points) using the linked list approach #include \"
ID: 3794654 • Letter: #
Question
- implement the Stack ADT (50 points) using the linked list approach
#include "StackLinked.h"
template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
}
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)
{
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
StackNode* t;
while ( top != NULL)
{
t = top;
top = top->next;
delete t;
}
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
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;
}
}
Class declaration for the array implementation of the Stack ADT
//
//--------------------------------------------------------------------
#ifndef STACKARRAY_H
#define STACKARRAY_H
#include <stdexcept>
#include <iostream>
using namespace std;
#include "Stack.h"
template <typename DataType>
class StackLinked : public Stack<DataType> {
public:
StackLinked(int maxNumber = Stack<DataType>::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
Program to implement stack using linked list #include #include struct Node { int data; struct Node *next; }*top = NULL; void push(int); void pop(); void display(); void main() { int choice, value; clrscr(); printf(" :: Stack using Linked List :: "); while(1){ printf(" ****** MENU ****** "); printf("1. Push 2. Pop 3. Display 4. Exit "); printf("Enter your choice: "); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the value to be insert: "); scanf("%d", &value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf(" Wrong selection!!! Please try again!!! "); } } } void push(int value) { struct Node *newNode; newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; if(top == NULL) newNode->next = NULL; else newNode->next = top; top = newNode; printf(" Insertion is Success!!! "); } void pop() { if(top == NULL) printf(" Stack is Empty!!! "); else{ struct Node *temp = top; printf(" Deleted element: %d", temp->data); top = temp->next; free(temp); } } void display() { if(top == NULL) printf(" Stack is Empty!!! "); else{ struct Node *temp = top; while(temp->next != NULL){ printf("%d--->",temp->data); temp = temp -> next; } printf("%d--->NULL",temp->data); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.