Bhaskar (r19 Ds Lab Manual (Cse) )
Bhaskar (r19 Ds Lab Manual (Cse) )
DATA STRUCTURES
LAB MANUAL
(R19)
WEEK-1
Write a program that uses functions to perform the following operations on singly linked
list.:
PROGRAM:
// Single Linked List
#include<stdio.h>
#include<stdlib.h>
struct node //Each node in list will contain data and next pointer
int data;
}*start=NULL;
void insertbeg()
int a;
printf("enter data:");
scanf("%d",&nn->data);
a=nn->data;
nn->next=NULL;
nn->next=start;
start=nn;
void insertend()
int b;
scanf("%d",&nn->data);
b=nn->data;
nn->next=NULL;
start=nn;
t=start;
while(t->next!=NULL)
t=t->next;
t->next=nn;
void insertmid()
int x,v;
printf("sll is empty\n");
scanf("%d",&nn->data);
v=nn->data;
scanf("%d",&x);
t=t->next;
}
if(t==NULL)
return;
else
nn->next=t->next;
t->next=nn;
void deletebeg()
return;
else
start=NULL;
}
start=start->next;
t->next=NULL;
free(t);
void deleteend()
int x;
else
start=NULL;
{
t1=t->next;
while(t1->next!=NULL)
t=t->next;
t1=t1->next;
t->next=NULL;
free(t1);
void deletemid()
int x;
else
start=NULL;
}
else
scanf("%d",&x);
t1=start->next;
t=t->next;
t1=t1->next;
if(t1==NULL)
else
t->next=t1->next;
t1->next=NULL;
free(t1);
}
void display()
if(start==NULL)
printf("sll is empty\n");
return;
printf("elements are:\n");
t=start;
while(t!=NULL)
printf("--> %d",t->data);
t=t->next;
return;
void main()
int c,a;
do
printf("\n1:insert\n2:delete\n3:display\n4:exit");
printf("\nenter choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nenter choice:");
scanf("%d",&a);
switch(a)
case 1:insertbeg();break;
case 2:insertend();break;
case 3:insertmid();break;
break;
case 2:
printf("\nenter choice:");
scanf("%d",&a);
switch(a)
case 1:deletebeg();break;
case 2:deleteend();break;
case 3:deletemid();break;
break;
case 3:display();
break;
default:printf("wrong choice\n");
break;
}while(c!=4);
OUTPUT:
WEEK-2
Write a program that uses functions to perform the following operations on doubly
linked list.:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node //Each node in list will contain data, previous and next pointer
int data;
}*start=NULL;
void insertbeg()
int a;
printf("enter data:");
scanf("%d",&nn->data);
a=nn->data;
nn->next=nn->prev=NULL;
{
printf("\n dll is empty, so new node inserted as start node\n");
start=nn;
nn->next=start;
start->prev=nn;
start=nn;
void insertend()
int b;
scanf("%d",&nn->data);
b=nn->data;
nn->next=nn->prev=NULL;
start=nn;
t=start;
while(t->next!=NULL)
t=t->next;
t->next=nn;
nn->prev=t;
void insertmid()
int x,v;
printf("dll is empty\n");
return;
scanf("%d",&nn->data);
v=nn->data;
nn->next=NULL;
nn->prev=NULL;
scanf("%d",&x);
t=t->next;
if(t==NULL)
return;
else
t->next=nn;
nn->prev=t;
else
t->next->prev=nn;
nn->prev=t;
nn->next=t->next;
t->next=nn;
}
void deletebeg()
return;
else
start=NULL;
start=start->next;
t->next=NULL;
start->prev=NULL;
void deleteend()
int x;
return;
else
start=NULL;
free(t);
while(t->next!=NULL)
t=t->next;
t->prev->next=NULL;
t->prev=NULL;
free(t);
void deletemid()
int x;
return;
else
start=NULL;
else
scanf("%d",&x);
while(t!=NULL && t->data!=x)
t=t->next;
if(t==NULL)
else
deleteend();
return;
deletebeg();
return;
t->prev->next=t->next;
t->next->prev=t->prev;
t->next=t->prev=NULL;
free(t);
void display()
if(start==NULL)
printf("dll is empty\n");
return;
printf("elements are:\n");
t=start;
while(t!=NULL)
printf("<--> %d",t->data);
t=t->next;
}
void main()
int c,a;
do
printf("\n1:insert\n2:delete\n3:display\n4:exit");
printf("\nenter choice:");
scanf("%d",&c);
switch(c)
case 1:
printf("\nenter choice:");
scanf("%d",&a);
switch(a)
case 1:insertbeg();break;
case 2:insertend();break;
case 3:insertmid();break;
break;
case 2:
printf("\nenter choice:");
scanf("%d",&a);
switch(a)
{
case 1:deletebeg();break;
case 2:deleteend();break;
case 3:deletemid();break;
break;
case 3:display();
break;
break;
default:printf("wrong choice\n");
break;
}while(c!=4);
OUTPUT:
WEEK-3
Write a program that uses functions to perform the following operations on circular
linked list.:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node //Each node in list will contain data and next pointer
int data;
}*last=NULL;
void insertbeg()
int a;
printf("enter data:");
scanf("%d",&nn->data);
a=nn->data;
nn->next=NULL;
last=nn;
last->next=nn;
nn->next=last->next;
last->next=nn;
void insertend()
int b;
scanf("%d",&nn->data);
b=nn->data;
nn->next=NULL;
last=nn;
last->next=nn;
nn->next=last->next;
last->next=nn;
last=last->next;
void insertmid()
printf("cll is empty\n");
return;
else
int x,v;
scanf("%d",&nn->data);
v=nn->data;
t=t->next;
if(t!=last)
nn->next=t->next;
t->next=nn;
else
printf("\n data does not exist or it is last node please choose other node",v);
void deletebeg()
else
{
if(last->next==last) //list contains single element
last=NULL;
t->next=NULL;
free(t);
last->next=t->next;
t->next=NULL;
free(t);
void deleteend()
else
last=NULL;
t->next=NULL;
free(t);
while(t->next!=last)
t=t->next;
t->next=last->next;
t1=last;
last=t;
t1->next=NULL;
free(t1);
void deletemid()
}
else
last=NULL;
t->next=NULL;
free(t);
int x;
scanf("%d",&x);
t1=t->next;
t=t->next;
t1=t1->next;
if(t->next==last)
printf("\n data does not exist or it is last node please choose other node",x);
else
{
t->next=t1->next;
t1->next=NULL;
free(t1);
void display()
if(last==NULL)
printf("\ncll is empty");
else
t=last->next;
t1=t;
printf("elements are:\n");
while(t->next!=t1)
printf("--> %d",t->data);
t=t->next;
printf("--> %d",t->data);
}
void main()
int c,a;
do
printf("\n1:insert\n2:delete\n3:display\n4:exit");
printf("\nenter choice:");
scanf("%d",&c);
switch(c)
case 1:
printf("\nenter choice:");
scanf("%d",&a);
switch(a)
case 1:insertbeg();break;
case 2:insertend();break;
case 3:insertmid();break;
break;
case 2:
printf("\nenter choice:");
scanf("%d",&a);
switch(a)
case 1:deletebeg();break;
case 2:deleteend();break;
case 3:deletemid();break;
break;
case 3:display();
break;
break;
default:printf("wrong choice\n");
break;
}while(c!=4);
OUTPUT:
Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]
Deleted value: (6,56)
Deleted value: (5,40)
Deleted value: (4,1)
Deleted value: (3,30)
Deleted value: (2,20)
Deleted value: (1,10)
List after deleting all items:
[]
WEEK-4
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#define max 10
void push();
void pop();
void display();
void main()
int choice;
while(1)
printf("1.push\n2.pop\n3.display\n4.exit\n");
scanf("%d",&choice);
switch(choice)
case 1: push();break;
case 2: pop();break;
case 3: display();break;
case 4:exit(0);
default: printf("Wrong selection!!! Try again!!!\n");
void push()
int element;
scanf("%d",&element);
stack[++top]=element;
printf("\nInsertion success!!!");
void pop()
int element;
top=top-1;
}
}
void display()
int i;
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
OUTPUT:
98
24
12
Press Next Choice
Enter the Choice:2
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
ii) Stack Using Pointers:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
}*top=NULL,*top1,*temp;
void push();
void pop();
void display();
void main()
int ch;
while (1)
printf("1.push\n2.pop\n3.display\n4.exit\n");
scanf("%d", &ch);
switch (ch)
case 1: push();break;
case 2: pop(); break;
case 3: display();break;
case 4: exit(0);
void push()
int element;
scanf("%d",&element);
top->next = NULL;
top->data = element;
temp->next = top;
temp->data = element;
top = temp;
}
}
void pop()
return;
top1 = top;
top = top->next;
top1->next=NULL;
free(top1);
void display()
printf("Stack is empty\n");
return;
top1 = top;
printf("%d\n", top1->data);
top1 = top1->next;
}
WEEK-5
i)Using Arrays
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 6
int queue[max],front=-1,rear=-1;
void add();
void del();
void display();
void main()
int choice;
while(1)
printf("\n1.Add\n2.Delete\n3.Display\n4.Exit\n");
scanf("%d",&choice);
switch(choice)
case 1: add();break;
case 2: del();break;
case 3: display();break;
case 4:exit(0);
void add()
int element;
if(rear==max-1)
printf("queue is full\n");
else
if(front==-1)
front=0;
scanf("%d",&element);
queue[++rear]=element;
void del()
int element;
if(front==-1)
printf("queue is empty\n");
else
{
printf("the element%d at position%d is deleted\n",queue[front-1],++front);
if(front>rear)
front=-1;
rear=-1;
void display()
int i;
if(front==-1)
printf("queue is an empty\n");
else
printf("Queue elements:\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
(ii) QUEUE USING POINTERS
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
int data;
void insert(int);
void delete();
void display();
void main()
while(1){
scanf("%d",&choice);
switch(choice){
scanf("%d", &value);
insert(value);
break;
case 4: exit(0);
newNode->data = value;
if(front == NULL)
else{
rear = newNode;
printf("\nInsertion is Success!!!\n");
void delete()
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
free(temp);
void display()
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
while(temp->next != NULL){
printf("%d--->",temp->data);
printf("%d--->NULL\n",temp->data);
}
WEEK-6
i) BUBBLE SORT:
PROGRAM:
//Bubble Sorting
#include<stdio.h>
void main()
int a[10],i,j,temp,n;
scanf("%d",&n);
scanf("%d",&a[i]);
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%d\t",a[i]);
}
ii)SELECTION SORT:
PROGRAM:
//Selection Sort
#include<stdio.h>
void main()
int size,i,j,temp,list[100];
scanf("%d",&size);
scanf("%d",&list[i]);
temp=list[i];
list[i]=list[j];
list[j]=temp;
printf(" %d",list[i]);
}
OUTPUT:
ii) INSERTION SORT:
PROGRAM:
//Insertion Sorting
#include<stdio.h>
void main(){
scanf("%d", &size);
scanf("%d", &list[i]);
temp = list[i];
j = i - 1;
list[j + 1] = list[j];
j = j - 1;
list[j + 1] = temp;
OUTPUT:
WEEK-7
Write a program that use both recursive and non recursive functions to
perform the following searching operations for a Key value in a given list of
integers:
i)LINEAR SEARCH
PROGRAM:
#include <stdio.h>
int pos = 0;
if(index >= n)
return 0;
pos = index + 1;
return pos;
else
return pos;
}
int main()
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &value);
if (pos != 0)
else
return 0;
}
LINEAR SEARCH NON-RECURSIVE:
ii)BINARY SEARCH
if (a[mid] < x)
return binarysearch(a, mid + 1, high, x);
else
return binarysearch(a, low, mid-1, x);
}
int main(void)
{
int a[100];
int len, pos, search_item;
pos = binarysearch(a,0,len-1,search_item);
if (pos < 0 )
printf("Cannot find the element %d in the array.\n", search_item);
else
printf("The position of %d in the array is %d.\n", search_item, pos+1);
return 0;
}
BINARY SEARCH NON RECURSIVE:
//binary search non-recursive
#include<stdio.h>
int main()
{
int i, n, key, low, high, mid, a[20];
printf("\n Enter the size of the array :");
scanf("%d",&n);
printf("Enter the array elements in ascending order");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element\n");
scanf("%d", &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf("The key element is found at location %d", mid + 1);
else
printf("the key element is not found");
return 0;
}
WEEK-8
PROGRAM:
// tree traversals
#include <stdio.h>
#include <stdlib.h>
getchar();
return 0;
}
WEEK-9
#define MAX 5
struct Vertex {
char label;
bool visited;
};
//stack variables
int stack[MAX];
int top = -1;
//graph variables
//array of vertices
struct Vertex* lstVertices[MAX];
//adjacency matrix
int adjMatrix[MAX][MAX];
//vertex count
int vertexCount = 0;
//stack functions
int pop() {
return stack[top--];
}
int peek() {
return stack[top];
}
bool isStackEmpty() {
return top == -1;
}
//graph functions
return -1;
}
void depthFirstSearch()
{
int i;
//mark first node as visited
lstVertices[0]->visited = true;
while(!isStackEmpty())
{
//get the unvisited vertex of vertex which is at top of the stack
int unvisitedVertex = getAdjUnvisitedVertex(peek());
int main()
{
int i, j;
addVertex('A'); // 0
addVertex('B'); // 1
addVertex('C'); // 2
addVertex('D'); // 3
addVertex('E'); // 4
addEdge(0, 1); // A - B
addEdge(0, 2); // A - C
addEdge(0, 3); // A - D
addEdge(1, 3); // B - D
addEdge(1, 4); // B - E
addEdge(2, 3); // C - D
addEdge(3, 4); // D - E
OUTPUT:
b) To implement Breadth First Search
PROGRAM:
//Graph Traversals BFS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 5
struct Vertex
{
char label;
bool visited;
};
//queue variables
int queue[MAX];
int rear = -1;
int front = 0;
int queueItemCount = 0;
//graph variables
//array of vertices
struct Vertex* lstVertices[MAX];
//adjacency matrix
int adjMatrix[MAX][MAX];
//vertex count
int vertexCount = 0;
//queue functions
void insert(int data)
{
queue[++rear] = data;
queueItemCount++;
}
int removeData()
{
queueItemCount--;
return queue[front++];
}
bool isQueueEmpty() {
return queueItemCount == 0;
}
//graph functions
//add vertex to the vertex list
void addVertex(char label)
{
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;
}
//add edge to edge array
void addEdge(int start,int end)
{
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
//display the vertex
void displayVertex(int vertexIndex)
{
printf("%c ",lstVertices[vertexIndex]->label);
}
//get the adjacent unvisited vertex
int getAdjUnvisitedVertex(int vertexIndex)
{
int i;
for(i = 0; i<vertexCount; i++) {
if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)
return i;
}
return -1;
}
void breadthFirstSearch()
{
int i;
//mark first node as visited
lstVertices[0]->visited = true;
//display the vertex
displayVertex(0);
//insert vertex index in queue
insert(0);
int unvisitedVertex;
while(!isQueueEmpty()) {
//get the unvisited vertex of vertex which is at front of the queue
int tempVertex = removeData();
//no adjacent vertex found
while((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) {
lstVertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
insert(unvisitedVertex);
}
}
//queue is empty, search is complete, reset the visited flag
for(i = 0;i<vertexCount;i++) {
lstVertices[i]->visited = false;
}
}
int main()
{
int i, j;
for(i = 0; i<MAX; i++) // set adjacency
{
for(j = 0; j<MAX; j++) // matrix to 0
adjMatrix[i][j] = 0;
}
addVertex('A'); // 0
addVertex('B'); // 1
addVertex('C'); // 2
addVertex('D'); // 3
addVertex('E'); // 4
addEdge(0, 1); // A - B
addEdge(0, 2); // A - C
//addEdge(0, 3); // A - D
addEdge(1, 3); // B - D
addEdge(1, 4); // B - E
addEdge(2, 3); // C - D
addEdge(3, 4); // D - E
breadthFirstSearch();
return 0;
}
OUTPUT: