Vanshika Ds Lab-File
Vanshika Ds Lab-File
Vanshika Ds Lab-File
DATA
STRUCTURES
Dated : 13-11-2024
3
Quick Sort
5 Merge Sort
8
Heap Sort
10
Stack Array
13
StackLinked List
16
Queue Array
19
Queue Linked List
2
Exp 1 – Write a program for quick sort
Solution:
#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first; // Choose the first element as pivot
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j) // swap two elements
{
swap(&number[i], &number[j]);
}
}
// Swap the pivot element with the element at i+1 position
swap(&number[pivot], &number[j]);
// Recursive call on the left of pivot
quicksort(number,first,j-1);
// Recursive call on the right of pivot
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
quicksort(number,0,count-1);
3
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output:
4
Exp-2 : Write a Program for Merge sort
Solution:
#include <stdio.h>
void merge(int A[], int mid, int low, int high)
{
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;
5
if(low<high)
{
// finding the mid value of the array.
mid = (low + high) /2;
// Calling the merge sort for the first half
mergeSort(number, low, mid);
// Calling the merge sort for the second half
mergeSort(number, mid+1, high);
// Calling the merge function
merge(number, mid, low, high);
}
}
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
mergeSort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
6
Output :
7
Exp -3 : Write a program for Heap sort:
Solution:
#include <stdio.h>
8
{
// swap the root node and the last leaf node
swap(&arr[i], &arr[0]);
// again heapify the max heap from the root
heapify(arr, i, 0);
}
}
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
heapsort(number,count);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output :
9
Exp – 4 : Write a program for Stack Array
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
#define MAX_SIZE 5
int stack[MAX_SIZE],top=-1;
10
{
int i;
if (isEmpty())
printf("Stack is Empty\n");
else{
for(i=top;i>-1;i--)
printf("%d\n",stack[i]);
}
}
// Main function
int main() {
int ch,data;
do{
printf("\n1. Push\n2. Pop\n3. Peek\n4. Show\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to push: ");
scanf("%d",&data);
push(data);
break;
case 2: printf("Popped: %d\n", pop());
break;
case 3: printf("Top element: %d\n", peek());
break;
case 4: show();
break;
case 5: break;
default: printf("Enter valid choice");
}
}while(ch!=5);
return 0;
}
11
Output:
12
Exp-5: Write a Program for Stack Linked List
Solution:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head = NULL;
void pop()
{
struct node *temp;
if (head == NULL)
{
printf("Stack is underflow");
}
else
{
temp = head;
head = head->next;
13
free(temp);
}
}
void show()
{
struct node *temp;
if (head == NULL)
{
printf("Stack is empty");
}
else
{
temp = head;
while (temp != NULL)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
}
void main()
{
int ch, pos, value;
do
{
printf("\n1. Push\n2. Pop\n3. Show\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter the value: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
break;
default:
printf("\nyour choice is wrong!.. ");
14
}
} while (ch != 4);
}
Output:
15
Exp-6: Write a program for QueueArray
Solution:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
16
// Function to display all the items from Queue
void display()
{
int i;
if (front == -1)
{
printf("Queue is Empty\n");
}
else
{
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}
// Main function
int main() {
int ch,data;
do{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to insert: ");
scanf("%d",&data);
enqueue(data);
break;
case 2: printf("Deleted: %d\n", dequeue());
break;
case 3: display();
break;
case 4: break;
default: printf("your choice is wrong!..");
}
}while(ch!=4);
return 0;
}
17
Output:
18
Exp-7 : Write a program for Queue Linked List
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
}*head=NULL;
void dequeue()
{
struct node *temp;
if(head==NULL)
{
printf("Queue Underflow");
}
else
{
19
temp=head;
head=head->next;
free(temp);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("Queue is empty");
}
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d, ",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to insert: ");
scanf("%d",&value);
enqueue(value);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4:break;
default: printf("\nyour choice is wrong!..");
}
}while(ch!=4);
20
}
Output:
21
Exp-8 Write a program for SingleLinkedList
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
}*head=NULL;
int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
i++;
}
return(i);
}
22
void insert_end(int value)
{
struct node *newnode, *temp;
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void delete_begin()
23
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void delete_end()
{
struct node *temp1,*temp2;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=NULL;
free(temp1);
}
}
void delete_pos(int pos)
{
struct node *temp1,*temp2;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else
{
temp1=head;
24
while(c<=pos && temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
c++;
}
temp2->next=temp1->next;
free(temp1);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d-> ",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void main()
{
int ch,pos,value;
do
{
printf("\n1.Insert Begin\n2.Insert End\n3.Insert Position\n4.Delete Begin\n5.Delete End\n6.Delete
Position\n7.Display\n8.Exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter the value:");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("enter value:");
scanf("%d",&value);
insert_end(value);
25
break;
case 3: printf("enter value:");
scanf("%d",&value);
printf("enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}
26
Output:
27