module 3
module 3
nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
Page 1
DATA STRUCTURES AND APPLICATIONS (BCS304)
Let us assume that the two linked lists are referenced by first1 and first2 respectively.
1. If the first linked list is empty then return first2.
2. If the second linked list is empty then return first1.
3. Store the address of the starting node of the first linked list in a pointer variable,
say temp.
4. Move the temp to the last node of the linked list through simple linked list traversal
technique.
5. Store the address of the first node of the second linked list in the link field of the node
pointed by temp. Return first1.
Program code to concatenate two Linked Lists
struct node {
int data;
};
NODE temp = first1; // place temp on the first node of the first linked list
return (first2);
return (first1);
Page 2
DATA STRUCTURES AND APPLICATIONS (BCS304)
temp = temp->link;
first2 = NULL;
return (first1);
Page 3
DATA STRUCTURES AND APPLICATIONS (BCS304)
int key;
temp = first;
printf(“Enter key”);
if (key == temp🡪data) {
printf(“Search successful”);
return;
} else
temp = temp🡪link;
printf(“Search failed”);}
List is sorted
If we want to search any key element then we need to follow the step to do the search
operation for the sorted list, we need to follow the following steps:
1. Set temp=first
2. Repeat step 3 while temp ≠ NULL
3. If key>temp🡪data then set temp=temp🡪link
Else if key= temp🡪data then display search is success
Else display search is failed
4. Exit
Program code for Searching a key in a Sorted Linked List
void search() {
int key;
temp = first;
printf(“Enter key”);
temp = temp🡪link;
Page 4
DATA STRUCTURES AND APPLICATIONS (BCS304)
printf(“Search successful”);
return;
} else
printf(“Search unsuccessful”);
printf(“Search unsuccessful”);
int node1_data;
temp->link = NULL;
printf(“enter the node1 data for entering the new node to data\n”);
if (first == NULL) {
first = temp;
} else {
ptr = first;
ptr = ptr->link;
temp->link = ptr->link;
ptr->link = temp;
Page 5
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 6
DATA STRUCTURES AND APPLICATIONS (BCS304)
int data;
};
scanf("%d", &newNode->data);
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
temp = head;
temp = temp->next;
newNode->next = head;
head = newNode;
temp->next = head;
printf("\nInsertion success!!!");
scanf("%d", &newNode->data);
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
Page 7
DATA STRUCTURES AND APPLICATIONS (BCS304)
temp = head;
temp = temp->next;
temp->next = newNode;
newNode->next = head;
printf("\nInsertion success!!!");
temp = head;
if (head == NULL) {
head = NULL;
free(temp);
} else {
temp = temp->next;
head = head->next;
temp->next = head;
free(temp2);
printf("\nDeletion success!!!");
temp = head;
if (head == NULL) {
Page 8
DATA STRUCTURES AND APPLICATIONS (BCS304)
head = NULL;
free(temp);
} else {
temp2 = temp;
temp = temp->next;
temp2->next = head;
free(temp);
printf("\nDeletion success!!!");
Int cnt = 0;
do {
cur = cur->link;
cnt++;
return cnt;
Session 17 questions:
Page 9
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 10
DATA STRUCTURES AND APPLICATIONS (BCS304)
#include <stdlib.h>
struct list {
};
void print();
int main() {
int Sparse_Matrix[4][4] = {
if (Sparse_Matrix[i][j] != 0) {
insert(i, j, Sparse_Matrix[i][j]);
Page 11
DATA STRUCTURES AND APPLICATIONS (BCS304)
print();
int item;
if (ptr == NULL) {
printf("\n OVERFLOW");
} else {
ptr->row = r;
ptr->column = c;
ptr->value = v;
if (HEAD == NULL) {
ptr->next = NULL;
HEAD = ptr;
} else {
temp = HEAD;
temp = temp->next;
temp->next = ptr;
ptr->next = NULL;
void print() {
tmp = tmp->next;
Page 12
DATA STRUCTURES AND APPLICATIONS (BCS304)
Session 18 questions:
Page 13
DATA STRUCTURES AND APPLICATIONS (BCS304)
We can create ‘n’ number of nodes together. When you create onenew node, then first is
not NULL now. So now we have to connect new nodes right link to old nodes left as in
Fig.1and then make first to point to temp as in Fig.1.
int data;
};
void create() {
int n, i = 1;
Page 15
DATA STRUCTURES AND APPLICATIONS (BCS304)
scanf("%d", &n);
while (i <= n) {
scanf("%d", temp->data);
if (FIRST == NULL)
else {
END->next = temp;
temp->prev = END;
END = temp;
scanf("%d", temp->data);
if (FIRST == NULL)
else {
END->next = temp;
temp->prev = END;
END = temp;
Page 16
DATA STRUCTURES AND APPLICATIONS (BCS304)
Fig.3: Steps to Inserting a new Node to the front of Doubly Linked List
scanf("%d", temp->data);
if (FIRST == NULL)
else {
temp->next = FIRST;
Page 17
DATA STRUCTURES AND APPLICATIONS (BCS304)
FIRST->prev = temp;
FIRST = temp;
Fig.4: Steps to Deleting a Node from the front of a Doubly Linked List
Program code for delete front operation
temp = FIRST;
Page 18
DATA STRUCTURES AND APPLICATIONS (BCS304)
printf("List is empty\n");
FIRST = NULL;
END = NULL;
free(temp);
FIRST = FIRST->next;
FIRST->prev = NULL;
free(temp);
return;
Page 19
DATA STRUCTURES AND APPLICATIONS (BCS304)
Fig.5: Steps to Deleting a Node from the end of a Doubly Linked List
Page 20
DATA STRUCTURES AND APPLICATIONS (BCS304)
void Deletionend() // delete node at end of DLL
temp = END;
printf("List is empty\n");
FIRST = NULL;
END = NULL;
free(temp);
END = END->prev;
END->next = NULL;
free(temp);
return;
Page 21
DATA STRUCTURES AND APPLICATIONS (BCS304)
void display_count() // Display the status of DLL and count the number of nodes
// in it
temp = FIRST;
int count = 0;
else {
count++;
temp = temp->next;
} // end of else
return;
} // end of display()
Page 22
DATA STRUCTURES AND APPLICATIONS (BCS304)
Session 19 questions:
Page 23
DATA STRUCTURES AND APPLICATIONS (BCS304)
● A doubly linked list points to not only the next node but to the previous node as well. A circular
doubly linked list contains 2 NULL pointers. The ‘Next’ of the last node points to the first node in
a doubly-linked list. The ‘Prev’ of the first node points to the last node.
● A doubly circular linked list looks as follows:
struct node {
int data;
};
In this section, we will see how a new node is added into an already existing circular doubly linked
list. We will take two cases and then see how insertion is done in each case. Rest of the cases are
similar to that given for doubly linked lists.
Page 24
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 25
DATA STRUCTURES AND APPLICATIONS (BCS304)
In this section, we will see how a node is deleted from an already existing circular doubly linked list.
We will take two cases and then see how deletion is done in each case. Rest of the cases are same as
that given for doubly linked lists.
3.6.2.1. Deleting the First Node from a Circular Doubly Linked List:
3.6.2.2. Deleting the Last Node from a Circular Doubly Linked List :
*******************
Page 26
DATA STRUCTURES AND APPLICATIONS (BCS304)
Programming Example
*******************
1. Write a program to create a circular doubly linked list and perform insertions and
deletions at the beginning and end of the list.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
Structnode
{
structnode *next;
intdata;
structnode *prev;
};
intmain()
{
intoption;
clrscr();
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Delete a node from the beginning");
printf("\n 6: Delete a node from the end");
printf("\n 7: Delete a given node");
printf("\n 8: Delete the entire list");
printf("\n 9: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n CIRCULAR DOUBLY LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = delete_beg(start);
break;
case 6: start = delete_end(start);
break;
case 7: start = delete_node(start);
break;
Page 27
DATA STRUCTURES AND APPLICATIONS (BCS304)
case 8: start = delete_list(start);
printf("\n CIRCULAR DOUBLY LINKED LIST DELETED");
break;
}
}while(option != 9);
getch();
return 0;
}
Page 28
DATA STRUCTURES AND APPLICATIONS (BCS304)
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (structnode *)malloc(sizeof(structnode));
new_node-> data = num;
ptr = start;
while(ptr -> next != start)
ptr = ptr -> next;
new_node ->prev = ptr;
ptr -> next = new_node;
new_node -> next = start;
start ->prev = new_node;
start = new_node;
return start;
}
Page 29
DATA STRUCTURES AND APPLICATIONS (BCS304)
structnode *ptr;
intval;
printf("\n Enter the value of the node which has to be deleted : ");
scanf("%d", &val);
ptr = start;
if(ptr -> data == val)
{
start = delete_beg(start);
returnstart;
}
else
{
while(ptr -> data != val)
ptr = ptr -> next;
ptr ->prev -> next = ptr -> next;
ptr -> next ->prev = ptr ->prev;
free(ptr);
return start;
}
}
Session 20 questions:
Page 30
DATA STRUCTURES AND APPLICATIONS (BCS304)
In linear data structure data is organized in sequential order and in non-linear data structure data is
organized in random order. A tree is a very popular non-linear data structure used in a wide range of
applications. In tree data structure, 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.
3.7.Tree Definition
Definition: A tree is a finite set of one or more nodes such that: (i) there is a specially designated node
called the root; (ii) the remaining nodes are partitioned into n>= 0 disjoint sets T1, ...,Tn where each of
these sets is a tree. T1, ...,Tn are called the sub-trees of the root.
In a tree data structure, if we have N number of nodes then we can have a maximum of N-1 number of
links.
3.8.Basic Terminology
Page 31
DATA STRUCTURES AND APPLICATIONS (BCS304)
● Root node: The root node R is the topmost node in the tree. If R = NULL, then it means the tree is
empty. Ex: A is the root node.
In a tree data structure, the first node is called as Root Node. Every tree must have a root node. We
can say that the root node is the origin of the tree data structure. In any tree, there must be only one
root node. We never have multiple root nodes in a tree.
● 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.
● Parent
In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE. In
simple words, the node which has a branch from it to any other node is called a parent node. Parent
node can also be defined as "The node which has child / children".
Page 32
DATA STRUCTURES AND APPLICATIONS (BCS304)
● 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.
Page 33
DATA STRUCTURES AND APPLICATIONS (BCS304)
● Sub-trees: In a tree data structure, each child from a node forms a subtree recursively. Every child
● Internal Nodes:
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.
Non-terminals: The nodes other than leaf nodes are called non terminals.
● Leaf node: A node that has no children is called the leaf node or the terminal node.
Page 34
DATA STRUCTURES AND APPLICATIONS (BCS304)
● Path: A sequence of consecutive edges is called a path. In a tree data structure, the sequence of
Nodes and Edges from one node to another node is called as PATH between that 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.
● Degree
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'
● Degree of a node: Degree of a node is equal to the number of children that a node has. The degree
of a leaf node is zero.
● The degree of a tree :The degree of a tree is the maximum degree of the nodes in the tree.
Page 35
DATA STRUCTURES AND APPLICATIONS (BCS304)
● Level: Every node in the tree is assigned a level number in such a way that the root node is at
level 1, children of the root node are at level number 2. Thus, every node is at one level higher
than its parent. So, all child nodes have a level number given by parent’s level number + 1.
● Height of the tree: 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'.
Height of the tree:is the maximum distance between the root node of the tree and the leaf node of the
tree.
● Depth of the tree: 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
Page 36
DATA STRUCTURES AND APPLICATIONS (BCS304)
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'.
● Ancestor node: An ancestor of a node is any predecessor node on the path from root to that node.
The root node does not have any ancestors.
● Descendant node: A descendant node is any successor node on any path from the node to a leaf
node. Leaf nodes do not have any descendants.
Page 37
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 38
DATA STRUCTURES AND APPLICATIONS (BCS304)
General tree is a tree in which each node can Whereas in binary tree, each node can have at
have many children or nodes. most two nodes.
The subtree of a general tree do not hold the While the subtree of binary tree hold the ordered
ordered property. property.
In general tree, a node can have at While in binary tree, a node can have at
most n(number of child nodes) nodes. most 2(number of child nodes) nodes.
In general tree, there is either zero subtree or While in binary tree, there are mainly two
many subtree. subtree: Left-subtree and Right-subtree.
Page 39
DATA STRUCTURES AND APPLICATIONS (BCS304)
(1) The maximum number of nodes on level i of a binary tree is 2 i-1, i ≥1.
Proof:
Step 1: The proof is by induction on i. Induction Base: The root is the only node on level i = 1.
Hence, the maximum number of nodes on level i =1 is 2i-1 = 20 = 1.
Step 2: Induction Hypothesis: Let i be an arbitrary positive integer greater than 1. Assume that
the maximum number of nodes on level i -1 is 2i-2
Page 40
DATA STRUCTURES AND APPLICATIONS (BCS304)
Step 3: Induction Step: The maximum number of nodes on level i-1 is 2i-2 by the induction
hypothesis. Since each node in a binary tree has a maximum degree of 2, the maximum number of
nodes on level i is two times the maximum number of nodes on level i-1, i.e or 2*2i-2= 2i-1
𝑘 𝑘
𝑖−1 𝑘−1
(2) The maximum number of nodes in a binary tree of depth k is ∑ = ∑ 2 =2
𝑖=1 𝑖=1
Lemma– 3:
For any nonempty binary tree, T, if no is the number of terminal nodes and n2 the
number of nodes of degree 2, then n0 = n2 + 1.
Proof:
Consider the binary tree in the Fig. 3.11. each node in the tree should have a maximum of 2
children. A node may not have any child or it can have single child or it can have 2 children.
But a node in a binary tree can not have more that 2 children.
Hence it is proved that for any nonempty binary tree, T, if no is the number of terminal nodes and n2
the number of nodes of degree 2, then n0 = n2 + 1.
Page 42
DATA STRUCTURES AND APPLICATIONS (BCS304)
2. Linked representation:
● While the above representation appears to be good for complete binary trees it is wasteful
for many other binary trees. In addition, there presentation suffers from the general
inadequacies of sequential representations. Insertion or deletion of nodes from the middle
of a tree requires the movement of potentially many nodes to reflect the change in level
number of these nodes.
● These problems can be easily overcome through the use of a linked representation. Each
node will have three fields LCHILD, DATA and RCHILD as in Fig.3.12.2.
1. Define tree.
2. What is depth of a tree?
3. What is degree of tree?
4. What are two representations of tree?
5. What is a skewed binary tree?
if (node != NULL) {
printf("%d", node->data);
preorder(node->left);
preorder(node->right);
return;
Page 44
DATA STRUCTURES AND APPLICATIONS (BCS304)
To traverse a non-empty binary tree in post-order, the following operations are performed recursively
at each node. The algorithm works by:
1. Traversing the left sub-tree,
2. Traversing the right sub-tree.
3. Visiting the root node
Function code:
void postorder(struct treeNode *node) {
if (node != NULL) {
postorder(node->left);
postorder(node->right);
printf("%d", node->data);
return;
Page 45
DATA STRUCTURES AND APPLICATIONS (BCS304)
To traverse a non-empty binary tree in inorder, the following operations are performed recursively at
each node. The algorithm works by:
1. Traversing the left sub-tree,
2. Visiting the root node
3. Traversing the right sub-tree.
Function code:
void inorder(struct treeNode *node)
{
if (node != NULL)
{
inorder(node->left);
printf("%d", node->data);
inorder(node->right);
}
return;
}
Page 46
DATA STRUCTURES AND APPLICATIONS (BCS304)
Example 2:
Page 47
DATA STRUCTURES AND APPLICATIONS (BCS304)
3.14. Iterative Inorder traversal (or) Inorder Tree Traversal without Recursion.
Here we can use a stack to perform inorder traversal of a Binary Tree. Below is the algorithm for
traversing a binary tree using stack.
● Create an empty stack (say S).
● Initialize the current node as root.
● Push the current node to S and set current = current->left until current is NULL
● If current is NULL and the stack is not empty then:
o Pop the top item from the stack.
o Print the popped item and set current = popped item->right
o Go to step 3.
● If current is NULL and the stack is empty then we are done.
Session 22 questions:
Page 48
DATA STRUCTURES AND APPLICATIONS (BCS304)
Example1:
Example 2:
Page 49
DATA STRUCTURES AND APPLICATIONS (BCS304)
2 3
/ \
4 5
Initially Creates an empty stack: S = NULL and set current as address of root: current -> 1
current = 1: Pushes the current node and set current = current->left until current is NULL:
● current -> 1, push 1: Stack S -> 1
● current -> 2, push 2: Stack S -> 2, 1
● current -> 4, push 4: Stack S -> 4, 2, 1
● current = NULL
Now Pop from S
● Pop 4: Stack S -> 2, 1. Print “4”.
● current = NULL /*right of 4 */ and go to step 3. Since current is NULL step 3 doesn’t do
anything.
Step 4 is repeated and pop again.
● Pop 2: Stack S -> 1. Print “2”.
● current -> 5 /*right of 2 */ and go to step 3
current = 5: Push 5 to stack and make current = current->left which is NULL
● Stack S -> 5, 1. current = NULL
Now pop from S
● Pop 5: Stack S -> 1. Print “5”.
● current = NULL /*right of 5 */ and go to step 3. Since current is NULL step 3 doesn’t do anything
Step 4 repeated again:
● Pop 1: Stack S -> NULL. Print “1”.
● current -> 3 /*right of 1 */
current = 3: Pushes 3 to stack and make current NULL
● Stack S -> 3
● current = NULL
Step 4 pops from S:
● Pop 3: Stack S -> NULL. Print “3”.
● current = NULL /*right of 3 */
Now the traversal is complete as the stack has become empty.
Example2:
Page 50
DATA STRUCTURES AND APPLICATIONS (BCS304)
In level order traversal nodes of tree are traversed level-wise from left to right.
Queue data structure is used to store nodes level wise so that each node’s children are visited.
Page 51
DATA STRUCTURES AND APPLICATIONS (BCS304)
● At level 0, we process the root node first, followed by the left and right children at level 1.
(assuming the order from left to right).
● Similarly, at the second level, we first process the children of the left child of the root then
process the children of the right child. This process goes on for all the levels in the tree.
Page 52
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 53
DATA STRUCTURES AND APPLICATIONS (BCS304)
Page 54
DATA STRUCTURES AND APPLICATIONS (BCS304)
..
Session 23 questions:
● There should be no loose threads in threaded binary tree. But in Figure B two threads have
been left dangling: one in the left child of H, the other in the right child of G.
● In above figure the new threads are drawn in broken lines. This tree has 9 node and 10
Null-links which has been replaced by threads. When trees are represented in memory, it
should be able to distinguish between threads and pointers. This can be done by adding two
additional fields to node structure, ie., leftThread and rightThread
● If ptr→leftThread = TRUE, then ptr→leftChild contains a thread, otherwise it contains
apointer to the left child.
Page 56
DATA STRUCTURES AND APPLICATIONS (BCS304)
threadPointer leftChild;
char data;
threadPointer rightChild;
}threadTree;
● The complete memory representation for the tree of figure is shown in Figure C .
● The variable root points to the header node of the tree, while root →leftChild points to the start
of the first node of the actual tree.
● This is true for all threaded trees. Here the problem of the loose threads is handled by pointing
to the head node called root.
Page 57
DATA STRUCTURES AND APPLICATIONS (BCS304)
Session 24 questions:
Question Bank
.
1. Give a node structure to create a doubly linked list of integers and write a C function to perform
the following.
a. Create a three-node list with data 10, 20 and 30
b. Inert a node with data value 15 in between the nodes having data values 10 and 20
c. Delete the node which is followed by a node whose data value is 20
d. Display the resulting singly linked list..
2. Write a note on: i. Linked representation of sparse matrix ii. Doubly linked list.
3. Write a function to insert a node at front and rear end in a circular linked list. Write down sequence
of steps to be followed.
6. Write a C program to perform the following operations on doubly linked list: i. Insert a node ii.
Delete a node.
7. Write a C function to insert a node at front and delete a node from the rear end in a circular linked
list.
8. Describe the doubly linked lists with advantages and disadvantages. Write a C function to delete a
node from a circular doubly linked list with header node.
9. Write a C function for the concatenation of linked lists.
10. Write a C function to perform the following i. Reversing a singly linked list ii. Concatenating
singly linked list. iii. Finding the length of the circular linked list. iv. To search an element in the
singly linked list
11. Write a node structure of linked stack. Write a function to perform push and pop operations on
linked stack.
12. List out the differences between doubly linked list over singly linked list. Write a C functions to
perform the following i. Inserting a node into a doubly linked circular list ii. Deletion from a
doubly linked circular list.
Page 58
DATA STRUCTURES AND APPLICATIONS (BCS304)
13. Given 2 singly linked lists. LIST-1 and LIST-2. Write an algorithm to form a new list LIST-3 using
concatenation of the lists LIST-1 and LIST-2.
14. Write a note on header linked list. Explain the widely used header lists with diagrams. 23.
Illustrate with examples how to insert a node at the beginning, INSERT a node at intermediate
position, DELETE a node with a given value
15. List out any 2 differences between doubly linked lists and singly linked list, Illustrate with
example the following operations on a doubly linked list: i. Inserting a node at the beginning. ii.
Inserting at the intermediate position. iii. Deletion of a node with a given value
16. For the given sparse matrix write the diagrammatic linked list representation
Page 59
DATA STRUCTURES AND APPLICATIONS (21CS32)
22. Suppose following sequence lists the node of binary tree in preorder and inorder
respectively, draw diagram of the tree:
Preorder: G B Q A C K F P D E R H
Inodrer: Q B K C F A G P E D H R
23. What is threaded binary tree? Explain with example
Page 60