Vous êtes sur la page 1sur 8

Implementing Stack using Class (with constructor etc).

# include<iostream.h> # include<conio.h> # define SIZE 20

class stack { int a[SIZE]; int tos; // Top of Stack public: stack(); void push(int); int pop(); int isempty(); int isfull(); }; stack::stack() { tos=0; //Initialize Top of Stack }

int stack::isempty() { return (tos==0?1:0);

} int stack::isfull() { return (tos==SIZE?1:0); }

void stack::push(int i) {

if(!isfull()) { cout<<"Pushing "<<i<<endl; a[tos]=i; tos++; } else { cerr<<"Stack overflow error ! Possible Data Loss !"; } } int stack::pop() { if(!isempty()) {

cout<<"Popping "<<a[tos-1]<<endl; return(a[--tos]); } else { cerr<<"Stack is empty! What to pop...!"; } return 0; }

void reverse(stack s) { stack s2; while(!s.isempty()) { s2.push(s.pop()); } cout<<"Reversed contents of the stack..."<<endl; while(!s2.isempty()) { cout<<s2.pop()<<endl; } }//end of fn. void main() {

clrscr(); stack s;

s.push(1); s.push(2); s.push(3);

reverse(s); getch(); }

C++ Program to implement a Queue using array.


/************************************************************** Author: Arun Vishnu M V Web: www.arunmvishnu.com Description: C++ Program to implement a Queue using array. ***************************************************************/ #include<conio.h> #include<iostream.h> #include<process.h> # define MAX_SIZE 150 class queues { int front,rear; int queue[MAX_SIZE]; public: queues() // Constructor { front=(-1); rear=(-1); } void insert_rear(int); void delete_front(); void display(); };

void queues::insert_rear(int item) { if((front==0 && rear==MAX_SIZE) || (front==(rear+1))) { cout<<"Queue is full!\nOverflow"; getch(); exit(0); } else { if(front==(-1) && rear==(-1)) { rear=0; front=0; } else if(front!=0 && rear==MAX_SIZE) rear=0; else rear=rear+1; queue[rear]=item; } } void queues::delete_front() { if(front==(-1) && rear==(-1)) { cout<<"Queue is empty!\nUnderflow"; return; } else { if(front==rear) front=rear=(-1); else if(front==MAX_SIZE && rear==MAX_SIZE) front=0; else front=front+1; } cout<<"\nItem deleted."; getch(); } void queues:: display() { int ptr; if(front==0 && rear==0) { cout<<"Queue is empty"; getch(); return; } cout<<"\nThe queue is\n"; if(front<=MAX_SIZE && rear<=front) { for(ptr=front;ptr<MAX_SIZE;ptr++) cout<<queue[ptr]<<"\n"; for(ptr=0;ptr<=rear;ptr++) cout<<queue[ptr]<<"\n"; } else for(ptr=front;ptr<=rear;ptr++) cout<<queue[ptr]<<"

";

} int main() { clrscr(); int length,i,element,choice; queues q1; while(1) { clrscr(); cout<<"1: Insert an item.\n2: Delete an item."; cout<<"\n3: Display elements\n4: Exit"; cout<<"\nEnter your choice: "; cin>>choice; switch(choice) { case 1: cout<<"How many elements are in the queue: "; cin>>length; cout<<"Enter "<<length<<" elements: "; for(i=0;i<length;i++) { cin>>element; q1.insert_rear(element); } q1.display(); getch(); break; case 2: q1.delete_front(); q1.display(); getch(); break; case 3: q1.display(); getch(); break; case 4: exit(0); break; default: cout<<"Please re-enter tour choice."; getch(); break; } } getch(); return(0); }

#include <iostream.h> #define SIZE 5 class queue

{ int *Queue, front, rear; public: queue() { Queue = new int[SIZE]; front = rear = -1; } void push() { if (rear == (SIZE-1)) { cout<<"\n Overflow!"; } else { rear++; cout<<"\n Enter element: "; cin>>Queue[rear]; } } void pop() { if (front == rear) { cout<<"\n Underflow!"; } else { cout<<"\nElement popped: "<<Queue[++front]; } } void display() { if (front == rear) { cout<<"\n Queue Empty"; } else { for(int i = (front+1); i<=rear; i++) { cout<<Queue[i]<<" "; } } } };

int main() { int choice; queue q; while(choice != 4) { cout<<"\n\n Enter your choice :" <<"\n 1. Push an element into Queue." <<"\n 2. Pop an element from Queue."

<<"\n 3. Display the Queue." <<"\n 4. Exit the program.\n\n"; cin>>choice; switch (choice) { case 1: q.push(); break; case 2: q.pop(); break; case 3: q.display(); break; case 4: break; } } return 0; }

Vous aimerez peut-être aussi