Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

Name: Vedant S Shetty Roll No.

: 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.

Rules for decaling variables:

i) Variable is any combination of digit, alphabets, underscore(_)


ii) Variable name is of 32 characters.
iii) The first character of variable must be alphabet or ‘_’
iv) No special symbol other than ‘_’ is allowed.
v) No blank space or commas’,’ are allowed.
vi) Keywords cannot be used as variable names.
vii) Variable names are the names given to the memory location.
viii) Particular type of variable can hold same type of data.

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:

Date of Performance: 28th July, Subject: Data Experiment

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.

It returns 1 if the stack is empty.

It returns 0 if the stack is not empty.


Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 28th July, Subject: Data Experiment

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:

Date of Performance: 28th July, Subject: Data Experiment

1. FUNCTION/PROCEDURE/ALGORITHM TO INSERT AN ELEMENT IN STACK

PUSH (STACK, N , TOP,


ELEMENT) STACK-STACK OF SIZE
N
N-SIZE OF STACK
TOP-STACK POINTER
ELEMENT-TO BE INSERTED

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

2. FUNCTION/PROCEDURE/ALGORITHM TO DELETE AN ELEMENT FROM STACK


 POP (STACK, N, TOP, ELEMENT)
 STACK-STACK OF SIZE N
 N-SIZE OF STACK
 TOP-STACK POINTER
 ELEMENT-TO BE INSERTED
1. START
2. CHECK FOR THE UNDERFLOW
IF (TOP==-1)
3. OTHERWISE
INITIALIZE AN ELEMENT TO BE DELETED
ELEMENT=STACK [TOP]
RETURN ELEMENT
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 28th July, Subject: Data Experiment

4. DECREMENT
TOP TOP=TOP-1
5. RETURN
6. STOP

Program:
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 28th July, Subject: Data Experiment


Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 28th July, Subject: Data Experiment

Output:

Result:
Implemented Successfully
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 4th August, Subject: Data Experiment No.

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:

Date of Performance: 4th August, Subject: Data Experiment No.

Infix to postfix conversion

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
}

3. Pop and output tokens until the stack are empty.


Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 4th August, Subject: Data Experiment No.

Program:
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 4th August, Subject: Data Experiment No.

Output:

Result:
Implemented Successfully
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 11th August, Subject: Data Experiment No.

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’.

Push 2 into stack

 Scan 3, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)

 Push 3 into stack


Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 11th August, Subject: Data Experiment No.

 Scan 1, again a number, push it to stack, stack now contains ‘2 3 1’

Push 1 into stack

 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’.

Evaluate * operator and push result in stack

 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’.

Evaluate + operator and push result in stack

 Scan 9, it’s a number. So we push it to the stack. The stack now becomes ‘5 9’.

Push 9 into stack


Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 11th August, Subject: Data Experiment No.

 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:

Date of Performance: 11th August, Subject: Data Experiment No.

Program:
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 11th August, Subject: Data Experiment No.

Output:

Result:
Program Executed Successfully.
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 18th August, Subject: Data Experiment No.

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.

FIFO Principle of Queue:

 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.

Fifo Property in Queue

Characteristics of Queue:

 Queue can handle multiple data.


 We can access both ends.
 They are fast and flexible.
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 18th August, Subject: Data Experiment No.

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

 Queue: the name of the array storing queue elements.


 Front: the index where the first element is stored in the array representing the queue.
 Rear: the index where the last element is stored in an array representing the queue.

Basic Operations on Queue:

Some of the basic operations for Queue in Data Structure are:

 enqueue() – Insertion of elements to the queue.


 dequeue() – Removal of elements from the queue.
 peek() or front()- Acquires the data element available at the front node of the queue without deleting it.
 rear() – This operation returns the element at the rear end without removing it.
 isFull() – Validates if the queue is full.
 isEmpty() – Checks if the queue is empty.
 size(): This operation returns the size of the queue i.e. the total number of elements it contains.

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:

 Check if the queue is full.


 If the queue is full, return overflow error and exit.
 If the queue is not full, increment the rear pointer to point to the next empty space.
 Add the data element to the queue location, where the rear is pointing.
 return success.
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 18th August, Subject: Data Experiment No.

Enqueue representation

Operation 2: dequeue()

This operation removes and returns an element that is at the front end of the queue.

The following steps are taken to perform the dequeue operation:

 Check if the queue is empty.


 If the queue is empty, return the underflow error and exit.
 If the queue is not empty, access the data where the front is pointing.
 Increment the front pointer to point to the next available data element.
 The Return success.

Dequeue operation

Operation 3: front()

This operation returns the element at the front end without removing it.

The following steps are taken to perform the front operation:

 If the queue is empty return the most minimum value.


 otherwise, return the front value.
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 18th August, Subject: Data Experiment No.

Operation 4 : rear()

This operation returns the element at the rear end without removing it.

The following steps are taken to perform the rear operation:

 If the queue is empty return the most minimum value.


 otherwise, return the rear value.

Operation 5: isEmpty():

This operation returns a boolean value that indicates whether the queue is empty or not.

The following steps are taken to perform the Empty operation:

 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.

The following steps are taken to perform the isFull() operation:

 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:

Date of Performance: 18th August, Subject: Data Experiment No.

Program:
Name: Vedant S Roll No. : Batch:

Branch:

Date of Performance: 18th August, Subject: Data Experiment No.

Output:

Result:
Implemented Successfully

You might also like