Data Structure Assignment 02
Data Structure Assignment 02
class STACK
int STACK::isFull()
{
{
private:
if(top==(SIZE-1))
int num[SIZE];
return 1;
int top;
else
public:
return 0;
STACK();
}
int push(int);
int pop();
int STACK::push(int n)
int isEmpty();
{
int isFull();
if(isFull())
void displayItems();
{
};
return 0;
STACK::STACK()
}
{
++top;
top=-1;
num[top]=n;
}
return n;
}
int STACK::isEmpty()
{
int STACK::pop() {
{ cout<<endl;
return temp;
cout<<"Enter your choice: ";
cin>>choice;
}
switch(choice)
void STACK::displayItems()
{
{
case 0: break;
int i;
cout<<"Stack is full."<<endl;
int main()
else
{
cout<<temp<<" inserted."<<endl;
STACK stk;
break;
int choice, n,temp;
do
case 2:
temp=stk.pop(); default:
cout<<"Stack is empty."<<endl; }
else }
break;
case 3: return 0;
stk.displayItems();
break; }
Output
2) Implementation of Queue void dequeue();
X peek();
#include <iostream>
int size();
#include <cstdlib>
bool isEmpty();
using namespace std;
bool isFull();
};
// define default capacity of the queue
#define SIZE 10
// Constructor to initialize queue
void queue<X>::dequeue()
public:
{
queue(int size = SIZE); //
// check for queue underflow
constructor
if (isEmpty())
{
{
// Utility function to add an item to the
queue cout << "UnderFlow\nProgram
Terminated\n";
template <class X>
exit(EXIT_FAILURE);
void queue<X>::enqueue(X item)
}
{
return arr[front];
// check for queue overflow
}
if (isFull())
{
// Utility function to return the size of the
cout << "OverFlow\nProgram queue
Terminated\n";
template <class X>
exit(EXIT_FAILURE);
int queue<X>::size()
}
{
return count;
cout << "Inserting " << item << '\n';
}
q.enqueue("c");
{ q.dequeue();
} if (q.isEmpty())
q.enqueue("a");
q.enqueue("b");
Output
3) Convert infix to postfix {
stack? if(full())
#include<iostream> {
{ {
public: top=top+1;
int top; }
stack() }
{ char pop()
top=-1; {
} if(empty())
while((entry2=s.pop())!='(') postfix[p++]=entry1;
postfix[p++]=entry2; break;
break; }
case '+': }
case '-': }
if(!s.empty())
{ postfix[p]='\0';
Output
void createstack()
#include<iostream>
{
#include<conio.h>
top=1;
using namespace std;
}
const char size=10;
bool isempty()
class stack{
{
private:
return (top==-1);
int top;
}
char arr[size];
bool isFull()
{ }
} for(int i=0;i<10;i++)
if(isFull()) cout<<arr[i];
{ }
cout<<"Stack is full"<<"\n"; }
} };
top=top+1; {
s1.createstack();
} int cont=1;
} char ch;
if(isempty()){ {
} if(ch=='('||ch==')')
else{ {
top--; if(ch=='('){
s1.push('(');
} cout<<endl<<"balanced";
else if(s1.isempty()) }
{ else{
} }
else{ return 0;
s1.pop(); }
Output