Lab Manual
Lab Manual
Lab Manual
Course Code:
CSL301 Academic
Year: 2021-22
This document contains information that is proprietary and confidential to NHITM, which shall not be
disclosed to any external entity that is not involved in the project and shall not be transmitted, or duplicated,
used in whole or in part for any purpose other than its intended purpose. Any use/copy or disclosure in whole
or in part of this information without express written of permission of NHITM is strictly prohibited.
2. Problem Analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues, and the consequent responsibilities
relevant to the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
11. Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
Life-long Learning: Recognize the need for and have the preparation and ability to engage in independent
and life-long learning in the broadest context of technological change.
Programmed: Undergraduate
Department / Course: Second Year Engineering (CS)
Theory:
Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or FILO(First
In Last Out). There are many real-life examples of a stack. Consider an example of
plates stacked over one another in the canteen. The plate which is at the top is the
first one to be removed, i.e. the plate which has been placed at the bottommost
position remains in the stack for the longest period of time. So, it can be simply seen
to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
There are some basic operations that allow us to perform different actions on a
stack.
Push: Add an element to the top of a stack
Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it
Algorithm:
Algorithm to implement the push operation under array representation of
Stacks:
Step 1: Start
Step 2: If top=MAX-1 goto Step 3 else goto Step 4
Step 3: Display message “Stack Full” and exit
Step 4: top=top+1
Step 5: stack[top]=element
Step 6: Stop
Algorithm to implement the pop operation under array representation of
Stacks:
Step 1: Start
Step 2: If top=-1 goto Step 3 else goto Step 4
Step 3: Display the message,” Stack Empty” and Exit
Step 4: Return stack[top] and set top=top-1
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
#define n 100
struct st
{
int top;
int stack[n];
}s;
void push();
void pop();
void peek();
void display();
void main()
{
char choice;
int option;
s.top=-1;
do
{
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.DISPLAY\n");
if(s.top==-1)
printf("Stack Underflow\n");
else
{
x=s.stack[s.top];
printf("Value popped = %d\n",x);
s.top--;
}
}
void display()
{
int i;
if(s.top==-1)
printf("Stack Empty\n");
else
for(i=0;i<=s.top;i++)
{
printf("%d\t",s.stack[i]);
}
}
Output:
Experiment No 2
Aim: Implement Linear Queue ADT using array.
Theory:
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is
open at both its ends. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first. A real-world example of queue can be a single-lane one-
way road, where the vehicle enters first, exits first. More real-world examples can be seen as
queues at the ticket windows and bus-stops.
Basic Queue Operations:
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic operations
associated with queues −
• enqueue() − add (store) an item to the queue.
• dequeue() − remove (access) an item from the queue.
Algorithm:
Algorithm to realize the insert operation under array implementation of queues:
©New Horizon Institution of Technology and Management www.nhitm.ac.in 7|Page
Data Structures Course Code:CSL301 Lab Manual
Step 1: Start
Step 2: If front =NULL goto Step 3 else goto Step 6
Step 3: front=rear=0
Step 4: queue[front]=element
Step 5: Goto Step 10
Step 6: If rear=MAX-1 goto Step 7 else goto Step 8
Step 7: Display the message, “Queue is Full” and goto Step 10
Step 8: rear=rear+1;
Step 9: queue[rear]=element
Step 10: Stop
Algorithm to realize the delete operation under array implementation of queues:
Step 1: Start
Step 2: If front=NULL and rear=NULL goto Step 3 else goto Step 4 Step 3:
Display the message,” Queue is Empty” and goto Step 10 Step 4: If front
!=NULL and front =rear goto Step 5 else goto Step 8 Step 5: Set
i=queue[front]
Step 6: Set front=rear=-1
Step 7: Return the deleted element i and goto Step 10
Step 8: Set i=queue[front]
Step 9: Return the deleted element i
Step 10: Stop
Program:
#include<stdio.h>
#define n 5
struct que
{
int f,r;
int queue[n];
}q;
void add();
void del();
void display();
©New Horizon Institution of Technology and Management www.nhitm.ac.in 8|Page
Data Structures Course Code:CSL301 Lab Manual
void main()
{
char choice;
int option;
q.f=-1;
q.r=-1;
do
{
printf("1.ADD\n");
printf("2.DELETE\n");
printf("3.DISPLAY\n");
printf("Enter your option\n");
scanf("%d",&option);
switch(option)
{
case 1:add();
break;
case 2:del();
break;
case 3:display();
break;
default:printf("Invalid Option\n"); }
}
while(option !=4);
}
void add()
{
int x;
if(q.r==n-1)
printf("Queue Overflow\n"); else
{
printf("Enter value to be added\n");
scanf("%d",&x);
q.r++;
q.queue[q.r]=x;
if(q.r==0)
q.f++;
printf("Value added successfully\n"); }
}
void del()
{
©New Horizon Institution of Technology and Management www.nhitm.ac.in 9|Page
Data Structures Course Code:CSL301 Lab Manual
int x;
if(q.f==-1)
printf("Queue Underflow\n");
else
{
x=q.queue[q.f];
printf("Value Deleted=%d\n",x);
if(q.f==q.r)
{
q.f=-1;
q.r=-1;
}
else
q.f++;
}
}
void display()
{
int i;
if(q.f==-1)
printf("Queue Underflow\n");
else
for(i=q.f;i<=q.r;i++)
{
printf("%d\t",q.queue[i]);
}
}
Output:
*Add output which will include adding, deleting, displaying, Queue Overflow, Queue
Underflow
Conclusion: The queue data structure is a linear type of data structure that is used to store
the elements. In this, elements are stored in the FIFO technique. A queue
data structure use an array or linked list during its implementation. Insertion in queue occurs
at the REAR end, and deletion from queue occurs at the FRONT end.
THEORY:
A linked list a non-sequential collection of data items called nodes. These nodes in principle are structures
containing fields. Each node in a linked list basically contains two fields.
1. Data field
2. Link field
The data field contains an actual value to be stored and processed. And the link field contains the address of
the next data item (or node). In other words, it establishes a link to the next data item in a linked list.
Algorithm:
Inserting a new node at the beginning of the linked list-
Step-1: Start
Step-2: Get the value for New Node to be added to the list.
Step-3: Create a New Node, empty node by calling malloc(). If malloc() returns no error, then go
to step-4 or else say "Memory shortage".
Step-4: New Node -> data = value.
©New Horizon Institution of Technology and Management www.nhitm.ac.in 11 | P a g e
Data Structures Course Code:CSL301 Lab Manual
Step-4: Set NEW node -> next = Head.
Step-5: Set the external pointer Head -> New Node.
Program Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
©New Horizon Institution of Technology and Management www.nhitm.ac.in 15 | P a g e
Data Structures Course Code:CSL301 Lab Manual
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
©New Horizon Institution of Technology and Management www.nhitm.ac.in 16 | P a g e
Data Structures Course Code:CSL301 Lab Manual
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
©New Horizon Institution of Technology and Management www.nhitm.ac.in 17 | P a g e
Data Structures Course Code:CSL301 Lab Manual
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
©New Horizon Institution of Technology and Management www.nhitm.ac.in 18 | P a g e
Data Structures Course Code:CSL301 Lab Manual
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\n Data values are: \n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Theory:
What is Stack?
A Stack is a linear data structure which allows adding and removing of elements in a particular order.
New elements are added at the top of Stack. If we want to remove an element from the Stack, we can
only remove the top element from Stack. Since it allows insertion and deletion from only one end and
the element to be inserted last will be the element to be deleted first, hence it is called Last in First
Out data structure (LIFO).
• Push - it specifies adding an element to the Stack. If we try to insert an element when
the Stack is full, then it is said to be Stack Overflow condition
• Pop - it specifies removing an element from the Stack. Elements are always removed
from top of Stack. If we try to perform pop operation on an empty Stack, then it is said
to be Stack Underflow condition.
• Peek - it will show the element on the top of Stack(without removing it).
static. It can also result in "Stack overflow" if we try to add elements after the array is full. So, to
alleviate this problem, we use linked list to implement the Stack so that it can grow in real
time.
What is Queue?
A Queue is also a linear data structure where insertions and deletions are performed from
two different ends. A new element is added from the rear of Queue and deletion of existing
element occurs from the front. Since we can access elements from both ends and the
element inserted first will be the one to be deleted first, hence Queue is called First in First
Out data structure (FIFO).
Program:-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Quit\n");
printf("\nEnter your choice ?");
} Output:
Theory:
A doubly linked list or a two-way linked list is a more complex type of linked list which contains a pointer to the next
as well as the previous node in the sequence. Therefore, it consists of three parts—data, a pointer to the next node, and
a pointer to the previous node.
In C, the structure of a doubly linked list can be given as,
struct node
{
struct node *prev;
int data;
struct node *next;
};
The PREV field of the first node and the NEXT field of the last node will contain NULL. The PREV
field is used to store the address of the preceding node, which enables us to traverse the list in the
backward direction.
Thus, we see that a doubly linked list calls for more space per node and more expensive basic
operations. However, a doubly linked list provides the ease to manipulate the elements of the
list as it maintains pointers to nodes in both the directions (forward and backward).
Program Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void deletion_beginning();
void display();
void main ()
{
insertion_beginning();
insertion_last();
insertion_last();
insertion_last();
insertion_beginning();
display();
©New Horizon Institution of Technology and Management www.nhitm.ac.in 28 | P a g e
Data Structures Course Code:CSL301 Lab Manual
deletion_beginning();
display();
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value: ");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value: ");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
©New Horizon Institution of Technology and Management www.nhitm.ac.in 29 | P a g e
Data Structures Course Code:CSL301 Lab Manual
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n values are: \n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
©New Horizon Institution of Technology and Management www.nhitm.ac.in 30 | P a g e
Data Structures Course Code:CSL301 Lab Manual
ptr=ptr->next;
}
}
Output:
Conclusion:
Thus we have successfully implemented the Doubly linked list.
Theory:
Algorithm:
Program:-
DFS:- #include<stdio.h>
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0); }
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1; for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
Output:
Algorithm:
Program:-
BFS:-
#include<stdio.h> #include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for (i=0;i<n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1; bfs(q[f++]);
}
}
void main()
{
int v;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=0;i<n;i++)
{
for (j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
for(i=0;i<n;i++)
{
if(visited[i])
{
printf("\n The node %d is reachable",i);
}
else
{
Experiment No: 7 Implement Binary Search Tree ADT using Linked List.
AIM: Write a C Program to implement binary search tree adt using linked list.
Theory:
A binary tree is a tree data structure in which each node has at most two children, which are referred to as
the left child and the right child.Every node excluding a root in a tree is connected by a directed edge from
exactly one other node. This node is called a parent. On the other hand, each node can be connected to
arbitrary number of nodes, called children. Nodes with no children are called leaves, or external nodes.
Nodes which are not leaves are called internal nodes. Nodes with the same parent are called siblings.
• The depth of a node is the number of edges from the root to the node.
• The height of a node is the number of edges from the node to the deepest leaf.
• The height of a tree is a height of the root.
• A full binary tree.is a binary tree in which each node has exactly zero or two children.
• A complete binary tree is a binary tree, which is completely filled, with the possible exception of
the bottom level, which is filled from left to right.
• Root − The node at the top of the tree is called root. There is only one
root per tree and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node
called parent.
• Child − The node below a given node connected by its edge downward is
called its child node.
• Leaf − The node which does not have any child node is called the leaf
node.
• Subtree − Subtree represents descendants of node.
No. of nodes, n, in a full binary tree is atleast n = 2h – 1, and atmost n = 2h+1 – 1, where h is the height of
the tree.
No. of leaf nodes l, in a full binary tree is number, L of internal nodes + 1, i.e, l = L+1.
• Perfect binary tree: It is a binary tree in which all interior nodes have two children and all leaves
have the same depth or same level.
A perfect binary tree with l leaves has n = 2l-1 nodes.
In perfect full binary tree, l = 2h and n = 2h+1 - 1 where, n is number of nodes, h is height of tree and l is
number of leaf nodes
• Balanced Binary tree: A binary tree is balanced if for each node it holds that the number of inner
nodes in the left subtree and the number of inner nodes in the right subtree differ by at most 1.
A binary tree is balanced if for any two leaves the difference of the depth is at most 1.
• Degenarate tree: It is a tree is where each parent node has only one child node. It behaves like
a linked list.
• Complete Binary Tree: A complete binary tree is a binary tree in which every level, except
possibly the last, is completely filled, and all nodes are as far left as
possible.
Program Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
void inorder(struct node *);
void insert(int);
if(v<t->data)
t=t->left;
else
t=t->right;
}
if(v<p->data)
p->left=r;
else
p->right=r;
}
}
void inorder(struct node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d ",t->data);
inorder(t->right);
}
}
if (x> T->data)
{
T->right = Delete(T->right, x);
return (T);
}
if (T->left==NULL && T->right==NULL)
{
temp=T;
free(temp);
return(NULL);
}
if (T->left == NULL)
{
temp=T;
T=T->right;
free(temp);
return(T);
}
if (T->right == NULL)
{
©New Horizon Institution of Technology and Management www.nhitm.ac.in 42 | P a g e
Data Structures Course Code:CSL301 Lab Manual
temp=T;
T=T->left;
free(temp);
return(T);
}
temp = minValueNode(T->right);
T->data=temp->data;
T->right = Delete(T->right, temp->data);
return (T);
}
Program Output:
Conclusion: