0% found this document useful (0 votes)
10 views43 pages

Lab Manual

The document is a lab manual for the Data Structures course (CSL301) at New Horizon Institute of Technology and Management for the academic year 2021-22. It includes proprietary information, program outcomes, and detailed instructions for various experiments such as implementing Stack ADT, Linear Queue ADT, and Singly Linked List using C programming. Each experiment outlines the theory, algorithms, and program codes necessary for students to complete their lab assignments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views43 pages

Lab Manual

The document is a lab manual for the Data Structures course (CSL301) at New Horizon Institute of Technology and Management for the academic year 2021-22. It includes proprietary information, program outcomes, and detailed instructions for various experiments such as implementing Stack ADT, Linear Queue ADT, and Singly Linked List using C programming. Each experiment outlines the theory, algorithms, and program codes necessary for students to complete their lab assignments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Data Structures Course Code:CSL301 Lab Manual

New Horizon Institute of Technology and Management,


Thane Affiliated to University of Mumbai

Program: Computer Engineering

Course: Data Structure Lab

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.

Program Outcomes (POs)

©New Horizon Institution of Technology and Management www.nhitm.ac.in 1|Page


Data Structures Course Code:CSL301 Lab Manual
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.

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.

3. Design/Development of Solutions: Design solutions for complex engineering problems and


design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

4. Conduct Investigations of Complex Problems: Use research-based knowledge and


research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.

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.

7. Environment and Sustainability: Understand the impact of the professional engineering


solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.

8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.

9. Individual and Teamwork: Function effectively as an individual, and as a member or


leader in diverse teams, and in multidisciplinary settings.

10. Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.

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)

Laboratory: Data Structure Lab


©New Horizon Institution of Technology and Management www.nhitm.ac.in 2|Page
Data Structures Course Code:CSL301 Lab Manual

Subject: Data Structures


Year: 2021-22 Semester: III

Resources / Hardware: Software: Online Computer System GDB Complier


Apparatus Required

Student Roll No.: Student Name:

Experiment No. 1: Implement Stack ADT using array.


AIM: Write a C Program to implement stack add using array.

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

©New Horizon Institution of Technology and Management www.nhitm.ac.in 3|Page


Data Structures Course Code:CSL301 Lab Manual

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

©New Horizon Institution of Technology and Management www.nhitm.ac.in 4|Page


Data Structures Course Code:CSL301 Lab Manual

printf("Enter your option");


scanf("%d",&option);
switch(option)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:printf("Invalid Option\n");
}
printf("Press 'Y' to continue\n");
choice=getch();
}
while(choice=='Y' || choice=='y');
}
void push()
{
int x;
if(s.top==n-1)
printf("Stack Overflow\n");
else
{
printf("Enter value to be pushed\n");
scanf("%d",&x);
s.top++;
s.stack[s.top]=x;
printf("Value added successfully\n");
}
}
void pop()
{
int x;

©New Horizon Institution of Technology and Management www.nhitm.ac.in 5|Page


Data Structures Course Code:CSL301 Lab Manual

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:

©New Horizon Institution of Technology and Management www.nhitm.ac.in 6|Page


Data Structures Course Code:CSL301 Lab Manual

Conclusion: Thus, we have successfully Implement Stack ADT using array.

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.

©New Horizon Institution of Technology and Management www.nhitm.ac.in 10 | P a g e


Data Structures Course Code:CSL301 Lab Manual

Experiment No: 3 Implement Singly Linked List.


AIM: Write a C Program to implement singly linked list operations.

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.

Deleting the firstnode of the linked list


Step-1:Start
Step-2: if Head = NULL then
Print “ Empty Linked List” and goto Step 6.
Step-3: Set p = Head
Step-4: Head = Head - > next
Step-5: Free the memory of node p.
Step-6: Stop
Search for Key element in Linked List
Step-1: If Head = NULL then
Print “ Empty Linked List” and goto Step 6.
Step-2: Read the key value to be searched
Step-3: Set p=Head
Step-4: Repeat while p !=NULL
If p-> data == key then
Print “ Element Found” and goto step 6.
Else
p = p -> next
Step-5: if p==NULL then
Print “ Element not found”
Step-6: Stop

Display the linked list


Step-1: If Head = NULL then
Print “ Empty Linked List” and goto Step 6.
Step-2: Set p=Head
Step-3: Repeat while p !=NULL
©New Horizon Institution of Technology and Management www.nhitm.ac.in 12 | P a g e
Data Structures Course Code:CSL301 Lab Manual
Print “p-> data”
p = p -> next
Step-4: Stop

Program Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\nChoose one option from the following list ...\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\n5.Search
for an element\n6.Show\n7.Exit\n");

©New Horizon Institution of Technology and Management www.nhitm.ac.in 13 | P a g e


Data Structures Course Code:CSL301 Lab Manual
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
©New Horizon Institution of Technology and Management www.nhitm.ac.in 14 | P a g e
Data Structures Course Code:CSL301 Lab Manual
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}

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

©New Horizon Institution of Technology and Management www.nhitm.ac.in 19 | P a g e


Data Structures Course Code:CSL301 Lab Manual

Experiment No. 4: Implement Stack/Linear Queue ADT using Linked List.


AIM: Write a C Program to implement stack/linear queue adt using linked list.

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

Here we will define three operations on Stack,

• 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).

Implementing Stack functionalities using Linked List


Stack can be implemented using both, arrays and linked list. The limitation in case of array is
that we need to define the size at the beginning of the implementation. This makes our Stack
©New Horizon Institution of Technology and Management www.nhitm.ac.in 20 | P a g e
Data Structures Course Code:CSL301 Lab Manual

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 ?");

©New Horizon Institution of Technology and Management www.nhitm.ac.in 21 | P a g e


Data Structures Course Code:CSL301 Lab Manual
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}

©New Horizon Institution of Technology and Management www.nhitm.ac.in 22 | P a g e


Data Structures Course Code:CSL301 Lab Manual
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;

©New Horizon Institution of Technology and Management www.nhitm.ac.in 23 | P a g e


Data Structures Course Code:CSL301 Lab Manual
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}

} Output:

©New Horizon Institution of Technology and Management www.nhitm.ac.in 24 | P a g e


Data Structures Course Code:CSL301 Lab Manual

Conclusion: Thus, we have successfully Implement Stack/Linear Queue ADT using


Linked List.

©New Horizon Institution of Technology and Management www.nhitm.ac.in 25 | P a g e


Data Structures Course Code:CSL301 Lab Manual

Experiment No: 5 Implement Doubly Linked List ADT.

AIM: Write a C Program to implement Doubly Linked List ADT.

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

Algorithm to insert a new node at the beginning:

©New Horizon Institution of Technology and Management www.nhitm.ac.in 26 | P a g e


Data Structures Course Code:CSL301 Lab Manual

Algorithm to insert a new node at the end:

Algorithm to insert a new node after a given node:

©New Horizon Institution of Technology and Management www.nhitm.ac.in 27 | P a g e


Data Structures Course Code:CSL301 Lab Manual
Algorithm to insert a new node before a given node:

Algorithm to delete the first node:

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.

©New Horizon Institution of Technology and Management www.nhitm.ac.in 31 | P a g e


Data Structures Course Code:CSL301 Lab Manual
Experiment No. 6: Implement Graph Travesal techniques.
AIM: Write a C Program to implement graph techniques:
a) Depth First Search
b) Breadth First Search

Theory:

a) Depth First Search: The depth-first search algorithm (Fig. 13.22)


progresses by expanding the starting node of G and then going deeper and
deeper until the goal node is found, or until a node that has no children is
encountered. When a dead-end is reached, the algorithm backtracks
returning to the most recent node that has not been completely explored. In
other words, depth-first search begins at a starting node A which becomes
the current node. Then, it examines each node N along a path P which
begins at A. That is, we process a neighbor of A, then a neighbor of
neighbor of A, and so on. During the execution of the algorithm if we reach
a path that has a node N that has already been processed, then we backtrack
to the current node. Otherwise, the unvisited (unprocessed) node becomes
the current node.

Algorithm:

Program:-

DFS:- #include<stdio.h>

void DFS(int); int


G[10][10],visited[10],n;
void main()
©New Horizon Institution of Technology and Management www.nhitm.ac.in 32 | P a g e
Data Structures Course Code:CSL301 Lab Manual

{
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:

©New Horizon Institution of Technology and Management www.nhitm.ac.in 33 | P a g e


Data Structures Course Code:CSL301 Lab Manual

b) Breadth First Search: Breadth-first search (BFS) is a graph search


algorithm that begins at the root node and explores all the neighbouring
nodes. Then for each of those nearest nodes. That is, we start examining the
node A and then all the neighbours of A are examined. In the next step, we
examine the neighbours of neighbours of A, so on and so forth. This means
that we need to track the neighbours of the node and guarantee that every
node in the graph is processed and no node is processed more than once.
This is accomplished by using a queue that will hold the nodes that are
waiting for further processing and a variable STATUS to represent the
current state of the node.

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;

©New Horizon Institution of Technology and Management www.nhitm.ac.in 34 | P a g e


Data Structures Course Code:CSL301 Lab Manual

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
{

©New Horizon Institution of Technology and Management www.nhitm.ac.in 35 | P a g e


Data Structures Course Code:CSL301 Lab Manual

printf("\n The node %d is not reachable \n",i);


}
}
}
Output:

Conclusion: Thus, We have successfully Implement Graph Traversal techniques.

©New Horizon Institution of Technology and Management www.nhitm.ac.in 36 | P a g e


Data Structures Course Code:CSL301 Lab Manual

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.

©New Horizon Institution of Technology and Management www.nhitm.ac.in 37 | P a g e


Data Structures Course Code:CSL301 Lab Manual
Types of Binary trees
• Rooted binary tree: It has a root node and every node has at most two children.
• Full binary tree: It is a tree in which every node in the tree has either 0 or 2 children.

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.

©New Horizon Institution of Technology and Management www.nhitm.ac.in 38 | P a g e


Data Structures Course Code:CSL301 Lab Manual

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

©New Horizon Institution of Technology and Management www.nhitm.ac.in 39 | P a g e


Data Structures Course Code:CSL301 Lab Manual
struct node * minValueNode(struct node *);
struct node * Delete(struct node * , int);
struct node *root=NULL;
int main()
{
insert(10);
insert(20);
insert(30);
insert(40);
printf("\n INORDER TRAVERSAL\n");
inorder(root);
root=Delete(root,20);
printf("\n INORDER TRAVERSAL\n");
inorder(root);
root=Delete(root,120);
getch();
return(0);
}
void insert(int v)
{
struct node *r,*p,*t;
r= (struct node *)malloc(sizeof(struct node));
r->data=v;
r->left=NULL;
r->right=NULL;
if(root==NULL)
root=r;
else
{
t=root;
©New Horizon Institution of Technology and Management www.nhitm.ac.in 40 | P a g e
Data Structures Course Code:CSL301 Lab Manual
while(t!=NULL)
{
p=t;

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

struct node* Delete(struct node * T, int x)


{
struct node *temp;
if (T == NULL)
©New Horizon Institution of Technology and Management www.nhitm.ac.in 41 | P a g e
Data Structures Course Code:CSL301 Lab Manual
{
printf("\nElement not found");
return (T);
}
if (x< T->data)
{
T->left = Delete(T->left, x);
return (T);
}

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

struct node * minValueNode(struct node *T)


{
while (T->left != NULL)
T = T->left;
return (T);
}

Program Output:

Conclusion:

©New Horizon Institution of Technology and Management www.nhitm.ac.in 43 | P a g e

You might also like