Umang 13
Umang 13
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
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]);
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]);
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;
}
return 0;
}
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;
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;
}
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");
}
while(myNode!=NULL)
{
if(myNode->num==value)
{
if(previous==NULL)
15
start = myNode->next;
else
previous->next = myNode->next;
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;
}
}
}
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--;}
27
return(front->info);
else
return 0;
}
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;
/* 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]);
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