Data Structures and Algorithms (R) : Lab Manual Week 2 - Lab 01
Data Structures and Algorithms (R) : Lab Manual Week 2 - Lab 01
Lab Manual
Week 2 Lab 01
Observe that EnQueue() function complexity saves the empty space in an array.
Sample Code
/*Circular Queue class holds necessary variables and functions to implement circular
queue using array*/
class CircularQueue{
private:
int Array[10]; //Array to hold 10 elements
int front; //points to the start of the queue
int rear; //points to the last of the queue
public:
/*All functions will be implemented here*/
};
Algorithm
1. Start the program
2. To insert an element,
Step-i: If "rear" of the queue is pointing to the last position then go to step-ii or else step-
iii
Step-iv:
Step iv-a: If the "front" points where "rear" is pointing and the queue holds a
not NULL value for it, then its a "queue overflow" state, so quit; else go to step
iv-b
Step iv-b. Insert the new value for the queue position pointed by the "rear"
Step-iii: If the "front" is pointing to the last position of the queue then step-iv else go to step-
v
Step-iv: Make the "front" point to the first position in the queue and quit
Sample Output
Home Task
Use the queue data structure to implement a customer shopping queue at a grocery store. For each
customer being added to the queue call the enqueue() function. Each customer should carry a name and
the number of items he shopped for which should be displayed using display() function. At the store
counter a separate function should calculate their bill based on the number of items and display it. Use
dequeue() function to remove the customer after being billed. Call other functions of queues as required
in the main method.
Data Structures and algorithms (F)
Lab Manual
Week 2 Lab 02
Learn about stacks i.e. a stack is a list of homogenous elements in which the addition and
deletion of elements occurs only at one end, called the top of the stack.
Implementation of Stack
There can be an array implementation of stacks. The methods for creating and printing stacks can also be
implemented with the help of arrays.
/* prog1.c */
/* array implementation of stacks */
#include <iostream.h>
class stack
{
public:
int array[20];
int top_element, max;
void stack::intial(int a)
{
top_element = -1;
max = a - 1;
}
bool stack::empty()
{
return top_element == -1;
}
bool stack::full()
{
return top_element == max;
}
Lab Tasks
1. Using prog1.c, do the following:
e. In the main method, call the display() function implemented in part a and display the stack from
top to bottom of the stack.
f. Implement pop() method to remove elements from the top of the stack.
g. In the main method, call the pop() method 3 times to remove first 3 elements from the stack.
h. Implement top() method to display the element at the top of the stack only.
i. In the main method, call the top() method to check the element at the top of the stack.
j. Implement a while loop in the main method. Inside the while loop: display the element at the
top of the stack and then remove the top most element. Use the while loop to remove all
elements from the stack.
k. In the main method, call display()function to display the stack. The display function should
appropriately display that the stack is empty.
2. Implement a parenthesis checker using stacks. Parenthesis checker checks if the parenthesis in a
user defined algebraic expression are balanced or not. Display appropriate message in either case.
(HINT: Combine the methods of top() and pop() for this program)
3. Create a stack to determine if a user defined string is a palindrome (i.e. a string that is spelled
identically backward and forward). The program should ignore spaces and punctuation marks.
Home Task
Hint (ALGORITHM):
4) If it is an operator, then
iii) If it has higher priority than the top of stack, push operator on stack.
iv) Else pop the operator from the stack and output it, repeat step 4
5) If it is a closing parenthesis, pop operators from stack and output them until an opening
parenthesis is encountered. Pop and discard the opening parenthesis.
Sample Input:
(a + b)*(c d/ e) + f
Sample Output:
ab+cde/*f+