Vanshika Ds Lab-File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

LAB FILE

DATA
STRUCTURES
Dated : 13-11-2024

Submitted To: MR.


Praveen Sir SUBMITTED BY VANSHIKA RATHORE
Global Institute of
Technology and LAB FILE ON DS-I
Management
DATED: 13-11-2024
Farrukhnagar, Gurugram
ROLL-NO: 23B1083
INDEX
Page No Experiment Date Sign

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;

while (i <= mid && j <= high)


{
if (A[i] < A[j])
{
B[k] = A[i];
i++;
k++;
}
else
{
B[k] = A[j];
j++;
k++;
}
}
while (i <= mid)
{
B[k] = A[i];
k++;
i++;
}
while (j <= high)
{
B[k] = A[j];
k++;
j++;
}
// It will copy data from temporary array to array
for (int i = low; i <= high; i++)
{
A[i] = B[i];
}
}

void mergeSort(int number[], int low, int high)


{
int mid;

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>

// Function to swap two elements


void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

/* heapify the subtree with root i */


void heapify(int* arr, int n, int i)
{
// store largest as the root element
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
// now check whether the right and left right is larger than the root or not
if (left < n && arr[left] > arr[largest])
{
largest = left;
}
if (right < n && arr[right] > arr[largest])
{
largest = right;
}
// if the root is smaller than the children then swap it with the largest children's value
if (largest != i)
{
swap(&arr[i], &arr[largest]);
// again heapify that side of the heap where the root has gone
heapify(arr, n, largest);
}
}

/* sorts the given array of n size */


void heapsort(int* arr, int n)
{
// build the binary max heap
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(arr, n, i);
}
// sort the max heap
for (int i = n - 1; i >= 0; i--)

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;

// Function to check if the stack is empty


bool isEmpty() {
return top == -1;
}

// Function to add an item to the stack


void push(int item) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = item;
}
}

// Function to remove an item from the stack


int pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return -1; // Indicating underflow
} else {
return stack[top--];
}
}

// Function to get the top item of the stack


int peek() {
if (isEmpty()) {
printf("Stack is Empty\n");
return -1; // Indicating empty stack
} else {
return stack[top];
}
}

// Function to show all the items from stack


void show()

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;

struct node *create(int value)


{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = value;
temp->next = NULL;
return temp;
}

void push(int value)


{
struct node *newnode;
newnode = create(value);
if (head == NULL)
{
head = newnode;
}
else
{
newnode->next = head;
head = newnode;
}
}

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

int queue[MAX_SIZE],front=-1, rear=-1;

// Function to add an element to the queue


void enqueue(int element)
{
if (rear == MAX_SIZE - 1)
{
printf("Queue Overflow\n");
}
else
{
if(front == -1)
front = 0;
queue[++rear] = element;
}
}

// Function to remove an element from the queue


int dequeue()
{
int item;
if (front == -1)
{
printf("Queue Underflow\n");
return -1; // Indicating underflow
}
else
{
int item = queue[front++];
if (front > rear) // Queue is now empty
{
front = -1;
rear = -1;
}
return item;
}
}

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;

struct node *create(int value)


{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}

void enqueue(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 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);
}

struct node *create(int value)


{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}
void insert_begin(int value)
{
struct node *newnode;
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}

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 insert_pos(int value,int pos)


{
struct node *newnode, *temp1,*temp2;
int i,c=1;
newnode=create(value);
i=count();
if(pos==1)
insert_begin(value);
else if(pos>i+1)
{
printf("insertion is not posible");
return;
}
else
{
temp1=head;
while(c<=pos-1 && temp1!=NULL)
{
temp2=temp1;
temp1=temp1->next;
c++;
}
newnode->next=temp2->next;
temp2->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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy