Use c to complete this assignment ThanksSolutionGiven below
Use c++ to complete this assignment. Thanks
Solution
Given below are the files. The LinkedList_Stack.impl is intentionally named so since we don\'t want to compile it explictly. It has been include in the .h file as #include \"LinkedList_Stack.impl\".
If compiling on command line, use
g++ Test.cpp
and run as ./a.out
Post a comment in cas of any error , I will help. If hte answer helped, please do rate it. Thank you.
LinkedList_Stack.h
#ifndef LinkedList_Stack_h
#define LinkedList_Stack_h
template <typename Node_dataType>
struct Node
{
Node_dataType data;
struct Node<Node_dataType> *next;
};
template <typename Node_dataType>
class LinkedList_Stack
{
private:
Node<Node_dataType> *top;
public:
LinkedList_Stack();
LinkedList_Stack(const LinkedList_Stack &other_stack);
LinkedList_Stack<Node_dataType>& operator =(const LinkedList_Stack &other_stack);
~LinkedList_Stack();
void push(const Node_dataType &newData);
Node_dataType pop();
void clear();
bool isEmpty() const;
bool isFull() const;
void showStructure() const;
};
#include \"LinkedList_Stack.impl\"
#endif /* LinkedList_Stack_h*/
LinkedList_Stack.impl
#include \"LinkedList_Stack.h\"
#include <iostream>
using namespace std;
template <typename Node_dataType>
LinkedList_Stack<Node_dataType>::LinkedList_Stack()
{
top = NULL;
}
template <typename Node_dataType>
LinkedList_Stack<Node_dataType>::LinkedList_Stack(const LinkedList_Stack &other_stack)
{
top = NULL;
*this = other_stack;
}
template <typename Node_dataType>
LinkedList_Stack<Node_dataType> & LinkedList_Stack<Node_dataType>::operator =(const LinkedList_Stack &other_stack)
{
clear();
Node<Node_dataType>* othercurr = other_stack.top;
if(othercurr != NULL)
{
top = new Node<Node_dataType>();
top->data = othercurr->data;
othercurr = othercurr->next;
Node<Node_dataType>* thiscurr = top;
while(othercurr != NULL)
{
//copy each of the nodes from other stack and link it up
thiscurr->next = new Node<Node_dataType>();
thiscurr = thiscurr->next;
thiscurr->data = othercurr->val;
othercurr = othercurr->next;
}
thiscurr->next = NULL; //for last node
}
return *this;
}
template <typename Node_dataType>
LinkedList_Stack<Node_dataType>::~LinkedList_Stack()
{
clear();
}
template <typename Node_dataType>
void LinkedList_Stack<Node_dataType>::push(const Node_dataType &newData)
{
Node<Node_dataType>* n = new Node<Node_dataType>();
n->data = newData;
n->next = top;
top = n;
}
template <typename Node_dataType>
Node_dataType LinkedList_Stack<Node_dataType>:: pop()
{
Node_dataType val = top->data;
Node<Node_dataType>* temp = top;
top = top->next;
delete temp;
return val;
}
template <typename Node_dataType>
void LinkedList_Stack<Node_dataType>::clear()
{
if(top != NULL)
{
Node<Node_dataType>* curr = top;
Node<Node_dataType>* temp;
while(curr != NULL)
{
temp = curr->next;
delete curr;
curr = temp;
}
top = NULL;
}
}
template <typename Node_dataType>
bool LinkedList_Stack<Node_dataType>::isEmpty() const
{
return top == NULL;
}
template <typename Node_dataType>
bool LinkedList_Stack<Node_dataType>::isFull() const
{
return false;
}
template <typename Node_dataType>
void LinkedList_Stack<Node_dataType>::showStructure() const
{
Node<Node_dataType>* curr = top;
while(curr != NULL)
{
cout << curr ->data << endl;
curr = curr->next;
}
}
Test.cpp
#include \"LinkedList_Stack.h\"
#include <iostream>
using namespace std;
int main()
{
LinkedList_Stack<int> numbers;
if(numbers.isEmpty())
cout << \"stack is empty\" << endl;
else
cout << \"stack is not empty\" <<endl;
for(int i = 1; i <= 10; i++)
numbers.push(i);
cout << \"showing stack contents\" << endl;
numbers.showStructure();
if(numbers.isEmpty())
cout << \"stack is empty\" << endl;
else
cout << \"stack is not empty\" <<endl;
cout << \"popping elements now\" << endl;
while(!numbers.isEmpty())
cout << \"popped \" << numbers.pop() << endl;
if(numbers.isEmpty())
cout << \"stack is empty\" << endl;
else
cout << \"stack is not empty\" <<endl;
}
output
stack is empty
showing stack contents
10
9
8
7
6
5
4
3
2
1
stack is not empty
popping elements now
popped 10
popped 9
popped 8
popped 7
popped 6
popped 5
popped 4
popped 3
popped 2
popped 1
stack is empty