0% found this document useful (0 votes)
103 views8 pages

Data Structures and Algorithms (R) : Lab Manual Week 2 - Lab 01

The document discusses implementing a circular queue and stack using arrays. It provides sample code for a CircularQueue class with functions like Enqueue(), Dequeue() etc. and explains the algorithm for inserting and removing elements. For stacks, it shows an array implementation with functions like push(), pop(), top() etc. and provides tasks to implement these functions along with applications like checking balanced parentheses and palindrome strings. The homework asks to implement a stack to convert infix to postfix notation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views8 pages

Data Structures and Algorithms (R) : Lab Manual Week 2 - Lab 01

The document discusses implementing a circular queue and stack using arrays. It provides sample code for a CircularQueue class with functions like Enqueue(), Dequeue() etc. and explains the algorithm for inserting and removing elements. For stacks, it shows an array implementation with functions like push(), pop(), top() etc. and provides tasks to implement these functions along with applications like checking balanced parentheses and palindrome strings. The homework asks to implement a stack to convert infix to postfix notation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures and Algorithms(R)

Lab Manual
Week 2 Lab 01

IMPLEMENTING CIRCULAR QUEUE USING


ARRAYS

Session: SPRING 2013

Faculty of Information Technology


UCP Lahore Pakistan
Objectives

Implementation of Circular Queue using arrays.

Observe that EnQueue() function complexity saves the empty space in an array.

Implementation of Circular Queue

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-ii: make the "rear" value as 0

Step-iii: increment the "rear" value by one

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"

3. To delete the particular item from circular queue


Step-i: If the queue is empty then say "empty queue" and quit; else continue

Step-ii: Delete the "front" element

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

Step-v: Increment the "front" position by one.

4. Terminate the program.

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

IMPLEMENTING STACK USING ARRAYS

Session: SPRING 2013

Faculty of Information Technology


UCP Lahore Pakistan
Objectives

Learn how to implement a stack as an array.

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.

Examine various stack operations.

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 intial(int a);


bool empty();
bool full();
void push(int element);
void pop(); // implement this method yourself
int top(); // implement this method yourself
void display(); // implement this method yourself
};

// initialize the size of the stack

void stack::intial(int a)
{
top_element = -1;
max = a - 1;
}

// check if the stack is empty

bool stack::empty()
{
return top_element == -1;
}

// check if the stack is full

bool stack::full()
{
return top_element == max;
}

// push method to insert an element in the stack

void stack::push(int element)


{
if(full())
{
cout<<"Error: the Stack is full"<<endl;
}
else
{
top_element++;
array[top_element] = element;
}
}

Lab Tasks
1. Using prog1.c, do the following:

a. Implement a function display() to display the stack.


b. In the main method, create an object s1 for stack class.
c. In the main method, use the stack object s1 created in part b to call the function initial(), pass the
size of the array you want to create to this function.
d. In the main method, use the object s1 to call the push() function to initialize the locations of the
stack array. You will have to call this function as many times as is the size of the array created.
Fill the stack with user defined elements.

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

IMPLEMENT STACK AND USE IT TO CONVERT INFIX TO POSTFIX


EXPRESSION.

Always scan given expression from left to right.

Hint (ALGORITHM):

1) Examine the next element in the input.

2) If it is operand, output it.

3) If it is opening parenthesis, push it on stack.

4) If it is an operator, then

i) If stack is empty, push operator on stack.

ii) If the top of stack is opening parenthesis, push operator on stack

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.

6) If there is more input go to step 1

7) If there is no more input, pop the remaining operators to output.

Sample Input:

(a + b)*(c d/ e) + f

Sample Output:
ab+cde/*f+

You might also like