Dsaiii PDF
Dsaiii PDF
h>
#include<stdlib.h>
int main()
{
int *arr;
int i,n;
int min,max;
printf("Enter the number of integers:");
scanf("%d",&n);
arr=(int*)calloc(n,sizeof(int));
if(arr==NULL)
{
printf("Memory allocation failed!!!!");
return 1;
}
printf("Enter %d integers:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
min=arr[0];
max=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]<min)
{
min=arr[i];
}
if(arr[i]>max)
{
max=arr[i];
}}
int range= max-min;
printf("Data Elements:");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);}
printf("\n");
printf("Maximum value:%d\n",max);
printf("Minimum value:%d\n",min);
printf("Range of data elements:%d\n",range);
free(arr);
return 0;
}
#include <stdio.h>
Void merge(int arr[], int l, int m, int r)
{
Int n1 = m – l + 1;
Int n2 = r – m;
Int L[n1], R[n2];
For (int I = 0; I < n1; i++)
L[i] = arr[l + i];
For (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
Int I = 0, j = 0, k = l;
While (I < n1 && j < n2)
{If (L[i] <= R[j])
{
Arr[k] = L[i];
I++;
}else{
Arr[k] = R[j];
J++; }
K++;}
While (I < n1)
{
Arr[k] = L[i];
I++;
K++; }
While (j < n2)
{
Arr[k] = R[j];
J++;
K++;}}
Void merge_sort(int arr[], int l, int r)
{ If (l < r) {
Int m = l + (r – l) / 2;
Merge_sort(arr, l, m);
Merge_sort(arr, m + 1, r);
Merge(arr, l, m, r);
} }
Int main() {
Int n;
Printf(“Enter the number of elements: “);
Scanf(“%d”, &n);
Int arr[n];
{
Int key,j;
For(int i=1;i<n-1;i++)
{
Key=arr[j];
J=i-1;
While(j>=0 && arr[j]<key)
{
Arr[j+1]=arr[j+1];
j--;
} }}
Void print(int arr[],int n)
{
For(int i=0; i<n; i++)
{
Printf(“%d\t”,arr[i]);
}
Printf(“\n”);
}
Int main()
{
Int arr[],n;
Printf(“enter the number of elements:”);
Scanf(“%d\n”,&n);
For(int i=0; i<n;i++)
{
Scanf(“%d\n”,&arr[i]);
}
Printf(“original array:”);
Printarray(arr,n);
Insertion_sort(arr,n);
Printf(“sorted array:”);
Printarray(arr,n);
Return 0;
}
#include <stdio.h>
Void swap(int* a, int* b)
{
Int t = *a;
*a = *b;
*b = t;
}
Int partition(int arr[], int low, int high)
{
Int pivot = arr[high];
Int I = (low – 1);
For (int j = low; j <= high – 1; j++)
{
If (arr[j] < pivot) {
I++;
Swap(&arr[i], &arr[j]);
}
}
Swap(&arr[I + 1], &arr[high]);
Return (I + 1);
}
Void quicksort(int arr[], int low, int high)
{
If (low < high)
{
Int pi = partition(arr, low, high);
Quicksort(arr, low, pi – 1);
Quicksort(arr, pi + 1, high);
}
}
Int main() {
Int n;
Printf(“Enter the number of elements: “);
Scanf(“%d”, &n);
Int arr[n];
Printf(“Enter the elements:\n”);
For (int I = 0; I < n; i++)
{
Scanf(“%d”, &arr[i]);
}
Quicksort(arr, 0, n – 1);
Printf(“Sorted array using Quicksort: “);
For (int I = 0; I < n; i++)
{
Printf(“%d “, arr[i]);
}
Return 0;}
#include <stdio.h>
If (arr[i] == x) {
Return I;
}
}
Return -1;
}
Int main() {
Int n, x;
Printf(“Enter the number of elements in the array: “);
Scanf(“%d”, &n);
Int arr[n];
Printf(“Enter %d integers:\n”, n);
For (int I = 0; I < n; i++) {
Scanf(“%d”, &arr[i]);
Scanf(“%d”, &x);
If (result != -1) {
} else {
Return 0;
}
#include <stdio.h>
Void bubbleSort(int arr[], int n) {
Int temp;
For (int I = 0; I < n-1; i++) {
For (int j = 0; j < n-i-1; j++) {
If (arr[j] > arr[j+1]) {
Temp = arr[j];
Arr[j] = arr[j+1];
Arr[j+1] = temp;
} } }}
Int binarySearch(int arr[], int n, int x) {
Int low = 0, high = n – 1, mid;
While (low <= high) {
Mid = (low + high) / 2;
If (arr[mid] == x) {
Return mid;
} else if (arr[mid] < x) {
Low = mid + 1;
} else {
High = mid – 1; } }
Return -1;
}
Int main() {
Int n, x;
Printf(“Enter the number of elements in the array: “);
Scanf(“%d”, &n);
Int arr[n];
Printf(“Enter %d integers:\n”, n);
For (int I = 0; I < n; i++) {
Scanf(“%d”, &arr[i]);
}
bubbleSort(arr, n);
Printf(“Sorted array: “);
For (int I = 0; I < n; i++) {
Printf(“%d “, arr[i]);
}
Printf(“\n”);
Printf(“Enter the value to search for: “);
Scanf(“%d”, &x);
Int result = binarySearch(arr, n, x);
If (result != -1) {
Printf(“Element %d is present at position %d in the sorted
array.\n”, x, result + 1);
} else {
Printf(“Element %d is not present in the array.\n”, x);
}
Return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX 100
Typedef struct {
Char name[50];
Int std_code;
} City;
Int linearSearch(City cities[], int n, char city_name[]) {
For (int I = 0; I < n; i++) {
If (strcmp(cities[i].name, city_name) == 0) {
Return I;
} }
Return -1;
}
Int main() {
FILE *file;
City cities[MAX];
Int count = 0;
Char city_name[50];
File = fopen(“cities.txt”, “r”);
If (file == NULL) {
Printf(“Error opening file.\n”);
Return 1;
}
While (fscanf(file, “%s %d”, cities[count].name,
&cities[count].std_code) != EOF) {
Count++;
If (count >= MAX) {
Break;
} }
Fclose(file);
If (result != -1) {
Printf(“The STD code for %s is %d.\n”, cities[result].name,
cities[result].std_code);
} else {
Printf(“City not in the list.\n”);
}
Return 0;
}
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
Struct Node* next;
};
Struct Node* createNode(int x) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
If (!newNode) return NULL;
newNode->data = x;
newNode->next = NULL;
return newNode;
}
Void insert(struct Node** head, int x, int pos) {
Struct Node* newNode = createNode(x);
If (pos == 1 || !*head) { newNode->next = *head; *head = newNode;
return; }
Struct Node* temp = *head;
While (--pos > 1 && temp) temp = temp->next;
If (!temp) free(newNode);
Else { newNode->next = temp->next; temp->next = newNode; }}
Void delete(struct Node** head, int x) {
Struct Node* temp = *head, *prev = NULL;
While (temp && temp->data != x) { prev = temp; temp = temp->next; }
If (!temp) return;
If (!prev) *head = temp->next;
Else prev->next = temp->next;
Free(temp);
}
Void display(struct Node* head) {
While (head) { printf(“%d -> “, head->data); head = head->next; }
Printf(“NULL\n”);
}
Int main() {
Struct Node* head = NULL;
Int ch, data, pos;
While (1) {
Printf(“1.Insert 2.Delete 3.Display 4.Exit: “);
Scanf(“%d”, &ch);
If (ch == 4) break;
If (ch == 1) { printf(“Data: “); scanf(“%d”, &data);
printf(“Position: “); scanf(“%d”, &pos); insert(&head, data, pos); }
Else if (ch == 2) { printf(“Delete data: “); scanf(“%d”, &data);
delete(&head, data); }
Else if (ch == 3) display(head);
Else printf(“Invalid choice\n”);
}
Return 0;
}
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
Struct Node *next, *prev;};
Struct Node* createNode(int x) {
Struct Node *newNode = malloc(sizeof(struct Node));
If (!newNode) return NULL;
newNode->data = x;
newNode->next = newNode->prev = NULL;
return newNode;}
Void insert(struct Node** head, int x, int pos) {
Struct Node *newNode = createNode(x), *temp = *head;
If (pos == 1 || !*head) {
newNode->next = *head;
if (*head) (*head)->prev = newNode;
*head = newNode; Return;}
While (--pos > 1 && temp) temp = temp->next;
If (temp) {
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next) temp->next->prev = newNode;
temp->next = newNode;
} else free(newNode);}
Void delete(struct Node** head, int x) {
Struct Node *temp = *head;
While (temp && temp->data != x) temp = temp->next;
If (!temp) return;
If (*head == temp) *head = temp->next;
If (temp->prev) temp->prev->next = temp->next;
If (temp->next) temp->next->prev = temp->prev;
Free(temp);}
Void display(struct Node* head) {
While (head) {
Printf(“%d <-> “, head->data);
Head = head->next;}
Printf(“NULL\n”);}
Int main() {
Struct Node *head = NULL;
Int ch, data, pos;
While (1) {
Printf(“1.Insert 2.Delete 3.Display 4.Exit: “);
Scanf(“%d”, &ch);
If (ch == 4) break;
If (ch == 1) {
Printf(“Data: “); scanf(“%d”, &data);
Printf(“Position: “); scanf(“%d”, &pos);
Insert(&head, data, pos);
} else if (ch == 2) {
Printf(“Delete data: “); scanf(“%d”, &data);
Delete(&head, data);
} else if (ch == 3) display(head);
Else printf(“Invalid choice\n”); }
Return 0;}
#include <stdio.h>
#include <stdlib.h>
Struct CNode {
Int data;
Struct CNode* next;
};
Struct CNode* createCNode(int x) {
Struct CNode* newNode = (struct CNode*)malloc(sizeof(struct CNode));
newNode->data = x;
newNode->next = newNode;
return newNode;}
Void append(struct CNode** last, int x) {
Struct CNode* newNode = createCNode(x);
If (*last == NULL) {
*last = newNode;
} else {
newNode->next = (*last)->next;
(*last)->next = newNode;
}
*last = newNode;
}
Void display(struct CNode* last) {
If (last == NULL) {
Printf(“Circular List is empty\n”);
Return;
}
Struct CNode* temp = last->next;
Do {
Printf(“%d -> “, temp->data);
Temp = temp->next;
} while (temp != last->next); Printf(“(Circular)\n”);
}
Int main() {
Struct CNode* circularLast = NULL;
Int choice, data;
While (1) { Printf(“\nMenu:\n”);
Printf(“1. Append\n”);
Printf(“2. Display\n”);
Printf(“3. Exit\n”);
Printf(“Enter your choice: “);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1: Printf(“Enter the element to append: “);
Scanf(“%d”, &data); Append(&circularLast,
data);
Break;
Case 2: Display(circularLast);
Break;
Case 3: Printf(“Exiting…\n”);
Exit(0);
Default: Printf(“Invalid choice! Please try
again.\n”);
} }
Return 0;}
#include <stdio.h>
Insertion_sort(arr, n);
Printf(“%d “, arr[i]);
Return 0;
}
#include<stdio.h>
#define SIZE 20
Typedef struct{
Int a[SIZE];
Int top;
Int size;
}stack;
Void init_stack(stack *s);
Void push(stack *s, int n);
Int pop(stack *s);
Int peek(stack s);
Int isEmpty(stack s);
Int isFull(stack s);
Void init_stack(stack *s){
s->top = -1;
s->size = 0;
}
Void push(stack *s, int n){
If(s->top < SIZE-1){
s->a[++(s->top)] = n;
(s->size)++;
}else{
Printf(“\nStack Overflow..\n”);
}}
Int pop(stack *s){
If(isEmpty(*s)==1){
Printf(“\nStack Underflow..\n”);
Return -1;
}
Int n = s->a[s->top];
(s->size)--;
(s->top)--;
Return n;
}
Int peek(stack s){
Return s.a[s.top];
}
Int isEmpty(stack s){
If(s.top == -1)
Return 1;
Return 0;
}
Int isFull(stack s){
If(s.top == SIZE-1)
Return 1;
Return 0;
}
Int main(){
Stack s;
Int choice, num;
Init_stack(&s);
Do
{
Printf(“\nMenu\n”);
Printf(“1. Push\n”);
Printf(“2. Pop\n”);
Printf(“3. Peek\n”);
Printf(“4. Is Empty\n”);
Printf(“5. Is Full\n”);
Printf(“Enter your choice (-1 to stop): “);
Scanf(“%d”,&choice);
Switch(choice){
Case 1: printf(“\nEnter a number to Push: “);
Scanf(“%d”, &num);
Push(&s, num);
Break;
Break;
Case 4: if(isEmpty(s) == 1)
Printf(“Stack is Empty..\n”);
Else