0
Lecture 5• Stack (Array Implementation)• Queue (Array Implementation )                FIST, Multi Media University
StackWhat is a Stack?  1) It is a data structure for storing some values  2) It is an ADT.  3) Implemented using Arrays or...
StackHow to Store and Retrieve from a Stack? 1) All Insertions ( Storing) and Deletions (Removing) of entries are made at ...
Stack    E                                       TOPF    D                          EG - PlatesG    C                     ...
Operations• There are Four operations :     1) Push ( )             used to add the data at the top of the stack.         ...
Stack - implementation• Uses ARRAY to represent a stack  Class stack  { private :      int stackelement[10],TOP;    public...
StackHow to Push?  Void stack :: Push(int data2add)  { if (!Full( ))     { TOP++;       Stackelement[TOP] = data2add;     ...
StackHow to Pop?  Int stack :: Pop( )  {int data2remove;   if (!Empty( ))        { data2remove = Stackelement[TOP];       ...
StackHow to check for full? Int Stack :: Full( ) {     if (TOP==9) return 1;     else return 0; }                 FIST, Mu...
StackHow to check for Empty? Int Stack :: Empty( ) {    if (TOP== -1) return 1;    else return 0; }              FIST, Mul...
StackVoid main( ){                                 stackobj.Push(55);stack stackobj;                   cout << stackobj.Po...
Applications of StackWe shall see two applications of the stack1) Use of Stacks in Function Calls2) Palindrome Checking   ...
Use of Stacks in Function Calls• Whenever a function begins execution an  ACTIVATION RECORD (Stack Frame) is created     •...
StackActivation Record    Function4    Function3                          Function3    Function2                          ...
Palindrome CheckingWhat is a palindrome? A palindrome is a word/sentence that is the same if read both ways. You might hav...
Palindrome• Here are some palindromesMALAYALAM (a south Indian language)RATS LIVE ON NO EVIL STARDADPOPRADARSTEP ON NO PET...
implementation#include <iostream.h>#include <string.h>class ADTstack{ char stack[10];   int topstack;   public: ADTstack( ...
implementationchar pop( ){ char num; if (!empty()){     num = stack[topstack];     topstack--; return num;} else {cout<<"S...
implementationvoid main(){ADTstack st;char str[10];int palin, l,i;     cout<<"type in a string to check if it is a palindr...
implementationi=0;        while(str[i]!=0) //push the string into stack                 { st.push(str[i]);                ...
QueueWhat is a Queue?  1) It is a data structure for storing some values  2) It is an ADT.  3) Implemented using Arrays or...
QueueHow to Store and Retrieve from a Queue? 1) A Data Structure in which all Additions to the list are   made at one end,...
QueueEG - Bank Counter   Front                                        Rear                FIST, Multi Media University
OperationsThere are Four operations :         1) Addend ( )                   used to add the data at the Tail of the Queu...
implementationUses ARRAY to represent a Queue  Class Queue { private :     int Queueelement[4],Head ,Tail;   public:      ...
implementationHow to check for full? Int Queue :: Full( ) {    if (Tail == 3) return 1;    else return 0; }               ...
implementationHow to check for Empty? Int Queue :: Empty( ) {    if (Tail == -1) return 1;    else return 0; }            ...
implementationHow to Add?Void Queue :: Addend(int data2add){ if (!Full( ))   { Tail++;     Queueelement[Tail] = data2add; ...
implementationHow to Delete?int Queue :: Serve( ){int data2remove; if (!Empty( ))      { data2remove = Queueelement[0];   ...
implementationVoid main( ){                            queueobj.Addend(55);                             cout << queueobj.S...
Serving  Three Methods of Serving  1) increment head to 1     runs out of storage  2) when the first person has be served ...
Lecture 6• Cyclic Queues              FIST, Multi Media University
Cyclic Queues What is a Cyclic Queue?       we need to imagine that the array allocated to the queue       is not linear b...
Cyclic QueueExample: Queue implemented using an array of size 6 with 4 data items                                         ...
Cyclic QueueAction   Head               Tail          0                   3Delete    1                   3Insert    1     ...
Cyclic QueueAction            Head                TailInsert             4                   1   Head     TailDelete      ...
Cyclic QueueThe wrapping around can be achieved by updating thepointers as follows:Delete: head = (head+1) % nInsert: tail...
Cyclic Queue Consider when there is only one element in the queue• In this case, both the pointers coincide (head == tail)...
Cyclic QueueFor an empty queue, we have the condition       head = (tail + 1) % nThe above condition is also satisfied whe...
implementationHow to Add?void Addend(int x){if (!Full()){  tail=(tail+1)%MAX;  a[tail]=x;  count++;  cout<<x<<" added to t...
implementationHow to Delete?int Serve(){ int x;if (!Empty()){  x=a[head];  head=(head+1)%MAX;  count--;  cout<<x<<" served...
Upcoming SlideShare
Loading in...5
×

901230 lecture5&6

566

Published on

stack

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
566
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "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
  30. 30. implementationVoid main( ){ queueobj.Addend(55); cout << queueobj.Serve();Queue queueobj; cout << queueobj.Serve();queueobj.Addend(23); }queueobj.Addend(46);queueobj.Addend(37);queueobj.Addend(10); 23 46 37 10 55 23 FIST, Multi Media University 46
  31. 31. Serving Three Methods of Serving 1) increment head to 1 runs out of storage 2) when the first person has be served the remaining persons in the Queue shuffle forward one place. Can serve unlimited number of persons as long as the queue never exceeds the maximum size 3) Use cyclic queuesOne of the application of Queues is simulation FIST, Multi Media University
  32. 32. Lecture 6• Cyclic Queues FIST, Multi Media University
  33. 33. Cyclic Queues What is a Cyclic Queue? we need to imagine that the array allocated to the queue is not linear but cyclic, i.e. that it forms a circle. EG: So that if for instance 10 elements are allocated to the queue with the element positions running from 0 to 9 then when the tail gets to position 9 the next element to be added can go to position 0 providing that position has already been “served” (vacant)..Thus the two problems of the queues are solved . FIST, Multi Media University
  34. 34. Cyclic QueueExample: Queue implemented using an array of size 6 with 4 data items inserted in to the queue Head Tail 0 1 2 3 4 5The following actions will cause the “head” and “tail” tomove through the arrayMulti Media University FIST, as listed below:
  35. 35. Cyclic QueueAction Head Tail 0 3Delete 1 3Insert 1 4Delete 2 4 Head TailInsert 2 5Insert 2 0Delete 3 0Delete 4 0 0 1 2 3 4 5 FIST, Multi Media University
  36. 36. Cyclic QueueAction Head TailInsert 4 1 Head TailDelete 5 1Insert 5 2Delete 0 2 0 1 2 3 4 5Note: A Cyclic queue will always be of fixed size (say n) and the size information must be available to update the pointers “head” and “tail”. FIST, Multi Media University
  37. 37. Cyclic QueueThe wrapping around can be achieved by updating thepointers as follows:Delete: head = (head+1) % nInsert: tail = (tail + 1) % ni.e., Whenever a pointer is incremented, take modulo n.The above operations is needed to check if the queue is empty or full. FIST, Multi Media University
  38. 38. Cyclic Queue Consider when there is only one element in the queue• In this case, both the pointers coincide (head == tail)• The queue becomes empty when the element is deleted Head ,Tail Tail Head 0 1 2 3 4 5 0 1 2 3 4 5 Empty Queue Only one elementFIST, Multi Media University Queue
  39. 39. Cyclic QueueFor an empty queue, we have the condition head = (tail + 1) % nThe above condition is also satisfied when the queue is fullWe resolve the ambiguity between two cases if the condition head = (tail+1)% n is satisfied up on deleting an item from the queue, then queue is empty if the condition head = (tail + 1) % n is satisfied up on inserting an item in to the queue, then queue is full. FIST, Multi Media University
  40. 40. implementationHow to Add?void Addend(int x){if (!Full()){ tail=(tail+1)%MAX; a[tail]=x; count++; cout<<x<<" added to the queue"<<endl;}else cout<<"Full Queue"<<endl;} FIST, Multi Media University
  41. 41. implementationHow to Delete?int Serve(){ int x;if (!Empty()){ x=a[head]; head=(head+1)%MAX; count--; cout<<x<<" served from the queue"<<endl; return x;}else cout<<"Empty FIST, Multi Media University Queue"<<endl; }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×