0% found this document useful (0 votes)
6 views44 pages

Umang 13

This document is a practical file on data structures using C, submitted by Umang Sharma for the session 2024-2026. It includes various programs such as linear and binary search (both recursive and non-recursive), linked list implementation, and operations for list ADT, stack, and queue using linked lists. Each program is accompanied by code snippets and explanations for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views44 pages

Umang 13

This document is a practical file on data structures using C, submitted by Umang Sharma for the session 2024-2026. It includes various programs such as linear and binary search (both recursive and non-recursive), linked list implementation, and operations for list ADT, stack, and queue using linked lists. Each program is accompanied by code snippets and explanations for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

A

PRACTICAL FILE
ON
DATA STRUCTURE USING ‘C’

( Session : 2024-2026 )
Submitted To: Submitted By:
Dr. Sandeep Umang Sharma
(Asst. Professor) 240000613013
Dept. of ICT MSc (C.S) 1st sem

Department of Information and Communication


Technology
Chaudhary Bansi Lal University, Bhiwani (Haryana)
Sr. No Program Page No.
1 WAP for recursive and non- 1-2
recursive Linear Search
2 WAP for recursive and non 3-5
recursive Binary search
3 WAP to implement searching 6-8
and sorting.
4 WAP for 9-11
creating(implementing) linked
list
5 WAP to implement list ADT to 11-17
perform following operations
a) Insert an element into a list.
b) Delete an element from list
c) Search for a key element in
list d) count number of nodes
in list
6 WAP for implementing Stack 18-22
in linked list.
7 Program to implement queue 23-28
using linked list.
8 WAP for Merge Sort 29-33
9 Program for circular queue 34-38
using array.
10 WAP to insert at any node in 39-42
single linked list.
1. WAP for recursive and non-recursive Linear Search
#include<stdio.h>
#include<conio.h>
int Lsearch(int[],int,int);
int rlsearch(int[],int,int);
int main(){
clrscr();
int arr[50],item,N,index,i,ch;
prin ("\nEnter the desired array size(max 50)=");
scanf("%d",&N);
prin ("\nEnter Arrray elements:");
for(i=0;i<N;i++)
scanf("%d",&arr[i]);

prin ("\nEnter the Element to be searched=");


scanf("%d",&item);

prin ("\nChoose how you want to be search:\n1.Linear


Search\n2.Recursive Linear Search\n");
scanf("%d",&ch);
switch(ch){
case 1: index=Lsearch(arr,N,item);
break;
case 2: index=rlsearch(arr,N,item);
break;
default:prin ("\nWrong choice");
}
1
if(index==-1)
prin ("\nSorry, Element not found");
else {
prin ("\nElement found at index %d ",index);
}

getch();
return 0;
}
//func on for non-recursive linear search
int Lsearch(int arr[],int size ,int item){
for(int i=0;i<size;i++)
{if(arr[i]==item)
return i+1;
}
return -1;
}
//func on for recursive linear search
int rlsearch(int arr[],int size,int item){
if(size==0) return -1;
else if(arr[size-1]==item)
return size;
return rlsearch(arr,size-1,item);
}
Output:-

2
2. WAP recursive and non recursive Binary search
#include<stdio.h>
#include<conio.h>
int bsearch(int[],int,int);
int rbsearch(int[],int,int,int);

int main(){
int arr[50],item,N,index,i,ch;
prin ("\nEnter the desired array size(max 50)=");
scanf("%d",&N);
prin ("\nEnter Arrray elements:");
for(i=0;i<N;i++)
scanf("%d",&arr[i]);

prin ("\nEnter the Element to be searched=");


scanf("%d",&item);

prin ("\nChoose how you want to be search:\n1.Binary


Search\n2.Recursive Binary Search\n");
scanf("%d",&ch);
switch(ch){
case 1: index=bsearch(arr,N,item);
break;
case 2: index=rbsearch(arr,0,N,item);
break;
default:prin ("\nWrong choice");
}
if(index==-1)
prin ("\nSorry, Element not found");
3
else {
prin ("\nElement found at index %d ",index);
}
getch();
return 0;
}

//func on for non-recursive binary search


int bsearch(int arr[],int size ,int item){
int beg,last,mid;
beg=0; last=size;
while(beg<=last){
mid=(beg+last)/2;
if(item==arr[mid]) return mid+1;
else if(item>arr[mid]) beg=mid+1;
else last=mid-1;
}
return -1;
}
//func on for recursive binary search
int rbsearch(int arr[],int beg,int size,int item){
int mid=(beg+size)/2;
if(size>=beg){
if(item==arr[mid]) return mid+1;
else if(item>arr[mid]) return
rbsearch(arr,mid+1,size,item);
else return rbsearch(arr,beg,mid-1,item);
}
4
return -1;
}

Output:-

5
3. WAP to implement searching and sor ng.
#include<stdio.h>
#include<conio.h>
int bsearch(int[],int,int);
int sort(int *,int);

int main(){
int arr[50],item,N,i,ch;
prin ("\nEnter the desired array size(max 50)=");
scanf("%d",&N);
prin ("\nEnter Arrray elements:");
for(i=0;i<N;i++)
scanf("%d",&arr[i]);

prin ("\nWhat you want?\n 1.Sor ng\n2. Searching\n");


scanf("%d",&ch);
switch(ch){
case 1: sort(arr,N);
break;
case 2:prin ("\nEnter the item to be searched=");
scanf("%d",&item);
sort(arr,N);
bsearch(arr,N,item);
break;
default:prin ("\nWrong choice");
}
getch();
return 0;
}
6
//func on for non-recursive binary search
int bsearch(int arr[],int size ,int item){
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last){
mid=(beg+last)/2;
if(item==arr[mid]) {prin ("\nFound at index
%d",mid+1); return 0;}
else if(item>arr[mid]) beg=mid+1;
else last=mid-1;
}
prin ("\nNOT FOUND"); //only in case not found
return -1;
}
//applying bubble sort
int sort(int *arr,int size){
int temp;
for(int i=0;i<size;i++)
{ for(int j=0;j<(size-1)-i;j++)
{
if(arr[j]>arr[j+1]) {
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}

7
prin ("\nSorted array= ");
for(int i=0;i<size;i++)
prin ("%d ",arr[i]);
return 0;
}

Output:-

8
4. WAP for crea ng(implemen ng) linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{ int num; //Data of the node
struct node *next; //Address of the next node
}*start ,*newptr ,*save , *ptr,*rear;

struct node * create_new_node(int);


void insert(struct node*);
void display(struct node*);
int main(){
start=rear=NULL;
int num;
char ch='y';
while(ch=='y'||ch=='Y')
{
prin ("\nEnter the data in node=");
scanf("%d",&num);
//crea ng new node
newptr=create_new_node(num);
if(newptr != NULL) {
prin ("\nData Entered successfully");
}
else {
prin ("\nCannot create new
node||||ABORTING|");
return 0;
}
9
prin ("\nInser ng new data at end=");
insert(newptr);
prin ("\nNow the list is:\n");
display(start);
prin ("\nPress Y to enter new nodes and N for
exit....\n");
scanf("%s",&ch);

}
return 0;
}

struct node *create_new_node(int num){


ptr=(struct node *)malloc(sizeof(struct node));
ptr->num=num;
ptr->next=NULL;
return ptr;
}
void insert(struct node*np)
{ if(start==NULL)
start=rear=np;
else
{rear->next=np;
rear=np; }
}
void display(struct node* np){
while(np!=NULL)
{prin ("%d ->",np->num);

10
np=np->next;
}
prin ("!!!\n");
}
Output:-

11
5. WAP to implement list ADT to perform following
opera ons a) Insert an element into a list. b) Delete
an element from list c) Search for a key element in
list d) count number of nodes in list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int num; //Data of the node
struct node *next; //Address of the next node
}*start ,*newptr ,*save , *ptr,*rear;

struct node * create_new_node(int);


void insert_end(struct node*);
void insert_beg(struct node*);
void display(struct node*);
void delete(int);
void search(int);
int main(){
start=rear=NULL;
int num,ch;
while(1){
prin ("\n1.Inser on at beginning\n2.Inser n at
end\n3.Dele on\n4.Searching\n Press 9 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: prin ("\nEnter the data in node=");
12
scanf("%d",&num);
//crea ng new node
newptr=create_new_node(num);
if(newptr==NULL){
prin ("\nCannot create new
node||||ABORTING|");
return 0;
}
prin ("\nInser ng new data at start=");
insert_beg(newptr);
prin ("\nNow the list is:\n");
display(start);
break;
case 2: prin ("\nEnter the data in node=");
scanf("%d",&num);
//crea ng new node
newptr=create_new_node(num);
if(newptr==NULL){
prin ("\nCannot create new
node||||ABORTING|");
return 0;
}
prin ("\nInser ng new data at end=");
insert_end(newptr);
prin ("\nNow the list is:\n");
display(start);
break;
case 3: prin ("\nEnter item to be deleted=");
scanf("%d",&num);

13
delete(num);
display(start);
break;
case 4: prin ("\nEnter item to be search=");
scanf("%d",&num);
search(num);
break;
case 9: return 0;
default:prin ("\nWrong choice");
}
}
return 0;
}
struct node *create_new_node(int num){
ptr=(struct node *)malloc(sizeof(struct node));
ptr->num=num;
ptr->next=NULL;
return ptr;
}

void insert_end(struct node*np)


{
if(start==NULL)
start=rear=np;
else
{rear->next=np;
rear=np;
}

14
}
void insert_beg(struct node* np)
{
if(start==NULL)
start=rear=np;
else {
save=start;
start=np;
np->next=save;
}
}
void display(struct node* np){
while(np!=NULL)
{prin ("%d ->",np->num);
np=np->next;
}
prin ("!!!\n");
}

void delete(int value)


{
struct node *myNode=start, *previous=NULL;
int flag = 0;

while(myNode!=NULL)
{
if(myNode->num==value)
{
if(previous==NULL)

15
start = myNode->next;
else
previous->next = myNode->next;

prin ("%d is deleted from list\n", value);

flag = 1;
free(myNode); //need to free up the memory to
prevent memory leak
break;
}

previous = myNode;
myNode = myNode->next;
}
}
void search(int value)
{
struct node *searchNode =start;
int flag = 0,ctr=1;

while(searchNode!=NULL)
{
if(searchNode->num==value)
{
prin ("%d is present in this list at %d posi on\n",
value, ctr);
flag = 1;
break;

16
}
else{
searchNode = searchNode->next;
ctr++;}
}
if(flag==0)
prin ("Item not found\n");
}
Output:-

17
6. WAP for implemen ng Stack in linked list.
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
prin ("\n*********Stack opera ons using linked
list*********\n");
prin ("\n----------------------------------------------\n");
while(choice != 4)
{
prin ("\n\nChose one from the below op ons...\n");
prin ("\n1.Push\n2.Pop\n3.Show\n4.Exit");
prin ("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
18
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
prin ("Exi ng....");
break;
}
default:
{
prin ("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;

19
struct node *ptr = (struct node*)malloc(sizeof(struct
node));
if(ptr == NULL)
{
prin ("not able to push the element");
}
else
{
prin ("Enter the value=");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
prin ("Item pushed");

}
}

void pop()

20
{
int item;
struct node *ptr;
if (head == NULL)
{
prin ("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
prin ("Item popped");

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
prin ("Stack is empty\n");
}
else
{ prin ("Prin ng Stack elements \n");
while(ptr!=NULL)

21
{ prin ("%d\n",ptr->val);
ptr = ptr->next;
} }}

Output:-

22
7. Program to implement queue using linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main(){
int no, ch, e;

printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
23
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is
empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:

24
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create an empty queue */


void create()
{
front = rear = NULL;
}
/* Returns queue size */
void queuesize()
{
printf("\n Queue size : %d", count);
}
/* Enqueing the queue */
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}

25
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;

rear = temp;
}
count++;
}
/* Displaying the queue elements */
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
/* Dequeing the queue */

26
void deq(){
front1 = front;

if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty
queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;}

/* Returns the front element of queue */


int frontelement()
{
if ((front != NULL) && (rear != NULL))

27
return(front->info);
else
return 0;
}

/* Display if queue is empty or not */


void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
Output:-

28
8. WAP for Merge Sort
#include <stdio.h>
#include <stdlib.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

/* Merge the temp arrays back into arr[l..r]*/


29
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
30
/* Copy the remaining elements of R[], if there
are any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

/* l is for left index and r is right index of the


sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l + (r - l) / 2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
31
merge(arr, l, m, r);
}
}

/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");


32
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
return 0;
}

Output :-

33
9. Program for circular queue using array
#include <stdio.h>
// Define the maximum size of the queue
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1, rear = -1;
// Function to check if the queue is full
int isFull()
{
// If the next position is the front, the queue is full
return (rear + 1) % MAX_SIZE == front;
}
int isEmpty()
{
// If the front hasn't been set, the queue is empty
return front == -1;
}
// Function to enqueue (insert) an element
void enqueue(int data)
{
// If the queue is full, print an error message and
// return
34
if (isFull()) {
printf("Queue overflow\n");
return;
}
// If the queue is empty, set the front to the first
// position
if (front == -1) {
front = 0;
}
// Add the data to the queue and move the rear pointer
rear = (rear + 1) % MAX_SIZE;
queue[rear] = data;
printf("Element %d inserted\n", data);
}
// Function to dequeue (remove) an element
int dequeue()
{
if (isEmpty()) {
printf("Queue underflow\n");
return -1;
}
int data = queue[front];
35
// If the front and rear pointers are at the same
// position, reset them
if (front == rear) {
front = rear = -1;
}
else {
// Otherwise, move the front pointer to the next
// position
front = (front + 1) % MAX_SIZE;
}
// Return the dequeued data
return data;
}
void display()
{
// If the queue is empty, print a message and return
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
// Print the elements in the queue
printf("Queue elements: ");
36
int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
}
printf("%d\n", queue[rear]);
}
int main()
{ int ch,num;
// Enqueue some elements
while(1){
printf("1.Enter element in queue\n2.Delete \n3.Show
Queue\n4.Quit\n");
scanf("%d",&ch);
switch(ch){
case 1:printf("\nEnter item=");
scanf("%d",&num);
enqueue(num);
break;
case 2:dequeue();
break;
case 3:display();

37
break;
case 4:return 0;
default:printf("\nWrong choice made.!!\n");
}}
// End of main function
return 0;
}
Output:-

38
10. WAP to insert at any node in single linked list.
#include <stdio.h>
#include <stdlib.h>
struct node *head=NULL;
struct node
{
int data;
struct node *next;
};
void ins(int data)
{
struct node *temp = (struct
node*)malloc(sizeof(struct node)); //it will insert a new
node to the start of the linked list
temp->data=data;
temp->next=head;
head=temp;
}
void ins_at_pos_n(int data,int position)
{
struct node *ptr = (struct node*)malloc(sizeof(struct
node));
ptr->data=data; //Creating a new node
int i;
39
struct node *temp=head;
if(position==1)
{
ptr->next=temp;
head=ptr;
return;
}
for(i=1;i<position-1;i++) //moving to the (n-1)th
position node in the linked list
{
temp=temp->next;
}
ptr->next=temp->next; //Make the newly created
node point to next node of ptr temp
temp->next=ptr; //Make ptr temp point to newly
created node in the linked list
}

void display()
{
struct node *temp=head;
printf("\nList: ");
while(temp!=NULL)
{
40
printf("\n%d ",temp->data);
temp=temp->next;
}
}
int main()
{
int i, n, pos, data;
printf("Enter the number of nodes: \n");
scanf("%d",&n);
printf("Enter the data for the nodes: \n");
for(i=0;i<n;i++)
{
scanf("%d",&data);
ins(data);
}
printf("Enter the data you want to insert in between the
nodes: \n");
scanf("%d",&data);
printf("Enter the position at which you want to insert the
nodes: \n");
scanf("%d",&pos);
if(pos>n)
{

41
printf("Enter a valid position: ");
}
else
{
ins_at_pos_n(data,pos);
}
display();
return 0;
}
Output:-

42

You might also like