Programs to be practiced in the Lab

 Programs to be practiced in the Lab. These programs will help u in your theory and practical exams.

A) Write a C program to swap m and n element of single linked list.

#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start=NULL;
void creat(int);
void swap();
void disp();
void main()
{
int ch,i,n,m;
clrscr();
do
{
printf(“\n1.create”);
printf(“\n2.display”);
printf(“\n3.Swap”);
printf(“\n4.exit”);
printf(“\nenter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\nHow many nodes”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the data”);
scanf(“%d”,&m);
creat(m);
}
break;
case 2:
disp();
break;
case 4:
exit(0);
case 3:
swap();
break;
}
}
while(ch!=4);
getch();
}
void creat(int m)
{
struct list *tmp,*q;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
if(start==NULL)
{
printf(“list is empty”);
}
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void swap()
{
int m,n,i,tmp;
struct list *q,*ptr,*ptr1;
printf(“\nEnter the mth and nth position”);
scanf(“%d%d”,&m,&n);
for(i=1,ptr=start;i<m && ptr!=NULL;ptr=ptr->link,i++);
for(i=1,ptr1=start;i<n && ptr1!=NULL;ptr1=ptr1->link,i++);
if(ptr!=NULL && ptr1!=NULL)
{
tmp=ptr->data;
ptr->data=ptr1->data;
ptr1->data=tmp;
}
else
{
printf(“\nPosition Not Found”);
}
}

B) Write a ‘C’ program to read an adjacency matrix of a directed graph and traverse using BFS.

#include <stdio.h>

void main() {
// Initialize the adjacency matrix.
int adjacency_matrix[10][10] = {{0}};

// Read the adjacency matrix from the user.
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
scanf("%d", &adjacency_matrix[i][j]);
}
}

// Traverse the graph using BFS.
int visited[10] = {0};
int queue[10] = {0};
int front = 0;
int rear = 0;

queue[rear++] = 0;
visited[0] = 1;

while (front != rear) {
int current_node = queue[front++];

for (int i = 0; i < 10; i++) {
if (adjacency_matrix[current_node][i] == 1 && visited[i] == 0) {
queue[rear++] = i;
visited[i] = 1;
}
}
}

// Print the visited nodes.
for (int i = 0; i < 10; i++) {
if (visited[i] == 1) {
printf("%d ", i);
}
}

printf("\n");
}

C)

Write a C program to count all non zero element,Odd number and even number in a single linked list.

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void main()
{
int ch,n,i,m,a,pos;
clrscr();
start=NULL;
do
{
printf(“\n\nMENU\n\n”);
printf(“\n1.CREATE\n”);
printf(“\n2.DISPLAY\n”);
printf(“\n3.COUNT\n”);
printf(“\n4.EXIT\n”);
printf(“\nENTER UR CHOICE\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nHOW MANY NODES U WANT TO CREATE\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nENTER THE DATA”);
scanf(“%d”,&m);
create(m);
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void count()
{
struct node *q;
int nonz=0,eno=0,ono=0;
q=start;
while(q!=NULL)
{
if(q->data>0)
{
nonz++;
}
if(q->data%2==0)
{
eno++;
}
else
{
ono++;
}

q=q->link;
}
printf(“\n\nPOSITVE NO ARE %d EVEN NO ARE %d ODD NO ARE %d”,nonz,eno,ono);
}

void create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void disp()
{
struct node *q;
if(start==NULL)
{
printf(“\n\nLIST IS EMPTY”);
}
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
}


D) Implementation of Queue using Array in Data Structure

 

#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");

    }

}


E) Write a program to implement Merge Sort

 

// Data Structure program 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]

    // Initial index of first subarray

    i = 0;

  

    // Initial index of second subarray

    j = 0;

  

    // Initial index of merged subarray

    k = l;

    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++;

    }

  

    // 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);

  

        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");

    printArray(arr, arr_size);

  

    mergeSort(arr, 0, arr_size - 1);

  

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

    printArray(arr, arr_size);

    return 0;

}

 


Comments

Popular posts from this blog

Classical Problems of Synchronization in OS

Semester IV : Assignment #1 OPERATING SYSTEM