Presentation is loading. Please wait.

Presentation is loading. Please wait.

ARRAY BASED STACK DATED: 30 TH AUG 2012 CLASS - XII.

Similar presentations


Presentation on theme: "ARRAY BASED STACK DATED: 30 TH AUG 2012 CLASS - XII."— Presentation transcript:

1 ARRAY BASED STACK DATED: 30 TH AUG 2012 CLASS - XII

2 #include #define MAX 6 int stack[MAX]; int top=-1; void push(int x) { if(top==MAX-1) { cout<<"\n Stack overflows \n"; return; } top=top+1; stack[top]=x; }

3 void pop() { if(top==-1) { cout<<"\n Stack underflows \n"; return; } cout<<"\n popped :"<<stack[top]; top=top-1; }

4 void show() { cout<<"\n showing....\n"; for(int i=top;i>=0;i--) cout<<stack[i]<<" "; }

5 void main() { void push(int); void pop(); void show(); for(int i=1;i<=5;i++) push(i); show(); pop(); show(); for(int j=6;j<=8;j++) push(j); show(); getch(); }

6 #include #define MAX 50 class Stack { int arr[MAX]; int top; public: Stack() { top =-1; } void push(int); void pop(); void show(); };

7 void Stack::push(int x) { if(top==MAX-1) { cout<<"\n stack overflows\n"; return; } arr[++top]=x; }

8 void Stack::pop() { if(top==-1) { cout<<"\n stack underflows\n"; return; } cout<<"\n popped:"<<arr[top]; top--; }

9 void Stack::show() { if(top==-1) { cout<<"\n stack underflows\n"; return; } cout<<"\n showing..\n"; for(int i=top;i>=0;i--) cout<<arr[i]<<" "; }

10 void main() { Stack s; for(int i=1;i<=5;i++) s.push(i); s.show(); s.pop(); s.show(); for(int j=6;j<=9;j++) s.push(j); s.show(); getch(); }

11 ARRAY BASED LINEAR QUEUE DATED: 30 TH AUG 2012 CLASS - XII

12 #include #define MAX 6 Class Queue { Int arr[MAX]; Int front, rear; Public: Queue( ) { front = rear =-1; } Void add(int); Void del(); Void show(); };

13 Void Queue:: add(int x) { If(rear == MAX -1) { cout<< “Queue is full “; return; } Rear++; Arr[rear]=x; If(front == -1) Front =0; }

14 Void Queue:: del() { If(front == -1) { cout<< “Queue is empty “; return; } Cout<<“\n value deleted :” << arr[front]; If(front ==rear) Front = rear = -1; Else Front ++; }

15 Void Queue:: show() { If(front == -1) { cout<< “Queue is Empty “; return; } Cout<<“\n showing…….\n”; For(int i =front ; i<=rear; i++) Cout<<arr[i]<<“ “; }

16 Void main() { Clrscr(); Queue q; For(int i =1; i< =6; i++) q.add(i*2); q.show(); For( int j=1; j<=3;j++) q.del(); q.show(); getch(); }

17 ARRAY BASED CIRCULAR QUEUE DATED: 30 TH AUG 2012 CLASS - XII

18 #include #define MAX 6 Class cQueue { Int arr[MAX]; Int front, rear; Public: Queue( ) { front = rear =0; } Void add(int); Void del(); Void show(); };

19 Void cQueue:: add(int x) { If( (front==0 && rear ==MAX -1) && (front == rear + 1) ) { cout<< “Queue is full “; return; } If(rear == -1) front = rear =0; Else if(rear == MAX -1) rear =0; Else rear++; Arr[rear]=x; }

20 Void cQueue:: del() { If(front == -1) { cout<< “Queue is empty “; return; } cout<<“\n value deleted :” << arr[front]; If(front == rear) Front = rear = -1; Else if(front == MAX – 1) front =0; Else Front ++; }

21 Void cQueue:: show() { If(front == -1) { cout<< “Queue is Empty “; return; } Cout<<“\n showing…….\n”; If(front< rear) { For(int i =front ; i<=rear; i++) Cout<<arr[i]<<“ “; } Else { for(int i=front; i<=MAX -1; i++) Cout<<arr[i]<<“ “; for( i= 0; i<=rear; i++) Cout<<arr[i]<<“ “; }

22 #define MAX 6 class cQueue { int arr[6]; int front, rear; public: cQueue() { front =rear =0; } void add(int); void del(); void show(); };

23 void cQueue::add(int x) { if( (rear+1)%MAX==front) { cout<<"\n Queue overflows"; return; } arr[rear]=x; rear = (rear+1)%MAX; }

24 void cQueue::del() { if(front == rear) { cout<<"\n Queue underflows"; return; } cout<<"\n deleted :"<<arr[front]; front = (front+1)%MAX; }

25 void cQueue::show() { if(front == rear) { cout<<"\n Queue underflows"; return; } cout<<"\n showing...\n"; for(int i=front; i!=rear; i = (i+1)%MAX) cout<<arr[i]<<“ “; }

26 Void main() { Clrscr(); Queue q; For(int i =1; i< =6; i++) q.add(i*2); q.show(); For( int j=1; j<=3;j++) q.del();q.add(21); q.show(); Getch(); }


Download ppt "ARRAY BASED STACK DATED: 30 TH AUG 2012 CLASS - XII."

Similar presentations


Ads by Google