RTU Study DSA
RTU Study DSA
CO1: Apply the concepts of data structure, data type and array data
structure and analyze the algorithms and determine their time complexity.
CO2: Apply various data structure such as stacks, Linked List, queues, trees
and graphs to solve various computing problems using C-programming
language.
CO3: Implement standard algorithms for searching & sorting and identify
when to choose which technique.
CO4: Apply the data structure that efficiently models the information in a
problem.
1
1
CONTENTS (TO BE COVERED)
❑ Introduction of Data Structures and Algorithms
❑ Stack
❑ Applications of Stack
❑ Tower of Hanoi
❑ Stack
❑ Applications of Stack
❑ Tower of Hanoi
1
What is Data Structure?
• A data structure is a way of organizing data in a
computer so that it can be used data effectively.
• For example, we can store a list of items having the
same data-type using the array data structure.
Types of Data Structures
Based on the organizing method of data structure, data structures are divided into two
types.
• Linear Data Structures
• Non - Linear Data Structures
Linear Data Structures Non-Linear Data Structures
• if a data structure organizes the data in • If a data structure organizes the data in
sequential order, then that data structure is random order then that data structure is
called a linear data structure
called as Non-linear data structure.
• Example
Arrays
• Example
List (Linked List) Tree
Stack Graph
Queue Dictionaries
Heaps
Tries, Etc.,
DATA
STRUCTURE
8
Basic Operations
⮚ PUSH(Insertion)
⮚ POP (Deletion)
Example
Example of Stack
PUSH Operation
• In a stack, push() is a function used to insert an element into the stack. In a stack,
the new element is always inserted at top position. Push function takes one
integer value as parameter and inserts that value into the stack. We can use the
following steps to push an element on to the stack...
1. Reversing Strings:
• A simple application of stack is reversing strings.
• To reverse a string , the characters of string are pushed
onto the stack one by one as the string is read from left
to right.
• Once all the characters of string are pushed onto stack,
they are popped one by one. Since the character last
pushed in comes out first, subsequent pop operation
results in the reversal of the string.
Example
•To reverse the string ‘REVERSE’ the string is read from left to right and
its characters are pushed .
LIKE: onto a stack.
Applications
2. Checking the validity of an expression containing nested
parenthesis:
• Stacks are also used to check whether a given arithmetic
expressions containing nested parenthesis is properly
parenthesized.
• The program for checking the validity of an expression
verifies that for each left parenthesis braces or
bracket ,there is a corresponding closing symbol and
symbols are appropriately nested.
For example:
VALID INPUTS INVALID INPUTS
{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]({ [{)}(]}]
})}]
Applications
3. Evaluating arithmetic expressions
–INFIX notation:
–The general way of writing arithmetic expressions is known as infix
notation. e.g, (a+b)
6. When all tokens of Infix expression have been scanned. Pop all the
elements from the stack and append them to the Output String.
The Output string is the Corresponding Postfix Notation.
Example: 2+(4-1)*3 step1
2+41-*3 step2
2+41-3* step3
241-3*+ step4
CONVERSION OF INFIX INTO POSTFIX
{2+(4-1)*3 into 241-3*+}
CURRENT ACTION STACK STATUS POSTFIX
SYMBOL PERFORMED EXPRESSION
( PUSH C C 2
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 241
1 POP 241-
) (+ 241-
* PUSH * (+* 241-
3 241-3
POP * 241-3*
POP + 241-3*+
)
Tower of Hanoi
• The Tower of Hanoi puzzle was invented by the French mathematician Edouard
Lucas in 1883.
• Tower Of Hanoi is a Puzzle involving the usage of Stacks.
• Shifting of discs from source tower to target tower.
• Rules for solving puzzle:
1. One disc can be shifted at once.
2. Only top disc can be shifted.
3. Large disc cannot be placed on smaller disc.
4. Only 3 stacks can be used for solving the puzzle.
Steps:
1. Move 1 from A to C
2. Move 2 from A to B
3. Move 1 from C to B
4. Move 3 from A to C
5. Move 1 from B to A
6. Move 2 from B to C
7. Move 1 from A to C
Real life analogy of Stack is Access single disc, Single Access location.
Use of Recursion for
: algorithm
Explanation
• The recursion used before
"movedisk" is to move all but the
bottom disk on the initial tower to
an intermediate pole.
• The next line simply moves the
bottom disk to its final resting
place.
• Then on line we move the tower
from the intermediate pole to the
top of the largest disk.
• The base case is detected when
the tower height is 0 and the base
of the tower is moved Use of recursion is again use of stack
Application
▪ Used as Backup rotation Scheme (Backups of Computers
having multiple tapes/media)
✔A backup rotation scheme is a system for managing your
backup storage media (tapes/DVDs/HDDs).
▪ Used by neuropsychologists trying to evaluate frontal lobe
deficits.
▪ Used to Solve mathematical problems related to Hamilton
Cycle.
Representation of Stack
• Stack can be represented in two ways
1. Using array
2. Using linked lists
Both the representations are having their own
advantages and disadvantages. So to select the
representation of stack is application dependent.
Stack representation using static array
• Stack data structure can be implemented using a one-
dimensional array.
• Size of array is fixed.
• Basic steps to create an empty stack.
1. Include all the header files which are used in the
program and define a constant 'SIZE' with specific
value.
2. Declare all the functions used in stack implementation.
3. Create a one dimensional array with fixed size.
4. Define a integer variable 'top' and initialize with '-1'.
(int top = -1)
5. In main method, display menu with list of operations
and make suitable function calls to perform operation
selected by the user on the stack.
• #include<stdio.h>
• #include<conio.h>
• #define SIZE 10
• int stack[SIZE], top=-1;
push(value) - Inserting value into the
stack
• Step 1 - Check whether stack is FULL. (top == SIZE-1)
• Step 2 - If it is FULL, then display "Stack is FULL!!!
Insertion is not possible!!!" and terminate the function.
• Step 3 - If it is NOT FULL, then increment top value by
one (top++) and set stack[top] to value (stack[top] =
value).
void push(int value)
{
If(top==SIZE-1)
printf(“stack is full!!! Insertion is not possible”);
else
{
top++;
stack[top]=value;
printf(“successfully inserted”);
}
}
pop() - Delete a value from the Stack
50
Linked List in Data Structure
• A linked list is a linear data structure, in which the elements are
not stored at contiguous memory locations. The elements in a
linked list are linked using pointers as shown in the below
image:
A linked list is represented by a pointer to the first node of the linked list. The first node
is called the head. If the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
Why Linked List?
• Arrays can be used to store linear data of similar types, but arrays
have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on
the number of elements in advance. Also, generally, the allocated
memory is equal to the upper limit irrespective of the usage.
Singly linked lists contain nodes which have a data part as well as
an address part i.e. next, which points to the next node in the
sequence of nodes.
The operations we can perform on singly linked lists
are insertion, deletion and traversal.
Doubly Linked List
In a doubly linked list, each node contains a data part and two
addresses, one for the previous node and one for the next node.
Circular Linked List
In circular linked list the last node of the list holds the address of
the first node hence forming a circular chain.
ARRAY LINKED LIST
Array is a collection of elements of similar data type. Linked List is an ordered collection of elements of same type, which
are connected to each other using pointers.
Array supports Random Access, which means elements can be Linked List supports Sequential Access, which means to access any
accessed directly using their index, like arr[0] for 1st element/node in a linked list, we have to sequentially traverse the
element, arr[6] for 7th element etc. complete linked list, upto that element.
Hence, accessing elements in an array is fast with a constant time To access nth element of a linked list, time complexity is O(n).
complexity of O(1).
In an array, elements are stored in contiguous memory location or In a linked list, new elements can be stored anywhere in the memory.
consecutive manner in the memory. Address of the memory location allocated to the new element is
stored in the previous node of linked list, hence formaing a link
between the two nodes/elements.
In array, Insertion and Deletion operation takes more time, as the In case of linked list, a new element is stored at the first free and
memory locations are consecutive and fixed. available memory location, with only a single overhead step of storing
the address of memory location in the previous node of linked list.
Insertion and Deletion operations are fast in linked list.
Memory is allocated as soon as the array is declared, at compile Memory is allocated at runtime, as and when a new node is added.
time. It's also known as Static Memory Allocation. It's also known as Dynamic Memory Allocation.
In array, each element is independent and can be accessed using it's In case of a linked list, each node/element points to the next, previous,
index value. or maybe both nodes.
Array can single dimensional, two Linked list can be Linear(Singly), Doubly or Circular linked list.
dimensional or multidimensional
Size of the array must be specified at time of array decalaration. Size of a Linked list is variable. It grows at runtime, as more nodes
are added to it.
Array gets memory allocated in the Stack section. Whereas, linked list gets memory allocated in Heap section.