Professional Documents
Culture Documents
122AX051 Sujeet D DS Lab Exp 1-5
122AX051 Sujeet D DS Lab Exp 1-5
: 59 Batch: B3
Branch: CSE(IoT)
Date of Performance: 21st July, 2023 Subject: Data Structure Experiment No. : 1
Aim:
To write a program for calculator using switch case.
Theory:
C language is the base of all programming languages. The operating system (OS) is built using C.
An entity that may vary during the program execution is called a variable.
Syntax is a combination of data type and variable name. eg. int a; int z; etc.
Types of variable:
i) Global
ii) Local
A word whose meaning has already been explained to the compiler is known as Keywords.
An entity does not vary during the program execution is called a Constant.
Operators:
i) Increment and decrement operators: It is used to increment the value of a variable by 1. It is used to
decrease the operand values by 1. The increment operator is represented as the double plus (++) symbol.
The decrement operator is represented as the double minus (--) symbol.
Name: Vedant S Shetty Roll No. : 59 Batch: B3
Branch: CSE(IoT)
Date of Performance: 21st July, 2023 Subject: Data Structure Experiment No. : 1
ii) Bitwise operators: This is an operator used to perform bitwise operations on bit patterns or binary numerals
that involve the manipulation of individual bits. These are used in: Communication stacks where the
individual bits in the header attached to the data signify important information.
iii) Assignment operators: In this, = assigns the value of its right-hand operand to a variable, a property, or an
indexer element given by its left-hand operand. The result of an assignment expression is the value assigned
to the left-hand operand.
iv) Logical operators: These are generally used for combining two or more relational statements. They return
Boolean values. The logical operators are used primarily in the expression evaluation to make a decision.
These operators allow the evaluation and manipulation of specific bits within the integer.
v) Relational operators: These are the operators used to create a relationship and compare the values of two
operands
vi) Special operators: In the C programming language, special operators are used to perform specific operations
that cannot be done with normal arithmetic or logical operators. These operators are special because they
have their own unique syntax and functionality.
vii) Conditional operators: This is the one and only ternary operator in the C programming language. It can be
used as an alternative for if-else condition if the 'if else' has only one statement each
viii) Arithmetic Operators: These are the type of operators in C that are used to perform mathematical
operations in a C program. They can be used in programs to define expressions and mathematical formulas.
Name: Vedant S Shetty Roll No. : 59 Batch: B3
Branch: CSE(IoT)
Date of Performance: 21st July, 2023 Subject: Data Structure Experiment No. : 1
Program:
Name: Vedant S Shetty Roll No. : 59 Batch: B3
Branch: CSE(IoT)
Date of Performance: 21st July, 2023 Subject: Data Structure Experiment No. : 1
Output:
Result:
Implemented Successfully
Name: Vedant S Roll No. : Batch:
Branch:
Aim:
Write a menu driven C program for implementing stack using array.
Theory:
STACK
A stack is an abstract data type that represents a collection of elements with a particular
It is an ADT.
Stack is an LIFO.
It is an ordered list of the same type of element
It is a linear list
In stack, insertion and deletion operation is performed at one end of the stack, called as the stack pointer,
or their top of stack
OPERATIONS ON STACK
1. INITIALISATION
It is used to initialise an empty stack
void init (stack *s)
{
s->top=-1;
}
2. EMPTY
It is used to check whether the stack is empty or not.
Branch:
3. FULL
It is used to check whether the stack is full or not
Returns 1 if the stack is full
Returns 0 if the stack is not full
4. PUSH
It is used to insert an element into the stack
5. POP
It is used to delete an element from stack
6. PRINT
IT IS USED TO DISPLAY THE STACK
Name: Vedant S Roll No. : Batch:
Branch:
1. STACK
2. CHECK FOR STACK OVERFLOW
IF (TOP==MAX-1)
3. OTHERWISE
TOP=TOP+1
4. INSERT THE ELEMENT
STACK [TOP]=ELEMENT
5. RETURN
6. STOP
Branch:
4. DECREMENT
TOP TOP=TOP-1
5. RETURN
6. STOP
Program:
Name: Vedant S Roll No. : Batch:
Branch:
Branch:
Output:
Result:
Implemented Successfully
Name: Vedant S Roll No. : Batch:
Branch:
Aim:
Write a program in C to convert an Infix expression to a Postfix expression using Stack.
Theory:
1. Infix
The typical mathematical form of expression that we encounter generally is known as infix notation. In infix form, an
operator is written in between two operands.
An expression in the form of a * ( b + c ) / d is in infix form. This expression can be simply decoded as: “add b and c, then
multiply the result by a, and then divide it by d for the final answer.”
2. Prefix
In prefix expression, an operator is written before its operands. This notation is also known as “polish notation”.
The above expression can be written in the prefix form as / * a + b c d. This type of expression cannot be simply decoded
as infix expressions.
3. Postfix
In postfix expression, an operator is written after its operands. This notation is also known as “reverse polish notation”.
The above expression can be written in the postfix form as a b c + * d /. This type of expression cannot be simply
decoded as infix expressions.
Name: Vedant S Roll No. : Batch:
Branch:
In infix expressions, the operator precedence is implicit unless we use parentheses. Therefore, we must define the
operator precedence inside the algorithm for the infix to postfix conversion
Points to consider:
The order of the numbers or operands remains unchanged. But the order of the operators gets changed in the
conversion.
Stacks are used for converting an infix expression to a postfix expression. The stack that we use in the algorithm
will change the order of operators from infix to postfix.
Postfix expressions do not contain parentheses.
Algorithm:
1. Create a stack.
2. For each character c in the input stream:
If c is an operand
{
Output c
}
Else if c is a right parentheses
{
Pop and output tokens until a left parentheses is popped
}
Else
{ // c is an operator or left parentheses
Pop and output tokens until one of the lower priorities than c
Are encountered, or a left parentheses is encountered, or the stack is empty.
Push c
}
Branch:
Program:
Name: Vedant S Roll No. : Batch:
Branch:
Output:
Result:
Implemented Successfully
Name: Vedant S Roll No. : Batch:
Branch:
Aim:
To write a program to evaluate postfix expression using stack.
Theory:
Introduction:
To evaluate a postfix expression we can use a stack.
Iterate the expression from left to right and keep on storing the operands into a stack. Once an operator is received,
pop the two topmost elements and evaluate them and push the result in the stack again.
Illustration:
Follow the below illustration for a better understanding:
Consider the expression: exp = “2 3 1 * + 9 -“
Scan 2, it’s a number, So push it into stack. Stack contains ‘2’.
Scan 3, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)
Branch:
Scan *, it’s an operator. Pop two operands from stack, apply the * operator on operands. We get 3*1 which
results in 3. We push the result 3 to stack. The stack now becomes ‘2 3’.
Scan +, it’s an operator. Pop two operands from stack, apply the + operator on operands. We get 3 + 2 which
results in 5. We push the result 5 to stack. The stack now becomes ‘5’.
Scan 9, it’s a number. So we push it to the stack. The stack now becomes ‘5 9’.
Branch:
Scan -, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9
which results in -4. We push the result -4 to the stack. The stack now becomes ‘-4’.
Evaluate ‘-‘ operator and push result in stack
There are no more elements to scan, we return the top element from the stack (which is the only element
left in a stack).
So the result becomes -4.
Follow the steps mentioned below to evaluate postfix expression using stack:
Create a stack to store operands (or values).
Scan the given expression from left to right and do the following for every scanned element.
If the element is a number, push it into the stack.
If the element is an operator, pop operands for the operator from the stack. Evaluate the
operator and push the result back to the stack.
Name: Vedant S Roll No. : Batch:
Branch:
Program:
Name: Vedant S Roll No. : Batch:
Branch:
Output:
Result:
Program Executed Successfully.
Name: Vedant S Roll No. : Batch:
Branch:
Aim:
To write a menu driven C program for Queue to Store, Retrieve and Display Queue.
Theory:
Queue:
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First
Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are
made at the other end. The element which is first pushed into the order, the operation is first performed on that.
A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e.
First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue,
is called the front of the queue(sometimes, head of the queue), similarly, the position of the last entry in the
queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See the below figure.
Characteristics of Queue:
Branch:
Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using the
array. Variables used in this case are
Operation 1: enqueue()
Inserts an element at the end of the queue i.e. at the rear end.
The following steps should be taken to enqueue (insert) data into a queue:
Branch:
Enqueue representation
Operation 2: dequeue()
This operation removes and returns an element that is at the front end of the queue.
Dequeue operation
Operation 3: front()
This operation returns the element at the front end without removing it.
Branch:
Operation 4 : rear()
This operation returns the element at the rear end without removing it.
Operation 5: isEmpty():
This operation returns a boolean value that indicates whether the queue is empty or not.
check if front value is equal to -1 or not, if yes then return true means queue is empty.
Otherwise return false, means queue is not empty
Operation 6 : isFull()
This operation returns a boolean value that indicates whether the queue is full or not.
Check if front value is equal to zero and rear is equal to the capacity of queue if yes then return true.
otherwise return false
Operation 7: size()
This operation returns the size of the queue i.e. the total number of elements it contains.
Name: Vedant S Roll No. : Batch:
Branch:
Program:
Name: Vedant S Roll No. : Batch:
Branch:
Output:
Result:
Implemented Successfully