Use C++ and make sure to test all the methods in the main function. Thanks In th
ID: 3587589 • Letter: U
Question
Use C++ and make sure to test all the methods in the main function. Thanks
In this task you are asked to implement and test LinkedList_Stack a stack data structure using linked list. Your program has to include at least three files; A header file with your stack class, the implementation of LinkedList_Stack, and a main function that tests all of your methods and operations. Your LinkedList_Stack has to include at least the following methods and operations: 1. The default constructor: LinkedList StackExplanation / Answer
CODE
#pragma once
#ifndef LONKEDLIST_STACK_H
#define LONKEDLIST_STACK_H
struct node
{
int data;
struct node *next;
node(int key)
{
data = key;
next = NULL;
}
};
template<typename Node_dataType>
class LINKEDLIST_STACK
{
private:
int MaxNumber;
node* head;
public:
LINKEDLIST_STACK(int maxNumber);
LINKEDLIST_STACK(const LINKEDLIST_STACK &other_stack);
LINKEDLIST_STACK<Node_dataType>& operator=(const LINKEDLIST_STACK &other_stack);
~LINKEDLIST_STACK();
void push(const Node_dataType &newDataItem);
Node_dataType pop();
void clear();
bool isEmpty() const;
bool isFull() const;
void ShowStructure() const;
};
#endif
//Linked_Stack_Impl.cpp
#include<iostream>
#include <iomanip>
#include"LinkedList_Stack.h"
using namespace std;
template<typename Node_dataType>
LINKEDLIST_STACK<Node_dataType>::LINKEDLIST_STACK(int maxNumber)
{
MaxNumber = maxNumber;
head = NULL;
}
template<typename Node_dataType>
LINKEDLIST_STACK<Node_dataType>::LINKEDLIST_STACK(const LINKEDLIST_STACK &other_stack)
{
this->MaxNumber = other_stack.MaxNumber;
//copy all element of other stack to this stack
node* curr = other_stack.head;
while (curr)
{
this->push(curr->data);
curr = curr->next;
}
}
template<typename Node_dataType>
LINKEDLIST_STACK<Node_dataType>& LINKEDLIST_STACK<Node_dataType>::operator=(const LINKEDLIST_STACK & other_stack)
{
// TODO: insert return statement here
if (*this == other_stack)
return *this;
else
{
this->MaxNumber = other_stack.MaxNumber;
//copy all element of other stack to this stack
node* curr = other_stack.head;
while (curr)
{
this->push(curr->data);
curr = curr->next;
}
return *this;
}
}
template<typename Node_dataType>
LINKEDLIST_STACK<Node_dataType>::~LINKEDLIST_STACK()
{
node* curr = head;;
while (curr)
{
node* tmp = curr;
curr = curr->next;
delete tmp;
}
}
template<typename Node_dataType>
void LINKEDLIST_STACK<Node_dataType>::push(const Node_dataType &newDataItem)
{
if (!this->isFull())
{
node *curr = head;
node * newNode = node(newDataItem);
newNode->data = newDataItem;
newNode->next = head;
head = newNode;
}
else
{
cout << "Stack reached Max Number" << endl;
}
}
template<typename Node_dataType>
Node_dataType LINKEDLIST_STACK<Node_dataType>::pop()
{
if (!this->isEmpty())
{
node *curr = head;
head = head->next;
delete curr;;
}
else
cout << "Stack is empty" << endl;
}
template<typename Node_dataType>
void LINKEDLIST_STACK<Node_dataType>::clear()
{
node* curr = head;
while (curr)
{
node* tmp = curr;
curr = curr->next;
delete tmp;
}
head = NULL;
}
template<typename Node_dataType>
bool LINKEDLIST_STACK<Node_dataType>::isEmpty() const
{
return (head == NULL); // if head is null then it is empty
}
template<typename Node_dataType>
bool LINKEDLIST_STACK<Node_dataType>::isFull() const
{
//count number of node in list
int count = 0;
node *curr = head;
while (curr)
{
count++;
curr = curr->next;
}
//if count is equql to max Number then it is full else not
return (count == MaxNumber);
}
template<typename Node_dataType>
void LINKEDLIST_STACK<Node_dataType>::ShowStructure() const
{
node *curr = head;
while (curr)
{
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
//StackMain.cpp
#include<iostream>
#include"LinkedList_Stack.h"
using namespace std;
int main()
{
LINKEDLIST_STACK<int> stack(10);
stack.push(3);
stack.push(2);
stack.push(1);
stack.push(9);
stack.push(7);
stack.ShowStructure();
system("pause");
return 0;
}
It is working code With the usage of above methods
If any queries please get back to me
Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.