You are on page 1of 12

Class Of Stack with Push ,pop operation.

#include<stdio.h> #include<conio.h> #include<iostream.h> class intstack { private: int *Arr; int size; int top; public: intstack(int n) { size=n; top=0; Arr= new int(size); } void push(int obj) { if(top==size)

cout<<"\nstack is full\n"; else Arr[top++]=obj; } int pop() { if(top==0) cout<<"\nstack is empty"; else return Arr[--top]; return -1; } }; void main() { intstack stack(10); clrscr(); stack.push(5); stack.push(7); cout<<stack.pop()<<endl; cout<<stack.pop()<<endl; getch();

Queues class with its operation .


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> class intqueue { int *arr; int size; int t,f; public: intqueue(int n) { size=n; t=0; f=0; arr=new int(n);

void enqueue(int x) { if (f+1==t) cout<<"queue is Full"; else { arr[f]=x; f=(f+1)%size; } } int dequeue() { if (f==t) { cout<<"queue is Empty"; return-1; } else { t=(t+1) % size;

return arr[t-1];

} return -1; }

int front() { if (f==t) { cout<<"queue is Empty"; return-1; } else return arr[t]; }

int Size() { if((f-t)>=0) return(f-t); else return(size -t+f);

int isempty() { if (f==t) return 1; else return 0; }

int isfull() { if(f+1==t) return 1; else return 0; } };

void main() { clrscr();

intqueue queue1(10); queue1.enqueue(3); queue1.enqueue(5); queue1.enqueue(9); queue1.enqueue(7);

cout<<queue1.dequeue()<<endl; cout<<queue1.dequeue()<<endl; cout<<queue1.dequeue()<<endl; cout<<queue1.dequeue()<<endl;

getch(); }

Decimal To binary Conversion Stack Application


#include<stdlib.h> #include<iostream.h> #include<conio.h> #include<math.h>

class intstack { private: int *Arr; int Size; int Top; public: intstack (int n) //constructor { Size=n; Top=0; Arr = new int(Size); } //end of intstack

void push (int x) { if (Top==Size) cout<<"\nThe stack is full"; else Arr[Top++] = x; } //end of push

int pop() { if (Top==0) cout<<"\n The stack is empty"; else return Arr[--Top] ; return -1; } //end of pop

int top() { if (Top==0) cout<<"\n The stack is empty"; else

return Arr[Top-1] ; return -1; } //end of top

int size() { return Top; } //end of size

int isempty() { if (Top == 0) return 1; else return 0; } //end of isempty

int isfull() { if (Top == Size) return 1; else

return 0; } //end of isfull

}; // end of class

void main() { clrscr(); long long int n; int m,r; cout<<"Enter an Decimal Number: "; cin>>n; m=ceil(log10(n)/log10(2)); intstack stack(m+1); while (n>1) { r=n%2; n=n/2; stack.push(r); } stack.push(n); while (stack.isempty() == 0)

cout<<stack.pop(); getch();

} //end of main

You might also like