Using C need help implementing the following functions Imple
Using C++, need help implementing the following functions
Implement the Stack ADT using LinkedList – based approach
In each of the operations, there are comments within /* */ or // which states what each operation is looking for and some of the implementations has already been started/done. Need help with finishing the rest of the implementation for each of the following operations below. Thank you.
-------------------------------------------------------------------------------
#include \"StackLinked.h\"
template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
//need implementation for the constructor
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
// // Copy constructor for linked stack
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
//needs implementation
// Overloaded assignment operator for the StackLinked clas
}
//used to clear the list
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
// Inserts newDataItem onto the top of a stack
if (this->isFull())
{
throw logic_error(\"Stack is full!\");
}
}
//pop funtion - pop(delete)
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
// Removes the topmost item from a stack and returns
//using if statment to check if the list is empty or not
if (this->isEmpty())
{
//throws an error
throw logic_error(\"Stack is empty!\");
}
}
//clear item in the list
template <typename DataType>
void StackLinked<DataType>::clear()
{
// Removes all the data items from a stack.
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
// Returns true if a stack is empty. Otherwise, returns false.
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
return false;
}
Solution
#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();
}





