0% found this document useful (0 votes)
41 views23 pages

#Include #Include

sjj

Uploaded by

kdaksh9528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views23 pages

#Include #Include

sjj

Uploaded by

kdaksh9528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Develop a C program to find an array's maximum and minimum elements

#include <stdio.h>
#include <stdio.h>
int main()
{
int a[100];
int i, max, min, n;
printf("Enter the number of elements in the array :");
scanf("%d", &n);
printf("Enter %d elements in the array :\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
max = a[0];
min = a[0];
// Traverse the array to find the maximum and minimum elements
for (i = 1; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
// Print the maximum and minimum elements
printf("Maximum element is : %d\n", max);
printf("Minimum element is : %d\n\n", min);
return 0;
}
Develop a C program to demonstrate stack operations using an 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("\n Enter the size of STACK[MAX=100]:");
scanf("%d", &n);
printf("\n 1.PUSH\t 2.POP\t 3.DISPLAY\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 END ");
break;
}
default:
{
printf("\n Enter a Valid Choice(1/2/3/4)");
}
}
} while (choice != 4);
return 0;
}
void push()
{
if (top >= n - 1)
{
printf("\n\tSTACK is over flow");
}
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 Empty");
}
else
{
printf("\n\t The popped elements is %d", stack[top]);
top--;
}
}
void display()
{
if (top >= 0)
{
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
}
else
{
printf("\n The STACK is empty");
}
}

Develop C programs to demonstrate the Queue operations using arrays.


#include <stdio.h>
#include <stdlib.h>
#define n 4
int main()
{
int queue[n], ch = 1, front = 0, rear = 0, i, j = 1, x = n;
// printf("Queue using Array");
printf("\n1.Insertion \t2.Deletion \t3.Display \t4.Exit");
while (ch)
{
printf("\nEnter the Choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
if (rear == x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:", j++);
scanf("%d", &queue[rear++]);
}
break;
case 2:
if (front == rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d", queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if (front == rear)
printf("\n Queue is Empty");
else
{
for (i = front; i < rear; i++)
{
printf("%d", queue[i]);
printf("\n");
}
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
return 0;
}

Develop C programs to demonstrate the following operations on a linked list:


#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct node
{
int info;
struct node *link;
};
struct node *start = NULL;
// Function to create list with n nodes initially
void createList()
{
int n;
int data;
struct node *newnode;
struct node *temp;
if (start == NULL)
{
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
newnode = malloc(sizeof(struct node));
start = newnode;
temp = start;
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
start->info = data;
}
for (int i = 2; i <= n; i++)
{
newnode = malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
}
}
// Function to traverse the linked list
void traverse()
{
struct node *temp;
// List is empty
if (start == NULL)
printf("\nList is empty");
// Else print the List
else
{
temp = start;
while (temp != NULL)
{
printf("Data = %d\n", temp->info);
temp = temp->link;
}
}
}
// Function to insert at the front of the linked list
void insertAtFront()
{
int data;
struct node *temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
temp->info = data;
// Pointer of temp will be assigned to start
temp->link = start;
start = temp;
}
// Function to insert at the end of the linked list
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));
// Enter the number
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
// Changes links
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
}
// Function to delete from the front of the linked list
void deleteFirst()
{
struct node *temp;
if (start == NULL)
printf("\nList is empty");
else
{
temp = start;
start = start->link;
free(temp);
}
}
// Function to delete from the end of the linked list
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty");
else
{
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}
int main()
{
createList();
int choice;
while (1)
{
printf("\n1 To see list");
printf("\n2 For insertion at starting");
printf("\n3 For insertion at end");
printf("\n4 For deletion of first element");
printf("\n5 For deletion of last element");
printf("\n6 To end");
printf("\nEnter Choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
deleteFirst();
break;
case 5:
deleteEnd();
break;
case 6:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}

Develop a binary tree and perform tree traversal


#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *left;
struct node *right;
};
// Create a new Node
struct node *createNode(int value)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node *insertLeft(struct node *root, int value)
{
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node *insertRight(struct node *root, int value)
{
root->right = createNode(value);
return root->right;
}
// Inorder traversal
void inorderTraversal(struct node *root)
{
if (root == NULL)
return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node *root)
{
if (root == NULL)
return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node *root)
{
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
int main()
{
struct node *root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
insertRight(root->left, 5);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
printf("\nInorder traversal \n");
inorderTraversal(root);
return 0;
}

Develop C programs to demonstrate Quick Sort


#include <stdio.h>
// Function to swap two elements
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Partition function
int partition(int arr[], int low, int high)
{
int pivot = arr[low];
int i = low;
int j = high;
while (i < j)
{
// condition 1: find the first element greater than
// the pivot (from starting)
while (arr[i] <= pivot && i <= high - 1)
{
i++;
}
// condition 2: find the first element smaller than
// the pivot (from last)
while (arr[j] > pivot && j >= low + 1)
{
j--;
}
if (i < j)
{
swap(&arr[i], &arr[j]);
}
}
swap(&arr[low], &arr[j]);
return j;
}
// QuickSort function
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
// call Partition function to find Partition Index
int partitionIndex = partition(arr, low, high);
// Recursively call quickSort()
quickSort(arr, low, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, high);
}
}
int main()
{
int arr[20], n;
printf("Enter the size of an array: ");
scanf("%d", &n);
printf("\nEnter %d elements: ", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// calling quickSort() to sort the given array
quickSort(arr, 0, n - 1);
// printing the sorted array
printf("\nSorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

Develop a C program to implement the Linear Search


#include <stdio.h>
int main()
{
int a[100], key, i, n, flag = 0;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Enter a number to search\n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (a[i] == key) /* If required element is found */
{
flag = 1;
printf("%d is present in an array", key);
break;
}
}
if (flag == 0)
printf("%d is not present in the array.\n", key);
return 0;
}

Develop a C program to implement the binary Search


#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, list[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &list[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last)
{
if (list[middle] < search)
first = middle + 1;
else if (list[middle] == search)
{
printf("%d found at location %d.\n", search, middle + 1);
break;
}
else
last = middle - 1;
middle = (first + last) / 2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}

Develop a C program on a graph to perform the Depth First Search (DFS) and the Breadth
First Search (BFS).
BFS

#include <stdio.h>
int top = -1, q[20], arr[20][20], visited[20] = {0};
int front = -1, rear = -1;
void bfs(int s, int n);
int main()
{
int i, j, n, ch, s;
printf("Enter the Number of Vertices");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
printf("Enter 1 if %d has a node with %d else 0:", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("Enter starting vertex");
scanf("%d", &s);
bfs(s, n);
return 0;
}
void add(int item)
{
if (rear == 19)
printf("QUEUE FULL");
else
{
if (rear == -1)
{
q[++rear] = item;
front++;
}
else
q[++rear] = item;
}
}
int delete()
{
int k;
if ((front > rear) || (front == -1))
return (0);
else
{
k = q[front++];
return (k);
}
}
void bfs(int s, int n)
{
int i, p;
add(s);
visited[s] = 1;
p = delete ();
if (p != 0)
printf("%d", p);
while (p != 0)
{
for (i = 1; i <= n; i++)
{
if ((arr[p][i] != 0) && (visited[i] == 0))
{
add(i);
visited[i] = 1;
}
}
p = delete ();
if (p != 0)
printf(" %d ", p);
}
for (i = 1; i <= n; i++)
{
if (visited[i] == 0)
bfs(i, n);
}
}

DFS

#include <stdio.h>
int top = -1, stack[20], arr[20][20], visited[20] = {0};
void dfs(int s, int n);
int main()
{
int i, j, n, s;
printf("Enter the Number of Vertices");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
printf("Enter 1 if %d has a node with %d else 0:", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("Enter starting vertex");
scanf("%d", &s);
dfs(s, n);
return 0;
}
void push(int item)
{
if (top == 19)
printf("Stack overflow ");
else
stack[++top] = item;
}
int pop()
{
int k;
if (top == -1)
return (0);
else
{
k = stack[top--];
return (k);
}
}
void dfs(int s, int n)
{
int k, i;
push(s);
visited[s] = 1;
k = pop();
if (k != 0)
printf(" %d ", k);
while (k != 0)
{
for (i = 1; i <= n; i++)
{
if ((arr[k][i] != 0) && (visited[i] == 0))
{
push(i);
visited[i] = 1;
}
k = pop();
if (k != 0)
printf(" %d \n", k);
}
}
for (i = 1; i <= n; i++)
{
if (visited[i] == 0)
dfs(i, n);
}
}

Develop a C program to find the shortest path in a graph using Floyd Warshall’s
Algorithm.
#include <stdio.h>
// defining the number of vertices
#define nV 4
#define INF 999
void printGraph(int matrix[][nV]);
// Implementing floyd warshall algorithm
void floydWarshall(int graph[][nV])
{
int matrix[nV][nV], i, j, k;
for (i = 0; i < nV; i++)
for (j = 0; j < nV; j++)
matrix[i][j] = graph[i][j];
// Adding vertices individually
for (k = 0; k < nV; k++)
{
for (i = 0; i < nV; i++)
{
for (j = 0; j < nV; j++)
{
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
printGraph(matrix);
}
void printGraph(int matrix[][nV])
{
printf("\n Final Shortest Path Graph Matrix:\n");
for (int i = 0; i < nV; i++)
{
for (int j = 0; j < nV; j++)
{
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
floydWarshall(graph);
}

You might also like