901230 lecture5&6
DESCRIPTION
stackTRANSCRIPT
![Page 1: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/1.jpg)
FIST, Multi Media University
Lecture 5
• Stack (Array Implementation)
• Queue (Array Implementation )
![Page 2: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/2.jpg)
FIST, Multi Media University
Stack
What 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
![Page 3: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/3.jpg)
FIST, Multi Media University
Stack
How 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.
Operations...
![Page 4: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/4.jpg)
FIST, Multi Media University
Stack
A
B
C
D
E TOPF
GEG - Plates
Books
(LIFO)
![Page 5: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/5.jpg)
FIST, Multi Media University
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 stack is full, TOP = maxstacksize
![Page 6: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/6.jpg)
FIST, Multi Media University
Stack - implementation
• Uses ARRAY to represent a stack
Class stack
{ private :
int stackelement[10],TOP;
public:
stack( ) { top = -1}
void push(int);
int Pop( );
int Empty( );int Full ( );};
![Page 7: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/7.jpg)
FIST, Multi Media University
Stack
How to Push?Void stack :: Push(int data2add)
{ if (!Full( ))
{ TOP++;
Stackelement[TOP] = data2add;
}
else
cout << “Stack is Full!!!”<<endl;
}
![Page 8: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/8.jpg)
FIST, Multi Media University
Stack
How to Pop?Int stack :: Pop( )
{int data2remove;
if (!Empty( ))
{ data2remove = Stackelement[TOP];
TOP--; return data2remove; }
else{ cout << “Stack is Empty!!!”<<endl;
return 0;
} }
![Page 9: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/9.jpg)
FIST, Multi Media University
Stack
How to check for full?
Int Stack :: Full( )
{
if (TOP==9) return 1;
else return 0;
}
![Page 10: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/10.jpg)
FIST, Multi Media University
Stack
How to check for Empty?
Int Stack :: Empty( )
{
if (TOP== -1) return 1;
else return 0;
}
![Page 11: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/11.jpg)
FIST, Multi Media University
Stack
Void main( )
{
stack stackobj;
stackobj.Push(23);
stackobj.Push(46);
stackobj.Push(37);
stackobj.Push(10);
stackobj.Push(55);cout << stackobj.Pop( );cout << stackobj.Pop( );}
23
3746
1055
55
10
![Page 12: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/12.jpg)
FIST, Multi Media University
Applications of Stack
We shall see two applications of the stack
1) Use of Stacks in Function Calls
2) Palindrome Checking
![Page 13: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/13.jpg)
FIST, Multi Media University
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 includes
Parameters
Caller’s state information
Local variables
Temporary storage
This structure should LIFO because , when a function terminates, the function with which to resume execution is the last function whose activation record was saved.
![Page 14: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/14.jpg)
FIST, Multi Media University
Stack
Function1
Function2
Function3
Function4
Activation Record
Function1
Function2
Function3
![Page 15: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/15.jpg)
FIST, Multi Media University
Palindrome Checking
What 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.
![Page 16: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/16.jpg)
FIST, Multi Media University
Palindrome
Here are some palindromes
MALAYALAM (a south Indian language)
RATS LIVE ON NO EVIL STAR
DAD
POP
RADAR
STEP ON NO PETS
MADAM
![Page 17: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/17.jpg)
FIST, Multi Media University
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;
}
![Page 18: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/18.jpg)
FIST, Multi Media University
implementation
char pop( )
{ char num;
if (!empty())
{
num = stack[topstack];
topstack--; return num;
}
else {cout<<"Stack is Empty"<<endl;
return '0';}
}
};
![Page 19: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/19.jpg)
FIST, Multi Media University
implementation
void 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 palindromes
else
{
![Page 20: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/20.jpg)
FIST, Multi Media University
implementation
i=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;
}
![Page 21: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/21.jpg)
FIST, Multi Media University
Queue
What 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
![Page 22: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/22.jpg)
FIST, Multi Media University
How 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 as FIFO
4) It can be maintained with two pointers namely FRONT (REMOVE) and REAR ( ADD).
Queue
![Page 23: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/23.jpg)
FIST, Multi Media University
Queue
Front Rear
EG - Bank Counter
![Page 24: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/24.jpg)
FIST, Multi Media University
Operations
There 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
![Page 25: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/25.jpg)
FIST, Multi Media University
implementation
Uses ARRAY to represent a Queue
Class Queue
{ private :
int Queueelement[4],Head ,Tail;
public:
Queue( ) { Head =0 ; Tail = -1}
void Addend(int);
int Serve( );
int Empty( );int Full ( );};
![Page 26: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/26.jpg)
FIST, Multi Media University
implementation
How to check for full?
Int Queue :: Full( )
{
if (Tail == 3) return 1;
else return 0;
}
![Page 27: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/27.jpg)
FIST, Multi Media University
implementation
How to check for Empty?
Int Queue :: Empty( )
{
if (Tail == -1) return 1;
else return 0;
}
![Page 28: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/28.jpg)
FIST, Multi Media University
implementation
How to Add?
Void Queue :: Addend(int data2add)
{ if (!Full( ))
{ Tail++;
Queueelement[Tail] = data2add;
}
else
cout << “Queue is Full!!!”<<endl;
}
![Page 29: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/29.jpg)
FIST, Multi Media University
implementation
How 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;
return 0; } } Methods
![Page 30: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/30.jpg)
FIST, Multi Media University
implementation
Void main( )
{
Queue queueobj;
queueobj.Addend(23);
queueobj.Addend(46);
queueobj.Addend(37);
queueobj.Addend(10);
queueobj.Addend(55);
cout << queueobj.Serve();cout << queueobj.Serve();}
23 46 37 10 55
23 46
![Page 31: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/31.jpg)
FIST, Multi Media University
Serving
Three Methods of Serving1) 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 queues
One of the application of Queues is simulation
![Page 32: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/32.jpg)
FIST, Multi Media University
Lecture 6
• Cyclic Queues
![Page 33: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/33.jpg)
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 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 .
![Page 34: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/34.jpg)
FIST, Multi Media University
Cyclic Queue
Head Tail
0 1 2 3 4 5
The following actions will cause the “head” and “tail” tomove through the array as listed below:
Example: Queue implemented using an array of size 6 with 4 data items inserted in to the queue
![Page 35: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/35.jpg)
FIST, Multi Media University
Cyclic Queue
Action Head Tail
0 3
Delete 1 3
Insert 1 4
Delete 2 4
Insert 2 5
Insert 2 0
Delete 3 0
Delete 4 0
Head Tail
0 1 2 3 4 5
![Page 36: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/36.jpg)
FIST, Multi Media University
Cyclic Queue
Action Head Tail
Insert 4 1
Delete 5 1
Insert 5 2
Delete 0 2
Note: 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”.
Head Tail
0 1 2 3 4 5
![Page 37: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/37.jpg)
FIST, Multi Media University
Cyclic Queue
The wrapping around can be achieved by updating the pointers as follows:
Delete: head = (head+1) % n
Insert: tail = (tail + 1) % n
i.e., Whenever a pointer is incremented, take modulo n.
The above operations is needed to check if the queue is empty or full.
![Page 38: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/38.jpg)
FIST, Multi Media University
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
Tail HeadHead ,Tail
0 1 2 3 4 5 0 1 2 3 4 5
Only one element Queue Empty Queue
![Page 39: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/39.jpg)
FIST, Multi Media University
Cyclic Queue
For an empty queue, we have the condition
head = (tail + 1) % n
The above condition is also satisfied when the queue is full
We 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.
![Page 40: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/40.jpg)
FIST, Multi Media University
implementation
How 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;
}
![Page 41: 901230 lecture5&6](https://reader036.vdocuments.us/reader036/viewer/2022081401/556a3cc1d8b42a4a1e8b4736/html5/thumbnails/41.jpg)
FIST, Multi Media University
implementation
How 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 Queue"<<endl; }