Data Structure
Data Structure
Linked lists
3
Introduction
Data Structures:
Data
➢ Data is a collection of facts, numbers, letters or symbols that the computer process
into meaningful information.
Data structure
➢ Data structure is representation of the logical relationship existing between
individual elements of data.
➢ Data structure is a specialized format for organizing and storing data in memory
that considers not only the elements stored but also their relationship to each other.
Data Structures:
Data structure affects the design of both structural & functional aspects of a program.
➢ A algorithm is a step by step procedure to solve a particular function.
➢ Program=algorithm + Data Structure
➢ A non-primitive data structure is built out of primitive data structures linked together
in meaningful ways, such as a or a linked-list, binary search tree, AVL Tree, graph etc.
Arrays:
An array is defined as a set of finite number of homogeneous elements or same data
items:
➢ Declaration of array is as follows:
➢ Syntax: Datatype Array_Name [Size];
➢ Example: int arr[10];
✓ Where int specifies the data type or type of elements arrays stores.
✓ “arr” is the name of array & the number specified inside the square brackets is the
number of elements an array can store, this is also called sized or length of array.
Arrays:
Represent a Linear Array in memory:
➢ The elements of linear array are stored in consecutive memory locations.
➢ It is shown below:
int A[5]={23, 4, 6, 15, 5, 7}
Searching in Arrays:
Searching: It is used to find out the location of the data item if it exists in the given
collection of data items:
➢ E.g. We have linear array A as below:
1 2 3 4 5
10 20 50 30 35
➢ Suppose item to be searched is 35. We will start from beginning and will compare 35
with each element.
➢ This process will continue until element is found or array is finished.
➢ Types of searching Algorithms:
❖ Linear searching
❖ Binary Searching
Prof. K. Adisesha (Ph. D)
23
Arrays
Linear search:
Linear Searching: Also called Sequential Searching.
➢ It is used to find out the location of the data item if it exists in the given collection of
data items.
➢ Example Searching element 33 from the array of elements:
Binary Searching:
Difference in Searching:
Difference in Searching:
Insertion Sorting:
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list)
one item at a time.
➢ This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained
which is always sorted.
Advantages of Array
: two dimensional array is a collection of elements and each element is identified by a
A
pair of subscripts. ( A[m] [n] )
➢ It is used to represent multiple data items of same type by using single name.
➢ It can be used to implement other data structures like linked lists, stacks, queues, tree,
graphs etc.
➢ Two-dimensional arrays are used to represent matrices.
➢ Many databases include one-dimensional arrays whose elements are records.
Disadvantages of Array
: two dimensional array is a collection of elements and each element is identified by a
A
pair of subscripts. ( A[m] [n] )
➢ We must know in advance the how many elements are to be stored in array.
➢ Array is static structure. It means that array is of fixed size. The memory which is
allocated to array cannot be increased or decreased.
➢ Array is fixed size; if we allocate more memory than requirement then the memory
space will be wasted.
➢ The elements of array are stored in consecutive memory locations. So insertion and
deletion are very difficult and time consuming.
Operation on Stacks:
The various operation performed on Stacks are:
➢ Stack( ): It creates a new stack that is empty. It needs no parameter and returns an
empty stack.
➢ push(item): It adds a new item to the top of the stack.
➢ pop( ): It removes the top item from the stack.
➢ peek( ): It returns the top item from the stack but does not remove it.
➢ isEmpty( ): It tests whether the stack is empty.
➢ size( ): It returns the number of items on the stack.
Stack Conditions:
The various operation performed on Stacks depend on the following conditions:
Application of Stacks:
➢ It is used to reverse a word. You push a given word to stack – letter by letter and then pop letter from
the stack.
➢ “Undo” mechanism in text editor.
➢ Backtracking: This is a process when you need to access the most recent data element in a series of
elements. Once you reach a dead end, you must backtrack.
➢ Language Processing: Compiler’ syntax check for matching braces in implemented by using stack.
➢ Data Conversion of decimal number to binary.
➢ To solve tower of Hanoi.
➢ Conversion of infix expression into prefix and postfix.
➢ To sort elements using Quick sort
➢ Runtime memory management.
Arithmetic Expression:
An expression is a combination of operands and operators that after evaluation results
in a single value.
◼ Operand consists of constants and variables.
◼ Operators consists of {, +, -, *, /, ), ] etc.
➢ Expression can be
❖ Infix Expression: If an operator is in between two operands, it is called infix expression.
✓ Example: a + b, where a and b are operands and + is an operator.
❖ Postfix Expression: If an operator follows the two operands, it is called postfix expression.
✓ Example: ab +
❖ Prefix Expression: an operator precedes the two operands, it is called prefix expression.
Prof. K. Adisesha (Ph. D)✓ Example: +ab
55
Stack
Arithmetic Expression:
Queue:
A queue is an ordered collection of items where an item is inserted at one end called the
“rear” and an existing item is removed at the other end, called the “front”.
➢ Queue is also called as FIFO list i.e. First-In First-Out.
➢ In the queue only two operations are allowed enqueue and dequeue.
➢ Dequeue means removing the front item.The people standing in a railway reservation
row are an example of queue.
Queue:
A queue is an ordered collection of items where an item is inserted at one end called the
“rear” and an existing item is removed at the other end, called the “front”.
➢ Let QUEUE is a array, two pointer variables called FRONT and REAR are
maintained.
➢ The pointer variable FRONT contains the location of the element to be removed or
deleted.
➢ The pointer variable REAR contains location of the last element inserted.
Types of Queues
: ueue can be of four types:
Q
➢ Simple Queue
➢ Circular Queue
➢ Priority Queue
➢ De-queue ( Double Ended Queue)
Types of Queues
: imple Queue:
S
➢ Simple Queue: In simple queue insertion occurs at the rear end of the list and
deletion occurs at the front end of the list.
Types of Queues
: ircular Queue:
C
➢ Circular Queue: A circular queue is a queue in which all nodes are treated as circular
such that the last node follows the first node.
Types of Queues
: riority Queue:
P
➢ A priority queue is a queue that contains items that have some present priority. An
element can be inserted or removed from any position depending upon some priority.
Types of Queues
: equeue Queue:
D
➢ Dequeue: It is a queue in which insertion and deletion takes place at the both ends.
Operation on Queues
: he various operation performed on Queue are :
T
➢ Queue( ): It creates a new queue that is empty.
Application of Queue:
➢ Simulation
➢ Various features of Operating system
➢ Multi-programming platform systems.
➢ Different types of scheduling algorithms
➢ Round robin technique algorithms
➢ Printer server routines
➢ Various application software’s is also based on queue data structure.
Prof. K. Adisesha (Ph. D)
69
Lists
➢ In the first node, if BACK contains NULL, it indicated that it is the first node in
the list.
➢ The in which FORW contains, NULL indicates that the node is the last node.
Prof. K. Adisesha (Ph. D)
74
Lists
Traversing is the process of accessing each node of the linked list exactly once to
perform some operation.
ALGORITHM: TRAVERS (START, P)
START contains the address of the first node. Another pointer p is temporarily used to visit all the nodes
from the beginning to the end of the linked list.
Step 1: P = START
Step 2: while P != NULL
Step 3: PROCESS data (P) [Fetch the data]
Step 4: P = link(P) [Advance P to next node]
Step 5: End of while
Step 6: Return
Prof. K. Adisesha (Ph. D)
79
Lists
Deleting an node:
Deleting an item from the linked list:
➢ Deletion of the first node
Trees:
A tree is a data structure consisting of nodes organized as a hierarchy:
➢ Tree is a hierarchical data structure which stores the information naturally in the form
of hierarchy style.
➢ It is a non-linear data structure compared to arrays, linked lists, stack and queue.
➢ It represents the nodes connected by edges.
Binary Tree:
A binary tree is an ordered tree in which each internal node can have maximum
of two child nodes connected to it:
➢ A binary tree consists of:
❖ A node ( called the root node)
❖ Left and right sub trees.
➢ A Complete binary tree is a binary tree in which each leaf is at the same distance from
the root i.e. all the nodes have maximum two subtrees.
Binary tree using array represents a node which is
numbered sequentially level by level from left to
right. Even empty nodes are numbered.
Prof. K. Adisesha (Ph. D)
94
Non-Linear Data structures
Graph:
Graph is a mathematical non-linear data structure capable of representing many
kind of physical structures:
➢ A graph is a set of vertices and edges which connect them.
➢ A graph is a collection of nodes called vertices and the connection between them
called edges.
➢ Definition: A graph G(V,E) is a set of vertices V and a set of edges E.
Thank you: