You are on page 1of 7

Benha University Electrical Engineering Department

Benha Faculty of Engineering Data Structures and Algorithms


(E1324)

lab (1) Stack and Queue


LIST OF EXPERIMENTS
1. Implementation of Stack using Array.
2. Infix to postfix Conversion using Stack ADT.
3. Evaluation of postfix expression using Stack ADT.
4. Decimal to Binary Conversion using stack ADT.
5. Implementation circular Queue ADT using array.
INTRODUCTION
In computer science, a data structure is a way of storing and organizing data in a computer so that
it can be used efficiently. Different kinds of data structures are suited to different kinds of
applications, and some are highly specialized to certain tasks. Data structures are used in almost
every program or software system. Specific data structures are essential ingredients of many
efficient algorithms and make possible the management of huge amounts of data, such as large
databases and internet indexing services. Some formal design methods and programming
languages emphasize data structures, rather than algorithms, as the key organizing factor in
software design. Data structures are generally based on the ability of a computer to fetch and store
data at any place in its memory, specified by an address — a bit string that can be itself stored in
memory and manipulated by the program. Thus, the record and array data structures are based on
computing the addresses of data items with arithmetic operations; while the linked data structures
are based on storing addresses of data items within the structure itself. Many data structures use
both principles. Data Structure is defined as the way in which data is organized in the memory
location. There are 2 types of data structures:
Linear Data Structure:
In linear data structure all the data are stored linearly or contiguously in the memory. All the data
are saved in continuously memory locations and hence all data elements are saved in one
boundary. A linear data structure is one in which we can reach directly only one element
from another while travelling sequentially. The main advantage, we find the first element, and
then it’s easy to find the next data elements. The disadvantage, the size of the array must
be known before allocating the memory. The different types of linear data structures are:
Array Stack Queue Linked List
Non-Linear Data Structure:
Every data item is attached to several other data items in a way that is specific for reflecting
relationships. The data items are not arranged in a sequential structure. The various types of
nonlinear data structures are:
Trees
Graphs
STACK
The stack is a very common data structure used in programs. By data structure, we mean something
that is meant to hold data and provides certain operations on that data. Stacks hold objects, usually

1
all of the same type. The main property of a stack is that objects go on and come off of the top of
the stack. Here are the minimal operations we'd need for an abstract stack
Push: Places an object on the top of the stack.
Pop: Removes an object from the top of the stack and produces that object.
IsEmpty: Reports whether the stack is empty or not.
QUEUES
A queue is a first-in first-out data structure (FIFO). Conceptually, the queue data structure behaves
like a line. New data is placed at the rear of the queue and when data is removed it is taken from
the front. A printer maintains a list of jobs in a queue; the oldest job, the one that has been in the
queue the longest, is serviced first. The operations supported by a queue are as follows:
• enqueue - place object o on the rear of the queue
• dequeue() - remove and return the object at the front of the queue
• isEmpty() - return true if the queue contains no elements, false otherwise.

2
Experiment No:1.
IMPLEMENTATION OF STACK USING ARRAY
Aim
To write a C++ program to implement stack using array implementation and understand its basic
operations.
Objectives:
1) Understand the Stack Data Structure and its basic operators.
2) Understand the method of defining stack ADT and implement the basic operators.
3) Learn how to create objects from an ADT and invoke member functions.

Software Required: Code::Blocks


Theory:
A Stack is an ordered list in witch insertion and deletion are made at one end called the Top. The
restrictions on the stack imply that if we add the elements 1,2,3,4,5 to the stack in that order, then
5 is the first element we delete from the stack. The add operation is called “Push”, and the delete
operation is called “Pop”. Since the last element pushed into the stack is the first element popped,
a stack is also known as a Last In First Out (LIFO) List
Algorithm
Step 1: Start.
Step 2: Initialize stack size MAX and top of stack -1.
Step 3: Push integer element on to stack and display the contents of the stack.
if stack is full give a message as ‘Stack is Overflow’.
Step 4: Pop element from stack along with display the stack contents.
if stack is empty give a message as ‘Stack is Underflow’.
Step 5: Check the stack contents.
Step 6: Stop.
Program:
To be written by the student using the above algorithm.
Observations:
To be written by student after implementation of the algorithm.
1)Input:
2)Output:

3
Experiment No: 2
Infix to postfix Conversion using Stack ADT

Aim: To write C++ code to convert an expression in infix form to postfix form using Stack
ADT.
Objective:
1) Understand the use of Stack.
2) Understand how to import an ADT in an application program.
3) Understand the instantiation of Stack ADT in an application program.
4) Understand how the member functions of an ADT are accessed in an
application program.
Theory:
In high level programming languages, we use arithmetic expression in its infix form. An
expression in infix form contains operators in between operands on which it operates. Parentheses
also appear in infix expressions to specify the order of evaluation. During compilation, the
compiler converts the infix expression to postfix for easy evaluation, since a postfix expression
does not contain any parenthesis. Also, a postfix expression can be evaluated easily by using a
stack.
Postfix notation has the following virtues:
No parenthesis.
The priority of the operations is no longer relevant.
Enables easy evaluation (evaluated by making a left to right scan, stacking the operands.)

An infix expression can be manually converted to its post-fix form by following these steps:
Step1: Fully parenthesis the expression.
Step2: Move all operators so that they replace their corresponding right parenthesis.
Step3: Delete all parentheses.

Program:
To be written by the student using the above algorithm.
Observations:
To be written by student after implementation of the algorithm.
1) Input:
2)Output

4
Experiment No: 3
Evaluation of postfix expression using Stack ADT
Aim: To write C++ code to evaluate a postfix expression using stack ADT.
Objective:
1) Understand the use of Stack.
2) Understand importing an ADT in an application program.
3) Understand the instantiation of Stack ADT in an application program.
4) Understand how the member functions of an ADT are accessed in an
application program.

Theory:
An infix expression in a High Level Language program is converted into its postfix form on its
compilation time, since the evaluation of a postfix expression is much simpler than direct
evaluation of an infix expression. The postfix expression is evaluated using Stack. Following is
the method for evaluation postfix expressions:
1) Create a stack to store operands.
2) Scan the given expression and do following for every scanned element.
a) If the element is a number, push it into the stack.
b) If the element is an operator, pop operands for the operator from stack.
Evaluate the operator and push the result back to the stack.
3) When the expression is ended, the number in the stack is the final answer

Program:
To be written by the student
Observations:
To be written by student after implementing the algorithm.
1) Input:
2) Output:

5
Experiment No: 4
Decimal to Binary Conversion using stack ADT
Aim:
To write C++ code to convert a decimal number to its binary equivalent using Stack ADT.
Objectives:
1) Understand the use of Stack.
2) Understand the method of importing an ADT in an application program.
3) Understand accessing of member functions of an ADT.
Theory:
As stack is a LIFO data structure, we can retrieve the content of stack in the reverse order of
storing. This property of stack is used in this application. To convert a decimal number to its binary
equivalent, we repeatedly divide the decimal number by 2 and the remainder of division (either 0
or 1) is pushed in to the stack, until the number becomes zero. When the division is completed,
pop out all the elements from the stack, which will give the binary equivalent.
Program:
To be written by the student
Observations:
To be written by student after implementing the algorithm.
1)Input:
2) Output:

H.W

1- Write C++ program that uses a stack to test for balanced bracket pairs
2- Consider the expression (2+ 3*(5 – 2) + 3*2) How do we write a program
to find the answer to the above expression?
3- Write the above expression in postfix notation Use a stack to evaluate it.

6
Experiment No: 5
Circular Queue ADT using array

Aim: To write C++ code to implement Queue ADT and test its features.
Objective:
1. Understand the Queue data structure and its basis operations.
2. Understand the method of defining Queue ADT and its basic operations.
3. Learn how to create objects from an ADT and member function are invoked.
Theory:
A Queue is an ordered list in which all insertion takes place at one end and all deletions take place
at the opposite end. Since the first element removed is the first element inserted, queues are also
known as First In First Out (FIFO) lists. The end at which insertions are taken place is called ‘rear’
and the end at which deletions take place is called ’front’. In a standard queue data structure re-
buffering problem occurs for each dequeue operation. That means, the queue tells it is full even
though there are empty locations. This problem is solved by joining the front and rear ends of the
queue to make the queue as a circular queue. When rear == MaxSize-1 the next element is entered
at queue[0] in case that is empty.

Queues are frequently used in computer programming, and one example is the creation of a job
queue by an Operating System. If the OS does not use priorities, the jobs are processed in the order
they enter the system.

Algorithm:

Step 1: Start.
Step 2: Initialize queue size to MAX.
Step 3: Insert the elements into circular queue. If queue is full give a message as ‘queue is
overflow”
Step 4: Delete an element from the circular queue. If queue is empt y give a message as ‘queue is
underflow’.
Step 5: Display the contents of the queue.
Step 6: Stop.

Program: To be written by the student using the above algorithm.


Observations: To be written by student after implementing the algorithm.
1)Input:
2)Output:

You might also like