Stack Using Array
Stack Using Array
#include <iostream>
class Stack
private:
int *arr;
int length;
int top_element;
public:
Stack(int l)
length=((l>0)?l:10);
top_element=-1;
if(!Is_Full())
arr[++top_element]=val;
}
else
t Pop()
if(!Is_Empty())
return arr[top_element--];
else
t Top()
return arr[top_element];
bool Is_Full()
if(top_element==9)
return true;
else
return false;
bool Is_Empty()
if(top_element==-1)
return true;
else
return false;
};
class Node
private:
t data;
public:
Node(t d)
{
data = d;
next = NULL;
void SetData(t d)
data = d;
t GetData()
return data;
next = ptr;
Node<t> *GetNext()
return next;
prev = ptr;
Node<t> *GetPrev()
{
return prev;
};
#include <iostream>
#include "TemplateNode.h"
class Stack
private:
Node<t> *head;
public:
Stack()
head = NULL;
ptr ->SetNext(head);
head = ptr;
}
t Pop()
if(!Is_Empty())
head = head->GetNext();
delete ptr;
return val;
else
t Top()
return head->GetData();
bool Is_Empty()
if(head == NULL)
return true;
else
return false;
~Stack()
Node<t> *ptr;
ptr=head;
while(ptr!=NULL)
head=head->GetNext();
delete ptr;
head=ptr;
};
#include <iostream>
#include "TemplateStackUsingArray.h"
//#include "TemplateStackUsingLinkedList.h"
int main()
Stack<int> s1(10);
//Stack s;
s1.Push(3);
s1.Push(5);
cout<<s1.Pop();
cout<<s1.Pop();
Stack<char> s2(10);
s2.Push('+');
s2.Push('*');
cout<<s2.Pop();
cout<<s2.Pop();