Professional Documents
Culture Documents
Recursion
Stacks
Queues
Recursion
Now let’s examine how recursive calls work. For that we’ll look under the hood of functions.
• The execution context is an internal data structure that contains details about the execution of a function: where
the control flow is now, the current variables, the value of this (we don’t use it here) and few other internal
details.
• One function call has exactly one execution context associated with it.
Any recursive function can be rewritten into an iterative one. And that’s sometimes required to optimize stuff. But for
many tasks a recursive solution is fast enough and easier to write and support.
If base case (condition) is not reached or not defined, then stack overflow problem may arise.
Function to calculate sum of all digits using Iteration
For example,
A stack can be helpful when you need to temporarily store an element that is going to be used in the immediate steps of
your program. For instance, programming languages usually use a stack structure to pass variables to functions. What they
do is store (or push) the function arguments in the stack and then jump to the function where they remove and retrieve
(or pop) the same number of elements from the stack. That way the size of the stack is dependent of the number of nested
calls of functions. Additionally, after a function has been called and finished what it was doing, it leaves the stack in the
exact same condition as before it has being called! That way any function can operate with the stack ignoring how other
functions operate with it.
stack.h
#define SIZE 5
class CStack
{
private:
int num[SIZE];
int top;
public:
CStack(); //defualt constructor
int push(int);
int pop();
int isEmpty();
int isFull();
void displayItems();
};
stack.cpp
#include "pch.h"
#include <iostream>
#include "stack.h"
using namespace std;
CStack::CStack() {
top = -1;
}
int CStack::isEmpty() {
if (top == -1)
return 1;
else
return 0;
}
int CStack::isFull() {
if (top == (SIZE - 1))
return 1;
else
return 0;
}
int CStack::push(int n) {
//check stack is full or not
if (isFull()) {
return 0;
}
++top;
num[top] = n;
return n;
}
int CStack::pop() {
//to store and print which number is deleted
int value;
//check for empty
if (isEmpty())
return 0;
value = num[top];
--top;
return value;
}
void CStack::displayItems() {
#include "pch.h"
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
CStack stk;
int choice, n, temp;
do
{
cout << endl;
cout << "0 - Exit." << endl;
cout << "1 - Push Item." << endl;
cout << "2 - Pop Item." << endl;
cout << "3 - Display Items (Print STACK)." << endl;
switch (choice) {
case 0: break;
case 1:
cout << "Enter item to insert: ";
cin >> n;
temp = stk.push(n);
if (temp == 0)
cout << "STACK is FULL." << endl;
else
cout << temp << " inserted." << endl;
break;
case 2:
temp = stk.pop();
if (temp == 0)
cout << "STACK IS EMPTY." << endl;
else
cout << temp << " is removed (popped)." << endl;
break;
case 3:
stk.displayItems();
break;
default:
cout << "An Invalid choice." << endl;
}
} while (choice != 0);
}
Queue
Queue is a collection of the same type of element. It is a linear list in which insertions can take place at one end of the
list,called rear of the list, and deletions can take place only at other end, called the front of the list.
For example
For GUI (Graphical User Interface) Application, events generated by keyboard,mouse input devices are sent to Application
Message Queue.
Message Pump implementation of Application Fetch message from queue and dispatched for event handling registerd
(subscribed ) in Application.
queue.h
#define MAX 5
class CQueue
{
private:
int front, rear;
int data[MAX];
public:
CQueue() //To Initalize queue
{
front = 0;
rear = -1;
}
int isFull();
int isEmpty();
void insertQueue(int item);
int deleteQueue(int *item);
};
queue.cpp
#include "pch.h"
#include <iostream>
#include "queue.h"
using namespace std;
#include "pch.h"
#include <iostream>
#include "queue.h"
using namespace std;
int main()
{
int item = 0;
CQueue q = CQueue();
q.insertQueue(10);
q.insertQueue(20);
q.insertQueue(30);
q.insertQueue(40);
if (q.deleteQueue(&item) == 0)
cout << "\nDeleted item : " << item;
if (q.deleteQueue(&item) == 0)
cout << "\nDeleted item : " << item;
if (q.deleteQueue(&item) == 0)
cout << "\nDeleted item : " << item;
if (q.deleteQueue(&item) == 0)
cout << "\nDeleted item : " << item;
if (q.deleteQueue(&item) == 0)
cout << "\nDeleted item : " << item;
return 0;
}
Priority Queue
For example, many operating systems used a scheduling algorithm where the nexe task executed in the onle with the
shortest execution time or the hightst priority
For another example, consider the problem of sorting a file of data representing persons.
priority.h
#include <iostream>
using namespace std;
#define MAX 5
public:
CPriorityQueue();
void insertWithPriority(int priority, int item);
int GetNext(int *item);
int PeekAtNext(int *item);
};
priority.cpp
#include "pch.h"
#include "priorityqueue.h"
CPriorityQueue::CPriorityQueue()
{
int i = 0;
count = 0;
//All priority value set to -1
for (i = 0; i < MAX; i++)
{
data[i][1] = -1;
}
}
data[i][0] = item;
data[i][1] = priority;
count++;
cout << "\nInserted item is : " << item;
}
//Remove & get element with highest priority in queue
if (count == 0)
{
cout << "\nPriority Queue is underflow";
return -1;
}
max = data[0][1];
for (i = 1; i < MAX; i++)
{
if (max < data[i][1])
{
pos = i;
max = data[i][1];
}
}
*item = data[pos][0];
data[pos][1] = -1;
count--;
return 0;
}
max = data[0][1];
for (i = 1; i < MAX; i++)
{
if (max < data[i][1])
{
pos = i;
max = data[i][1];
}
}
*item = data[pos][0];
return 0;
}
main.cpp
#include "pch.h"
#include "priorityqueue.h"
#include <iostream>
int main()
{
int item;
CPriorityQueue q = CPriorityQueue();
q.insertWithPriority(1, 10);
q.insertWithPriority(2, 20);
q.insertWithPriority(3, 30);
q.insertWithPriority(4, 40);
q.insertWithPriority(5, 50);
q.insertWithPriority(6, 60);
if (q.PeekAtNext(&item) == 0)
cout << "\nPeek Item : " << item;
if (q.GetNext(&item) == 0)
cout << "\nItem : " << item;
if (q.PeekAtNext(&item) == 0)
cout << "\nPeek Item : " << item;
if (q.GetNext(&item) == 0)
cout << "\nItem : " << item;
if (q.GetNext(&item) == 0)
cout << "\nItem : " << item;
if (q.PeekAtNext(&item) == 0)
cout << "\nPeek Item : " << item;
if (q.GetNext(&item) == 0)
cout << "\nItem : " << item;
if (q.GetNext(&item) == 0)
cout << "\nItem : " << item;
if (q.GetNext(&item) == 0)
cout << "\nItem : " << item;
cout << endl;
}