Lab Manual
Lab Manual
Lab Manual
LAB MANUAL
4. Implementation of Stacks 39
1|P age
5. Implementation of Queues 42
6. Searching Techniques
A Linear Search 45
B Binary Search 45
7. Sorting Techniques
A Bubble Sort 48
B Merge Sort 48
8. Trees 51
9. Graphs 55
2|P age
CLOSED ENDED PROBLEMS
EXPT NO 1 IMPLEMENTATION OF LINKED LIST
Aim:
To create and display the data stored using single linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
Program Code:
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode;
// Creating function for various operations
3|P age
void createNodeList(int n); // function to create the list
void displayList(); // function to display the list
int main()
{
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory
allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL; // links the address field to NULL
tmp = newnode;
4|P age
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
5|P age
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
}
Ouput
6|P age
1.B. Doubly Linked List
Aim:
To create and display the data stored using double linked list
Algorithm:
Step 1: Initialize a structure with three members one for handling data and the other for
handling address of both forward and previous nodes.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
7|P age
printf("-------------------------------------------------------------\n");
create(); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
display(); // Calling a function to display the created list
return 0;
}
void create()
{
head = 0;
int choice,data;
while(choice)
{
newnode = (struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
newnode->data = data;
newnode->next = 0;
newnode->prev = 0;
if(head==0)
{
head = temp = newnode;
}
else
{
temp->next = newnode;
newnode->prev = temp;
}
printf("do you want to continue- enter 1 to continue");
scanf("%d",&choice);
}
//free(temp);
8|P age
}
void display()
{
temp = head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp = temp->next;
}
}
Ouput
9|P age
1.C. Circular Linked List
Aim:
To create and display the data stored using single linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
Program Code:
10 | P a g e
printf("\n\n Linked List : To create and display Circular Linked List :\n");
printf("-------------------------------------------------------------\n");
create(); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
display(); // Calling a function to display the created list
return 0;
}
void create()
{
head=0;
int choice;
while(choice)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Enter data : ");
scanf("%d", &newnode->data);
newnode->next=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
temp->next=head;
}
printf("Enter 1 to continue");
scanf("%d", &choice);
}
}
11 | P a g e
void display()
{
temp = head;
while(temp->next!=head)
{
printf("%d",temp->data);
temp = temp->next;
}
}
Ouput
12 | P a g e
1.D. Circularly Double Linked List
Aim:
To create and display the data stored using single linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
13 | P a g e
printf("\n\n Linked List : To create and display Circular Double Linked List :\n");
printf("-------------------------------------------------------------\n");
create(); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
display(); // Calling a function to display the created list
return 0;
}
void create()
{
head = 0;
int choice,data;
while(choice)
{
newnode = (struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
newnode->data = data;
newnode->next = 0;
newnode->prev = 0;
if(head==0)
{
head = temp = newnode;
}
else
{
temp->next = newnode;
newnode->prev = temp;
}
printf("do you want to continue- enter 1 to continue");
scanf("%d",&choice);
}
//free(temp);
14 | P a g e
}
void display()
{
temp = head;
while(temp->next!=head)
{
printf("%d",temp->data);
temp = temp->next;
}
printf("\t");
printf("%d",temp->data);
}
Ouput
15 | P a g e
EXPT NO 2 INSERT AN ELEMENT IN LINKED LIST
Aim:
To insert and display the data stored using single linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
Step 5: Call various functions to insert the element at given position, end and at
beginning and display the same
Program Code:
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void NodeInsertatEnd(int data); // function to insert at end
void NodeInsertatBegin(int data); // function to insert at beginning
void insertNodeAtMiddle(int data, int pos); // function to insert in the middle
void displayList(); // function to display the list
16 | P a g e
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatEnd(data); // Calling a function to insert at END
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatBegin(data); // Calling a function to insert at BEGINING
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d%d", &data,&pos);
insertNodeAtMiddle(data,pos); // Calling a function to insert at given POSITION
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
17 | P a g e
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = NULL; // links the address field to NULL
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
18 | P a g e
}
}
}
void NodeInsertatEnd(int data)
{
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = NULL;
tmp = newnode;
while(tmp->next != NULL)
tmp = tmp->next;
tmp->next = fnNode; //Links the address part
}
}
19 | P a g e
}
else
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = newnode; //Links the address part
newnode = fnNode; //Makes stnode as first node
}
}
20 | P a g e
if(tmp == NULL)
break;
}
if(tmp != NULL)
{
fnNode->next = tmp->next; //Links the address part of new node
tmp->next = fnNode;
}
else
{
printf(" Insert is not possible to the given position.\n");
}
}
}
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
}
21 | P a g e
Output
Aim:
To insert and display the data stored using Circular linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
22 | P a g e
Step 5: Call various functions to insert the element at given position, end and at
beginning and display the same
Program Code:
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void NodeInsertatEnd(int data); // function to insert at end
void NodeInsertatBegin(int data); // function to insert at beginning
void insertNodeAtMiddle(int data, int pos); // function to insert in the middle
void displayList(); // function to display the list
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatEnd(data); // Calling a function to insert at END
23 | P a g e
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d", &data);
NodeInsertatBegin(data); // Calling a function to insert at BEGINING
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
printf(" Input the number to be inserted : ");
scanf("%d%d", &data,&pos);
insertNodeAtMiddle(data,pos); // Calling a function to insert at given POSITION
printf("\n Data entered in the list after insert: \n");
displayList(); // Calling a function to display the created list after insert
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = head; // links the address field to head
tmp = newnode;
// Creating n nodes and adding to linked list
24 | P a g e
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
25 | P a g e
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = NULL;
tmp = newnode;
while(tmp->next != head)
tmp = tmp->next;
tmp->next = fnNode; //Links the address part
}
}
26 | P a g e
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for insert : ");
scanf("%d", &data);
fnNode->data = data; //Links the data part
fnNode->next = NULL;
tmp = newnode;
for(i=2; i<=pos-1; i++)
{
tmp = tmp->next;
if(tmp == NULL)
break;
}
if(tmp != NULL)
{
fnNode->next = tmp->next; //Links the address part of new node
tmp->next = fnNode;
}
else
{
printf(" Insert is not possible to the given position.\n");
}
}
}
void displayList()
{
27 | P a g e
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp->next != head)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
}
Output:
28 | P a g e
EXPT NO 3 DELETE AN ELEMENT IN LINKED LIST
Aim:
To delete and display the data stored using single linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
Step 5: Call various functions to delete the element at given position, end and at
beginning and display the same
Program Code:
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode, *tmp,*head,*prevnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void deleteend(); // function to delete at end
void deletegivenpos(); // function to delete in the middle
void displayList(); // function to display the list
29 | P a g e
int main()
{
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
//printf(" Input the number to be deleted : ");
//scanf("%d", &data);
deleteend(); // Calling a function to delete at END
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
deletegivenpos(); // Calling a function to delete at given POSITION
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory
allocation
{
printf(" Memory can not be allocated.");
}
else
30 | P a g e
{
// reads data for the node through keyboard
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = head; // links the address field to head
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
void deleteend()
31 | P a g e
{
struct node*prevnode;
tmp = head;
while(tmp->next!=0)
{
prevnode = tmp;
tmp = tmp->next;
if(tmp == head)
{
tmp = 0;
free(tmp);
}
else
{
prevnode->next = 0;
free(tmp);
}
}
}
void deletegivenpos()
{
struct node*nextnode;
int pos,i=1;
tmp = head;
printf("enter the position");
scanf("%d",&pos);
while(i<pos-1)
{
tmp = tmp->next;
i++;
}
nextnode = tmp->next;
32 | P a g e
tmp->next = nextnode->next;
free(nextnode);
}
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp->next != 0)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
//printf("\t");
printf(" Data = %d\n", tmp->data);
}
Output:
33 | P a g e
3.B. Circular Linked List
Aim:
To delete and display the data stored using circular linked list
Algorithm:
Step 1: Initialize a structure with two members one for handling data and the other for
handling address.
Step 2: Declare and call two functions for creation of node list and for display of the
contents stored.
Step 3: Call malloc function for allocating dynamic memory to nodes and till the number
of data entry is reached enter the information
Step 4: Display the data by traversing through the first node to last node.
Step 5: Call various functions to delete the element at given position, end and at
beginning and display the same
Program Code:
#include <stdio.h>
#include <stdlib.h>
// Creation of node for linked list
struct node
{
int data; //Data of the node
struct node *next; //Address of the next node
}*newnode, *tmp,*head,*prevnode;
// Creating function for various operations
void createNodeList(int n); // function to create the list
void deleteend(); // function to delete at end
void deletegivenpos(); // function to delete in the middle
void displayList(); // function to display the list
int main()
{
34 | P a g e
int n,data,pos;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n); // Calling a Function to create a list
printf("\n Data entered in the list : \n");
displayList(); // Calling a function to display the created list
//printf(" Input the number to be deleted : ");
//scanf("%d", &data);
deleteend(); // Calling a function to delete at END
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
deletegivenpos(); // Calling a function to delete at given POSITION
printf("\n Data entered in the list after delete: \n");
displayList(); // Calling a function to display the created list after delete
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int data, i;
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL) //check whether the fnnode is NULL and if so no memory
allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
35 | P a g e
printf(" Input data for node 1 : ");
scanf("%d", &data);
newnode->data = data;
newnode->next = head; // links the address field to head
tmp = newnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &data);
void deleteend()
{
struct node*prevnode;
36 | P a g e
tmp = head;
while(tmp->next!=0)
{
prevnode = tmp;
tmp = tmp->next;
if(tmp == head)
{
tmp = 0;
free(tmp);
}
else
{
prevnode->next = 0;
free(tmp);
}
}
}
void deletegivenpos()
{
struct node*nextnode;
int pos,i=1;
tmp = head;
printf("enter the position");
scanf("%d",&pos);
while(i<pos-1)
{
tmp = tmp->next;
i++;
}
nextnode = tmp->next;
tmp->next = nextnode->next;
free(nextnode);
37 | P a g e
}
void displayList()
{
struct node *tmp;
if(newnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = newnode;
while(tmp->next != head)
{
printf(" Data = %d\n", tmp->data); // prints the data of current node
tmp = tmp->next; // increments the position of current node
}
}
//printf("\t");
printf(" Data = %d\n", tmp->data);
}
Output:
38 | P a g e
SEMI OPEN ENDED PROBLEMS
40 | P a g e
Program Code:
41 | P a g e
Output:
Aim:
To implement a queues using arrays and perform push and pop operation.
Algorithm:
1. To implement queues using arrays
Step 1: Create an array arr of size n and take two variables front and rear both of which
will be initialized to 0 which means the queue is currently empty.
Step 2: rear is the index up to which the elements are stored in the array and front is the
index of the first element of the array.
Step 3: Initialize front and rear as 0, but in general we have to initialize it with -1.
Step 4: If we assign rear as 0, rear will always point to next block of the end element, in
fact , rear should point the index of last element,
Step 5: front=rear = 0
Step 6: void queueEnqueue(int data) else part will be executed,
so arr[rear] =data;// rear =0, rear pointing to the latest element
rear++; //now rear = 1, rear pointing to the next block after end element not the
end element
42 | P a g e
Program Code:
43 | P a g e
Program Code:
Output:
44 | P a g e
Expt No: 6 SEARCHING TECHNIQUES
Aim:
To identify an element using linear search algorithm and Binary Search algorithm.
Algorithm:
To search an element using Linear Search
Step 1: Create an array and initialize the necessary Variables.
Step 2: Enter the elements of array and also input the element to be searched.
Step 3: Traverse through the array till the end and Identify whether the element in an
array is equal Search element.
Step 4: If array[i] = = search, then display the index and element value
Step 5: Else display element not found
45 | P a g e
Output:
46 | P a g e
Program Code for Binary Search:
47 | P a g e
Output:
48 | P a g e
2. Algorithm for sorting the elements using merge sort
49 | P a g e
Output:
50 | P a g e
Output:
Algorithm:
51 | P a g e
Program Code:
52 | P a g e
53 | P a g e
Output:
54 | P a g e
Expt No: 9 Graphs
Question:
Given a weighted graph and a source vertex in the graph, find the shortest paths from the source
to all the other vertices in the given graph.
Note: The given graph does not contain any negative edge.
Aim:
55 | P a g e
Algorithm:
56 | P a g e
Program Code:
57 | P a g e
Program Code:
58 | P a g e
Ouput:
59 | P a g e