0% found this document useful (0 votes)
0 views

DS lab File

Uploaded by

Shivanshu Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

DS lab File

Uploaded by

Shivanshu Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Raj Kumar Goel Institute Of Technology

Ghaziabad UP

LAB FILE
BTECH 2nd YEAR

Name : Shivanshu Pandey


Branch : CSE(DS)
Roll No. : 2100331540064
Faculty Name : Anurag Gupta
Subject Name : Data Structure Using C
Subject Code : KCS-351
Semester : 3rd
Mob. No. : 9628032712
E-mail Id : [email protected]
Academic Year : 2022-23
DATA STRUCTURE USING C
INDEX

Program Program Name Date Remark


No.
PROGRAM 1

TRAVERSING 1D ARRAY

#include <stdio.h>

int main()
{
/* PROGRAM TO TRAVERSE A ID ARRAY*/
printf("\n");
int i,n;
printf("ENTER NUMBER OF ELEMENTS YOU WANT TO INSERT :");
scanf("%d",&n);
int A[n];
printf("START ENTERING ELEMENTS IN ARRAY :");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf("ARRAY IS \n");
for(i=0;i<n;i++)
{
printf("%d \t",A[i]);
}
return 0;
}
OUTPUT:
PROGRAM 2
TRAVERSING OF 2D ARRAY

#include <stdio.h>
int main()
{
int i,j,m,n;
printf("ENTER NUMBER OF ROWS IN ARRAY :");
scanf("%d",&m);
printf("ENTER NUMBER OF COLUMNS IN ARRAY :");
scanf("%d",&n);
int arr[m][n];
printf("\n");
printf("START ENTERING ELEMENTS IN ARRAY \n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("2D ARRAY IS \n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d \t",arr[i][j]);
}
}
return 0;
}

OUTPUT:
PROGRAM 3
INSERTION OF ELEMENT IN ARRAY

#include <stdio.h>
int main()
{
/* PROGRAM TO INSERT ELEMENT IN ARRAY*/
int i,n,ele,pos;
printf("ENTER NUMBER OF ELEMENTS YOU WANT IN ARRAY : ");
scanf("%d",&n);
int arr[n];
printf("START ENTERING ELEMENTS IN ARRAY \n");
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
printf("ARRAY BEFORE INSERTION IS \n");
for(i=1;i<=n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
printf("ENTER ELEMENT YOU WANT TO INSERT IN ARRAY :");
scanf("%d",&ele);
printf("\n");
printf("ENTER POSITION YOU WANT TO INSERT THE ELEMENT :");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
arr[i+1]=arr[i];
if (i==pos)
{
arr[i]=ele;
}
}
printf("ARRAY AFTER INSERTION IS \n");
for(i=0;i<n+1;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
OUTPUT:
PROGRAM 4
DELETION OF ELEMENT FROM AN ARRAY

#include <stdio.h>
int main()
{
int i,n,pos;
printf("ENTER NUMBER OF ELEMENTS IN ARRAY :");
scanf("%d",&n);
int arr[n];
printf("START ENTERING ELEMENTS IN ARRAY \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("ARRAY BEFORE DELETION IS \n ");
for(i=0;i<n;i++)
{
printf("%d \t",arr[i]);
}
printf("\n");
printf("ENTER POSITION FROM WHERE YOU WANT TO DELETE THE ELEMENT :");
scanf("%d",&pos);
if(pos>n)
{
printf("DELETION IS NOT POSSIBLE");
}
else
{
for(i=pos;i<n;i++)
{
arr[i]=arr[i+1];
}
}
printf("ARRAY AFTER DELETION IS \n");
for(i=0;i<n-1;i++)
{
printf("%d \t",arr[i]);
}
return 0;
}
OUTPUT:
PROGRAM 5
MERGING OF TWO ARRAY

#include<stdio.h>
#include<conio.h>
int main()
{
int arr1[50], arr2[50], size1, size2, i, k, merge[100];
printf("Enter Array 1 Size: ");
scanf("%d", &size1);
printf("Enter Array 1 Elements: ");
for(i=0; i<size1; i++)
{
scanf("%d", &arr1[i]);
merge[i] = arr1[i];
}
k = i;
printf("\nEnter Array 2 Size: ");
scanf("%d", &size2);
printf("Enter Array 2 Elements: ");
for(i=0; i<size2; i++)
{
scanf("%d", &arr2[i]);
merge[k] = arr2[i];
k++;
}
printf("\nThe new array after merging is:\n");
for(i=0; i<k; i++)
printf("%d ", merge[i]);
getch();
return 0;
}
OUTPUT:
PROGRAM 6
STACK USING ARRAY

#include <stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("Enter the size of stack[max=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATION USING ARRAY");
printf("\n\t............................");
printf("\n\t1.PUSH\n\t 2.POP\n\t 3. DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf("\n\t Please enter a valid choice");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\t Stack is overflow");
}
else
{
printf("Enter a value to be pushed");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t the popped elementis %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n the element in STACK \n");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\n Press next choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT:
Program-7
QUEUE USING C

#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
main()
{
int ch;
while (1)
{
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("Incorrect choice \n");
}
}
}

void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)

Front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &insert_item);
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}

void dequeue()
{
if (Front == - 1 || Front > Rear)
{
printf("Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n", inp_arr[Front]);
Front = Front + 1;
}
}

void show()
{

if (Front == - 1)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}
OUTPUT:
PROGRAM- 8
BUBBLE SORT
#include <stdio.h>

int main(){

int arr[50], num, x, y, temp;

printf("Please Enter the Number of Elements you want in the array: ");

scanf("%d", &num);

printf("Please Enter the Value of Elements: ");

for(x = 0; x < num; x++)

scanf("%d", &arr[x]);

for(x = 0; x < num - 1; x++){

for(y = 0; y < num - x - 1; y++){

if(arr[y] > arr[y + 1]){

temp = arr[y];

arr[y] = arr[y + 1];

arr[y + 1] = temp;

printf("Array after implementing bubble sort: ");

for(x = 0; x < num; x++){

printf("%d ", arr[x]);

return 0;

}
OUTPUT:
PROGRAM 9

SELECTION SORT

#include <stdio.h>

int main()

int array[100], n, c, d, position, t;

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)

t = array[c];
array[c] = array[position];

array[position] = t;

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

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

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

return 0;

}
OUTPUT:
PROGRAM 10

INSERTION SORT

#include <stdio.h>

int main(void)

int n, i, j, temp;

int arr[64];

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

scanf("%d", &n);

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

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

scanf("%d", &arr[i]);

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

j = i;

while (j > 0 && arr[j - 1] > arr[j])

temp = arr[j];

arr[j] = arr[j - 1];


arr[j - 1] = temp;

j--;

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

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

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

return 0;

}
OUTPUT:
PROGRAM 11

QUICK SORT

#include <stdio.h>

#include <stdlib.h>

int quickSort(int *arr, int low, int high)

int i = low, j = high;

int pivot = arr[(low + high) / 2];

while (i <= j)

while (arr[i] < pivot)

i++;

while (arr[j] > pivot)

j--;

if (i <= j)

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

i++;

j--;

}
}

if (low < j)

quickSort(arr, low, j);

if (i < high)

quickSort(arr, i, high);

return 0;

int main(void)

puts("Enter the number of elements in the array: ");

int n;

scanf("%d", &n);

int arr[n];

puts("Enter the elements of the array: ");

for (int i = 0; i < n; i++)

printf("arr[%d]: ", i);

scanf("%d", &arr[i]);

int low = 0;

int high = n - 1;

int pivot = arr[high];

int k = low - 1;
for (int j = low; j < high; j++)

if (arr[j] <= pivot)

k++;

int temp = arr[k];

arr[k] = arr[j];

arr[j] = temp;

int temp = arr[k + 1];

arr[k + 1] = arr[high];

arr[high] = temp;

int pi = k + 1;

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

puts("The sorted array is: ");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;

}
OUTPUT:
PROGRAM 12

MERGE SORT

#include <stdio.h>

#include <stdlib.h>

void Merge(int arr[], int left, int mid, int right)

int i, j, k;

int size1 = mid - left + 1;

int size2 = right - mid;

// created temporary array

int Left[size1], Right[size2];

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

Left[i] = arr[left + i];

for (j = 0; j < size2; j++)

Right[j] = arr[mid + 1 + j];

i = 0;

j = 0;

k = left;

while (i < size1 && j < size2)

{
if (Left[i] <= Right[j])

arr[k] = Left[i];

i++;

else

arr[k] = Right[j];

j++;

k++;

while (i < size1)

arr[k] = Left[i];

i++;

k++;

while (j < size2)

arr[k] = Right[j];

j++;

k++;
}

void Merge_Sort(int arr[], int left, int right)

if (left < right)

int mid = left + (right - left) / 2;

Merge_Sort(arr, left, mid);

Merge_Sort(arr, mid + 1, right);

Merge(arr, left, mid, right);

int main()

int size;

printf("Enter the size: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of array: ");

for (int i = 0; i < size; i++)

scanf("%d", &arr[i]);

}
Merge_Sort(arr, 0, size - 1);

printf("The sorted array is: ");

for (int i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

return 0;

}
OUTPUT:
PROGRAM 13

LINEAR SEARCH

#include <stdio.h>

int main()

int array[100],search,c,n;

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

scanf("%d",&n);

printf("Enter the 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) {

printf("%d is Present at location %d. \n",search,c+1);

break;

if(c==n)

printf("%d is not Present in the array. \n",search);

return 0;

}
OUTPUT:
PROGRAM 14

BINARY SEARCH

#include <stdio.h>

int main()

int i, low, high, mid, n, key, array[100];

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

scanf("%d",&n);

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

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

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

printf("Enter value to find\n");

scanf("%d", &key);

low = 0;

high = n - 1;

mid = (low+high)/2;

while (low <= high)

if(array[mid] < key)

low = mid + 1;

else if (array[mid] == key)

printf("%d found at location %d.\n", key, mid+1);

break;
}

else

high = mid - 1;

mid = (low + high)/2;

if(low > high)

printf("Not found! %d isn't present in the list.\n", key);

return 0;

}
OUTPUT:
PROGRAM 15

TOWER OF HANOI

#include <stdio.h>

void towers(int, char, char, char);

int main()

int num;

printf("Enter the number of disks : ");

scanf("%d", &num);

printf("The sequence of moves involved in the Tower of Hanoi are :\n");

towers(num, 'A', 'C', 'B');

return 0;

void towers(int num, char frompeg, char topeg, char auxpeg)

if (num == 1)

printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);

return;

towers(num - 1, frompeg, auxpeg, topeg);

printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);

towers(num - 1, auxpeg, topeg, frompeg);

}
OUTPUT:
PROGRAM 16

CIRCULAR QUEUE IMPLEMENTATION IN C

#include<stdio.h>

# define MAX 5

int cqueue_arr[MAX];

int front = -1;

int rear = -1;

void insert(int item)

if((front == 0 && rear == MAX-1) || (front == rear+1))

printf("Queue Overflow n");

return;

if(front == -1)

front = 0;

rear = 0;

else

if(rear == MAX-1)

rear = 0;

else
rear = rear+1;

cqueue_arr[rear] = item ;

void deletion()

if(front == -1)

printf("Queue Underflown");

return ;

printf("Element deleted from queue is : %d\n",cqueue_arr[front]);

if(front == rear)

front = -1;

rear=-1;

else

if(front == MAX-1)

front = 0;

else

front = front+1;

}
void display()

int front_pos = front,rear_pos = rear;

if(front == -1)

printf("Queue is empty\n");

return;

printf("Queue elements :\n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

printf("%d ",cqueue_arr[front_pos]);

front_pos++;

else

while(front_pos <= MAX-1)

printf("%d ",cqueue_arr[front_pos]);

front_pos++;

front_pos = 0;

while(front_pos <= rear_pos)

{
printf("%d ",cqueue_arr[front_pos]);

front_pos++;

printf("\n");

int main()

int choice,item;

do

printf("1.Insert\n");

printf("2.Delete\n");

printf("3.Display\n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1 :

printf("Input the element for insertion in queue : ");

scanf("%d", &item);

insert(item);

break;

case 2 :
deletion();

break;

case 3:

display();

break;

case 4:

break;

default:

printf("Wrong choicen");

}while(choice!=4);

return 0;

}
OUTPUT:

You might also like