KCA205: DATA STRUCTURES& ANALYSIS OF
ALGORITHMS
ALGORITHMS
• Unit-I-
• Introduction to data structure: Data, Entity, Information,
Difference between Data and Information, Data type ,
Build in data type, Abstract data type, Definition of data
structures, Types of Data Structures: Linear and Non-
Linear Data Structure
• Introduction to Algorithms: Definition of Algorithms,
Difference between algorithm and programs, properties of
algorithm, Algorithm Design Techniques, Performance
Analysis of Algorithms, Complexity of various code
structures, Order of Growth, Asymptotic Notations.
• Arrays: Definition, Single and Multidimensional Arrays,
Representation of Arrays: Row Major Order, and Column
Major Order, Derivation of Index Formulae for 1-D, 2-D
Array Application of arrays, Sparse Matrices and their
representations.
• Linked lists: Array Implementation and Pointer
Implementation of Singly Linked Lists, Doubly Linked List,
3.
KCA205: DATA STRUCTURES& ANALYSIS OF
ALGORITHMS
ALGORITHMS
• Unit-II-
• Stacks: Abstract Data Type, Primitive Stack
operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of stack:
Prefix and Postfix Expressions, Evaluation of
postfix expression.
• Iteration and Recursion- Principles of recursion,
Tail recursion, Removal of recursion Problem
solving using iteration and recursion with examples
such as binary search, Fibonacci numbers, and
Hanoi towers.
• Queues: Operations on Queue: Create, Add,
Delete, Full and Empty, Circular queues, Array and
linked implementation of queues in C, Dequeue
and Priority Queue.
4.
KCA205: DATA STRUCTURES& ANALYSIS OF ALGORITHMS
ALGORITHMS
• Unit-III-
• Sorting: Insertion Sort, Selection Sort,
Bubble Sort, Heap Sort, Comparison of
Sorting Algorithms.
• Sorting in Linear Time: Counting Sort and
Bucket Sort.
• Graphs: Terminology used with Graph, Data
Structure for Graph Representations:
Adjacency Matrices, Adjacency List,
Adjacency.
• Graph Traversal: Depth First Search and
5.
KCA205: DATA STRUCTURES& ANALYSIS OF ALGORITHMS
ALGORITHMS
• Unit-IV-
• Trees: Basic terminology used with Tree, Binary
Trees.
• Binary Tree Representation: Array
Representation and Pointer (Linked List)
Representation, Binary Search Tree, Complete
Binary Tree, A Extended Binary Trees.
• Tree Traversal algorithms: In-order, Preorder
and Post-order, Constructing Binary Tree from
given Tree Traversal, Operation of Insertion,
Deletion, Searching & Modification of data in
Binary Search Tree.
6.
KCA205: DATA STRUCTURES& ANALYSIS OF ALGORITHMS
ALGORITHMS
• Unit-V-
• Divide and Conquer with Examples Such
as Merge Sort, Quick Sort.
• Matrix Multiplication: Strassen’s Algorithm
Dynamic Programming: Dijikstra
Algorithm, Bellman Ford Algorithm.
• Allpair Shortest Path: Warshal Algorithm,
Longest Common Sub-sequence
• Greedy Programming: Prims and Kruskal
algorithm.
KCA205: DATA STRUCTURES& ANALYSIS OF
ALGORITHMS
ALGORITHMS
• Unit-II-
• Stacks: Abstract Data Type, Primitive Stack
operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of stack:
Prefix and Postfix Expressions, Evaluation of
postfix expression,
• Iteration and Recursion- Principles of recursion,
Tail recursion, Removal of recursion Problem
solving using iteration and recursion with examples
such as binary search, Fibonacci numbers, and
Hanoi towers.
• Queues: Operations on Queue: Create, Add,
Delete, Full and Empty, Circular queues, Array and
linked implementation of queues in C, Dequeue
and Priority Queue.
9.
DATA STRUCTURES USINGC
• Introduction to Stack
• There are certain situations in computer science that one wants
to restrict insertions and deletions so that they can take place
only at the beginning or the end of the list, not in the middle.
• Linear lists and arrays allow one to insert and delete elements
at any place in the list i.e., at the beginning, at the end or in the
middle.
• Two of such data structures that are useful are:
• • Stack. • Queue.
• When we add or remove data structures, they grow and shrink.
If we restrict the growth of a components or elements of linear
linear data structure so that new components or elements can
only be added or removed only at one end, we have a stack.
• Stacks are useful data structures and are used in a variety of
ways in computer science.
10.
• What isStack:-
• It is an ordered collection of homogeneous items of elements.
Elements are added to and removed from the top of the stack (the
most recently added items are at the top of the stack). The stack works
based on LIFO or FILO policy.
• The last element to be added is the first to be removed(LIFO: Last In,
First Out) one end, called TOP of the stack. The elements are removed
in reverse order of that in which they were inserted into the stack.
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-
world stack, for example – a deck of cards or a pile of plates, etc.
11.
• Applications ofStacks in Data Structures
• There a lot of places where we use stack without even realizing it.
• Let’s see some of the most common uses of stack data structure.
• Real life examples of Stack:-
• Stacking up dishes after washing them.
• Deck of cards.
• Stacking a pile of boxes in our store rooms.
• Libraries stack pile of books and articles.
• Some Technique/Programming /Applications of Stack
• Recursion
• Backtracking
• UNDO/REDO
• String reversal
• DFS(Depth First Search)
• Expression conversion
• Balancing of symbols
• Memory management
• Process Management
13.
• The stackcan be used -to convert some infix expression into
its postfix equivalent, or prefix equivalent.
• -Postfix or Prefix Evaluation.
• Stacks make excellent mechanisms for temporary storage of
information within procedures. A primary reason for this is
that they allow recursive invocations of procedures without
risk of destroying data from previous invocations of the
routine. They also support re-entrant code.
• Another great use of stack is during the function call and
return process. When we call a function from one other
function, that function call statement may not be the first
statement. After calling the function, we also have to come
back from the function area to the place, where we have left
our control. So we want to resume our task, not restart. For
that reason, we store the address of the program counter into
the stack, then go to the function body to execute it. After
completion of the execution, it pops out the address from
stack and assign it into the program counter to resume the
task again.
14.
• Advantages ofUsing Stack over other data structures
• 1. Manages the data in a Last In First Out(LIFO) method which is not
possible with Linked list and array.
• 2. When a function is called the local variables are stored in a stack,
and it is automatically destroyed once returned.
• 3. A stack is used when a variable is not used outside that function.
• 4. It allows you to control how memory is allocated and deallocated.
• 5. Stack automatically cleans up the object.
• 6. Not easily corrupted
• 7. Variables cannot be resized.
• Disadvantages of Using Stack over other data structures
• 1. Stack memory is very limited.
• 2. Creating too many objects on the stack can increase the risk of
stack overflow.
• 3. Random access is not possible.
• 4. Variable storage will be overwritten, which sometimes leads to
undefined behaviour of the function or program.
• 5. The stack will fall outside of the memory area, which might lead to
an abnormal termination.
15.
A stack isa list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack. Stacks are
sometimes known as LIFO (last in, first out) Or FILO(First In last out)
lists.
OR
16.
• STACK OPERATIONS:-
•There are basically three operations that can be
performed on stacks. They are-
• 1) inserting an item into a stack (push).
• 2) deleting an item from the stack (pop).
• 3) displaying the contents of the stack(pip).
• These are main two basic operations associated with
stack:
• 1. Push():- Push() is the term used to insert/add an
element into a stack. If the stack is full, then it is said to
be an Overflow condition.
• 2. Pop ():-Pop() is the term used to delete/remove an
element from a stack. If the stack is empty, then it is
said to be an Underflow condition.
17.
• When datais PUSHed onto stack-
• To use a stack efficiently, we need to check the status of stack
as well. For the same purpose, the following functionality is
added to stacks −
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
• peek() − Returns top element of stack. Or get the top data
element of the stack, without removing it.
• count(): It returns the total number of elements available in a
stack.
• change(): It changes the element at the given position.
• display(): It prints all the elements available in the stack.
• Other names for stacks are piles and push-down lists.
• At all times, we maintain a pointer to the last PUSHed data on
the stack. As this pointer always represents the top of the
stack, hence named top. The top pointer provides top value
of the stack without actually removing it.
18.
• 1. PUSHoperation():-
• The steps involved in the PUSH operation is given
below:
• Before inserting an element in a stack, we check
whether the stack is full.
• If we try to insert the element in a stack, and the stack
is full, then the overflow condition occurs.
• When we initialize a stack, we set the value of top as -1
to check that the stack is empty.
• When the new element is pushed in a stack, first, the
value of the top gets incremented, i.e., top=top+1, and
the element will be placed at the new position of
the top.
• The elements will be inserted until we reach
the max size of the stack.
19.
• OR-Push Operation:-Pushan item onto the top of the stack (insert an item)
• Algorithm for PUSH:
Or-Push(Stack, Maxsize, Top, Item)
1. [stack is filled ]
If Top=Maxsize-1,then
print: Stack is Overflow.
2. Set Top:=Top+1 . [Increase Top by 1].
3.Set Stack[Top]=Item.[Insert Item in new Top position]
4. return .
• POP operation():-
•The steps involved in the POP operation is given below:
• Before deleting the element from the stack, we
check whether the stack is empty.
• If we try to delete the element from the empty
stack, then the underflow condition occurs.
• If the stack is not empty, we first access the element
which is pointed by the top.
• Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1
23.
• Pop Operation():-Deletionfrom stack is also known as POP
operation in stack.
•
• Algorithm for Pop:-
• pop(stack, top, item) --This procedure deletes the top element eleme
of stack.
• 1.[stack has an item to be removed]
• If top=-1 then print underflow/ stack is empty, and return.
• 2. Set item =stack[top].
3. Set top=top-1. ----decrease by top by 1 4.
return.
• Analysis ofStack Operations
• Below mentioned are the time complexities for
various operations that can be performed on the
Stack data structure.
• Push Operation : O(1)
• Pop Operation : O(1)
• Top Operation : O(1)
• Search Operation : O(n)
• The time complexities for push() and pop() functions
are O(1) because we always have to insert or remove the
data from the top of the stack, which is a one step process.
26.
DATA STRUCTURES USINGC
• Representation of Stack:- Let us consider a stack with 6
elements capacity. This is called as the size of the stack. The
number of elements to be added should not exceed the
maximum size of the stack. If we attempt to add new element
beyond the maximum size, we will encounter a stack overflow
condition.
• Similarly, you cannot remove elements beyond the base of the
stack. If such is the case, we will reach a stack underflow
condition.
• There are two ways to represent Stack in memory. One is using
array and other is using linked list.
• 1. Array representation of stack/Array implementation of
stack/implementation of stack using array
• 2. Linked list representation of stack/Linked list implementation
of stack/implementation of stack using linked list.
27.
DATA STRUCTURES USINGC
• 1. Array representation of stack/Array implementation of
stack/implementation of stack using array:-
• Usually the stacks are represented in the computer by a linear array.
In the following algorithms/procedures of pushing and popping an
item from the stacks, we have considered, a linear array STACK, a
variable TOP which contain the location of the top element of the
stack; and a variable STACKSIZE which gives the maximum number of
elements that can be hold by
• the stack.
• Here are the minimal operations we'd need for an abstract stack (and
their typical names):
• o Push: Places an element/value on top of the stack.
• o Pop: Removes value/element from top of the stack.
• o IsEmpty: Reports whether the stack is Empty or not.
• o IsFull: Reports whether the stack is Full or not.
28.
• Example onStack(Push Operation):-
void push()
{
if(top>=n-1)
{
printf("nt STACK is
over flow");
}
else
{
printf(" Enter a value
to be pushed:");
scanf("%d", &x);
top++;
stack[top]=x;
}}
void pop()
{
if(top<=-1)
{
printf("nt Stack is
under flow");
}
else
{
printf("nt The
popped elements is
%d", stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("n The elements in
STACK n");
for(i=top; i>=0; i--)
printf("n%d",stack[i]);
printf("n Press Next
Choice");
}
else
{
printf("n The STACK is
empty");
}
}
29.
Implementation of StackUsing
Array in C
#include<stdio.h>
int
stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{ top=-1;
printf("n Enter the size of
STACK[MAX=100]:");
scanf("%d",&n);
printf("nt STACK
OPERATIONS USING ARRAY");
printf("nt------------------);
printf("nt 1.PUSHnt
2.POPnt 3.DISPLAYnt 4.EXIT");
do
{
printf("n Enter the Choice:");
scanf("%d",&choice);
switch(choice) {
case 1: { push(); break;
}
case 2: { pop(); break; }
case 3: { display(); break; }
case 4: { printf("nt EXIT POINT
"); break; }
default: {
printf ("nt Please Enter a
Valid Choice(1/2/3/4)");
} } }
while(choice!=4);
return 0;
}
void push()
{ if(top>=n-1) {
printf("ntSTACK is over flow");
}
else {
printf(" Enter a value to be
pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}}
void pop()
{
if(top<=-1)
{
printf("nt Stack is under flow");
} else
{
printf("nt The popped
elements is %d",stack[top]);
top--;
}}
void display()
{ if(top>=0)
{
printf("n The elements in STACK
n");
for(i=top; i>=0; i--)
printf("n%d",stack[i]);
printf("n Press Next Choice");
}
else {
printf("n The STACK is empty");
}
}
30.
• Question:
• Ifthe sequence of operations- push (1), push (2),
pop, push (1), push (2), pop, pop, pop, push (2),
pop, are performed on a stack, the sequence of
popped out values are-.
• Question:
• The following sequence of the operations is
performed on a stack PUSH(10), PUSH(20), POP,
PUSH(10), PUSH(20), POP, POP, POP, PUSH(20), POP
the sequence of values popped out is