0% found this document useful (0 votes)
97 views27 pages

Data Structure Lab Manual

The document contains programs to perform various sorting algorithms: 1) Bubble sort is implemented to sort an integer array using bubble sort. 2) Insertion sort is implemented to sort an integer array using insertion sort. 3) Selection sort is implemented to sort an integer array using selection sort. 4) Quicksort is implemented to sort an integer array using quicksort recursively. 5) Mergesort is implemented to sort an integer array using mergesort by recursively dividing and merging subarrays.

Uploaded by

Shweta Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
97 views27 pages

Data Structure Lab Manual

The document contains programs to perform various sorting algorithms: 1) Bubble sort is implemented to sort an integer array using bubble sort. 2) Insertion sort is implemented to sort an integer array using insertion sort. 3) Selection sort is implemented to sort an integer array using selection sort. 4) Quicksort is implemented to sort an integer array using quicksort recursively. 5) Mergesort is implemented to sort an integer array using mergesort by recursively dividing and merging subarrays.

Uploaded by

Shweta Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

Experiment No.

11 (a)
Write a program to perform bubble sort.
#include<stdio.h>
main()
{
int a[100],i,j,temp,n;
//clear();
printf("\n Enter the max no.of Elements to Sort: \n");
scanf("%d",&n);
printf("\n Enter the Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
getch();
}
Output:
Enter the max no.of Elements to Sort:
5

Enter the Elements :


45
21
89
78
99
21 45 78 89 99

Experiment No. 11 (b)


Write a program to perform insertion sort.

#include<stdio.h>
int main(){
/* Here i & j for loop counters, temp for swapping,
* count for total number of elements, number[] to
* store the input numbers in array. You can increase
* or decrease the size of number array as per requirement
*/
int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);

// Implementation of insertion sort algorithm


for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
Output:

Experiment No. 11 (c)


Write a program to perform selection sort.
#include <stdio.h>

int main()
{
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;

for (d = c + 1; d < n; d++)


{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}
Output:

Experiment No. 12
Write a program to perform quick sort.
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[25];

printf("How many elements are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
Output:

Experiment No. 13
Write a program to perform merge sort.
#include<stdio.h>
#include<stdlib.h>

void Merge(int a[], int tmp[], int lpos, int rpos, int rend)
{
int i, lend, n, tmppos;
lend = rpos - 1;
tmppos = lpos;
n = rend - lpos + 1;

while(lpos <= lend && rpos <= rend)


{
if(a[lpos] <= a[rpos])
tmp[tmppos++] = a[lpos++];
else
tmp[tmppos++] = a[rpos++];
}
while(lpos <= lend)
tmp[tmppos++] = a[lpos++];
while(rpos <= rend)
tmp[tmppos++] = a[rpos++];

for(i = 0; i < n; i++, rend--)


a[rend] = tmp[rend];
}

void MSort(int a[], int tmp[], int left, int right)


{
int center;
if(left < right)
{
center = (left + right) / 2;
MSort(a, tmp, left, center);
MSort(a, tmp, center + 1, right);
Merge(a, tmp, left, center + 1, right);
}
}
void MergeSort(int a[], int n)
{
int *tmparray;
tmparray = malloc(sizeof(int) * n);
MSort(a, tmparray, 0, n-1);
free(tmparray);
}
main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
MergeSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}

Output:

Experiment No. 1(b)


Write a program to perform linear search.
#include <stdio.h>

int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);

return 0;
}
Output:

Experiment No. 1(c)


Write a program to perform Binary search.
#include <stdio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

output:

Experiment No. 1(a)


Write a program to perform insertion & deletion in 1-D array.

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if (position >= n+1)


printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];

printf("Resultant array:\n");

for (c = 0; c < n - 1; c++)


printf("%d\n", array[c]);
}

return 0;
}

Output:

Experiment No. 2
Write a program to implement stack & perform push & pop operation

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

#define MAX 5 //Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;

while(1) //infinite loop, will end when choice will be 4


{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);

default: printf("\nWrong Choice!!");


}
}
}

void push()
{
int val;

if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}

void display()
{
int i;

if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
Output

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):1

Enter element to push:3

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):1

Enter element to push:6

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):3

Stack is…
6
3

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):2

Deleted element is 6
*** Stack Menu ***
1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):3

Stack is…
3

*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):2

Deleted element is 3
*** Stack Menu ***

1.Push
2.Pop
3.Display
4.Exit

Enter your choice(1-4):2

Stack is empty!!

Experiment No. 3
Write a program to convert infix to postfix expression
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}

main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}

OUTPUT:

Enter the expression :: a+b*c


abc*+

Enter the expression :: (a+b)*c+(d-a)


ab+c*da-+
Experiment No. 4
Write a program to perform following operations on a linear queue

#include<stdio.h>
#include<stdlib.h>
#define max_size 5
int queue[max_size],front=-1,rear=-1;
/*------ Function Prototype------------*/
void insert();
void del();
void display();
/*-------------------------------------*/
int main()
{
int choice;
do{

printf("\n\n--------QUEUE OPERATIONS-----------\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);
return 0;
}
void insert() //Inserting an element in to the queue
{
int item;
if(rear==(max_size-1))
{
printf("\nQueue Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;

if(front==-1)
front=0;
}

}//end of insert()
void del() //deleting an element from the queue
{
int item;
if(front==-1)
{
printf("\nQueue Underflow:");
}
else
{
item=queue[front];
printf("\nThe deleted element: %d\t",item);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
}
}//end of del()
void display() //To display the queue elements
{
int i;
if(front==-1)
{
printf("\nQueue is Empty:");
}
else
{
printf("\nThe queue elements are:\n" );
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}

}//end of display()

Output:

Experiment No. 5
Write a program to perform following operations on a circular queue

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

#define max_size 5
int cqueue[max_size],front=-1,rear=-1;
/*------ Function Prototype------------*/
void insert();
void del();
void display();
/*-------------------------------------*/
main()
{
int choice;
do{

printf("\n\n--------CIRCULAR QUEUE OPERATIONS-----------\n");


printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);

}
void insert() //Inserting an element in to the queue
{
int item;
if(front==(rear+1)%max_size)
{
printf("\nQueue Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
rear=(rear+1)%max_size;
cqueue[rear]=item;

if(front==-1)
{
front=0;
rear=0;
}

}//end of insert()

void del() //deleting an element from the queue


{
int item;
if(front==-1)
{
printf("\nQueue Underflow:");
}
else
{
item=cqueue[front];
printf("\nThe deleted element: %d\t",item);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=(front+1)%max_size;
}
}
}//end of del()

void display() //To display the queue elements


{
int i;
if(front==-1)
{
printf("\nQueue is Empty:");
}
else
{
printf("\nThe queue elements are:\n" );
if(front<rear)
{
for(i=front;i<=rear;i++)
{
printf("%d\t",cqueue[i]);
}
}
else
{
for(i=0;i<=rear;i++)
{
printf("%d\t",cqueue[i]);
}
for(i=front;i<max_size;i++)
{
printf("%d\t",cqueue[i]);
}
}

}//end of display()

Output:

Experiment No. 6
Write a program to perform following operations on a double ended queue

Experiment No. 7
Write a program to perform following operations on a single linked list

#include<stdio.h>
#include<stdlib.h>
/*----Function Prototypes-----*/
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
/*-----------------------------*/
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main() //main() starts
{
int choice;
while(1){
printf("\n***SINGLE LINKED LIST OPERATIONS:****\n");
printf("\n MENU \n");
printf("---------------------------------------\n");
printf("\n 1.Create \n");
printf("\n 2.Display \n");
printf("\n 3.Insert at the beginning \n");
printf("\n 4.Insert at the end \n");
printf("\n 5.Insert at specified position \n");
printf("\n 6.Delete from beginning \n");
printf("\n 7.Delete from the end \n");
printf("\n 8.Delete from specified position \n");
printf("\n 9.Exit \n");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}//end of switch()
}
return 0;
}//end of main()
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}//end of create()
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}//end of while
}//end of else
}//end of display()
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}//end of insert_begin()
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}//end of insert_end
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->info) ;

temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}//end of else
}//end of insert_pos
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}//end of delete_begin()
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}//end of delete_begin()
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}//end of else
}//end of delete_pos()

Experiment No. 10
Write a program to implement linked stack

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

struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
int no, ch, e;

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");

create();

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create empty stack */
void create()
{
top = NULL;
}

/* Count stack elements */


void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}

/* Push data into stack */


void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}

/* Display stack elements */


void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)


{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

/* Pop Operation on stack */


void pop()
{
top1 = top;

if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

/* Return top element */


int topelement()
{
return(top->info);
}

/* Check if stack is empty or not */


void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}

/* Destroy entire stack */


void destroy()
{
top1 = top;

while (top1 != NULL)


{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");


count = 0;
}
$ cc pgm2.c
$ a.out
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56

Enter choice : 1
Enter data : 80

Enter choice : 2

Popped value : 80
Enter choice : 3

Top element : 56
Enter choice : 1
Enter data : 78

Enter choice : 1
Enter data : 90

Enter choice : 6
90 78 56
Enter choice : 7

No. of elements in stack : 3


Enter choice : 8

All stack elements destroyed


Enter choice : 4

Stack is empty
Enter choice : 5

You might also like