unit - 1
unit - 1
UNIT – 1
Introduction to Data Structure
Data
Data is the basic fact or entity that is utilized in calculation or manipulation.
There are two different types of data Numeric data and Alphanumeric data.
When a programmer collects such type of data for processing, he would require to store them in
computer’s main memory.
The process of storing data items in computer’s main memory is called representation.
Data to be processed must be organized in a particular fashion, these organization leads to
structuring of data, and hence the mission to study the Data Structures starts.
Data structure
Data Structure is a representation of the logical relationship existing between individual
elements of data.
In other words, a data structure is a way of organizing all data items that considers not only the
elements stored but also their relationship to each other.
We can also define data structure as a mathematical or logical model of a particular
organization of data items.
Data Structure mainly specifies the following four things
Organization of Data
Accessing Methods
Degree of Associativity
Processing alternatives for information
Example:
Abstract / logical view
Implementation view
- Store a set of elements of integer type.
int arr[5] = {1,2,3,4,5};
- Read elements by position i.e. index
printf(“%d”,a[1]);
- Modify elements by index
a[2] = 20;
- Perform sorting
and many more….
Overview of Arrays
Array is a container which can hold a fix number of items and these items should be of the same
type. Following are the important terms to understand arrays:
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index,
which is used to identify the element.
Array Representation
Self Referential structures are those structures that have one or more pointers which point to
the same type of structure, as their member.
Union
A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same memory
location for multiple-purpose.
union [union tag] {
member definition;
member definition; ...
member definition;
} [one or more union variables];
Example :-
union Data {
int i;
float f;
char str[20];
} data;
Syntax:-
<datatype> *variable_name
Example :-
int *ptr1 – ptr1 references to a memory location that holds data of int datatype.
int var = 30;
int *ptr1 = &var; // pointer to var
int **ptr2 = & ptr1; // pointer to pointer variable ptr1
print(“%d”, *ptr1) // prints 30
print(“%d”,**ptr2) // prints 30
Types of Array
Array
Multidimensional Array
In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays.
For example:-
float x[3][4];
Asymptotic Notations
Asymptotic Notations are the expressions that are used to represent the complexity of an algorithm.
There are three types of analysis that we perform on a particular algorithm.
Best Case: In which we analyze the performance of an algorithm for the input, for which the
algorithm takes less time or space.
Worst Case: In which we analyze the performance of an algorithm for the input, for which the
algorithm takes long time or space.
Average Case: In which we analyze the performance of an algorithm for the input, for which the
algorithm takes time or space that lies between best and worst case
Types of Data Structure Asymptotic Notation
• Big-O Notation (Ο) – Big O notation specifically describes worst case scenario.
• Omega Notation (Ω) – Omega(Ω) notation specifically describes best case scenario.
• Theta Notation (θ) – This notation represents the average complexity of an
algorithm.
Big-O Notation (Ο)
It represents the upper bound running time complexity of an algorithm. Let’s take few examples to
understand how we represent the time and space complexity using Big O notation.
O(1) :- Big O notation O(1) represents the complexity of an algorithm that always execute in
same time or space regardless of the input data.
• Example
The following step will always execute in same time(or space) regardless
of the size of input data.
• Accessing array index(int num = arr[5])
O(n) :- Big O notation O(N) represents the complexity of an algorithm, whose performance
will grow linearly (in direct proportion) to the size of the input data.
• Example
The execution time will depend on the size of array. When the size of
the array increases, the execution time will also increase in the same
proportion (linearly)
• Traversing an array
O(n^2) :-Big O notation O(n^2) represents the complexity of an algorithm, whose
performance is directly proportional to the square of the size of the input data.
• O(n^2) example
• Traversing a 2D array
UNIT – 2
Stack
A linear list which allows insertion and deletion of an element at one end only is called stack.
The insertion operation is called as PUSH and deletion operation as POP.
The most accessible elements in stack is known as top.
The elements can only be removed in the opposite orders from that in which they were added
to the stack.
Such a linear list is referred to as a LIFO (Last In First Out) list.
Applications of Stack
Recursion
Keeping track of function calls
Evaluation of expressions
Reversing characters
Servicing hardware interrupts
Solving combinatorial problems using backtracking
Expression Conversion (Infix to Postfix, Infix to Prefix)
Game Playing (Chess)
Microsoft Word (Undo / Redo)
Compiler – Parsing syntax & expression
Finding paths
PUSH
Procedure : PUSH (S, TOP, X)
This procedure inserts an element X to the top of a stack.
Stack is represented by a vector S containing N elements.
A pointer TOP represents the top element in the stack.
POP
Function : POP (S, TOP)
This function removes & returns the top element from a stack.
Stack is represented by a vector S containing N elements.
A pointer TOP represents the top element in the stack.
Algorithm
1. [Check for stack underflow]
If TOP = -1
Then write (‘STACK UNDERFLOW’)
Return (0)
2. [Decrement TOP]
TOP ← TOP - 1
3. [Return former top element of stack]
Return(S[TOP + 1])
PEEP
Function : PEEP (S, TOP, P)
This function returns the value of the (pth)element from the TOP of the stack. The element is
not deleted by this function.
Stack is represented by a vector S containing N elements.
Algorithm
1. [Check for stack underflow]
If TOP == -1
Then write (‘STACK UNDERFLOW’)
Return (0)
2. [Check for stack overflow]
If P > TOP + 1
Then write (‘STACK OVERFLOW’)
Return (0)
3. [Return pth element from top
of the stack]
Return(S[TOP–P+1])
Operation
Overflow
Underflow
CHANGE
CHANGE (S, TOP, X, P)
This procedure changes the value of the Pth element from the top of the stack to X.
Stack is represented by a vector S containing N elements.
Algorithm
1. [Check for stack underflow]
If TOP-P+1 ≤ 0
Then write (‘STACK UNDERFLOW’)
Return
2. [Check for stack overflow]
If P > TOP + 1
Then write (‘STACK OVERFLOW’)
Return (0)
3. [Change Pth element from top
of the stack]
S[TOP–P+1] ← X
4. [Finished]
Return
Example:-
CHANGE (S, TOP, 50, 2)
TOP = 3 -5
CHANGE (S, TOP, 9, 3)
8
CHANGE (S, TOP, 25, 8)
10
Underflow S
TRAVERSE
Traverse (S, Top, I)
This procedure displays the elements of the stack with for loop using a variable I.
Stack is represented by a vector S containing N elements.
Algorithm
1. [Check for stack underflow]
If TOP == -1
Then write (‘STACK UNDERFLOW’)
Return
2. Display the elements of stack
for (i=top;i>=0;--i)
display the elements of the stack.
3. [Finished]
Return
Example:-
Display(S,TOP,20) 20
TOP
30
Display(S,TOP,30)
40
Display(S,TOP,40)
Underflow