0% found this document useful (0 votes)
13 views

Module-3 Linked-List Final

Data structure module 3 notes vtu syllabus.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module-3 Linked-List Final

Data structure module 3 notes vtu syllabus.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

DATA STRUCTURES AND APPLICATIONS

MODULE -3

Searching for key item in the list:


 Here we have to search key by comparing info field of each node in a list.
 if item found, then return pointer to the node.
 if item not found, return NULL.

C Function to Search for key item in the list.


NODE search ( int key, NODE first)
{
NODE cur;

if (first = = NULL) // search for empty list


return NULL;

/* compare one after the other */


cur = first;
while( cur ! = NULL)
{
if ( key = = cur -> info)
return cur; //if key found
cur = cur ->link; // point cur to next node
}
return NULL; // key not found
}

JIT,DAVANAGERE 1
DATA STRUCTURES AND APPLICATIONS
Display the items in the list
C Function to display the contents of linked list.

Void display ( NODE first)


{
NODE cur;
if ( first = = NULL)
{
printf(“List is empty”);
return;
}

printf(“the contents of linked list”);


cur = first; //holds address of first node
while( cur != NULL)
{
Printf(“%d”, cur ->info); //display info field of node
cur = cur ->link; //points to the next node
}
printf(“\n”);
}

3.4.1.2 Concatenate two lists:


 Concatenation of two lists means combining the second list at the end of the first list.
Step 1: concatenation of two lists is possible only if two lists exist. If one of the lists is empty,
return the address of the first node of the non-empty list.
Step 2: if two lists are exist, obtain the address of the last node of first list. Pointer cur contains
the address of the last node of the first list.
cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link;
}
First cur second

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

Additional List Operations:


1. C Function to concatenate two list.

NODE concate ( NODE first, NODE second)


{
NODE cur;

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;
}

Reverse a list without creating new nodes.


2. C Function to reverse list.

NODE reverse ( NODE first )


{
NODE cur,temp;
cur = NULL; //initial reversed list

while (first ! = NULL)


{
temp = first -> link; //obtain address of second node
first-> link = cur; // attach first node of list to be reversed
// at the beginning of partially reversed list
cur = first; // point cur to point to newly
// partially reversed list
first = temp;
}
return cur; //contains address of reversed list
}

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

Representing a node of Circular Linked List:


Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE last; // last- self-referential structure variable
last = NULL; // Empty list by name last is created with NULL value
if (last = = NULL)
{
printf(“list is empty\n”);
}

Operations on Circular Singly Lined List:


1. Inserting a node into list : a) insert front b) insert rear
2. Deleting a node from list: a) delete front b) delete rear
3. Display the content of list

3.4.2.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 40 into info field of temp. temp


temp -> info = item; 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

C Function to insert an item at the front end of the list.


NODE insert_front (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; // link last node to first node
return last; // return the last node
}

3.4.2.2 Inserting a node at rear:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

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”);
}

Step 2: if list is existing, obtain the address of the first node.


first = last -> link;
first last

10 20 30 50

Step 3: Make second node as first node.


last-> link = first -> link;
first last

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.

NODE delete_front ( NODE last)


{
NODE first;
if ( last = = NULL)
{
printf(“List is empty”);
}
first = last ->link; //obtain node to be deleted
last ->link = first->link; // store new first node in link of last
printf(“item deleted =%d”, first->info);
free(first); // delete old node
return last; // return address of last node
}

3.4.2.3 Delete a node from rear 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”);
}
Step 2: if list is existing, obtain the address of the first node.
prev = last -> link;
first last

10 20 30 50

Step 3: find the address of the last but one node.


While(prev -> link ! = last)
{ prev = prev -> link;
}
first prev last

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.

NODE delete_rear ( NODE last)


{
NODE prev;
if ( last = = NULL)
{
printf(“List is empty”);
}
prev = last ->link; //obtain the address of prev node
while(prev -> link ! = last)
{
prev = prev -> link;
}
prev->link=last->link;
printf(“item deleted =%d”, last->info);
free(last); // delete old last node
return prev; // return address of new last node
}

Doubly linked 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

3.4.3.1 Insert node at front


Step 1: create a node using getnode() function, copy the item 40 into info field and assign
NULL to llink & rlink.
temp = getnode (); temp
temp -> info = item;
\0 40 \0
temp -> llink = temp -> rlink = NULL;

Step 2: insert into existing link.


temp-> rlink = first;
first -> llink = temp;
temp first

\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

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> llink = temp-> rlink =NULL;
if(first == NULL)
return temp; //insert node for first time
temp ->rlink = first; //insert at front of existing list
first -> llink = temp;
return temp; //return the address of new first node
}

3.4.3.2 Insert node at the rear


Step 1: create a node using getnode() function, copy the item 40 into info field and assign
NULL to llink & rlink.
temp = getnode (); temp
temp -> info = item;
\0 40 \0
temp -> llink = temp -> rlink = NULL;

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

C Function to insert an item at the rear end of the list.


NODE insert_rear (int item, NODE first )
{
NODE temp,cur;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> llink = temp-> rlink =NULL;
if(first == NULL)
return temp; //insert node for first time
cur = first;
while(cur -> rlink ! = NULL) //find address of last node
{
cur = cur -> rlink;
}
cur -> rlink = temp; //insert node at the end
temp -> llink = cur;
return first; //return the address of first 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”);
}

Step 2: if list is existing, obtain the address of the second node.


second = first -> rlink;
first second

\0 10 20 30 \0

Step 3: Make second node as first node.


second->l link =NULL;
first second

\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

C Function to delete an item from front end of the list.

NODE delete_front ( NODE first)


{
NODE second;
if ( first = = NULL)
{
printf(“List is empty”);
}
second = first -> rlink; //get address of second node
second -> llink =NULL; // make second as first node
printf(“item deleted =%d”, first->info);
free(first); // delete first node
return second;
}

3.4.3.4 Delete a node at the rear


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

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

Step 3:the last node pointed by cur can be deleted.


free(cur);

first prev cur

\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

C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE first)


{
NODE cur,prev;
if ( first = = NULL)
{
printf(“List is empty”);
}
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
printf(“item delted =%d”, cur -> info);
free (cur);
prev -> rlink = NULL;
return first;
}

Sparse Matrix

Representation

 The sparse matrix can be more efficiently represented using linked list.
 Header node is represented by 3 fields.

Next
Down right

 Items/elements in matrix are represented by 5 fields.

row col val


Down right

JIT,DAVANAGERE 12
DATA STRUCTURES AND APPLICATIONS

Representation of sparse matrix using linked list:

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:

4.1 Trees Terminology


1. Root: In a tree data structure, the first node is called as Root Node. Every tree must have root
node. We can say that root node is the origin of tree data structure. In any tree, there must be only
one root node. We never have multiple root nodes in a tree. Ex: „A‟ in the above tree
2. Edge: In a tree data structure, the connecting link between any two nodes is called as EDGE. In a
tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges. Ex: Line between
two nodes.
3. Parent: In a tree data structure, the node which is predecessor of any node is called as PARENT
NODE. In simple words, the node which has branch from it to any other node is called as parent
node. Parent node can also be defined as "The node which has child / children".
Ex: A,B,C & D are parent nodes
4. Child: In a tree data structure, the node which is descendant of any node is called as CHILD
Node. In simple words, the node which has a link from its parent node is called as child node. In a
tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are
child nodes. Ex: B & C are children of A, G & F are children of C and E child of B.
5. Siblings: In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In
simple words, the nodes with same parent are called as Sibling nodes. Ex: B & C are siblings, D, E,
F and G are siblings, I & H are siblings.

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

9. Level of a node: In a tree data structure, the root node is


said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1
will be at Level 2 and so on... In simple words, in a tree
each step from top to bottom is called as a Level and the
Level count starts with '0' and incremented by one at each
level (Step).

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'.

11. Depth: In a tree data structure, the total number of


edges from root node to a particular node is called as
DEPTH of that Node. In a tree, the total number of
edges from root node to a leaf node in the longest path
is said to be Depth of the tree. In simple words, the
highest depth of any leaf node in a tree is said to be
depth of that tree. In a tree, depth of the root node 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.

4.2 General Tree Representations


A general Tree Structure can be represented with the following three methods.
1. List Representation
2. Left Child - Right Sibling Representation
3. Degree two Representation (Binary Tree Representation)
Consider the following tree...

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.

2. Left Child - Right Sibling Representation

3. Degree two Representation (Binary Tree Representation)

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:

4.3.1 Types of Binary Trees


1. Strictly Binary Tree
 A strictly Binary Tree is a binary tree, where every node should have exactly two children or none.
That means every internal node must have exactly two children.
 Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree.

2. Complete Binary Tree


 A binary tree in which every internal node has exactly two children and all leaf nodes are at same
level is called Complete Binary Tree. Complete binary tree is also called as Perfect Binary Tree.
 In complete binary tree all the nodes must have exactly two children and at every level of complete
binary tree there must be 2level number of nodes. For example at level 2 there must be 22 = 4 nodes
and at level 3 there must be 23 = 8 nodes.

3. Almost complete Binary Tree


 It is complete binary tree but completeness property is not followed in last level. In the above tree
absence of leaf nodes L, M, N, O and P indicates its almost complete binary tree.

JIT,DAVANAGERE 5
DATA STRUCTURES AND APPLICATIONS

Binary Tree Representations


 A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
2. Linked List Representation
Consider the following binary tree...

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.

2. Linked List Representation


 The linked notation uses a doubly linked list to represent a binary tree. In a double linked list, every
node consists of three fields. First field for storing left child address, second for storing actual data
and third for storing right child address. In this linked list representation, a node has the following
structure...

JIT,DAVANAGERE 6
DATA STRUCTURES AND APPLICATIONS
The above example of binary tree represented using Linked list representation is shown as follows...

4.4 Binary Tree Traversals


 Tree traversal is a method of visiting the nodes of a tree in a particular order. The tree nodes are
visited exactly once and displayed as they are visited. Displaying (or) visiting order of nodes in a
binary tree is called as Binary Tree Traversal.
 There are three types of binary tree traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal

4.4.1 In - Order Traversal ( left Child - root – right Child )


 In In-Order traversal, the root node is visited between left child and right child. In this traversal, the
left child node is visited first, then the root node is visited and later we go for visiting right child
node. This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.
void inorder(NODE root)
{
inorder (root->llink);
printf("%d\t",root->data);
inorder (root->rlink);
}

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);
}

4.4.3 Post - Order Traversal ( leftChild - rightChild - root )


 In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left
child node is visited first, then its right child and then its root node. This is recursively performed
until the right most node is visited.
void postorder(NODE root)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->data);
}

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

Threaded Binary Tree


 A binary tree is represented using array representation or linked list representation. When a binary
tree is represented using linked list representation, if any node is not having a child we use NULL
pointer in that position.
 In any binary tree linked list representation, there are more number of NULL pointer than actual
pointers. Generally, in any binary tree linked list representation, if there are 2N number of reference
fields, then N+1 number of reference fields are filled with NULL ( N+1 are NULL out of 2N ). This
NULL pointer does not play any role except indicating there is no link (no child).
 A. J. Perlis and C. Thornton have proposed new binary tree called "Threaded Binary Tree", which
make use of NULL pointer to improve its traversal processes. In threaded binary tree, NULL
pointers are replaced by references to other nodes in the tree, called threads.
 Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked
list representation) points to its in-order predecessor, and all right child pointers that are NULL (in
Linked list representation) points to its in-order successor. If there is no in-order predecessor or in-
order successor, then it points to root node.
 Consider the following binary tree with header node...

 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

1. Define linked list? List different types of linked list.


2. Explain the operations on singly linked list in detail (functions of insert, delete rear &front).
3. What is circular linked list? Explain insert front and delete front operation on CLL.
4. Write the function for following operation on SLL.
i) Concatenation of two SLL
ii) Reversing a SLL
iii) Search a key element in the SLL
5. Write the function for following operation on DLL.
i) Inserting node at front end.
ii) Deleting node from rear end.
6. Represent the given polynomials in SLL and Write an algorithm for addition of two polynomials.
A = 3X14 + 8X6 + 1 B = 8X14 + 5X10 + 3X4
7. Represent the sparse matrix in linked list format.

8. Define Binary tree. Explain the following with examples.


i. Complete binary tree iii. Almost complete binary tree
ii. Skewed binary tree iv. Degree of binary tree

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

You might also like