Ds File
Ds File
#include <stdio.h>
int main() {
int n, j, i, swap;
int array[n];
return 0;
}
Output:
Enter number of elements
4
Enter 4 integers
2
1
4
9
Sorted list in ascending order:
1
2
4
9
#include <stdio.h>
int main()
{
int array[10], i, size;
printf("Enter Number of Elements: ");
scanf("%d", &size);
printf("\nEnter %d numbers\t", size);
printf("\n");
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
selectionSort(array, size);
printf("\nSorted array (Using Selection Sort) ");
for (i = 0; i < size; i++)
printf(" %d ", array[i]);
return 0;
}
Output:
Enter Number of Elements: 4
Enter 4 numbers
2
1
9
4
Sorted array (Using Selection Sort) 1 2 4 9
#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:
Enter number of elements
4
Enter 4 integers
9
2
4
5
Sorted list in ascending order:
2
4
5
9
#include <stdio.h>
int main()
{
int a[20], i, x, n;
printf("How many elements?");
scanf("%d", &n);
if (i < n)
printf("Element found at index %d", i);
else
printf("Element not found");
return 0;
}
Output:
How many elements?8
Enter array elements:
3
2
78
67
12
21
87
90
Enter element to search:12
Element found at index 4
#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:
Enter number of elements: 5
Enter 5 integers: 2 1 3 4 5
Enter value to find: 5
5 found at location 5.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list\n");
traverseList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for (i = 2; i <= n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if (newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
Output:
Enter the total number of nodes: 5
Enter the data of node 1: 23
Enter the data of node 2: 32
Enter the data of node 3: 45
Enter the data of node 4: 67
Enter the data of node 5: 89
Data in the list
Data = 23
Data = 32
Data = 45
Data = 67
Data = 89
#include <stdio.h>
int main() {
int array[100];
int i, n, x, pos;
n = n + 1;
array[pos - 1] = x;
return 0;
}
Output:
Enter the number of elements in the array
3
Enter the elements
1
4
5
Input array elements are:
145
Enter the new element to be inserted: 78
Enter the position where element is to be inserted: 3
Array elements after insertion are:
1 4 78 5
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, n, index, arr[10];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i < n; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
if (index >= n + 1)
{
printf(" \n Deletion is not possible in the array.");
}
else
{
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
printf("The array after deleting the element is: ");
for (i = 0; i < n - 1; i++)
printf("%d ", arr[i]);
return 0;
}
}
Output:
Enter the size of the array: 4
Enter the elements of the array:
arr[0] = 3
arr[1] = 4
arr[2] = 2
arr[3] = 9
Enter the index of the element to be deleted: 2
The array after deleting the element is: 3 4 9
#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\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.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(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;
Roll No: 489344334984390 mdnjkfd
}
}
void pop()
{
if (top <= -1)
{
printf("\n\t Stack is under flow");
}
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]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
Output (Example):
Enter the size of STACK[MAX=100]: 5
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice: 1
Enter a value to be pushed: 12
Enter the Choice: 1
Enter a value to be pushed: 24
Enter the Choice: 1
Enter a value to be pushed: 98
Enter the Choice: 3
The elements in STACK
98
24
12
Press Next Choice
Enter the Choice: 2
The popped elements is 98
Enter the Choice: 3
The elements in STACK
24
12
Press Next Choice
Enter the Choice: 4
EXIT POINT
Roll No: 489344334984390 mdnjkfd
Experiment 10: Write a Program to implement stack using linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
if (top1 == NULL)
{
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
Output (Example):
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Display
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
No. of elements in stack : 3
Enter choice : 8
All stack elements destroyed
Enter choice : 4
Stack is empty
Enter choice : 5
#include <stdio.h>
Output:
Sorted array:
1 5 6 12 17 25
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
Output:
List before sorting
10 14 19 26 27 31 33 35 42 44 0
List after sorting
0 10 14 19 26 27 31 33 35 42 44
#include<stdio.h>
#include<stdlib.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.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;
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
void enqueue(int d)
{
struct node* new_n;
new_n = (struct node*)malloc(sizeof(struct node));
new_n->data = d;
new_n->next = NULL;
void display()
{
struct node* temp;
while (temp)
{
printf(" %d ", temp->data);
temp = temp->next;
}
}
}
void dequeue()
{
struct node *temp;
temp = front;
int main()
{
enqueue(5);
enqueue(10);
enqueue(15);
enqueue(20);
enqueue(25);
printf("Queue:");
display();
OUTPUT:
Queue: 5 10 15 20 25
Queue After Dequeue: 10 15 20 25
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}
int isEmpty() {
if (front == -1) return 1;
return 0;
}
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
OUTPUT:
Queue is empty !!
Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5
Queue is full!!
Front -> 0
Items -> 1 2 3 4 5
Rear -> 4
Deleted element -> 1
Front -> 1
Items -> 2 3 4 5
Rear -> 4
Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0
Queue is full!!
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void enqueue(int d)
{
struct node *n;
n = (struct node *)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if ((r == NULL) && (f == NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
}
void dequeue()
{
struct node *t;
t = f;
if ((f == NULL) && (r == NULL))
printf("\nQueue is Empty");
else if (f == r)
{
f = r = NULL;
free(t);
}
else
{
f = f->next;
r->next = f;
free(t);
}
}
void display()
{
struct node *t;
t = f;
if ((f == NULL) && (r == NULL))
printf("\nQueue is Empty");
else
{
int main()
{
enqueue(34);
enqueue(22);
enqueue(75);
enqueue(99);
enqueue(27);
dequeue();
return 0;
}
OUTPUT:
Circular Queue: 34 22 75 99 27
Circular Queue After dequeue: 22 75 99 27
#include <stdio.h>
#include <stdlib.h>
return root;
}
return 0;
}
OUTPUT:
In-order traversal of the binary tree: 20 30 40 50 60 70 80
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
int main() {
struct Node *root = NULL;
insert(&root, 5);
insert(&root, 3);
insert(&root, 7);
insert(&root, 2);
insert(&root, 4);
insert(&root, 6);
insert(&root, 8);
return 0;
}
OUTPUT:
Inorder traversal: 2 3 4 5 6 7 8
Preorder traversal: 5 3 2 4 7 6 8
Postorder traversal: 2 4 3 6 8 7 5
#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && a[left] > a[largest])
largest = left;
if (right < n && a[right] > a[largest])
largest = right;
if (largest != i) {
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
for (int i = n - 1; i >= 0; i--) {
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, i, 0);
}
}
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
OUTPUT:
Before sorting array elements are -
48 10 23 43 28 26 1
After sorting array elements are -
1 10 23 26 28 43 48
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
return 0;
}
OUTPUT:
Inorder traversal of the BST: 20 30 40 50 60 70 80
Preorder traversal of the BST: 50 30 20 40 70 60 80
Postorder traversal of the BST: 20 40 30 60 80 70 50
#include <stdio.h>
#include <stdlib.h>
// If including this edge doesn't cause a cycle, include it in the result and increment the index
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
free(subsets);
}
int main() {
int V = 4; // Number of vertices
int E = 5; // Number of edges
struct Graph* graph = createGraph(V, E);
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 6;
graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 5;
graph->edge[4].src = 2;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;
KruskalMST(graph);
return 0;
}
OUTPUT:
Edges in MST:
2 - 3 Weight: 4
0 - 3 Weight: 5
0 - 1 Weight: 10
#include <stdio.h>
#include <limits.h>
int visited[V]; // visited[i] will be true if vertex i is included in the shortest path tree or the shortest
// distance from src to i is finalized
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
visited[i] = 0;
}
dist[src] = 0; // Distance of source vertex from itself is always 0
printSolution(dist);
}
OUTPUT:
Vertex Distance from Source
00
14
2 12
3 19
4 21
5 11
69
78
8 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int k = 0;
int k = 0;
for (int i = strlen(infix) - 1; i >= 0; i--) {
if (isalnum(infix[i])) {
prefix[k++] = infix[i];
} else if (infix[i] == ')') {
push(stack, infix[i]);
int main() {
char infix[MAX_SIZE];
char prefix[MAX_SIZE];
char postfix[MAX_SIZE];
strcpy(infix, "a+b*(c^d-e)^(f+g*h)-i");
infixToPrefix(infix, prefix);
printf("Prefix expression: %s\n", prefix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
OUTPUT:
Prefix expression: +a-*b^-^cde+f*ghi
Postfix expression: abcd^e-fgh*+^*+i-