Module-3 Linked-List Final
Module-3 Linked-List Final
MODULE -3
JIT,DAVANAGERE 1
DATA STRUCTURES AND APPLICATIONS
Display the items in the list
C Function to display the contents of linked list.
10 20 30 \0 40 50 \0
Step 3: now, attach address of the first node of the second list to cur node.
cur -> link = second;
First cur second
10 20 30 40 50 \0
JIT,DAVANAGERE 2
DATA STRUCTURES AND APPLICATIONS
if (first = = NULL)
return second;
if (second = = NULL)
return first;
cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link; // point cur to next node
}
Cur -> link =second;
return first;
}
JIT,DAVANAGERE 3
DATA STRUCTURES AND APPLICATIONS
3.4.1 Circular Singly Linked List (CSLL)
A circular singly linked list is a singly linked list where the link field of last node of the list
contains address of the first node.
first
10 20 30 40
Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
temp first last
40 10 20 30
JIT,DAVANAGERE 4
DATA STRUCTURES AND APPLICATIONS
Step 4: make temp as the first node by copying temp into link field of last node.
last->link = temp;
first last
40 10 20 30
Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
first last temp
10 20 30 50
Step 4: make temp as the last node by copying temp into link field of last node.
last->link = temp;
first temp
10 20 30 50
JIT,DAVANAGERE 5
DATA STRUCTURES AND APPLICATIONS
C Function to insert an item at the rear end of the list.
NODE insert_rear (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
if(last != NULL)
temp -> link = last ->link; //insert at front
else
last = temp ;
last -> link = temp;
last=temp; // link last node to first node
return temp; // make new node as last node
}
3.4.2.3 Delete a node from the front end:
Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}
10 20 30 50
10 20 30 50
Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);
first last
10 20 30 50
JIT,DAVANAGERE 6
DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from front end of the list.
10 20 30 50
10 20 30 50
Step 4: Make the last but one node as the last node and De-allocate the memory of the last node.
prev -> link = last ->link;
free( last);
first last
10 20 30 50
JIT,DAVANAGERE 7
DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from rear end of the list.
A doubly linked list is a linear collection of nodes where each node isdivided into three parts:
Info – contains information to be stored.
llink – contains the address of the left or previous node. llink info rlink
rlink - contains the address of the right or next node.
Example:
first
\0 10 20 30 \0
\0 40 10 20 30 \0
JIT,DAVANAGERE 8
DATA STRUCTURES AND APPLICATIONS
Step 3: temp is inserted and then temp becomes the first node.
return temp;
first
\0 40 10 20 30 \0
Step 2: To insert the node at rear end, first find the address of the last node.
cur = first;
while(cur -> rlink ! = NULL)
{
cur = cur -> rlink;
}
first cur temp
\0 10 20 30 \0 \0 40 \0
Step 3: Insert node at the end.
cur -> rlink = temp;
temp -> llink = cur;
first cur temp
\0 10 20 30 40 \0
JIT,DAVANAGERE 9
DATA STRUCTURES AND APPLICATIONS
Step 4: return address of the first node.
return first;
first
\0 10 20 30 40 \0
\0 10 20 30 \0
\0 10 \0 20 30 \0
JIT,DAVANAGERE 10
DATA STRUCTURES AND APPLICATIONS
Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);
first first
\0 10 \0 20 30 \0
Step 2:if list is existing then find the address of last node and last but one node.
cur = first, prev = NULL;
while(cur -> rlink ! = NULL)
{ prev = cur;
cur = cur -> rlink;
}
first prev cur
\0 10 20 30 40 \0
\0 10 20 30 40 \0
JIT,DAVANAGERE 11
DATA STRUCTURES AND APPLICATIONS
Step 4: The node pointed by prev becomes the last node.
prev -> rlink = NULL;
first
\0 10 20 30 \0
Sparse Matrix
Representation
The sparse matrix can be more efficiently represented using linked list.
Header node is represented by 3 fields.
Next
Down right
JIT,DAVANAGERE 12
DATA STRUCTURES AND APPLICATIONS
JIT,DAVANAGERE 1
DATA STRUCTURES AND APPLICATIONS
TREES
Trees
“Tree is a non-linear data structure which organizes data in hierarchical fashion and the tree
structure follows a recursive pattern of organizing and storing data”.
Every individual element is called as Node. Node in a tree data structure, stores the actual data of
that particular element and link to next element in hierarchical structure.
If there are N number of nodes in a tree structure, then there can be a maximum of N-1 number of
links.
Example:
JIT,DAVANAGERE 2
DATA STRUCTURES AND APPLICATIONS
6. Leaf: In a tree data structure, the node which does not have a child is called as LEAF Node. In
simple words, a leaf is a node with no child. In a tree data structure, the leaf nodes are also called as
External Nodes. External node is also a node with no child. In a tree, leaf node is also called as
'Terminal' node. Ex: H,I,E,F and G are leaf nodes
7. Internal Nodes: In a tree data structure, the node which has atleast one child is called as
INTERNAL Node. In simple words, an internal node is a node with atleast one child. In a tree data
structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be
Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-Terminal'
nodes. Ex: B,C & D.
8. Degree of a node: In a tree data structure, the total number of children of a node is called as
DEGREE of that Node. In simple words, the Degree of a node is total number of children it has. The
highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'. Ex: Degree of B
is 2, A is 2 and of F is 0
10. Height
In a tree data structure, the total number of edges from leaf
node to a particular node in the longest path is called as
HEIGHT of that Node. In a tree, height of the root node is
said to be height of the tree. In a tree, height of all leaf
nodes is '0'.
JIT,DAVANAGERE 3
DATA STRUCTURES AND APPLICATIONS
12. Path:
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as
PATH between the two Nodes. Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
13. Sub Tree: In a tree data structure, each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.
The ancestors of a node are all the nodes along the path from the root to that node. Ex: ancestor of j
is B & A A forest is a set of n 0 disjoint trees. The notion of a forest is very close to that of a tree
because if we remove the root of a tree we get a forest. For example, in figure 1 if we remove A we
get a forest with three trees.
JIT,DAVANAGERE 3
DATA STRUCTURES AND APPLICATIONS
1. List Representation
A tree can be represented using list as shown below:
1. The information in the root node comes first.
2. It is immediately followed by a list of subtrees of that node.
3. It is recursively repeated for each subtree,
The above tree example can be represented using List representation as follows...
In this representation, we use two types of nodes, one for representing the node with data and
another for representing only references.
We start with a node with data from root node in the tree. Then it is linked to an internal node
through a reference node and is linked to any other node directly. This process repeats for all the
nodes in the tree.
JIT,DAVANAGERE 4
DATA STRUCTURES AND APPLICATIONS
4.3 Binary Tree
Binary tree is a special type of tree data structure in which every node can have either 0 children or
1 child or 2 children but not more than 2 children. One is known as left child and the other is known
as right child.
Example:
JIT,DAVANAGERE 5
DATA STRUCTURES AND APPLICATIONS
1. Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree. Consider the above example of binary tree and it is represented as follows...
To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n+1 - 1.
Advantages of array representation:
Faster access
Easy for implementation
Good for complete binary trees
Disadvantages:
Wastes memory for skewed trees
Implementation of operations requires rearranging (shifting) of array elements.
JIT,DAVANAGERE 6
DATA STRUCTURES AND APPLICATIONS
The above example of binary tree represented using Linked list representation is shown as follows...
JIT,DAVANAGERE 7
DATA STRUCTURES AND APPLICATIONS
4.4.2 Pre - Order Traversal ( root - leftChild - rightChild )
In Pre-Order traversal, the root node is visited before left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order
traversal is applicable for every root node of all subtrees in the tree.
void preorder(NODE root)
{
printf("%d\t",root->data);
preorder(root->llink);
preorder(root->rlink);
}
JIT,DAVANAGERE 8
DATA STRUCTURES AND APPLICATIONS
DATA STRUCTURES AND APPLICATIONS
Level Order Traversal
Level order traversal is a method of traversing the nodes of a tree level by level as in breadth first
traversal.
Level order traversal uses queue thus avoiding stack space usage.
Here the nodes are numbered starting with the root on level zero
continuing with nodes on level 1,2,3…..
The nodes at any level is numbered from left to right
Visiting the nodes using the ordering of levels is called level order
traversal
Queue uses FIFO principle
The level order traversal of the above tree is F B G A D I C E H
Right in threaded binary tree: if the right link of a node is NULL and if it is replaced by the
9
DATA STRUCTURES AND APPLICATIONS
address of the inorder successor.
Left in threaded binary tree: if the left link of a node is NULL and if it is replaced by the address of the
inorder successor.
In-order Threaded binary tree: left and right links are replaced by the address of the inorder
successor.
10
DATA STRUCTURES AND APPLICATIONS
11
DATA STRUCTURES AND APPLICATIONS
12
DATA STRUCTURES AND APPLICATIONS
13
DATA STRUCTURES AND APPLICATIONS
9. Write recursive C functions for in-order, pre-order, post-order traversals of binary tree
(BT). Also give the 3 traversals for the BT shown in figure.
10. What is the advantage of threaded binary tree over binary tree? Explain the construction of
threaded binary tree for 10,20,30,40 and 50.
JIT,DAVANAGERE 14
DATA STRUCTURES AND APPLICATIONS
JIT,DAVANAGERE 15