Data Structure
Data Structure
Data structure is a way of grouping multiple data of same or different datatypes for efficient
usage of memory. This kind of grouped data is called data structure. Ex: arrays, stack, queue
1. Linear data structure - A data structure is said to be linear, if the elements form a
sequence. That is data are arranged in a linear order.
Ex: arrays, stack, queue
2. Non linear data structure - Here the data elements can be attached to several
other data elements to represent specific relationships that exist among them
Ex: trees, graphs
Homogenous data structures - if elements of the data structure are of same datatype, it is
called Homogenous data structures Ex: arrays, stack, queue
Non - Homogenous data structures - if elements of the data structure are of different
datatype , it is called Non- Homogenous data structures Ex: class , interface
Trees
Tree is a nonlinear data structure .it is a rooted structure with one specially designed node called
Root.
Binary tree
It is a structure in which each node can have at the most two children and in which a unique path
exists from the root to every other node. A binary tree is either empty ( also called null tree) or it
consists of a node called root together with remaining two nodes each being a binary tree itself
1. It is acyclic
2. No two nodes can be similar
3. It is recursive
4. There is only one unique path between two nodes
5. Every nodes has a maximum of two sub nodes
6. A binary tree contain only one root node
1. Full binary tree – Every node other than the child node has two children
1
2. Complete binary tree
A tree in which every level except possibly the last level is completely filled , and all nodes are
as far left as possible
A binary tree consists of other binary trees as its children. That is a binary tree is made up of
other binary trees A
B C
D E F G
The above binary tree is a recursive structure as it has two binary trees as its children
TERMS
1. Node - It is a structured data having two parts, data and link to another node
2. Root node - It is the first or top most node of a tree. It does not have any parent
node.
3. External node or Leaf node or Terminal node - It is a node that does not have
any child node.
4. Internal node - It is a node that has child node or nodes excluding root.
5. Sibling node - Nodes that appear from the same parent are called Sibling node.
6. Sub tree - It is a part of a tree in which a node behaves as the root having its
own set of child nodes. Every node is capable of becoming a root.
7. Predecessor node and Successor node - Every node in a binary tree, except
the root has a unique parent, called the predecessor of it. Successor is also called child
node in which it is a branch of another node.
8. Edge - The connecting line between two nodes ia called edge.
9. Path - It is the sequence of continues edges between two nodes.
10. Size of a tree - It is the total number of nodes present in the tree.
11. Degree of a tree - The maximum degree of a node in a given tree is called
degree of a tree.
12. In – degree - The number of edges that are going to a node
13. Out – degree - The number of edges that are coming out of a node.
14. Level of a tree - Each node in a binary tree is assigned a level number as root at
level 0, and then each child node having level 1 more than its parent node.
15. Height of a tree - It is the number of edges of the largest path from root to
leaf
16. Depth of a tree - it is the number of edges from root to that node.
2
Q. Difference between internal nodes and external nodes?
LINKED LIST
A Linked list is a linear collection of data elements, called nodes, pointing to the next following
nodes by means of pointers. There are three types of linked list
Singly linked list contains nodes with single pointer pointing to the next node in sequence. In a
singly linked list, the reference START is a special reference which stores the first address of a
linked list. The NEXT or LINK references of the last node stores null value, which means this
node is not pointing to any other node. That is it is the last node in the list. A linked list has two
parts
1. Data or Info
2. Next or Link
Doubly linked list
It contains 2 pointers, one pointing to the previous node and the other pointing to the next
node.
Circular Linked list
Here the last item contains link of the first element as Next and the first element has a link to
the last element as Previous.
Difference between arrays and linked list.
Array elements can be modified easily it is a complex process for modifying the node in a linked
by identifying the index value it is a complex process for modifying the node in a linked
list
Arrays have a fixed size Link list do not have a fixed size. Nodes can be added or
deleted
3
LINKED LIST ALGORITHMS
START=NEWPTR
Then {
SAVE=START
START = NEWPRT
NEWPTR. Link=SAVE
8) END
2. Inserting a node at the middle in a sorted linked list
1) BEGIN
2) Declare the pointers START, PTR, NEWPTR, SAVE
3) Initialize the pointer PTR=START
4) Allocate memory for new node
NEWPTR = new Node
5) If NEWPTR= Null
PRINT “NO SPACE AVAILABLE”
GO TO Step 8
Else
{
NEWPTR. Info= ITEM
NEWPTR. Link=NULL
4
}
6) Repeat step 7 until PTR = NULL
7) If NEWPTR.INFO > PTR.INFO
THEN {
SAVE = PTR
PTR = PTR.LINK
ELSE {
SAVE.LINK = NEWPTR
NEWPTR.LINK = PTR
8) END
3. Inserting a node at the ends in a sorted linked list
1) BEGIN
2) Declare the pointers START, PTR, NEWPTR, SAVE
3) Initialize the pointer PTR=START
4) Allocate memory for new node
NEWPTR = new Node
5) If NEWPTR= Null
PRINT “NO SPACE AVAILABLE”
GO TO Step 8
Else
{
NEWPTR. Info= ITEM
NEWPTR. Link=NULL
}
6) Repeat step 7 until PTR = NULL
7) SAVE= PTR
PTR= PTR.LINK
8) If PTR = NULL then
{
SAVE. LINK = NEWPTR
NEWPTR. LINK = NULL
}
9) END
5
4. Deleting a node from the beginning of a sorted linked list
1) If START = NULL, THEN, PRINT “Underflow”
2) Else if (START==PTR)
START = START.LINK
3) END
5. Deleting a node from the middle of a sorted linked list
1) Set PTR = START
2) Repeat steps 3 and 4 until PTR= NULL
3) SAVE = PTR
PTR= PTR.LINK
4) If PTR. INFO = ITEM then
SAVE . LINK = PTR. LINK
5) If PTR= NULL, then Print” Item Not Found”
6) END
6. Deleting a node from the end of a sorted linked list
1) Set PTR = START
2) Repeat steps 3 until PTR. LINK= NULL
3) SAVE = PTR
PTR= PTR.LINK
4) SAVE. LINK = NULL
5) END
7. Process and print the info in each node of the linked list( Traversal)
1) Set PTR = START
2) Repeat steps 3 and 4 until PTR= NULL
3) Print PTR. INFO
4) PTR = PTR. LINK
5) END
8. Count the number of nodes in a linked list
1) Set PTR = START, ct=0
2) Repeat steps 3 and 4 until PTR= NULL
3) Ct++
4) PTR = PTR. LINK
5) If PTR= NULL, the, PRINT ct
6) END
9. Compute and print the sum of integer items in a linked list
1) Set PTR = START, sum=0
2) Repeat steps 3 and 4 until PTR= NULL
3) sum= sum+ PTR.INFO
4) PTR = PTR. LINK
5) If PTR= NULL, the, PRINT sum
6) END
6
10. Compute and return the product of integer items in a linked list
1) Set PTR = START, product=1
2) Repeat steps 3 and 4 until PTR= NULL
3) product = product * PTR.INFO
4) PTR = PTR. LINK
5) If PTR= NULL, the, return product
6) END
11. Compute and return the sum of nodes that contain only odd integers in a linked list
1) Set PTR = START, sum=0
2) Repeat steps 3 and 4 until PTR= NULL
3) If PTR. INFO%2!=0
sum =sum+PTR.INFO
4) PTR = PTR. LINK
5) If PTR= NULL, the, return sum
6) END
12. Compute and print the sum of nodes that contain only even integers in a linked list
1) Set PTR = START, sum=0
2) Repeat steps 3 and 4 until PTR= NULL
3) If PTR. INFO%2==0
sum =sum+PTR.INFO
4) PTR = PTR. LINK
5) If PTR= NULL, the, print sum
6) END
13. Compute and print the number of nodes that contain only even integers in a linked
list
1) Set PTR = START, ct=0
2) Repeat steps 3 and 4 until PTR= NULL
3) If PTR. INFO%2==0
ct++
4) PTR = PTR. LINK
5) If PTR= NULL, the, print ct
6) END
14. Compute and return the number of nodes that contain only odd integers in a linked
list
1) Set PTR = START, ct=0
2) Repeat steps 3 and 4 until PTR= NULL
3) If PTR. INFO%2!=0
ct++
4) PTR = PTR. LINK
5) If PTR= NULL, the, return ct
6) END
7
15. Compute and print the sum of nodes that contain only positive integers in a linked
list
1) Set PTR = START, sum=0
2) Repeat steps 3 and 4 until PTR= NULL
3) If PTR. INFO>0
sum =sum+PTR.INFO
4) PTR = PTR. LINK
5) If PTR= NULL, the, print sum
6) END
16. Compute and print the product of nodes that contain only negative integers in a
linked list
1) Set PTR = START, product=1
2) Repeat steps 3 and 4 until PTR= NULL
3) If PTR. INFO < 0
product = product *PTR.INFO
4) PTR = PTR. LINK
5) If PTR= NULL, the, print product
6) END
17. Search for an item in a linked list. Also display the contents of that node
1) Set PTR = START, pos=0
2) Repeat steps 3 and 4 until PTR= NULL
3) If ITEM ==PTR. INFO, then
{
pos=pos +1
PRINT ITEM
PRINT pos
break
}
4) PTR = PTR. LINK, pos= pos+1
5) If PTR= NULL, the, print “Item not found”
6) END
18. Concatenate two linked list L and M(involves making last node of L point to start of
M)
1) Set PTR = STARTL
2) Repeat steps 3 and 4 until PTR= NULL
3) SAVE = PTR
4) PTR = PTR. LINK
5) SABE = LINK = STARTM
6) END
8
STACK
It is a linear data structure which follows LIFO principle. (Last In First Out). It means that the
item which is inserted last will be removed first. In stack all the insertion and deletion operations
are performed only on the top of the stack. An insertion in a stack is called pushing and deletion
from a stack is called popping. The 3 basic operations performed on a stack are
1. Push - Adds an item in the stack. If the stack is full, then it is said to be an overflow
condition.
2. Pop - removes an item from the stack. The items are popped in the reversed order
in which they are pushed. If the stack is empty, then it is said to be an underflow condition.
3. Peek or top - returns top element of stack.
Applications of Stack
For push
Step 1 - start
Step 3 - top=top+1
Step 4 - stack[top]=value
Step 5 - stop
For pop
Step 1 - start
Step 3 - top - -
Step 4 - stop
QUEUES
Queue is a linear data structure which follows FIFO principle (First In First Out). It means the
item which is inserted first will be removed first. A queue contains two types of pointers –
FRONT end and REAR end. FRONT end pointer points the data which can be deleted and
REAR end pointer points the element which is inserted last.
9
Applications of queue
1. They are used for searching in special data structures like trees, graphs etc
2. For any computational FIFO services, it acts as some resource.
Ex: printer Queue
3. To implement FIFO services like reservation, telephone enquiry etc
4. For handling scheduling of processors in a multitasking Operating system.
Drawback of stacks and queue when implemented using arrays
Wastage of memory - Even though the items are removed from stack and queue,
they are not destroyed from memory
DEQUE
Deque or Double Ended Queue is a generalized version of Queue data structure that allows
insert and delete at both ends(FRONT END and REAR END). And no changes can be made
elsewhere in the list. The operations are performed based on FIFO (First In First Out)
principle.
The following can be performed in a DEQUE - Insertion at the FRONT END,
Deletion at the FRONT END, Insertion at the REAR END, and Deletion at the REAR END
To insert an element at the REAR end, the REAR end pointer increases, but decreases by one to
delete. To insert an element at the FRONT end, the FRONT end pointer decreases, but increases
by one to delete
CIRCULAR QUEUE
A Circular Queue is an extended version of a normal queue where the last element of the queue
is connected to the first element of the queue forming a circle. The operations are performed
based on FIFO (First In First Out) principle. It is also called ‘Ring Buffer’.
10