Upcoming SlideShare
×

# 901230 lecture5&6

835 views

Published on

stack

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
835
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
8
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 901230 lecture5&6

1. 1. Lecture 5• Stack (Array Implementation)• Queue (Array Implementation ) FIST, Multi Media University
2. 2. StackWhat is a Stack? 1) It is a data structure for storing some values 2) It is an ADT. 3) Implemented using Arrays or Linked List 4) used in Operating System and in System Programming FIST, Multi Media University
3. 3. StackHow to Store and Retrieve from a Stack? 1) All Insertions ( Storing) and Deletions (Removing) of entries are made at one end called the TOP of Stack 2) Last item added to a stack (PUSH) is always the first that will be removed (POP) from the stack. 3) This property is called Last In First Out also known as LIFO 4) The operations are done with a single pointer to the top of list of elements. FIST, Multi Media University Operations...
4. 4. Stack E TOPF D EG - PlatesG C Books B (LIFO) A FIST, Multi Media University
5. 5. Operations• There are Four operations : 1) Push ( ) used to add the data at the top of the stack. Check whether the stack is full or not 2) Pop ( ) used to remove data at the top of the stack. Check whether the stack is empty or not 3) Empty ( ) Checks whether stack is empty, TOP = -1 4) Full ( ) checks whether stackUniversity TOP = maxstacksize FIST, Multi Media is full,
6. 6. Stack - implementation• Uses ARRAY to represent a stack Class stack { private : int stackelement[10],TOP; public: stack( ) { top = -1} int Empty( ); void push(int); int Full ( ); int Pop( ); Multi Media University }; FIST,
7. 7. StackHow to Push? Void stack :: Push(int data2add) { if (!Full( )) { TOP++; Stackelement[TOP] = data2add; } else cout << “Stack is Full!!!”<<endl; } FIST, Multi Media University
8. 8. StackHow to Pop? Int stack :: Pop( ) {int data2remove; if (!Empty( )) { data2remove = Stackelement[TOP]; TOP--; return data2remove; } else{ cout << “Stack is Empty!!!”<<endl; return 0; } } FIST, Multi Media University
9. 9. StackHow to check for full? Int Stack :: Full( ) { if (TOP==9) return 1; else return 0; } FIST, Multi Media University
10. 10. StackHow to check for Empty? Int Stack :: Empty( ) { if (TOP== -1) return 1; else return 0; } FIST, Multi Media University
11. 11. StackVoid main( ){ stackobj.Push(55);stack stackobj; cout << stackobj.Pop( );stackobj.Push(23); cout << stackobj.Pop( ); } 55stackobj.Push(46); 55 10stackobj.Push(37); 37 10stackobj.Push(10); 46 FIST, Multi Media University 23
12. 12. Applications of StackWe shall see two applications of the stack1) Use of Stacks in Function Calls2) Palindrome Checking FIST, Multi Media University
13. 13. Use of Stacks in Function Calls• Whenever a function begins execution an ACTIVATION RECORD (Stack Frame) is created • to store the current environment for that function which includesThis structure should LIFO Parametersbecause , when a function Caller’s state informationterminates, the function withwhich to resume execution is Local variablesthe last function whoseactivation record was saved. Temporary storage FIST, Multi Media University
14. 14. StackActivation Record Function4 Function3 Function3 Function2 Function2 Function1 Function1 FIST, Multi Media University
15. 15. Palindrome CheckingWhat is a palindrome? A palindrome is a word/sentence that is the same if read both ways. You might have noticed that if you push some characters into a stack and pop them out , they will appear in reverse order. So Stacks are used for this purpose. FIST, Multi Media University
16. 16. Palindrome• Here are some palindromesMALAYALAM (a south Indian language)RATS LIVE ON NO EVIL STARDADPOPRADARSTEP ON NO PETSMADAM FIST, Multi Media University
17. 17. implementation#include <iostream.h>#include <string.h>class ADTstack{ char stack[10]; int topstack; public: ADTstack( ) {topstack = -1;}; int empty() {if (topstack == -1) return 1; else return 0;}; int full() {if (topstack == 9) return 1; else return 0;}; void push(char num) { if (!full()) { topstack++; stack[topstack] = num; } else cout<<" Stack is Full"<<endl; FIST, } Multi Media University
18. 18. implementationchar pop( ){ char num; if (!empty()){ num = stack[topstack]; topstack--; return num;} else {cout<<"Stack is Empty"<<endl; return 0;}}}; FIST, Multi Media University
19. 19. implementationvoid main(){ADTstack st;char str[10];int palin, l,i; cout<<"type in a string to check if it is a palindrome"<<endl; cin>>str; l=strlen(str);if (l==1) {palin=1;} //all strings with 1 character are palindromeselse{ FIST, Multi Media University
20. 20. implementationi=0; while(str[i]!=0) //push the string into stack { st.push(str[i]); i++; }i=0;while(str[i]!=0) //pop the string and compare with original{ if (str[i]!=st.pop()) {palin=0;break;} i++;}} if (palin==0) cout<<"Not Palindrome"<<endl; else cout<<"Palindrome"<<endl; FIST, Multi Media University}
21. 21. QueueWhat is a Queue? 1) It is a data structure for storing some values 2) It is an ADT. 3) Implemented using Arrays or Linked List FIST, Multi Media University
22. 22. QueueHow to Store and Retrieve from a Queue? 1) A Data Structure in which all Additions to the list are made at one end, and all Deletions from the list are made at other end. 2) First item added to a queue (ADD) is always the first that will be removed (REMOVE) from the queue. 3) This property is called First In First Out also known asFIFO 4) It can be maintained with two pointers namely FRONT(REMOVE) and REAR ( ADD). FIST, Multi Media University
23. 23. QueueEG - Bank Counter Front Rear FIST, Multi Media University
24. 24. OperationsThere are Four operations : 1) Addend ( ) used to add the data at the Tail of the Queue. Check whether the Queue is full or not 2) Serve ( ) used to remove data at the top of the stack. Check whether the Queue is empty or not 3) Empty ( ) Checks whether Queue is empty, Tail = -1 4) Full ( ) checks whether Queue is full, Tail = maxqueuesize FIST, Multi Media University
25. 25. implementationUses ARRAY to represent a Queue Class Queue { private : int Queueelement[4],Head ,Tail; public: int Empty( ); Queue( ) { Head =0 ; Tail = -1} int Full ( ); void Addend(int); }; int Serve( ); FIST, Multi Media University
26. 26. implementationHow to check for full? Int Queue :: Full( ) { if (Tail == 3) return 1; else return 0; } FIST, Multi Media University
27. 27. implementationHow to check for Empty? Int Queue :: Empty( ) { if (Tail == -1) return 1; else return 0; } FIST, Multi Media University
28. 28. implementationHow to Add?Void Queue :: Addend(int data2add){ if (!Full( )) { Tail++; Queueelement[Tail] = data2add; } else cout << “Queue is Full!!!”<<endl;} FIST, Multi Media University
29. 29. implementationHow to Delete?int Queue :: Serve( ){int data2remove; if (!Empty( )) { data2remove = Queueelement[0]; for ( i =0;i<Tail;i++) Queueelement[i] = Queueelement[i+1] Tail--; return data2remove; } else{ cout << “Queue is Empty!!!”<<endl; Methods FIST, Multi Media University