[go: up one dir, main page]

0% found this document useful (0 votes)
12 views18 pages

Dsaiii PDF

Uploaded by

vmusale908
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)
12 views18 pages

Dsaiii PDF

Uploaded by

vmusale908
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/ 18

#include<stdio.

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

Printf(“Enter the elements:\n”);


For (int I = 0; I < n; i++) {
Scanf(“%d”, &arr[i]); }
Merge_sort(arr, 0, n – 1);
Printf(“Sorted array using Merge Sort: “);
For (int I = 0; I < n; i++) {
Printf(“%d “, arr[i]); }
Return 0;}
#include<stdio.h>

Void bubble_sort(int arr[],int n)


{
For(int i=0; i<n-1; i++)
{

For(int j=0; j<n-i-1; j++)


{
If(arr[j]>arr[j+1])
{
Int temp=arr[j];
Arr[j]=arr[j+1];
Arr[j+1]=temp; } }}}
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]);
}
Bubble_sort(arr,n);
Printf(“sorted array using bubble_sort:”);
For(int i=0; i<n;i++)
{
Printf(“%d\n”,arr[i]);
}
Return 0;
}
#include<stdio.h>

Void insertion_sort(int arr[],int 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>

Int linearSearch(int arr[], int n, int x) {

For (int I = 0; I < n; i++) {

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

Printf(“Enter the value to search for: “);

Scanf(“%d”, &x);

Int result = linearSearch(arr, n, x);

If (result != -1) {

Printf(“Element %d is present at position %d.\n”, x, result + 1);

} else {

Printf(“Element %d is not present in the array.\n”, x);

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

Printf(“Enter the name of the city: “);


Scanf(“%s”, city_name);

Int result = linearSearch(cities, count, city_name);

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>

Void insertion_sort(int arr[], int n)


{
For (int I = 1; I < n; i++)

Int key = arr[i];


Int j = I – 1;
While (j >= 0 && arr[j] > key)
{
Arr[j + 1] = arr[j];
J = j – 1;
}
Arr[j + 1] = key;
}
}
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]);

Insertion_sort(arr, n);

Printf(“Sorted array using Insertion Sort: “);

For (int I = 0; I < n; i++) {

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;

Case 2: num = pop(&s);


If(num != -1)
Printf(“\nPopped element is: %d\n”, num);
Break;

Case 3: printf(“\nThe element at the top is: %d\n”, peek(s));

Break;

Case 4: if(isEmpty(s) == 1)
Printf(“Stack is Empty..\n”);
Else

Printf(“Stack is not Empty..\n”);


Break;
Case 5: if(isFull(s) == 1)
Printf(“Stack is Full..\n”);
Else
Printf(“Stack is not Full..\n”);
Break;
}
} while (choice != -1);
Return 0;}
#include <stdio.h>
#include <stdlib.h>

Typedef struct Node {


Int data;
Struct Node *next;
} Node;
Typedef struct {
Node *head;
} Stack;
Void init_stack(Stack *s) { s->head = NULL; }
Void push(Stack *s, int n) {
Node *newn = (Node *)malloc(sizeof(Node));
Newn->data = n;
Newn->next = s->head;
s->head = newn;
}
Int pop(Stack *s) {
If (!s->head) { printf(“\nStack Underflow..\n”); return -1; }
Int num = s->head->data;
Node *temp = s->head;
s->head = s->head->next;
free(temp);
return num;
}
Int peek(Stack s) { return s.head ? s.head->data : -1; }
Int isEmpty(Stack s) { return s.head == NULL; }
Int main() {
Stack s;
Int choice, num;
Init_stack(&s);
While (1) {
Printf(“\nMenu\n1. Push\n2. Pop\n3. Peek\n4. Is Empty\nEnter
choice (-1 to stop): “);
Scanf(“%d”, &choice);
If (choice == -1) break;
Switch (choice) {
Case 1: printf(“\nEnter number to Push: “); scanf(“%d”,
&num); push(&s, num); break;
Case 2: num = pop(&s); if (num != -1) printf(“\nPopped
element is: %d\n”, num); break;
Case 3: printf(“\nTop element: %d\n”, peek(s)); break;
Case 4: printf(isEmpty(s) ? “\nStack is Empty..\n” : “\nStack
is not Empty..\n”); break;
}
}
Return 0;}
#include <string.h>
#include<stdio.h>
#define SIZE 50
Typedef struct{
Char a[SIZE];
Int top;
Int size;
}stack;
Void init_stack(stack *s);
Void push(stack *s, char 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, char c){
If(s->top < SIZE-1){
s->a[++(s->top)] = c;
(s->size)++;
}else{
Printf(“\nStack Overflow..\n”);
}}
Int pop(stack *s){
If(isEmpty(*s)==1){
Printf(“\nStack Underflow..\n”);
Return -1;
}
Char c = s->a[s->top];
// printf(“%c “,c);
(s->size)--;
(s->top)--;
Return (int)c;
}
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;
Init_stack(&s);
Char word[50], reverse[50];
Printf(“Enter a string: “);
Scanf(“%s”,word);
Int I = 0;
While (word[i] != ‘\0’){
Push(&s, word[i]);
I++;
}
Int c;
I = 0;
While ((c=pop(&s)) != -1){
Reverse[i] = (char)c;
I++;
}
Reverse[i] = ‘\0’;
If(strcmp(word, reverse) == 0)
Printf(“String is Palindrom..\n”);
Else
Printf(“String is not Palindrom..\n”);
Return 0;
}

You might also like