?DS Programs?
?DS Programs?
if (g == 1) {
printf("Pattern not found.\n");
}
}
return 0;
}
3.SEARCHING IN 2D ARRAY
#include<stdio.h>
int main()
{
int ar[5][5],valr,flag=0,j,i,valc,val;
printf("enter number of rows and columns of array:\n");
scanf("%d",&valr);
scanf("%d",&valc);
printf("enter the numbers:\n");
for(i=0;i<valr;i++)
{
for(j=0;j<valc;j++)
{
scanf("%d",&ar[i][j]);
}
}
//search an element
printf("enter a value to search its postion:");
scanf("%d",&val);
for(i=0;i<valr;i++)
{
for(j=0;j<valc;j++)
{
if(ar[i][j]==val)
{
printf("position of value %d in the array is %d,%d:",val,i,j);
flag=1;
break;
}
}
}
if(flag==0)
{
printf("element not found");
}
}
4.APPEND 2 ARRAY
#include<stdio.h>
int main()
{
int i, j, m, n, ar1[100], ar2[100];
printf("appended array:\n");
for (i = 0; i < m + n; i++)
{
printf("%d\t", ar1[i]);
}
return 0;
}
5.BINARY SEARCH
#include<stdio.h>
int binary(int ar[], int max, int min, int r);
int main()
{
int n, x, i, k;
printf("Enter maximum number of elements: ");
scanf("%d", &k);
n = k - 1;
int ar[k];
printf("Enter numbers in ascending order:\n");
for (i = 0; i < k; i++)
{
scanf("%d", &ar[i]);
}
printf("Enter the number to search: ");
scanf("%d", &x);
int result = binary(ar, n - 1, 0, x);
if (result == -1)
{
printf("Element not found\n");
}
else
{
printf("Element is present at index %d\n", result);
}
return 0;
}
int binary(int ar[], int max, int min, int r)
{
if (max >= min)
{
int mid = (max + min) / 2;
if (ar[mid] == r)
{
return mid;
}
else if (ar[mid] > r)
{
return binary(ar, mid - 1, min, r);
}
else
{
return binary(ar, max, mid + 1, r);
}
}
return -1;
}
6.SPARSE MATRIX
#include<stdio.h>
int main()
{
int i, j, k, r, c, rs;
printf("Enter number of columns of the matrix: ");
scanf("%d", &c);
printf("Enter number of rows of the matrix: ");
scanf("%d", &r);
printf("Enter elements of the matrix:\n");
rs = r * c + 1;
int matrixa[r][c];
int triple[rs][3];
triple[0][0] = r;
triple[0][1] = c;
k = 0;
return 0;
}
7.SINGLY LINKED LIST INSERTION
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *newnode, *temp, *start = NULL;
int i, n;
printf("Enter number of nodes: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for(i = 0; i < n; i++)
{
newnode = (struct node*)malloc(sizeof(struct node));
scanf("%d", &newnode->data);
newnode->next = NULL;
if(start == NULL)
{
start = newnode;
temp = newnode;
}
else
{
temp->next = newnode;
temp = newnode;
}
}
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *newnode, *temp, *start = NULL;
int i, n;
printf("Enter number of nodes: ");
scanf("%d", &n);
printf("Enter the elements:\n");
if(start == NULL)
{
start = newnode;
temp = newnode;
}
else
{
temp->next = newnode;
temp = newnode;
}
}
int main()
{
int x=1, max;
printf("enter maximum size of stack:\n");
scanf("%d", &max);
int stack[max], top = -1;
return 0;
}
11.STACK USING LINKED LIST
#include<stdio.h>
#include<malloc.h>
int main()
{
struct node{
int data;
struct node* next;
struct node* prev;
};
struct node *temp,*newnode,*p,*last,*start=NULL;
int x=1,max,i=0;
printf("enter the maximum size of stack:");
scanf("%d",&max);
printf("1.choice for push\n");
printf("2.choice for pop\n");
printf("3.choice for display\n");
printf("4.choice for exit\n");
while(x!=0)
{
printf("enter your choice\n");
scanf("%d",&x);
if(x==0)
{
break;
}
switch(x)
{
case 1:
{
if(i==max)
{
printf("stack overflow");
}
else
{
printf("enter a value");
newnode=(struct node*)malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
temp=newnode;
last=temp;
temp->prev=NULL;
}
else
{
temp->next=newnode;
temp=newnode;
temp->prev=last;
last=temp;
}
i++;
}
break;
}
case 2:
{
if(start==NULL)
{
printf("stack is empty\n");
}
else
{
printf("pop %d\n",temp->data);
temp=last;
p=temp;
temp=temp->prev;
last=temp;
free(p);
i--;
}
break;
}
case 3:
printf("stack is \n");
temp=last;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->prev;
}
break;
default:
{
printf("\n wrong choice\n");
break;
}
}
}
}
12.INFIX EXP TO POSTFIX EXP
#include <stdio.h>
#include <ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if (top == -1)
{
return -1;
}
else
{
return stack[top--];
}
}
int priority(char x)
{
if (x == 'c')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
return -1;
}
int main()
{
char exp[100];
char *e;
char x;
printf("Enter the expression: ");
scanf("%s", exp);
printf("\n");
e = exp;
while (*e != '\0')
{
if (isalnum(*e))
printf("%c", *e);
else if (*e == 'c')
push(*e);
else if (*e == '1')
{
while ((x = pop()) != 'c')
printf("%c", x);
}
else
{
while (top != -1 && priority(stack[top]) >= priority(*e))
printf("%c", pop());
push(*e);
}
e++;
}
return 0;
}
13.QUEUE USING ARRAY
#include<stdio.h>
#define max 5
int front=-1,rear=-1;
int queue[max];
void insertq();
void deleteq();
void displayq();
int main()
{
int choice;
printf("1.INSERT\n");
printf("2.DELETE;\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
while(1)
{
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:insertq();
break;
case 2:deleteq();
break;
case 3:displayq();
break;
case 4:return 0;
default:printf("invalid choice");
}
}
return 0;
}
void insertq()
{
int data;
if(rear==max-1)
printf("stack is full\n");
else
{
printf("enter the data:");
scanf("%d",&data);
rear++;
queue[rear]=data;
}
}
void deleteq()
{
int data;
if(front==rear)
printf("queue is empty");
else
{
front++;
data=queue[front];
printf("deleted element from queue is %d\n",data);
}
}
void displayq()
{
int i;
if(front==rear)
printf("queue is empty\n");
else
{
printf("the element in queue are :\n");
for(i=front+1;i<=rear;i++)
printf("\t%d",queue[i]);
printf("\n");
}
}
14.QUEUE USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
void display();
void insertq();
void deleteq();
int main() {
int choice;
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
while(1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
insertq();
break;
case 2:
deleteq();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
}
void insertq() {
struct node *temp;
int data;
temp = (struct node*)malloc(sizeof(struct node));
if(temp == NULL) {
printf("Overflow\n");
} else {
printf("Enter the data: ");
scanf("%d", &data);
temp->data = data;
temp->link = NULL;
if(rear != NULL) {
rear->link = temp;
rear = temp;
} else {
front = temp;
rear = temp;
}
}
}
void deleteq() {
struct node *temp;
int item;
if(front == NULL) {
printf("Queue is empty\n");
} else {
temp = front;
item = front->data;
front = front->link;
free(temp);
printf("Deleted element from the queue is %d\n", item);
}
}
void display() {
struct node *temp;
if(front == NULL) {
printf("Queue is empty\n");
} else {
temp = front;
printf("The elements in the queue are: ");
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->link;
}
printf("\n");
}
}
15.BINARY SEARCH TREE
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node {
int data;
struct node *left;
struct node *right;
};
typedef struct node node;
int main() {
int op;
printf("1. insert\n");
printf("2. display\n");
printf("3. search\n");
printf("4. exit\n");
while (1) {
printf("\t choice: ");
scanf("%d", &op);
switch (op) {
case 1:
root = insert(root);
break;
case 2:
display(root);
break;
case 3:
search(root);
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
}
}
return 0;
}
do {
tempnode = (node *)malloc(sizeof(node));
printf("Enter the data to insert: ");
scanf("%d", &data);
tempnode->data = data;
tempnode->left = NULL;
tempnode->right = NULL;
if (root == NULL)
root = tempnode;
else {
current = root;
while (current != NULL) {
parent = current;
if (data < parent->data) {
current = parent->left;
if (current == NULL)
parent->left = tempnode;
} else {
current = current->right;
if (current == NULL)
parent->right = tempnode;
}
}
}
return root;
}
if (flag == 1)
printf("\nThe element is found in the tree\n");
else
printf("The element does not exist in the tree\n");
}
16.BUBBLE SORT
#include <stdio.h>
int main() {
int a[10], num, i, j, swap;
printf("Enter the size of the array: ");
scanf("%d", &num);
return 0;
}
17.SELECTION SORT
#include <stdio.h>
int main() {
int a[10], num, i, j, pos, temp;
if (pos != i) {
temp = a[i];
a[i] = a[pos];
a[pos] = temp;
}
}
return 0;
}
18.INSERTION SORT
#include<stdio.h>
int main() {
int a[10], num, i, j, temp;
printf("Enter the size of the array: ");
scanf("%d", &num);
return 0;
}
19.SORTED STRINGS
#include<stdio.h>
#include<string.h>
struct student {
char name[50];
};
int main() {
struct student list[50];
struct student temp;
int n, i;
printf("\nSorted strings:\n");
for (i = 0; i < n; i++) {
printf("%s\n", list[i].name);
}
return 0;
}
20.LINEAR SEARCH
#include<stdio.h>
int main()
{
int ar[100];
int n, target = 0;
return 0;
}