Lab1,2 Stack Queue
Lab1,2 Stack Queue
Lab1,2 Stack Queue
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.
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.