Dsa Lab
Dsa Lab
Dsa Lab
#include<stdio.h>
void main()
int arr[100],limit,i,search,flag = 0;
clrscr();
scanf("%d",&limit);
for(i=0;i<limit;i++){
scanf("&d",&arr[i]);
scanf("%d",&search);
for(i=0;i<limit;i++){
if(arr[i] == search){
flag = 1;
break;
if(flag == 1)
else
getch()
}
BINARY SEARCH
#include<stdio.h>
void main(){
int arr[100],limit,i,first,last,middle,search;
clrscr();
scanf(“%d”,&limit);
for(i=0;i<limit;i++){
scanf(“%d”,&arr [i]); }
scanf(“%d”,search);
first = 0;
last = limit – 1;
middle =(first+last)/2;
break; }else {
last =n middle – 1; }
}}
getch() }
POLYNOMIAL ADDITION
#include<stdio.h>
#include<conio.h>
int main() {
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &poly1[i]);
scanf("%d", &poly2[i]);
if(x > y)
large = x;
else
large = y;
if(i != 0)
printf("+");
getch();
return 0;
}
*QUICKSORT*
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int a[], int start, int end, int pivot) {
while (start <= end) {
while (a[start] < pivot)
start++;
while (a[end] > pivot)
end--;
if (start <= end) {
swap(&a[start], &a[end]);
start++;
end--;
}
}
return start;
}
void quicksort(int a[], int start, int end) {
if (start >= end)
return;
int pivot = a[(start + end) / 2];
int index = partition(a, start, end, pivot);
quicksort(a, start, index - 1);
quicksort(a, index, end);
}
int main() {
int a[100], n, i;
printf("Enter the limit of array: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
quicksort(a, 0, n - 1);
printf("Sorted array:\n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
*MERGESORT*
#include <stdio.h>
void merge(int a[], int temp[], int start, int middle, int end) {
int left = start;
int right = middle + 1;
int index = start;
while (left <= middle && right <= end) {
if (a[left] <= a[right]) {
temp[index] = a[left];
left++;
index++;
} else {
temp[index] = a[right];
right++;
index++;
}
}
while (left <= middle) {
temp[index] = a[left];
index++;
left++;
}
while (right <= end) {
temp[index] = a[right];
index++;
right++;
}
for (index = start; index <= end; index++) {
a[index] = temp[index];
}
}
void sort(int a[], int temp[], int start, int end) {
if (start >= end)
return;
int middle = (start + end) / 2;
sort(a, temp, start, middle);
sort(a, temp, middle + 1, end);
merge(a, temp, start, middle, end);
}
int main() {
int a[10], temp[10], n, i;
printf("Enter limit of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, temp, 0, n - 1);
printf("Sorted array:\n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
*Stack using array*
#include<stdio.h>
int stack[10], top = -1, i, n;
void push(int);
void pop();
void traversal();
int main() {
int exit = 0, d;
printf("Enter the limit of the stack: ");
scanf("%d", &d);
while (!exit) {
printf("\nEnter the operation to perform:");
printf("\n1. PUSH");
printf("\n2. POP");
printf("\n3. TRAVERSAL");
printf("\n4. EXIT");
printf("\nYour choice: ");
scanf("%d", &n);
switch (n) {
case 1:
push(d);
break;
case 2:
pop();
break;
case 3:
traversal();
break;
case 4:
exit = 1;
break;
default:
printf("\nInvalid input. Please try again.");
}
}
return 0;
}
void push(int d) {
if (top == d - 1) {
printf("\nCan't add element. Stack is full.\n");
} else {
int add;
printf("\nEnter the element to add: ");
scanf("%d", &add);
top++;
stack[top] = add;
printf("\n%d added to the stack.\n", add);
}
}
void pop() {
if (top == -1) {
printf("\nStack is empty. Nothing to pop.\n");
} else {
printf("\n%d removed from the stack.\n", stack[top]);
top--;
}
}
void traversal() {
if (top == -1) {
printf("\nStack is empty.\n");
} else {
printf("\nStack elements are:\n");
for (i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
*QUEUE USING ARRAY*
#include<stdio.h>
int queue[10],i,n,rear=-1,front=-1;
void enqueue();
void dequeue();
void display();
int main()
{
int a,exit=0;
printf("enter the limit of the queue:");
scanf("%d",&n);
while(!exit)
{
printf("\n enter the operation: enqueue (1), dequeue (2),display(3),exit(4):");
scanf("%d",&a);
switch(a)
{
case 1:
enqueue ();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit=1;
break;
default:
printf("invalid input");
}
}
return 0;;
}
void enqueue()
{
int item;
if(rear==n-1)
{
printf ("\n Queue overflow\n");
}
else
{
if(front==-1)
{
front=0;
}
printf("enter the element to insert:");
scanf ("%d",&item);
rear++;
queue [rear]=item;
}
}
void dequeue()
{ if((front==-1)||(front>rear))
{
printf("\n queue underflow\n");
}
else
{
printf("\n deleted element is %d", queue[front]);
front++;
}
}
void display ()
{
if(front==-1)
{
printf("\n queue is empty \n");
}
else
{
for(i=front; i<=rear; i++)
{
printf ("%d",queue[i]);
}
printf("\n");
}
}
insertion sort
#include<stdio.h>
voidmain() {
int arr[100],limit,key,i,j;
clrscr();
scanf(“%d”,&limit);
for(i=0;i<limit;i++) {
scanf(“%d”,arr[i]); }
for(i=0;i<limit;i++){
key = arr[i];
j =i-1;
j-- ; }
printf(“sorted array”);
for( i=0;i<limit;i++) {
printf(“\n%d”,arr[i]) ;
getch():
}
INSERTION AT BEGINNING AND DISPLAY
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next; // Change to struct node
} Node;
Node *head = NULL;
Node *tail = NULL;
int getChoice(char *[], int);
void insertion();
void display();
int main() {
int isExit = 0;
while (!isExit) {
char *choices[] = {"Insertion at beginning", "Display", "Exit"};
int choice = getChoice(choices, 3);
switch (choice) {
case 1:
insertion();
break;
case 2:
display();
break;
case 3:
isExit = 1;
break;
default:
printf("Invalid option.\n");
}
}
return 0;
}
void insertion() {
Node *newnode = (Node *) malloc(sizeof(Node)); // Use pointer
if (newnode == NULL) {
printf("Memory allocation failed.\n");
return;
}
printf("Enter data part of new node: ");
scanf("%d", &newnode->data);
newnode->next = head; // Insert at the beginning
head = newnode;
printf("Element inserted successfully\n");
// Update tail if the list was empty
if (tail == NULL) {
tail = newnode;
}
}
void display() {
Node *temp = head;
if (temp == NULL) {
printf("Linked List is empty\n");
} else {
printf("Linked List elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}
int getChoice(char *args[], int length) {
int choice;
printf("\nEnter choice:\n");
for (int i = 0; i < length; i++) {
printf("%d. %s\n", i + 1, args[i]);
}
printf(">>> ");
scanf("%d", &choice);
return choice <= length ? choice : 0; }
LINKED LIST DELETION AT BEGGINNING
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next; // Use struct node for pointer type
} Node;
Node *head = NULL;
Node *tail = NULL;
int getChoice(char *[], int);
void insertion();
void deletion();
void display();
int main() {
int isExit = 0;
while (!isExit) {
char *choices[] = {"Insertion", "Deletion at Beginning", "Display", "Exit"};
int choice = getChoice(choices, 4);
switch (choice) {
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
case 4: isExit = 1;
break;
default: printf("Invalid option.\n");
} }
return 0;
}
void insertion() {
Node *newnode = (Node *)malloc(sizeof(Node)); // Use pointer
if (newnode == NULL) {
printf("Memory allocation failed.\n");
return;
}
printf("Enter data part of new node: ");
scanf("%d", &newnode->data);
newnode->next = NULL;
if (head == NULL) { // If the list is empty
head = newnode;
tail = newnode; // Set tail to the new node
} else {
tail->next = newnode; // Link the new node at the end
tail = newnode; // Update tail to the new node
}
printf("Element inserted successfully\n");
}
void deletion() {
Node *temp = head; // Temporarily store the head
head = head->next; // Move head to the next node
free(temp); // Free the old head
if (head == NULL) {
printf("Linked List is empty\n");
return;
}
printf("Element deleted successfully from the beginning\n");
if (head == NULL) {
tail = NULL; // If the list becomes empty, update tail
}}
void display() {
Node *temp = head;
if (temp == NULL) {
printf("Linked List is empty\n");
} else {
printf("Linked List elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
} }
int getChoice(char *args[], int length) {
int choice,i;
printf("\nEnter choice:\n");
for (i = 0; i < length; i++) {
printf("%d. %s\n", i + 1, args[i]);
}
printf(">>> ");
scanf("%d", &choice);
return choice <= length ? choice : 0; // Fixed here
}
Infix to post fix
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char x) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
exit(1);
}
stack[++top] = x;
}
char pop() {
if (top == -1) {
printf("Stack Underflow!\n");
exit(1);
}
return stack[top--];
}
int priority(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
return 0;
}
int main() {
char exp[MAX];
char *e, x;
printf("Enter the expression: ");
scanf("%99s", exp); // Prevent buffer overflow
e = exp;
while (*e != '\0') {
if (isalnum(*e))
printf("%c", *e);
else if (*e == '(')
push(*e);
else if (*e == ')') {
while (top != -1 && (x = pop()) != '(')
printf("%c", x);
if (top == -1) {
printf("Unbalanced parentheses!\n");
exit(1);
}
}
else {
while (top != -1 && priority(stack[top]) >= priority(*e)) {
printf("%c", pop());
}
push(*e);
}
e++;
}
return 0;
}
*DFS*
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define MAX 20
int stack[MAX], top = -1, n = 0, a[MAX][MAX], visited[MAX];
void push(int item) {
stack[++top] = item;
}
int pop() {
return stack[top--];
}
int peek() {
return stack[top];
}
int isStackEmpty() {
return top == -1 ? 1 : 0;
}
int unvisitedVertex(int idx) {
int i;
for (i = 1; i < n; i++) {
if (a[idx][i] == 1 && visited[i] == 0) {
return i;
}
}
return -1;
}
void dfs(int v) {
int i;
visited[v] = 1;
printf("%d ", v);
push(v);
while (!isStackEmpty()) {
int unvisited = unvisitedVertex(peek());
if (unvisited == -1) {
pop();
} else {
visited[unvisited] = 1;
printf("%d ", unvisited);
push(unvisited);
}
}
// Reset visited array for other vertices
for (i = 1; i < n; i++) {
visited[i] = 0;
}
}
int main() {
int i, j, v;
clrscr();
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
visited[i] = 0;
}
printf("Enter graph data in matrix form:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the starting vertex: ");
scanf("%d", &v);
printf("Depth First Search: ");
dfs(v);
getch();
return 0;
}