DSA in C
DSA in C
Definition: - Data structure refers to the set of rules that are required to perform any
operation on the program
Operations of array: -
1. TRAVERSAL
This operation is used to visit all the elements of the array.
Void traverse (int a[], int n)
{
int i;
for(i=0; i<n; i++)
Printf(“%d”,a[i]);
}
2. INSERTION
Inserting the array at the end position can be done easily, but to insert at the
middle of the array we have to move the element to create a vacant position to
insert the new element.
int insert (int a[], int n, int pos, int ele)
{
int i;
for(i=n-1; i>=pos-1; i--)
{
a[i+1] = a[i];
}
a[pos-1] = ele;
n++;
return(n);
}
3. DELETION
Deleting an element at the end of the array can be done easily by only decreasing
the array size by 1.
But deleting an element at the middle of the array require that each subsequent
element from the position where to be deleted should be moved to fill up the array.
int delete (int a[], int n, int pos)
{
for (i = pos-1; i<n-1; i++)
{
a[i] = a[i+1];
} n- - ;
return(0);
}
Sparse matrices
Matrix with relatively a high proportion of zero entries are called sparse matrix. In
other words, the sparse matrix can be defined as the matrix that has a greater
number of zero elements than the non-zero elements.
Benefits of using the sparse matrix
i)Storage - We know that a sparse matrix contains lesser non-zero elements than
zero, so less memory can be used to store elements. It evaluates only the non-zero
elements.
ii)Computing time: In the case of searching in sparse matrix, we need to traverse
only the non-zero elements rather than traversing all the sparse matrix elements. It
saves computing time by logically designing a data structure traversing non-zero
elements.
Representation of sparse matrix
There are two ways to represent the sparse matrix that are listed as follows -
o Array representation
o Linked list representation
ARITHMETIC EXPRESSION
There are 3 notation to represent an arithmetic expression.
1. Infix notation
2. Prefix notation
3. Postfix notation
1. INFIX NOTATION
• The conventional way of writing an expression is called as infix. Ex: A+B, C+D,
E*F, G/M etc.
• Here the notation is <Operand><Operator><Operand>
• This is called infix because the operators come in between the operands.
2. PREFIX NOTATION
• This notation is also known as “POLISH NOTATION”
• Here the notation is <Operator><Operand><Operand>
• Ex: +AB, -CD, *EF, /GH
3. POSTFIX NOTATION
• This notation is called as postfix or suffix notation where operator is suffixed by
operand.
• Here the notation is <Operand ><Operand><Operator>
• Ex: AB+, CD-, EF*, GH/
• This notation is also known as “ REVERSE POLISH NOTATION.”
CONVERSION FROM INFIX TO POSTFIX EXPRESSION
Algorithm:
POLISH(Q,P)
Q is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression P.
1. Push “ ( ” onto stack and add “ ) ” to the end of Q.
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the
STACK is empty.
3. If an operand is encountered, add it top.
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator X is encountered, then:
a) Repeatedly POP from STACK and add to P each operator (on the top of the STACK)
which has the same precedence as or higher precedence than the operator X .
b) Add the operator X t
CONVERSION FROM INFIX TO POSTFIX EXPRESSION
Algorithm:
POLISH(Q,P)
Q is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression P.
1. Push “ ( ” onto stack and add “ ) ” to the end of Q.
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the
STACK is empty.
3. If an operand is encountered, add it top.
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator X is encountered, then:
a) Repeatedly POP from STACK and add to P each operator (on the top of the STACK)
which has the same precedence as or higher precedence than the operator X .
b) Add the operator X t
EVALUATION OF POSTFIX EXPRESSION
Algorithm:
This algorithm finds the value of an arithmetic expression P written in Postfix
notation.
1. Add a right parenthesis “ ) ” at the end of P.
2. Scan P from left to right and repeat steps 3 and 4 for each element of P until the “
) ” is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator X is encountered then:
a) Remove the two top element of STACK, where A is top element and B is the next-
to-top element.
b) Evaluate B X A.
c) Place the result of (b) on STACK.
[End of if str.]
5. Set value equal to the top element on STACK.
6. Exit
APPLICATIONS OF STACK:
Recursion process uses stack for its implementation.
Quick sort uses stack for sorting the elements.
Evaluation of antithetic expression can be sone by using STACK.
Conversion from infix to postfix expression
Evaluation of postfix expression
Conversion from infix to prefix expression
Evaluation of prefix expression.
Backtracking
Keeptrack of post-visited (history of a web- browsing)
IMPLEMENTATION OF RECURSION
A function is said to be Recursive function if it call itself or it call a function such that
the function call back the original function. This concept is called as Recursion. The
Recursive function has two properties.
Front = Front + 1
The insertion will be done by setting Rear = Rear + 1
QUEUE
• Queue is a linear data structure or sequential data structure where insertion take
place at one end and deletion take place at the other end.
• The insertion end of Queue is called rear end and deletion end is called front end.
• Queue is based on (FIFO) First in First Out Concept that means the node i.e. added
first is processed first.
• Here Enqueue is Insert Operation.
• Dequeue is nothing but Delete Operation.
Front = Front + 1
The insertion will be done by setting Rear = Rear + 1
Rear = Rear +1
3. Q [Rear] = ITEM
4. Exit
DELETION IN QUEUE(Dequeue)
Algorithm:
Delete (Q, ITEM, FRONT, REAR) This procedure remove element from queue Q.
1. If Front = Rear = NULL
Front = NULL
Rear = NULL
else
Front = Front + 1
4. Exit
CIRCULAR QUEUE
Let we have a queue that contain ‘n’ elements in which Q[1] comes after Q[n].
When this technic is used to construct a queue then the queue is called circular
queue.
In Circular queue when REAR is ‘n’ and any element is inserted then REAR will set
as 1.
Similarly when the front is n and any element is deleted then front will be 1.
INSERTION ALGORITHM OF CIRCULAR QUEUE
Insert (Q, N, FRONT, REAR, ITEM) This procedure inserts an element ITEM into the
circular queue Q.
1. If Front = 1 and Rear = N
PRIORITY QUEUE
If is a type of queue in which each element has been assigned a priority such that
the order in which the elements are processed according to the elements are
processed according to the following rule
I. This element of high priority is processed first.
II. The element having same priority is processed according to the order in which
they are inserted.
The lower ranked no. enjoy high priority.
MODULE 3
PROGRAMMING WITH LINKED LIST
They are designed such that info[K] contains the Kth element and Link[K] contains
the next pointer field i.e. the address of the Kth element in the list.
The end of the List is indicated by NULL pointer i.e. the pointer field of the last
element will be NULLorZero
Start = 4
info [4] = N Link [4] = 3
info [3] = T Link [3] = 6
info [6] = E Link [6] = 2
info [2] = F Link [2] = 0 i.e. the null value
So the list has ended .
Representation of a node in a Linked List
Struct Link
{
int data ;
Struct Link * add ;
};
2.Insertion
The insertion operation is used to add an element in an existing Linked List. There is
various positions where node can be inserted.
Insert at the beginning
Insert at end
Insert at specific location.
Step-3 : Set info [P] = 𝑥 (copies a new data into a new node)
Step-4 : Set next [P] = Start (new node now points to original first node)
Step-5 : Set Start = P
Step-6 : Exit
Insert At The End
To insert a node at the end of the list, we need to traverse the List and advance the
pointer until the last node is reached.
Suppose the new node whose information field contains 77 is inserted at the last
node
Algorithm:
This algorithm is used to insert a node at the end of the linked list. ‘Start’ holds the
address of the first node.
Step-1 : Create a new node named as P.
Step-2 : If P = NULL then print “Out of memory space” and Exit.
Step-3 : Set info [P] = 𝑥 (copies a new data into a new node)
Step-4 : Set next [P] = NULL
Step-5 : Set Ptr = Start
Step-6 : Repeat Step-7 while Ptr ≠ NULL
Step-7 : Set temp = Ptr
Ptr = next [Ptr] (End of step-6 loop)
Step-8 : Set next [temp] = P
Step-9 : Exit
Insert At Any Specific Location
To insert a new node at any specific location we scan the List from the beginning
and move up to the desired node where we want to insert a new node.
In the below fig. Whose information field contain 88 is inserted at 3rd location.
Algorithm:
‘Start’ holds the address of the 1st node.
Step-1 : Set Ptr = Start
Step-2 : Create a new node named as P.
Step-3 : If P = NULL then write ‘Out of memory’ and Exit.
Step-4 : Set info [P] = 𝑥 (copies a new data into a new node)
Step-5 : Set next [P] = NULL
Algorithm:
Start holds the address of the 1st node.
Step-1 : Set Ptr = Start
Step-2 : Set temp = Start
Step-3 : If Ptr = NULL then write ‘UNDERFLOW’ and Exit.
Step-4 : Set i = 1
Step-5 : Read Loc
Step-6 : Repeat Step-7 to 9 while Ptr ≠ NULL and i < Loc
Step-7 : Set temp = Ptr
Step-8 : Set Ptr = next[Ptr]
Step-9 : Set i = i+1
(End of Step 6 loop)
Step-10 : Set next[temp] = next[Ptr]
Step-11 : Free the space associated with Ptr.
Step-12 : Exit
4. SEARCHING
Searching means finding an element from a given list
Algorithm:
Start holds the address of the 1st node.
Step-1 : Set Ptr = Start
Step-2 : Set Loc = 1
Step-3 : Read element
Step-4 : Repeat Step-5 and 7 While Ptr ≠ NULL
Step-5 : If element = info[Ptr] then Write ‘Element found at position’, Loc and Exit.
Step-6 : Set Loc = Loc+1
Step-7 : Set Ptr = next[Ptr]
(End of step 4 loop)
Step-8 : Write ‘Element not found’
Step-9 : Exit
Garbage Collection
The operating system of a computer may periodically collect all the deleted space
on to the free storage list. Any technique which does these collections is called
garbage collection.
When we delete a particular note from an existing linked list or delete the linked list
the space occupied by it must be given back to the free pool. So that the memory
can be the used by some other program that needs memory space.
To the free pool is done.
The operating system will perform this operation whenever it finds the CPU idle or
whenever the programs are falling short of memory space. The OS scans through
the entire memory cell & marks those cells. That are being by some program then it
collects the entire cell which are not being used & add to the free pool. So that this
cells can be used by other programs. This process is called garbage collection. The
garbage collection is invisible to the programmer.