Ds 1
Ds 1
Unit 1
❖ Data Structure:
Definition:
● Data is a collection of numbers, alphabets and special symbols which are used to represent an
information.
● The proper arrangement of data is known as data structure.
● The systematic representation of data in main memory is known as data structure.
● Data can be organized in different ways.
● The logical or mathematical model of a particular organization of data is called as data structure.
● A data structure is a way of storing data in a computer so that it can be used efficiently.
● Data structures are basically a way of storing and logically implementing the data elements.
● Primitive data structures which include the int, char, float, double and the non-primitive data
structures can broadly be classified into two types:
● In this type of data structure, all the data elements are stored in a particular sequence.
● All elements are arranged in linear fashion.
● The sequential organization of data elements is known as linear data structure.
● Examples: Array, Linked List, Stack, Queue, etc.
● In this type of data structure, all the data elements do not form any sequence.
● All elements are arranged in non-linear fashion.
● The randomly organization of data elements is known as non-linear data structure.
● Examples: Tree, Graph, Table, etc.
The basic operations that are performed on data structures are as follows:
1. Insertion:
It is adding a new data in data structure.
2. Deletion:
It is removing a data from existing data structure
3. Searching:
It is finding location of data within given data structure
4. Sorting:
It is an arranging data in some logical order, it may be in ascending or descending order.
5. Traversing:
It is an operation that access each and every element.
6. Merging:
It is used to combine the data items of two data structure into single data structure.
- Abstract data type Algorithm,
•A useful tool for specifying the logical properties of a data type is the
abstract data type, or ADT. Fundamentally, a data type is collection of values and a set of
operations on those values.
That collection and those operations form a mathematical construct
that may be implemented using a particular hardware or software
data structure.
The term "abstract data type" refers to the basic mathematical
concept that defines the data type.
Stack is a linear data structure in which the operations are performed based on LIFO or FILO
principle.
A Collection of similar data items in which both insertion and deletion operations are performed
based on LIFO or FILO principle.
In a stack, adding and removing of elements is performed at single position which is known as
"Top".
That means, new element is added at the top of the stack and an element is removed from the
top of the stack.
In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out
and First In Last Out) manner.
Below diagram shows the behavior of a stack:
In a stack, the insertion operation is performed using a function called "push" and deletion
operation is performed using a function called "pop".
In the above figure, PUSH and POP operations are performed at top position in the stack. That
means, both the insertion and deletion operations are performed at one end (i.e at Top).
Example:
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom
most element and 50 is the top most element. Top is at 50 as shown in the image below:
- stack applications
Applications of STACK:
1. Expression Evaluation.
2. Expression Conversion.
a) Infix to Postfix
b) Infix to Prefix
c) Postfix to Infix
d) Prefix to Infix
3. Simulation of recursion.
4. Function call.
5. Reversing the list.
6. Parsing.
- Basic Definition and examples: Infix, Postfix, and Prefix,
What is an Expression?
Expression is a collection of operands and operators.
Operator is a symbol which indicate operation to be perform like arithmetic operation or
logical operation or conditional operation etc.
Operands are the values on which operation to be perform.
Expression Types:
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
Example:
2. Postfix Expression: In postfix expression, operator is used after operands.
Syntax:
Example:
Example:
Any expression can be represented using the above three different types of expressions. And we can
convert an expression from one form to another form like Infix to Postfix, Infix to Prefix and vice versa.
Example:
Convert A+(B*C) into Postfix form
Examples:
- Prefix Expression Evaluation: Below steps are required to evaluate prefix expression.
Step 1: Scan prefix expression from right to left.
Step 2: If reading symbol is operand then PUSH it on to stack.
Step 3: If reading symbol is operator then POP two values from stack and perform the operations
between them and push result back onto stack.
Note: First pop value become first operand and second pop value become second operand
in expression.
Step 4: Repeat above all steps until input expression End.
Examples:
#include <stdio.h>
int stack[MAX];
stack[++top] = x;
int pop() {
return stack[top--];
if (isdigit(exp[i])) {
} else {
switch (exp[i]) {
}
}
return pop();
int main() {
char exp[MAX];
scanf("%s", exp);
return 0;
}
- limitations of the provided C program for evaluating postfix expressions:
Queue is a linear data structure in which the insertion and deletion operations are performed at
two different ends.
Queue data structure is a collection of similar data items in which insertion and deletion
operations are performed based on FIFO or LILO manner.
In a queue data structure, adding and removing of elements are performed at two different
positions.
The insertion is performed at one end and deletion is performed at other end.
In a queue data structure, the insertion operation is performed at a position which is known as
'rear' and the deletion operation is performed at a position which is known as 'front'.
In queue data structure, the insertion and deletion operations are performed based on FIFO (First
In First Out) or LILO (Last In Last Out) manner.
queue.
Rear end is used for inserting an new element and Front end is used for deleting an element from
the queue.
Front Variable: While implementing the queue data structure, front variable is important which
is an integer type. Front is a variable which hold the index of the element that is to be deleted.
The initial value of front variable is -1.
Rear Variable: While implementing the queue data structure, rear variable is important which is
an integer type. Rear is a variable which hold the index of the element that has been inserted. The
initial value of rear variable is -1.
Example:
If we want to create a queue by inserting 25,30,51,60 and 85. Then rear points to 85 element and
front point to 25 element of array as mentioned in below:
- The queue as an ADT.
-
- Priority Queue:
Priority queue is a one of the types of queue.
Priority Queue is a linear data structure.
The rules for processing the elements of priority queue are:
1. Elements are processed based on priority.
2. Highest priority elements are processed first before the lowest priority elements.
3. If two elements have the same priority, they are processed in FIFO manner.
Example of priority queue:
Hospital waiting room where emergency patient admitted first before the normal patient.
Operating system scheduler
Routing
Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum Spanning Tree, etc
Operations on a Priority Queue: The following operations are performed on the Priority Queue.
1. Initialize – To initialize the priority queue, in this operation -1 value set to rear and front
variables.
2. Empty – To check whether priority queue is empty or not.
3. Full – To check whether the priority queue is full or not.
4. Insertion– To insert the new element into the priority queue using rear end.
5. Deletion – To delete the element from the priority queue using front end on the basis of
priority.
6. Print – To display all elements of the priority queue.