0% found this document useful (0 votes)
4 views167 pages

DS All Week Programs R-23

The document contains multiple C programs demonstrating various data structures and algorithms, including array reversal, linear search, binary search, sorting techniques (bubble sort, insertion sort, selection sort, quick sort), and linked list operations. Each section includes code snippets, input prompts, and output examples. The programs illustrate fundamental concepts in data structures and algorithms for educational purposes.

Uploaded by

h68364238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views167 pages

DS All Week Programs R-23

The document contains multiple C programs demonstrating various data structures and algorithms, including array reversal, linear search, binary search, sorting techniques (bubble sort, insertion sort, selection sort, quick sort), and linked list operations. Each section includes code snippets, input prompts, and output examples. The programs illustrate fundamental concepts in data structures and algorithms for educational purposes.

Uploaded by

h68364238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DATE: EXPNO: PAGE NO: 1

/* C Program to Reverse an Array elements */

#include<stdio.h>
int main()
{
int a[20],i,n,temp,size;
clrscr();
printf("Enter size of the array:");
scanf("%d",&n);
size=n;
printf("Enter %d array elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Array elements are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;i++)
{
temp=a[i];
a[i]=a[n-1];
a[n-1]=temp;
n=n-1;
}
printf("\nArray elements in reverse are:");
for(i=0;i<size;i++)
printf("%d ",a[i]);
getch();
return -5;
}

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 2

OUTPUT:

Enter size of the array:20

Enter %d array elements:5

Array elements are: 10 -6 38 21 24

Array elements in reverse are:24 21 38 -6 10

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 3

/* C Program to implement Linear Search technique */


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
int arr[50], num, i, n, found = 0;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched: ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
printf("\n %d is found in the array at position = %d", num,i+1);
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array", num);
getch();
return 0;
}

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 4

OUTPUT:

Enter the number of elements in the array :10

Enter the elements:10 56 31 9 -10 7 88 -1 22 -3

Enter the number that has to be searched: 22

22 is found in the array at position = 8

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 5

/* C Program to implement Binary Search technique */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int smallest(int arr[], int k, int n);


void selection_sort(int arr[], int n);

int main()
{
int arr[50], num, i, n, beg, end, mid, found=0;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter %d elements: ",n);
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\n\n Enter the number that has to be searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg<=end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array at position %d", num, mid+1);
found =1;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 6

break;
}
else if (arr[mid]>num)
end = mid-1;
else
beg = mid+1;
}
if (beg > end && found == 0)
printf("\n %d does not exist in the array", num);
getch();
return 0;
}

OUTPUT:

Enter the number of elements in the array: 5

Enter the elements: 10 -3 22 -5 49

The sorted array is:


-5 -3 10 22 49

Enter the number that has to be searched: 10

10 is present in the array at position 3

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 7

/* C Program to implement Bubble sort technique */


#include<stdio.h>
int main()
{
int a[100],n,i,j,temp;
clrscr();
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The list of elements are:\n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe list of elements in sorted order are:\n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
getch();
return -20;
}

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 8

OUTPUT:

Enter number of elements:8

Enter 8 elements:10 -6 22 95 32 77 -5 -86

The list of elements are:

10 -6 95 32 77 -5 -86

The list of elements in sorted order are:

-86 -6 -5 10 22 32 77 95

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 9

/* C Program to implement Insertion sort technique */

#include <stdio.h>
#include <conio.h>
void insertion_sort(int arr[], int n);

void main()
{
int arr[50], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter %d elements of the array: ",n);
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}

void insertion_sort(int arr[], int n)


{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1] = arr[j];
j--;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 10

arr[j+1] = temp;
}
}

OUTPUT:
Enter the number of elements in the array: 6

Enter the elements of the array: 8 -4 76 38 21 -4

The sorted array is:


-4 -4 8 21 38 76

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 11

/* C Program to implement Selection Sort technique */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int smallest(int arr[], int k, int n);
void selection_sort(int arr[], int n);

void main()
{
int arr[50], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter %d elements of the array: ",n);
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i] < small)
{
small = arr[i];
pos = i;
}

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 12

}
return pos;
}

void selection_sort(int arr[],int n)

{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}

OUTPUT:
Enter the number of elements in the array: 4

Enter the elements of the array: 19 -43 39 -99

The sorted array is:


-99 -43 19 39

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 13

/* C Program to implement Quick sort technique */

#include <stdio.h>
#include <conio.h>

int partition(int a[], int beg, int end);


void quick_sort(int a[], int beg, int end);
void main()
{
int arr[50], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter %d elements of the array: ",n);
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n-1);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}
int partition(int a[], int beg, int end)
{
int left, right, temp, loc, flag;
loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 14

if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}

void quick_sort(int a[], int beg, int end)


{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 15

quick_sort(a, beg, loc-1);


quick_sort(a, loc+1, end);
}

OUTPUT:
Enter the number of elements in the array: 9

Enter the elements of the array: 29 -35 66 12 -67 40 33 86 23

The sorted array is:


-67 -35 12 23 29 33 40 66 86

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 16

/* C program to create a linked list and perform insertions and deletions of all cases.
Creating functions to sort and finally delete the entire list at once. */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *insert_before(struct node *);
struct node *insert_after(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_node(struct node *);
struct node *delete_after(struct node *);
struct node *delete_list(struct node *);
struct node *sort_list(struct node *);

int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 17

printf("\n 2: Display the list");


printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Add a node before a given node");
printf("\n 6: Add a node after a given node");
printf("\n 7: Delete a node from the beginning");
printf("\n 8: Delete a node from the end");
printf("\n 9: Delete a given node");
printf("\n 10: Delete a node after a given node");
printf("\n 11: Delete the entire list");
printf("\n 12: Sort the list");
printf("\n 13: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = insert_before(start);
break;
case 6: start = insert_after(start);
break;
case 7: start = delete_beg(start);
break;
case 8: start = delete_end(start);
break;
case 9: start = delete_node(start);

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 18

break;
case 10: start = delete_after(start);
break;
case 11: start = delete_list(start);
printf("\n LINKED LIST DELETED");
break;
case 12: start = sort_list(start);
break;
}
}while(option !=13);
getch();
return 0;
}

struct node *create_ll(struct node *start)


{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 19

ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
if (ptr == NULL)
{
printf("\nList is empty");
return ptr;
}
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}

struct node *insert_beg(struct node *start)


{
struct node *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 20

new_node -> next = start;


start = new_node;
return start;
}

struct node *insert_end(struct node *start)


{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = NULL;
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
return start;
}

struct node *insert_before(struct node *start)


{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data : ");
scanf("%d", &num);
printf("\n Enter the value before which the data has to be inserted : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val)
{

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 21

preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = new_node;
new_node -> next = ptr;
return start;
}

struct node *insert_after(struct node *start)


{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data : ");
scanf("%d", &num);
printf("\n Enter the value after which the data has to be inserted : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next=new_node;
new_node -> next = ptr;
return start;
}

struct node *delete_beg(struct node *start)


{
struct node *ptr;
ptr = start;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 22

start = start -> next;


free(ptr);
return start;
}

struct node *delete_end(struct node *start)


{
struct node *ptr, *preptr;
ptr = start;
while(ptr -> next != NULL)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = NULL;
free(ptr);
return start;
}
struct node *delete_node(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value of the node which has to be deleted : ");
scanf("%d", &val);
ptr = start;
if(ptr -> data == val)
{
start = delete_beg(start);
return start;
}
else
{
while(ptr -> data != val)
{

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 23

preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = ptr -> next;
free(ptr);
return start;
}
}

struct node *delete_after(struct node *start)


{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value after which the node has to deleted : ");
scanf("%d", &val);
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next=ptr -> next;
free(ptr);
return start;
}

struct node *delete_list(struct node *start)


{
struct node *ptr; // Lines 252-254 were modified from original code to fix
unresposiveness in output window
if(start!=NULL)
{
ptr=start;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 24

while(ptr != NULL)
{
printf("\n %d is to be deleted next", ptr -> data);
start = delete_beg(ptr);
ptr = start;
}
}
return start;
}

struct node *sort_list(struct node *start)


{
struct node *ptr1, *ptr2;
int temp;
ptr1 = start;
while(ptr1 -> next != NULL)
{
ptr2 = ptr1 -> next;
while(ptr2 != NULL)
{
if(ptr1 -> data > ptr2 -> data)
{
temp = ptr1 -> data;
ptr1 -> data = ptr2 -> data;
ptr2 -> data = temp;
}
ptr2 = ptr2 -> next;
}
ptr1 = ptr1 -> next;
}
return start; // Had to be added
}

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 25

OUTPUT:
**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 1

Enter -1 to end
Enter the data : 10

Enter the data : 20

Enter the data : 30

Enter the data : 40

Enter the data : 99

Enter the data : -1

LINKED LIST CREATED


**MAIN MENU **
1: Create a list

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 26

2: Display the list


3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


10 20 30 40 99

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 3

Enter the data : 9

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 27

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


9 10 20 30 40 99

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 4

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 28

Enter the data : 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


9 10 20 30 40 99 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 29

13: EXIT

Enter your option : 5


Enter your option : 5
Enter the data : 19

Enter the value before which the data has to be inserted : 20

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


9 10 19 20 30 40 99 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 30

7: Delete a node from the beginning


8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT
Enter your option : 6

Enter the data : 33

Enter the value after which the data has to be inserted : 30

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


9 10 19 20 30 33 40 99 100

**MAIN MENU **
1: Create a list

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 31

2: Display the list


3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT
Enter your option : 7

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


10 19 20 30 33 40 99 100

**MAIN MENU **

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 32

1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 8


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


10 19 20 30 33 40 99

**MAIN MENU **

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 33

1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 9

Enter the value of the node which has to be deleted : 20


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2


10 19 30 33 40 99

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 34

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 10

Enter the value after which the node has to deleted : 33


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 35

Enter your option : 2


10 19 30 33 99

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 12


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 36

Enter your option : 2


10 19 30 33 99

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 11

10 is to be deleted next
19 is to be deleted next
30 is to be deleted next
33 is to be deleted next
99 is to be deleted next
LINKED LIST DELETED
**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 37

8: Delete a node from the end


9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option : 2

List is empty

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option :13

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 38

// Iterative C program to reverse a linked list

#include <stdio.h>

struct Node {
int data;
struct Node* next;
};
struct Node* reverseList(struct Node* head)
{
struct Node *curr = head, *prev = NULL, *next;
while (curr != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
void printList(struct Node* node) {
while (node != NULL) {
printf(" %d", node->data);
node = node->next;
}
}
struct Node* createNode(int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}

int main() {

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 39

// Create a hard-coded linked list:


// 1 -> 2 -> 3 -> 4 -> 5
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("Given Linked list:");
printList(head);
head = reverseList(head);
printf("\nReversed Linked List:");
printList(head);

return 0;
}

OUTPUT:

Given Linked list: 1 2 3 4 5


Reversed Linked List: 5 4 3 2 1

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 40

// Recursive C program to reverse a linked list

#include <stdio.h>

struct Node {
int data;
struct Node* next;
};
struct Node* reverseList(struct Node* head) {
if (head == NULL || head->next == NULL)
return head;
struct Node* rest = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
struct Node* createNode(int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
void printList(struct Node* node) {
while (node != NULL) {
printf(" %d", node->data);
node = node->next;
}
printf("\n");
}

int main() {

// Create a hard-coded linked list:

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 41

// 1 -> 2 -> 3 -> 4 -> 5


struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);

printf("Given Linked List:");


printList(head);

// Function call to return the reversed list


head = reverseList(head);

printf("Reversed Linked List:");


printList(head);

return 0;
}

OUTPUT:

Given Linked List: 1 2 3 4 5

Reversed Linked List: 5 4 3 2 1

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 42

/* C program to reverse a Singly Linked List iteratively */

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data; //Data part
struct node *next; //Address part
}*head;

void createList(int n);


void reverseList();
void displayList();

int main()
{
int n, choice;

/* Create a singly linked list of n nodes */


printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
/* Reverse the list */

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 43

printf("\nPress 1 to reverse the order of singly linked list\n");


scanf("%d", &choice);
if(choice == 1)
{
reverseList();
}

printf("\nData in the list after reverse is:\n");


displayList();
getch();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
if(n <= 0)
{
printf("List size must be greater than zero.\n");
getch();
exit(1);
}
head = (struct node *)malloc(sizeof(struct node));
/* If unable to allocate memory for head node */
if(head == NULL)
{

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 44

printf("Unable to allocate memory.");


}
else
{
/* Read data of node from the user */
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;

/* Create n nodes and adds to linked list */


for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 45

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode


temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}
void reverseList()
{
struct node *prevNode, *curNode;

if(head != NULL)
{
prevNode = head;
curNode = head->next;
head = head->next;
prevNode->next = NULL; // Make first node as last node
while(head != NULL)
{
head = head->next;
curNode->next = prevNode;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 46

prevNode = curNode;
curNode = head;
}

head = prevNode; // Make last node as head

printf("SUCCESSFULLY REVERSED LIST\n");


}
}
void displayList()
{
struct node *temp;

/* If the list is empty i.e. head = NULL */


if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)

{
printf(" %d\t", temp->data); // Print the data of current node
temp = temp->next; // Move to next node
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 47

}
}
}

A C program to create a doubly linked list and perform insertions and deletions in all cases.
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 48

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node {

struct node *next;

int data;

struct node *prev;

};

struct node *start = NULL;

struct node *create_ll(struct node *);

struct node *display(struct node *);

struct node *insert_beg(struct node *);

struct node *insert_end(struct node *);

struct node *insert_before(struct node *);

struct node *insert_after(struct node *);

struct node *delete_beg(struct node *);

struct node *delete_end(struct node *);

struct node *delete_before(struct node *);

struct node *delete_after(struct node *);

struct node *delete_list(struct node *);

int main()

int option;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 49

clrscr();

do

printf("\n\n **MAIN MENU **");

printf("\n 1: Create a list");

printf("\n 2: Display the list");

printf("\n 3: Add a node at the beginning");

printf("\n 4: Add a node at the end");

printf("\n 5: Add a node before a given node");

printf("\n 6: Add a node after a given node");

printf("\n 7: Delete a node from the beginning");

printf("\n 8: Delete a node from the end");

printf("\n 9: Delete a node before a given node");

printf("\n 10: Delete a node after a given node");

printf("\n 11: Delete the entire list");

printf("\n 12: EXIT");

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

case 1: start = create_ll(start);

printf("\n DOUBLY LINKED LIST CREATED");

break;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 50

case 2: start = display(start);

break; case 3:

start = insert_beg(start);

break;

case 4: start = insert_end(start);

break;

case 5: start = insert_before(start);

break;

case 6: start = insert_after(start);

break;

case 7: start = delete_beg(start);

break;

case 8: start = delete_end(start);

break;

case 9: start = delete_before(start);

break;

case 10: start = delete_after(start);

break;

case 11: start = delete_list(start);

printf("\n DOUBLY LINKED LIST DELETED");

break;

}
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 51

while(option != 12);

getch();

return 0;

struct node *create_ll(struct node *start)

struct node *new_node, *ptr;

int num;

printf("\n Enter –1 to end");

printf("\n Enter the data : ");

scanf("%d", &num);

while(num != –1)

if(start == NULL)

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

new_node -> prev = NULL;

new_node -> data = num;

new_node -> next = NULL;

start = new_node;

else {

ptr=start;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 52

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

new_node–>data=num;

while(ptr–>next!=NULL)

ptr = ptr–>next;

ptr–>next = new_node;

new_node–>prev=ptr;

new_node–>next=NULL;

printf("\n Enter the data : ");

scanf("%d", &num);

return start;

struct node *display(struct node *start)

struct node *ptr;

ptr=start;

while(ptr!=NULL)

printf("\t %d", ptr -> data);

ptr = ptr -> next;

return start;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 53

struct node *insert_beg(struct node *start)

struct node *new_node;

int num;

printf("\n Enter the data : ");

scanf("%d", &num);

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

new_node -> data = num;

start -> prev = new_node;

new_node -> next = start;

new_node -> prev = NULL;

start = new_node; return start;

struct node *insert_end(struct node *start)

struct node *ptr, *new_node;

int num;

printf("\n Enter the data : ");

scanf("%d", &num);

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

new_node -> data = num;

ptr=start;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 54

while(ptr -> next != NULL)

ptr = ptr -> next;

ptr -> next = new_node;

new_node -> prev = ptr;

new_node -> next = NULL;

return start;

struct node *insert_before(struct node *start)

struct node *new_node, *ptr;

int num, val;

printf("\n Enter the data : ");

scanf("%d", &num);

printf("\n Enter the value before which the data has to be inserted : ");

scanf("%d", &val);

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

new_node -> data = num;

ptr = start;

while(ptr -> data != val)

ptr = ptr -> next;

new_node -> next = ptr;

new_node -> prev = ptr-> prev;

ptr -> prev -> next = new_node;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 55

ptr -> prev = new_node;

return start;

struct node *insert_after(struct node *start)

struct node *new_node, *ptr;

int num, val;

printf("\n Enter the data : ");

scanf("%d", &num);

printf("\n Enter the value after which the data has to be inserted : ");

scanf("%d", &val);

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

new_node -> data = num;

ptr = start;

while(ptr -> data != val)

ptr = ptr -> next;

new_node -> prev = ptr;

new_node -> next = ptr -> next;

ptr -> next -> prev = new_node;

ptr -> next = new_node;

return start;

struct node *delete_beg(struct node *start)


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 56

struct node *ptr;

ptr = start;

start = start -> next;

start -> prev = NULL;

free(ptr);

return start;

struct node *delete_end(struct node *start)

struct node *ptr;

ptr = start;

while(ptr -> next != NULL)

ptr = ptr -> next;

ptr -> prev -> next = NULL;

free(ptr);

return start;

struct node *delete_after(struct node *start)

{ struct node *ptr, *temp;

int val;

printf("\n Enter the value after which the node has to deleted : ");

scanf("%d", &val);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 57

ptr = start;

while(ptr -> data != val)

ptr = ptr -> next;

temp = ptr -> next;

ptr -> next = temp -> next;

temp -> next -> prev = ptr;

free(temp);

return start;

struct node *delete_before(struct node *start)

struct node *ptr, *temp;

int val;

printf("\n Enter the value before which the node has to deleted : ");

scanf("%d", &val);

ptr = start;

while(ptr -> data != val)

ptr = ptr -> next;

temp = ptr -> prev;

if(temp == start)

start = delete_beg(start);

else

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 58

ptr -> prev = temp -> prev;

temp -> prev -> next = ptr;

free(temp);

return start;

struct node *delete_list(struct node *start)

while(start != NULL)

start = delete_beg(start);

return start;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 59

OUTPUT:

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 1

Enter -1 to end
Enter the data : 1

Enter the data : 2

Enter the data : 3

Enter the data : 4

Enter the data : 9

Enter the data : -1

DOUBLY LINKED LIST CREATED


**MAIN MENU **
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 60

1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


1 2 3 4 9

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 3

Enter the data : 22


**MAIN MENU **

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 61

1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


22 1 2 3 4 9

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 4


Enter the data : 100

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 62

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


22 1 2 3 4 9 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 5


Enter the data : 19

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 63

Enter the value before which the data has to be inserted : 2

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


22 1 19 2 3 4 9 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT
Enter your option : 6

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 64

Enter the data : 33

Enter the value after which the data has to be inserted : 3

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


9 1 19 2 3 33 4 9 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 65

11: Delete the entire list


12: EXIT
Enter your option : 7

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


1 19 2 3 33 4 9 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 66

12: EXIT

Enter your option : 8


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


1 19 2 3 33 4 9

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 67

Enter your option : 9

Enter the value of the node before which has to be deleted : 2


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


1 2 3 33 4 9

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 68

Enter your option : 10

Enter the value after which the node has to deleted : 33


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT

Enter your option : 2


1 19 3 33 9

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a node before a given node
10: Delete a node after a given node
11: Delete the entire list
12: EXIT
Enter your option : 11

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 69

1 is to be deleted next
1 is to be deleted next
3 is to be deleted next
33 is to be deleted next
9 is to be deleted next
DOUBLY LINKED LIST DELETED
**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node before a given node
6: Add a node after a given node
7: Delete a node from the beginning
8: Delete a node from the end
9: Delete a given node
10: Delete a node after a given node
11: Delete the entire list
12: Sort the list
13: EXIT

Enter your option :13

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 70

A C program to create a circular linked list perform insertion and deletion at the beginning
and end of the list.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *start = NULL;

struct node *create_cll(struct node *);

struct node *display(struct node *);

struct node *insert_beg(struct node *);

struct node *insert_end(struct node *);

struct node *delete_beg(struct node *);

struct node *delete_end(struct node *);

struct node *delete_after(struct node *);

struct node *delete_list(struct node *);

int main()

int option;

clrscr();

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 71

do {

printf("\n\n *****MAIN MENU *****");

printf("\n 1: Create a list");

printf("\n 2: Display the list");

printf("\n 3: Add a node at the beginning");

printf("\n 4: Add a node at the end");

printf("\n 5: Delete a node from the beginning");

printf("\n 6: Delete a node from the end");

printf("\n 7: Delete a node after a given node");

printf("\n 8: Delete the entire list");

printf("\n 9: EXIT");

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

case 1: start = create_cll(start);

printf("\n CIRCULAR LINKED LIST CREATED");

break;

case 2: start = display(start);

break;

case 3: start = insert_beg(start);

break;

case 4: start = insert_end(start);


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 72

break;

case 5: start = delete_beg(start);

break;

case 6: start = delete_end(start);

break;

case 7: start = delete_after(start);

break;

case 8: start = delete_list(start);

printf("\n CIRCULAR LINKED LIST DELETED");

break;

while(option !=9);

getch();

return 0;

struct node *create_cll(struct node *start)

struct node *new_node, *ptr;

int num;

printf("\n Enter –1 to end");

printf("\n Enter the data : ");

scanf("%d", &num);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 73

while(num!=–1)

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

new_node -> data = num;

if(start == NULL)

new_node -> next = new_node;

start = new_node;

else

ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = new_node;

new_node -> next = start;

printf("\n Enter the data : ");

scanf("%d", &num);

return start;

struct node *display(struct node *start)


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 74

struct node *ptr;

ptr=start;

while(ptr -> next != start)

printf("\t %d", ptr -> data);

ptr = ptr -> next;

printf("\t %d", ptr -> data);

return start;

struct node *insert_beg(struct node *start)

struct node *new_node, *ptr;

int num;

printf("\n Enter the data : ");

scanf("%d", &num);

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

new_node -> data = num;

ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = new_node;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 75

new_node -> next = start;

start = new_node;

return start;

struct node *insert_end(struct node *start)

struct node *ptr, *new_node;

int num;

printf("\n Enter the data : ");

scanf("%d", &num);

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

new_node -> data = num;

ptr = start;

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = new_node;

new_node -> next = start;

return start;

struct node *delete_beg(struct node *start)

struct node *ptr;

ptr = start;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 76

while(ptr -> next != start)

ptr = ptr -> next;

ptr -> next = start -> next;

free(start);

start = ptr -> next;

return start;

struct node *delete_end(struct node *start)

struct node *ptr, *preptr;

ptr = start;

while(ptr -> next != start)

preptr = ptr;

ptr = ptr -> next;

preptr -> next = ptr -> next;

free(ptr);

return start;

struct node *delete_after(struct node *start)

struct node *ptr, *preptr;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 77

int val;

printf("\n Enter the value after which the node has to deleted : ");

scanf("%d", &val);

ptr = start;

preptr = ptr;

while(preptr -> data != val)

preptr = ptr;

ptr = ptr -> next;

preptr -> next = ptr -> next;

if(ptr == start)

start = preptr -> next;

free(ptr);

return start;

struct node *delete_list(struct node *start)

struct node *ptr;

ptr = start;

while(ptr -> next != start)

start = delete_end(start);

free(start);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 78

return start;

OUTPUT:

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 1

Enter -1 to end
Enter the data : 99

Enter the data : 78

Enter the data : 22

Enter the data : 44

Enter the data : -1

CIRCULAR LINKED LIST CREATED


**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 79

5: Delete a node from the beginning


6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 2


99 78 22 44

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 3

Enter the data : 70

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 80

Enter your option : 2


70 99 78 22 44

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 4


Enter the data : 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 2


70 99 78 22 44 100

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 81

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 5

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 2


99 78 22 44 100

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 82

6: Delete a node from the end


7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 6

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 2


99 78 22 44

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 7

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 83

Enter the value after which the node has to deleted : 78

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 2


99 78 44

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 8

99 is to be deleted next
78 is to be deleted next
44 is to be deleted next
CIRCULAR LINKED LIST DELETED

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 84

**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Delete a node from the beginning
6: Delete a node from the end
7: Delete a node after a given node
8: Delete the entire list
9:EXIT

Enter your option : 9

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 85

A C program to perform Push, Pop, and Peek operations on a stack.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 3

int st[MAX], top=-1;

void push(int st[], int val);

int pop(int st[]);

int peek(int st[]);

void display(int st[]);

int main(int argc, char *argv[])

int val, option;

do

printf("\n *****MAIN MENU*****");

printf("\n 1. PUSH");

printf("\n 2. POP");

printf("\n 3. PEEK");

printf("\n 4. DISPLAY");

printf("\n 5. EXIT");

printf("\n Enter your option: ");

scanf("%d", &option);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 86

switch(option)

case 1: printf("\n Enter the number to be pushed on stack: ");

scanf("%d", &val);

push(st, val);

break;

case 2: val = pop(st);

if(val != -1)

printf("\n The value deleted from stack is: %d", val);

break;

case 3: val = peek(st);

if(val != -1)

printf("\n The value stored at top of stack is: %d", val);

break;

case 4: display(st); break;

while(option != 5);

return 0;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 87

void push(int st[], int val)

if(top == MAX-1)

printf("\n STACK OVERFLOW");

else

top++;

st[top] = val;

int pop(int st[])

int val;

if(top == -1)

printf("\n STACK UNDERFLOW");

return -1;

else

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 88

val = st[top];

top--;

return val;

void display(int st[])

int i;

if(top == -1)

printf("\n STACK IS EMPTY");

else

for(i=top;i>=0;i--)

printf("\n %d",st[i]);

printf("\n");

int peek(int st[])

if(top == -1)

printf("\n STACK IS EMPTY");

return -1;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 89

else

return (st[top]);

OUTPUT:

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 3

**MAIN MENU**

1. PUSH 2.
2. POP
3. PEEK
4. DISPLAY
5. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 4

**MAIN MENU**

1. PUSH

2. POP

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 90

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:1

Enter the number to be pushed on stack:5

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT Enter your option:1

Enter the number to be pushed on stack:6

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:4

3
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 91

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:2

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:4

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 92

Enter your option:3

The value stored at top of stack is:5

**MAIN MENU**

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:5

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 93

A program to implement a linked stack.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct stack

int data;

struct stack *next;

};

struct stack *top = NULL;

struct stack *push(struct stack *, int);

struct stack *display(struct stack *);

struct stack *pop(struct stack *);

int peek(struct stack *);

int main()

int val, option;

do

printf("\n *****MAIN MENU*****");

printf("\n 1. PUSH");

printf("\n 2. POP");

printf("\n 3. PEEK");
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 94

printf("\n 4. DISPLAY");

printf("\n 5. EXIT");

printf("\n Enter your option: ");

scanf("%d", &option);

switch(option)

case 1: printf("\n Enter the number to be pushed on stack: ");

scanf("%d", &val);

top = push(top, val);

break;

case 2: top = pop(top);

break;

case 3: val = peek(top);

if (val != -1)

printf("\n The value at the top of stack is: %d", val);

else

printf("\n STACK IS EMPTY");

break;

case 4: top = display(top);

break;

while(option != 5);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 95

return 0;

struct stack *push(struct stack *top, int val)

struct stack *ptr;

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

ptr -> data = val;

if(top == NULL)

ptr -> next = NULL;

top = ptr;

else

ptr -> next = top;

top = ptr;

return top;

struct stack *display(struct stack *top)

struct stack *ptr;

ptr = top;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 96

if(top == NULL)

printf("\n STACK IS EMPTY");

else

while(ptr != NULL)

printf("\n %d", ptr -> data);

ptr = ptr -> next;

return top;

struct stack *pop(struct stack *top)

struct stack *ptr;

ptr = top;

if(top == NULL)

printf("\n STACK UNDERFLOW");

else

top = top -> next;

printf("\n The value being deleted is: %d", ptr -> data);

free(ptr);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 97

return top;

int peek(struct stack *top)

if(top==NULL)

return -1;

else {

return top->data;

OUTPUT:

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:1

Enter the number to be pushed on stack: 10

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 98

4. DISPLAY

5. EXIT

Enter your option:1

Enter the number to be pushed on stack: 9

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:1

Enter the number to be pushed on stack: 8

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:1

Enter the number to be pushed on stack: 7

*****MAIN MENU*****

1. PUSH

2. POP
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 99

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:4

10

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:2

The value being deleted is : 7

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:3


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 100

The value at the top of stack is: 8

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option:4

10

*****MAIN MENU*****

1. PUSH

2. POP

3. PEEK

4. DISPLAY

5. EXIT

Enter your option: 5

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 101

Write a program to evaluate a postfix expression.

#include

#include

#include

#define MAX 100

float st[MAX];

int top=–1;

void push(float st[], float val);

float pop(float st[]);

float evaluatePostfixExp(char exp[]);

int main()

float val;

char exp[100];

clrscr();

printf("\n Enter any postfix expression : ");

gets(exp);

val = evaluatePostfixExp(exp);

printf("\n Value of the postfix expression = %.2f", val);

getch();

return 0;

float evaluatePostfixExp(char exp[])


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 102

int i=0;

float op1, op2, value;

while(exp[i] != '\0')

if(isdigit(exp[i]))

push(st, (float)(exp[i]–'0'));

else

op2 = pop(st);

op1 = pop(st);

switch(exp[i])

case '+': value = op1 + op2;

break;

case '–': value = op1 – op2;

break;

case '/': value = op1 / op2;

break;

case '*': value = op1 * op2;

break;

case '%': value = (int)op1 % (int)op2;

break;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 103

push(st, value);

i++;

return(pop(st));

void push(float st[], float val)

if(top==MAX–1)

printf("\n STACK OVERFLOW");

else

top++;

st[top]=val;

float pop(float st[])

float val=–1;

if(top==–1)

printf("\n STACK UNDERFLOW");

else
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 104

val=st[top];

top––;

return val;

OUTPUT:

Enter any postfix expression : 3 4 5* + 7 –

value of the postfix expression = -16.00

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 105

Write a program to check nesting of parentheses using a stack.

#include

#include

#include

#define MAX 10 int top = –1;

int stk[MAX];

void push(char);

char pop();

void main()

char exp[MAX],temp;

int i, flag=1;

clrscr();

printf("Enter an expression : ");

gets(exp);

for(i=0;i<strlen(exp);i++)

if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')

push(exp[i]);

if(exp[i]==’)’ || exp[i]==’}’ || exp[i]==’]’)

if(top == –1)

flag=0;

else
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 106

temp=pop();

if(exp[i]==')' && (temp=='{' || temp=='['))

flag=0;

if(exp[i]=='}' && (temp=='(' || temp=='['))

flag=0;

if(exp[i]==']' && (temp=='(' || temp=='{'))

flag=0;

if(top>=0)

flag=0;

if(flag==1)

printf("\n Valid expression");

else

printf("\n Invalid expression");

void push(char c)

if(top == (MAX–1))

printf("Stack Overflow\n");

else

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 107

top=top+1;

stk[top] = c;

char pop()

if(top == –1)

printf("\n Stack Underflow");

else

return(stk[top––]);

OUTPUT:

Enter an expression : [({})]

valid expression

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 108

Write a program to implement a linear queue.

#include<stdio.h>

#include<stdlib.h>

#define MAX 10

int queue[MaX];

int front = -1, rear = -1;

void insert(void);

int delete_element(void);

int peek(void);

void display(void);

int main()

int option, val;

do

printf(“\n\n ***** MAIN MENU *****”);

printf(“\n 1. Insert an element”);

printf(“\n 2. Delete an element”);

printf(“\n 3. Peek”);

printf(“\n 4. Display the queue”);

printf(“\n 5. EXIT”);

printf(“\n Enter your option : “);

scanf(“%d”, &option);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 109

switch(option)

case 1: insert();

break;

case 2: val = delete_element();

if (val != -1)

printf(“\n The number deleted is : %d”, val);

break;

case 3: val = peek();

if (val != -1)

printf(“\n The first value in queue is : %d”, val);

break;

case 4: display();

break;

while(option != 5);

getch();

return 0;

void insert()

int num;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 110

printf(“\n Enter the number to be inserted in the queue : “);

scanf(“%d”, &num);

if(rear == MAX-1)

printf(“\n OVERFLOW”);

else if(front == -1 && rear == -1)

front = rear = 0;

else rear++;

queue[rear] = num;

int delete_element()

int val;

if(front == -1 || front>rear)

printf(“\n UNDERFLOW”);

return -1;

else

val = queue[front];

front++;

if(front > rear)

front = rear = -1;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 111

return val;

int peek()

if(front==-1 || front>rear)

printf(“\n QUEUE IS EMPTY”);

return -1;

else

return queue[front];

void display()

int i;

printf(“\n”);

if(front == -1 || front > rear)

printf(“\n QUEUE IS EMPTY”);

else

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 112

for(i = front;i <= rear;i++)

printf(“\t %d”, queue[i]);

OUTPUT:

***** MAIN MENU *****

1. Insert an element

2. Delete an element

3. Peek

4. Display the queue

5. EXIT Enter your option : 1

Enter the number to be inserted in the queue : 50

***** MAIN MENU *****

1. Insert an element

2. Delete an element

3. Peek

4. Display the queue

5. EXIT Enter your option : 1

Enter the number to be inserted in the queue : 60

***** MAIN MENU *****

1. Insert an element

2. Delete an element

3. Peek
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 113

4. Display the queue

5. EXIT Enter your option : 1

Enter the number to be inserted in the queue : 70

***** MAIN MENU *****

1. Insert an element

2. Delete an element

3. Peek

4. Display the queue

5. EXIT Enter your option : 4

50

60

70

***** MAIN MENU *****

1. Insert an element

2. Delete an element

3. Peek

4. Display the queue

5. EXIT

Enter your option : 5

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 114

Write a program to implement a linked queue.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct queue

struct node *front;

struct node *rear;

};

struct queue *q;

void create_queue(struct queue *);

struct queue *insert(struct queue *,int);

struct queue *delete_element(struct queue *);

struct queue *display(struct queue *);

int peek(struct queue *);

int main()

int val, option;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 115

create_queue(q);

clrscr();

do

printf("\n *****MAIN MENU*****");

printf("\n 1. INSERT");

printf("\n 2. DELETE");

printf("\n 3. PEEK");

printf("\n 4. DISPLAY");

printf("\n 5. EXIT");

printf("\n Enter your option : ");

scanf("%d", &option);

switch(option)

case 1: printf("\n Enter the number to insert in the queue:");

scanf("%d", &val);

q = insert(q,val);

break;

case 2: q = delete_element(q);

break;

case 3: val = peek(q);

if(val! = –1)

printf("\n The value at front of queue is : %d", val);


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 116

break;

case 4: q = display(q);

break;

while(option != 5);

getch();

return 0;

void create_queue(struct queue *q)

q -> rear = NULL;

q -> front = NULL;

struct queue *insert(struct queue *q,int val)

struct node *ptr;

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

ptr -> data = val;

if(q -> front == NULL)

q -> front = ptr;

q -> rear = ptr;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 117

q -> front -> next = q -> rear -> next = NULL;

else

q -> rear -> next = ptr;

q -> rear = ptr;

q -> rear -> next = NULL;

return q;

struct queue *display(struct queue *q)

struct node *ptr;

ptr = q -> front;

if(ptr == NULL)

printf("\n QUEUE IS EMPTY");

else

printf("\n");

while(ptr!=q -> rear)

printf("%d\t", ptr -> data);

ptr = ptr -> next;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 118

printf("%d\t", ptr -> data);

return q;

struct queue *delete_element(struct queue *q)

struct node *ptr;

ptr = q -> front;

if(q -> front == NULL)

printf("\n UNDERFLOW");

else

q -> front = q -> front -> next;

printf("\n The value being deleted is : %d", ptr -> data);

free(ptr);

return q;

int peek(struct queue *q)

if(q->front==NULL)

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 119

printf("\n QUEUE IS EMPTY");

return –1;

else

return q->front->data;

OUTPUT:

*****MAIN MENU*****

1. INSERT

2. DELETE

3. PEEK

4. DISPLAY

5. EXIT

Enter your option : 3

QUEUE IS EMPTY

Enter your option : 5

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 120

C Program to stimulate simple printer queue system

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct PrintJob {

int job_id;

char description[100];

struct PrintJob* next;

} PrintJob;

typedef struct {

PrintJob* front;

PrintJob* rear;

} PrinterQueue;

// Function to create a new print job

PrintJob* create_print_job(int job_id, const char* description) {

PrintJob* new_job = (PrintJob*)malloc(sizeof(PrintJob));

new_job->job_id = job_id;

strcpy(new_job->description, description);

new_job->next = NULL;

return new_job;

// Function to initialize the printer queue

void init_printer_queue(PrinterQueue* queue) {


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 121

queue->front = queue->rear = NULL;

// Function to add a job to the queue

void enqueue(PrinterQueue* queue, int job_id, const char* description) {

PrintJob* new_job = create_print_job(job_id, description);

if (queue->rear == NULL) {

queue->front = queue->rear = new_job;

return;

queue->rear->next = new_job;

queue->rear = new_job;

printf("Job %d added to the queue.\n", job_id);

// Function to process a job from the queue

void dequeue(PrinterQueue* queue) {

if (queue->front == NULL) {

printf("No jobs in the queue.\n");

return;

PrintJob* temp = queue->front;

queue->front = queue->front->next;

if (queue->front == NULL) {

queue->rear = NULL;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 122

printf("Processing job %d: %s\n", temp->job_id, temp->description);

free(temp);

// Function to display the current state of the queue

void display_queue(PrinterQueue* queue) {

if (queue->front == NULL) {

printf("No jobs in the queue.\n");

return;

PrintJob* temp = queue->front;

while (temp != NULL) {

printf("Job %d: %s\n", temp->job_id, temp->description);

temp = temp->next;

// Main function with a simple command-line interface

int main() {

PrinterQueue queue;

init_printer_queue(&queue);

int choice, job_id;


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 123

char description[100];

while (1) {

printf("\nPrinter Queue System\n");

printf("1. Add Job\n");

printf("2. Process Job\n");

printf("3. Display Queue\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter job ID: ");

scanf("%d", &job_id);

printf("Enter job description: ");

scanf(" %[^\n]", description);

enqueue(&queue, job_id, description);

break;

case 2:

dequeue(&queue);

break;

case 3:

display_queue(&queue);

break;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 124

case 4:

printf("Exiting...\n");

exit(0);

default:

printf("Invalid choice. Please try again.\n");

return 0;

OUTPUT DATE:

Printer Queue System

1. Add print job

2. Process next print job

3. Display print jobs

4. Exit

Enter your choice: 1

Enter print job description: data strctures

Print job added: ID = 0, Description = data strctures

Printer Queue System

1. Add print job

2. Process next print job

3. Display print jobs

4. Exit
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 125

Enter your choice: 1

Enter print job description: c programming language

Print job added: ID = 1, Description = c programming language

Printer Queue System

1. Add print job

2. Process next print job

3. Display print jobs

4. Exit

Enter your choice: 2

Processing print job: ID = 0, Description = data strctures

Printer Queue System

1. Add print job

2. Process next print job

3. Display print jobs

4. Exit

Enter your choice: 3

Current print jobs in the queue:

ID: 1, Description: c programming language

Printer Queue System

1. Add print job

2. Process next print job

3. Display print jobs

4. Exit
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 126

Enter your choice: 4

Exiting...

RESULT: c programme for creating simple printer queue system is executed successfully

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 127

C program to solve problems involving circular queues

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int player_id;

struct node *next;

};

struct node *start, *ptr, *new_node;

int main()

int n, k, i, count;

clrscr();

printf("\n Enter the number of players : ");

scanf("%d", &n);

printf("\n Enter the value of k (every kth player gets eliminated): ");

scanf("%d", &k);

start = malloc(sizeof(struct node));

start–>player_id = 1;

ptr = start;

for (i = 2; i <= n; i++)

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 128

new_node = malloc(sizeof(struct node));

ptr–>next = new_node;

new_node–>player_id = i;

new_node–>next=start;

ptr=new_node;

for (count = n; count > 1; count––)

for (i = 0; i < k – 1; ++i)

ptr = ptr–>next;

ptr–>next = ptr–>next–>next;

printf("\n The Winner is Player %d", ptr–>player_id);

getch();

return 0;

OUTPUT:

Enter the number of players : 5

Enter the value of k( every kth player gets eliminated) the Winner is player 3

RESULT:

C programme for operation on the circular queues is executed successfully.

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 129

Write a program to convert an infix expression to a prefix expression.

#include

#include

#include

#include

#define MAX 100

char st[MAX];

int top=–1;

void reverse(char str[]);

void push(char st[], char);

char pop(char st[]);

void InfixtoPostfix(char source[], char target[]);

int getPriority(char);

char infix[100], postfix[100], temp[100];

int main()

clrscr();

printf("\\n Enter any infix expression : ");

gets(infix);

reverse(infix);

strcpy(postfix, "");

InfixtoPostfix(temp, postfix);

printf("\n The corresponding postfix expression is : ");


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 130

puts(postfix);

strcpy(temp,"");

reverse(postfix);

printf("\n The prefix expression is : \n");

puts(temp);

getch();

return 0;

void reverse(char str[])

int len, i=0, j=0;

len=strlen(str);

j=len–1;

while(j>= 0)

if (str[j] == '(')

temp[i] = ')';

else if ( str[j] == ')')

temp[i] = '(';

else

temp[i] = str[j];

i++, j––;

}
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 131

temp[i] = '\0';

void InfixtoPostfix(char source[], char target[])

int i=0, j=0;

char temp;

strcpy(target, "");

while(source[i]!= '\0')

if(source[i]=='(')

push(st, source[i]);

i++;

else if(source[i] == ')')

while((top!=–1) && (st[top]!='('))

target[j] = pop(st);

j++;

if(top==–1)

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 132

printf("\n INCORRECT EXPRESSION");

exit(1);

temp = pop(st);

i++;

else if(isdigit(source[i]) || isalpha(source[i])) { target[j] = source[i];

j++;

i++;

else if( source[i] == '+' || source[i] == '–' || source[i] == '*' || source[i] == '/' || source[i] ==
'%')

while( (top!=–1) && (st[top]!= '(') && (getPriority(st[top]) > getPriority(source[i])))

target[j] = pop(st);

j++;

push(st, source[i]);

i++;

else

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 133

printf("\n INCORRECT ELEMENT IN EXPRESSION");

exit(1);

while((top!=–1) && (st[top]!='('))

target[j] = pop(st);

j++;

target[j]='\0';

int getPriority( char op)

if(op=='/' || op == '*' || op=='%')

return 1;

else if(op=='+' || op=='–')

return 0;

void push(char st[], char val)

if(top==MAX–1)

printf("\n STACK OVERFLOW");

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 134

else

top++;

st[top] = val;

char pop(char st[])

char val=' ';

if(top==–1)

printf("\n STACK UNDERFLOW");

else

val=st[top];

top––;

return val;

OUTPUT:

Enter any infix expression :( (5+2)*(8*2))-5

The corresponding postfix expression is : 52+82**5-

Result: Hence , c program to evaluate an infix expression and convert it to postfix


executed successfully.

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 135

Create a C program to determine whether a given string is a palindrome or not

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 10

int top = -1, front = 0;

int stack[MAX];

void push(char);

void pop();

void main()

int i, choice;

char s[MAX], b;

printf("Enter the String : \n");

scanf("%s", s);

for (i = 0;s[i] != '\0';i++)

b = s[i];

push(b);

for (i = 0;i < (strlen(s) / 2);i++)

if (stack[top] == stack[front])
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 136

pop();

front++;

else

printf("%s is not a palindrome\n", s);

break;

if ((strlen(s) / 2) == front)

printf("%s is palindrome\n", s);

front = 0;

top = -1;

exit(0);

void push(char a)

top++;

stack[top] = a;

void pop()

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 137

top--;

OUTPUT:

MENU

---------------------------

[Link] string is palindrome.

[Link]

---------------------------

Choose operation : 1

Enter string : amma

'amma' is palindrome.

Choose operation : 1

Enter string : nanna

'nanna' is not palindrome.

Choose operation : 1

Enter string : Jalaja

'jalaja' is not palindrome.

Choose operation : 1

Enter string : madam

'madam' is palindrome.

Choose operation : 2
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 138

RESULT: c programme for checking whether a string is a palindrome or not using stacks is
executed successfully.

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 139

Write a c program to Implement a stack or queue to perform comparison and check for
symmetry.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define bool int

#define true 1

#define false 0

struct Stack {

int top;

unsigned capacity;

char* array;

};

struct Stack* createStack(unsigned capacity) {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (char*)malloc(stack->capacity * sizeof(char));

return stack;

bool isFull(struct Stack* stack) {

return stack->top == stack->capacity - 1;}

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 140

bool isEmpty(struct Stack* stack) {

return stack->top == -1;

void push(struct Stack* stack, char item) {

if (isFull(stack))

return;

stack->array[++stack->top] = item;

char pop(struct Stack* stack) {

if (isEmpty(stack))

return '\0';

return stack->array[stack->top--];

bool checkSymmetry(char* string) {

int len;

int i;

struct Stack* stack;

len = strlen(string);

stack = createStack(len);

for (i = 0; i < len / 2; i++) {

push(stack, string[i]);

for (i = (len + 1) / 2; i < len; i++) {


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 141

if (string[i] != pop(stack)) {

free(stack->array);

free(stack);

return false;

free(stack->array);

free(stack);

return true;

int main() {

char string[100];

printf("Enter a string: ");

scanf("%s", string);

if (checkSymmetry(string)) {

printf("The string is symmetric.\n");

} else {

printf("The string is not symmetric.\n");

return 0;

OUTPUT:

Enter a string: department


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 142

The string is not symmetric.

RESULT:

Hence the C program to check whether the given string symmetric or not is executed
successfully.

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 143

Implementing a BST using linked list and traversal.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node {

int data;

struct node *left; struct node *right;

};

struct node *tree;

void create_tree(struct node *);

struct node *insertElement(struct node *, int);

void preorderTraversal(struct node *);

void inorderTraversal(struct node *);

void postorderTraversal(struct node *);

struct node *findSmallestElement(struct node *);

struct node *findLargestElement(struct node *);

struct node *deleteElement(struct node *, int);

struct node *mirrorImage(struct node *);

int totalNodes(struct node *);

int totalExternalNodes(struct node *);

int totalInternalNodes(struct node *);

int Height(struct node *);

struct node *deleteTree(struct node *);


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 144

int main()

int option, val;

struct node *ptr; create_tree(tree);

clrscr();

do

printf("\n ******MAIN MENU******* \n");

printf("\n 1. Insert Element");

printf("\n 2. Preorder Traversal");

printf("\n 3. Inorder Traversal");

printf("\n 4. Postorder Traversal");

printf("\n 5. Find the smallest element");

printf("\n 6. Find the largest element");

printf("\n 7. Delete an element");

printf("\n 8. Count the total number of nodes");

printf("\n 9. Count the total number of external nodes");

printf("\n 10. Count the total number of internal nodes");

printf("\n 11. Determine the height of the tree");

printf("\n 12. Find the mirror image of the tree");

printf("\n 13. Delete the tree");

printf("\n 14. Exit");

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 145

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

case 1: printf("\n Enter the value of the new node : ");

scanf("%d", &val);

tree = insertElement(tree, val);

break;

case 2: printf("\n The elements of the tree are : \n");

preorderTraversal(tree);

break;

case 3: printf("\n The elements of the tree are : \n");

inorderTraversal(tree);

break;

case 4: printf("\n The elements of the tree are : \n");

postorderTraversal(tree);

break;

case 5: ptr = findSmallestElement(tree);

printf("\n Smallest element is :%d",ptr–>data);

break;

case 6: ptr = findLargestElement(tree);

printf("\n Largest element is : %d", ptr–>data);

break;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 146

case 7: printf("\n Enter the element to be deleted : ");

scanf("%d", &val);

tree = deleteElement(tree, val);

break;

case 8: printf("\n Total no. of nodes = %d", totalNodes(tree));

break;

case 9: printf("\n Total no. of external nodes = %d", totalExternalNodes(tree));

break;

case 10: printf("\n Total no. of internal nodes = %d", totalInternalNodes(tree));

break;

case 11: printf("\n The height of the tree = %d",Height(tree));

break;

case 12: tree = mirrorImage(tree);

break;

case 13: tree = deleteTree(tree);

break;

while(option!=14);

getch();

return 0;

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 147

void create_tree(struct node *tree)

tree = NULL;

struct node *insertElement(struct node *tree, int val)

struct node *ptr, *nodeptr, *parentptr;

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

ptr–>data = val;

ptr–>left = NULL;

ptr–>right = NULL;

if(tree==NULL)

tree=ptr;

tree–>left=NULL;

tree–>right=NULL;

Else

parentptr=NULL;

nodeptr=tree;

while(nodeptr!=NULL)

{
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 148

parentptr=nodeptr;

if(valdata) nodeptr=nodeptr–>left;

else nodeptr = nodeptr–>right;

if(valdata) parentptr–>left = ptr;

else parentptr–>right = ptr;

return tree;

void preorderTraversal(struct node *tree)

if(tree != NULL)

printf("%d\t", tree–>data);

preorderTraversal(tree–>left);

preorderTraversal(tree–>right);

void inorderTraversal(struct node *tree)

if(tree != NULL)

inorderTraversal(tree->left);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 149

printf("%d\t", tree->data);

inorderTraversal(tree->right);

void postorderTraversal(struct node *tree)

if(tree != NULL)

postorderTraversal(tree->left);

postorderTraversal(tree->right);

printf("%d\t", tree->data);

struct node *findSmallestElement(struct node *tree)

if( (tree == NULL) || (tree->left == NULL))

return tree;

else

return findSmallestElement(tree ->left);

struct node *findLargestElement(struct node *tree)

if( (tree == NULL) || (tree->right == NULL))


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 150

return tree;

else

return findLargestElement(tree->right);

struct node *deleteElement(struct node *tree, int val)

struct node *cur, *parent, *suc, *psuc, *ptr;

if(tree–>left==NULL)

printf("\n The tree is empty ");

return(tree);

parent = tree;

cur = tree–>left;

while(cur!=NULL && val!= cur–>data)

parent = cur;

cur = (valdata)? cur–>left:cur–>right;

if(cur == NULL)

printf("\n The value to be deleted is not present in the tree");

return(tree);
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 151

if(cur–>left == NULL)

ptr = cur–>right;

else if(cur–>right == NULL)

ptr = cur–>left;

else

// Find the in–order successor and its parent

psuc = cur;

cur = cur–>left;

while(suc–>left!=NULL)

psuc = suc; suc = suc–>left;

if(cur==psuc)

// Situation 1

suc–>left = cur–>right;

Else

// Situation 2

suc–>left = cur–>left;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 152

psuc–>left = suc–>right;

suc–>right = cur–>right;

ptr = suc;

// Attach ptr to the parent node

if(parent–>left == cur)

parent–>left=ptr;

else parent–>right=ptr;

free(cur);

return tree;

int totalNodes(struct node *tree)

if(tree==NULL)

return 0;

else return(totalNodes(tree–>left) + totalNodes(tree–>right) + 1);

int totalExternalNodes(struct node *tree)

if(tree==NULL)

return 0;

else if((tree–>left==NULL) && (tree–>right==NULL))


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 153

return 1;

else return (totalExternalNodes(tree–>left) + totalExternalNodes(tree–>right));

int totalInternalNodes(struct node *tree)

if( (tree==NULL) || ((tree–>left==NULL) && (tree–>right==NULL)))

return 0;

else return (totalInternalNodes(tree–>left) + totalInternalNodes(tree–>right) + 1);

int Height(struct node *tree)

int leftheight, rightheight;

if(tree==NULL)

return 0;

else

leftheight = Height(tree–>left);

rightheight = Height(tree–>right);

if(leftheight > rightheight)

return (leftheight + 1);

else

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 154

return (rightheight + 1);

struct node *mirrorImage(struct node *tree)

struct node *ptr;

if(tree!=NULL)

mirrorImage(tree–>left);

mirrorImage(tree–>right);

ptr=tree–>left;

ptr–>left = ptr–>right;

tree–>right = ptr;

struct node *deleteTree(struct node *tree)

if(tree!=NULL)

deleteTree(tree–>left);

deleteTree(tree–>right);

free(tree);

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 155

OUTPUT:

**MAIN MENU**

1. Insert Element

2. Preorder Traversal

3. Inorder Traversal

4. Postorder Traversal

5. Find the smallest element

6. Find the largest element

7. Delete an element

8. Count the total number of nodes

9. Count the total number of external nodes

10. Count the total number of internal nodes

11. Determine the height of the tree

12. Find the mirror image of the tree

13. Delete the tree

14. Exit

Enter your option : 1

Enter the value of the new node : 1

Enter the value of the new node : 2

Enter the value of the new node : 4

Enter your option : 3


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 156

2 1 4

Enter your option : 14

RESULT:

Hence the C program to implement BST using linked list and traversal is executed
successfully.

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 157

Implement a hash table with hash resolution techniques.

#include<stdio.h>

#include<conio.h>

int ht[10], i, found = 0, key;

void insert_val();

void delete_val();

void search_val();

void display();

int main()

int option;

clrscr();

for ( i = 0;i < 10;i++ ) //to initialize every element as ‘–1’

ht[i] = –1;

do

printf( "\n MENU \[Link] \[Link] \[Link] \[Link] \[Link]");

printf( "\n Enter your option.");

scanf( "%d", &option);

switch (option)

case 1: insert_val();

break;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 158

case 2: delete_val();

break;

case 4: search_val();

break;

case 3: display();

break;

default: printf( "\nInvalid choice entry!!!\n" );

break;

while (option!=4)

getch();

return 0;

void insert_val()

int val, f = 0;

printf( "\nEnter the element to be inserted : " );

scanf( "%d", &val );

key = ( val % 10 ) – 1;

if ( ht[key] == –1 )

ht[key] = val;
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 159

else { if ( key < 9 )

for ( i = key + 1;i < 10;i++ )

if ( ht[i] == –1 )

ht[i] = val;

break;

for ( i = 0;i < key;i++ )

if ( ht[i] == –1 )

ht[i] = val;

break;

void display()
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 160

for (i = 0;i < 10;i++)

printf( "\t%d", ht[ i ] );

void search_val()

int val, flag = 0;

printf( "\nEnter the element to be searched :: " );

scanf( "%d", &val );

key = ( val % 10 ) – 1;

if ( ht[ key ] == val )

flag = 1;

else

for (i = key + 1;i < 10;i++)

if(ht[i] == val)

flag = 1;

key = i;

break;

}
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 161

if (flag == 0)

for (i = 0;i < key;i++)

if (ht[ i ] == val)

flag = 1;

key = i;

break;

if (flag == 1)

found=1;

printf("\n The item searched was found at position %d !", key + 1 );

else { key = –1;

printf( "\nThe item searched was not found in the hash table" );

void delete_val()
DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 162

search_val();

if (found==1)

if ( key != –1 )

printf( "\nThe element deleted is %d ", ht[ key ] );

ht[ key ] = –1;

Output : Enter key and value to insert into the hash table: 3 56

Do you want to insert another key-value pair? (y/n): y

Enter key and value to insert into the hash table: 14 24

Do you want to insert another key-value pair? (y/n): y

Enter key and value to insert into the hash table: 6 0

Do you want to insert another key-value pair? (y/n): y

Enter key and value to insert into the hash table: 4 89

Do you want to insert another key-value pair? (y/n): n

Enter key to search in the hash table: 3

Value of Key 3 = 56

Enter key to delete from the hash table: 14


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 163

Node value of key 14 is deleted successfully

Enter key to search again in the hash table: 3

Value of Key 3 = 56

RESULT : A C Program to Implement a hash table with collision resolution techniques is


successfully executed

DATA STRUCTURES LABORATORY


DATE: EXPNO: PAGE NO: 164

Write a c program to implement a simple cache using hashing

#include <stdio.h>

#include <stdlib.h>

#define SIZE 10 // Size of the hash table

struct Node {

int key;

int value;

struct Node* next;

};

struct Node* hashTable[SIZE] = {NULL};

struct Node* createNode(int key, int value)

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->key = key;

newNode->value = value;

newNode->next = NULL;

return newNode;

void insert(int key, int value) {

int index = key % SIZE; // Calculate hash index


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 165

struct Node* newNode = createNode(key, value);

newNode->next = hashTable[index];

hashTable[index] = newNode;

int search(int key) {

int index = key % SIZE; // Calculate hash index

struct Node* current = hashTable[index];

while (current != NULL) {

if (current->key == key) {

return current->value; // Key found, return its value

current = current->next;

return -1; // Key not found

int main() {

int key, value, foundValue;

char choice;

do

printf("Enter key and value to insert into the cache: ");

scanf("%d %d", &key, &value);

insert(key, value); // Insert the key-value pair into the cache


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 166

printf("Do you want to insert another key-value pair? (y/n): ");

scanf(" %c", &choice);

while(choice == 'y' || choice == 'Y');

printf("Enter key to search in the cache: ");

scanf("%d", &key);

foundValue = search(key); // Declare foundValue at the top and use it here

if (foundValue != -1) {

printf("Value of Key %d = %d\n", key, foundValue);

} else {

printf("Key %d not found in the cache\n", key);

return 0;

OUTPUT :

Enter key and value to insert into the cache: 14 22

Do you want to insert another key-value pair? (y/n): y

Enter key and value to insert into the cache: 35 44

Do you want to insert another key-value pair? (y/n): y

Enter key and value to insert into the cache: 46 48

Do you want to insert another key-value pair? (y/n): n

Enter key to search in the cache: 2

Key 2 not found in the cache


DATA STRUCTURES LABORATORY
DATE: EXPNO: PAGE NO: 167

RESULT : A C Program to implement a simple cache using hashing is successfully executed

DATA STRUCTURES LABORATORY

You might also like