Il 0% ha trovato utile questo documento (0 voti)
33 visualizzazioni28 pagine

Write A Program To Implement Sequential Search.: Unit 2 Searching

Il documento contiene tre programmi in C per implementare operazioni di ricerca, ordinamento e stack utilizzando array e liste concatenate. I programmi mostrano come implementare ricerca sequenziale e binaria, ordinamento a bolle, selezione e fusione, e operazioni di push, pop e peep su uno stack.

Caricato da

Pal Bharti
Copyright
© © All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Il 0% ha trovato utile questo documento (0 voti)
33 visualizzazioni28 pagine

Write A Program To Implement Sequential Search.: Unit 2 Searching

Il documento contiene tre programmi in C per implementare operazioni di ricerca, ordinamento e stack utilizzando array e liste concatenate. I programmi mostrano come implementare ricerca sequenziale e binaria, ordinamento a bolle, selezione e fusione, e operazioni di push, pop e peep su uno stack.

Caricato da

Pal Bharti
Copyright
© © All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Sei sulla pagina 1/ 28

Unit 2

Searching
1. Write a program to implement sequential search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,x,i,flag=0;
clrscr();
printf("\nEnter element: ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter %d elemtnet :",i+1);
scanf("%d",&a[i]);
}

printf("\nElemenet enter to fine :");


scanf("%d",&x);

for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
printf("\nfound %d postion:",i+1);
}
}
if(flag==0)
{
printf("Element not found");
}
getch();
}

Instagram mr_faisal_607 Telegram : GUBCA


2. Write a program to implement binary search.
#include<stdio.h>
#include<conio.h>
void binary_search(int [],int ,int);
void main()
{
int a[20],i,n,x,low,mid,high;
clrscr();
printf("\nEnter number of element:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter %d element:",i+1);
scanf("%d",&a[i]);
}

printf("\nEnter element to find:");


scanf("%d",&x);

binary_search(a,n,x);
getch();
}
void binary_search(int a[],int n,int x)
{
int low,mid,high;
low=1;
high=n-1;
mid=(low+high)/2;
while(low<=high && x!=a[mid])
{
if(x<a[mid])
high=mid-1;
else if(x>a[mid])
low=mid+1;
mid=(low+high)/2;
}
if(x==a[mid])
printf("\nElement %d fond at potion %d",x,mid+1);
if(low>high)
{
printf("\nElement not fond");
}
}

Instagram mr_faisal_607 Telegram : GUBCA


Sorting
1. Write a program to implement bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,j,temp;
clrscr();
printf("How many element u want to enter? :");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("Enter %d element : ",i);
scanf("%d",&a[i]);
}

printf("Unsorted array is ... \n");


for(i=1;i<=n;i++)
{
printf("%d \n",a[i]);
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Sorted array is...");
for(i=1;i<=n;i++)
{
printf("\n %d \n",a[i]);
}
getch();

Instagram mr_faisal_607 Telegram : GUBCA


}
2. Write a program to implement selection sort
#include<stdio.h>
#include<conio.h>
void selection_sort(int [],int);
void main()
{
int a[20],i,n,j,temp;
clrscr();
printf("How many elemetns u want to enter ? ::");
scanf("%d",&n);

//Initialize array
for(i=1;i<=n;i++)
{
printf("Enter %d element :",i);
scanf("%d",&a[i]);
}

printf("Unsorted array is ..");


for(i=1;i<=n;i++)
{
printf("%d \n",a[i]);
}

selection_sort(a,n);
getch();
}
void selection_sort(int a[],int n)
{
int i,j,min,temp;
for(i=1;i<n-1;i++)
{
min=i;
for(j=i+1;j<=n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{

Instagram mr_faisal_607 Telegram : GUBCA


temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("sorted array is..\n");
for(i=1;i<=n;i++)
{
printf("%d \n",a[i]);
}
}

Instagram mr_faisal_607 Telegram : GUBCA


3. Write a program to implement merge sort
#include<stdio.h>
#include<conio.h>
void merge(int [],int,int,int);
void merge_sort(int [],int,int);
void main()
{
int a[20],i,j,k,n;
clrscr();
printf("Enter number of elements the array :");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter %d element:",i+1);
scanf("%d",&a[i]);
}

merge_sort(a,0,n-1);

printf("sorted array...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
void merge(int a[],int beg,int mid,int end)
{
int i=beg,j=mid+1,index=beg,k,temp[10];
while((i<=mid)&&(j<=end))
{
if(a[i]<a[j])
{
temp[index]=a[i];
i++;
}
else
{
temp[index]=a[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index]=a[j];
index++;
}

Instagram mr_faisal_607 Telegram : GUBCA


}
else
{
while(i<=mid)
{
temp[index]=a[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
{
a[k]=temp[k];
}
}
void merge_sort(int a[],int beg,int end)
{
int mid;
if(beg<end)
{
mid=(beg+end)/2;
merge_sort(a,beg,mid);
merge_sort(a,mid+1,end);
merge(a,beg,mid,end);
}
}

Instagram mr_faisal_607 Telegram : GUBCA


Unit 3
1. Write a program to implement following operations in
stack Using array and Linked List.
• PUSH
• POP
• PEEP
Array
#include<stdio.h>
#include<conio.h>
int n,s[10],top=0;
void push();
void pop();
void peep();
void display();
void main()
{
int ch;
clrscr();
printf("Enter the size of stack::\n");
scanf("%d",&n);
do
{
clrscr();
printf("\n1..PUSH");
printf("\n2..POP");
printf("\n3..PEEP");
printf("\n4..DISPLAY");
printf("\n5..EXIT");
printf("\nEnter your choice::");
scanf("%d",&ch);

switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peep();
break;
case 4:display();
break;
case 5:
break;
}
getch();

Instagram mr_faisal_607 Telegram : GUBCA


}while(ch!=5);
}
void push()
{
if(top>=n)
{
printf("\n........Overflow.........");
}
else
{
top++;
printf("\nEnter the element :");
scanf("%d",&s[top]);
}
}
void pop()
{
if(top==0)
{
printf("\n........underflow........");
}
else
{
int x;
x=s[top];
top--;
printf("\n%d is deleted.",x);
}
}
void peep()
{
if(top==0)
{
printf("\nStack is empty...");
}
else
{
printf("\nTop most element of stack is %d",s[top]);
}
}

void display()
{
int i;
for(i=1;i<=top;i++)
{
printf("\n%d",s[i]);
}
}

Instagram mr_faisal_607 Telegram : GUBCA


Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack * next;
};
struct stack * top=NULL;
struct stack * push(struct stack *,int);
struct stack * pop(struct stack *);
struct stack * display(struct stack *);

struct stack * push(struct stack * top,int x)


{
struct stack *ptr;
ptr=(struct stack *) malloc(sizeof(struct stack *));
ptr->data=x;
if(top==NULL)
{
top=ptr;
top->next=NULL;
}
else
{
ptr->next=top;
top=ptr;
}
return top;
}
struct stack *pop(struct stack * top)
{
struct stack * ptr;
ptr=top;
if(top==NULL)
{
printf("Stack is empty...");
}
else
{
top=top->next;
printf("%d is delete",ptr->data);
free(ptr);
}
return top;
}

struct stack * display(struct stack * top)


{
struct stack * ptr;
ptr=top;
if(top==NULL)

Instagram mr_faisal_607 Telegram : GUBCA


{
printf("Stack is empty..");
}
else
{
while(ptr!=NULL)
{
printf("\t%d",ptr->data);
ptr=ptr->next;
}
}
return top;
}
void main()
{
int x,ch;
clrscr();
do
{
printf("\n1..PUSH");
printf("\n2..POP");
printf("\n3..Display");
printf("\n4.Exit");
printf("\nEnter your choice..");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\nEtner the number to push...");
scanf("%d",&x);
top=push(top,x);
break;
case 2:
top=pop(top);
break;
case 3:
top=display(top);
break;

}
}while(ch!=4);
getch();

Instagram mr_faisal_607 Telegram : GUBCA


2. Write a program to implement Evaluation of given
postfix expression
#include <stdio.h>
#include <conio.h>
#include <ctype.h> // Include ctype.h for isdigit() function
#define MAX 100

int s[MAX];
int top = -1; // Initialize top to -1

void push(int s[], int x);


int pop(int s[]);
int evaluatePostfix(char exp[]); // Change the function name

void main()
{
int x;
char exp[100];
clrscr();
printf("\nEnter postfix expression: ");
gets(exp);
x = evaluatePostfix(exp); // Correct the function call
printf("\nEvaluation is %d", x);
getch();
}

int evaluatePostfix(char exp[]) // Correct the function name


{
int i = 0;
int op1, op2, val;
while (exp[i] != '\0')
{
if (isdigit(exp[i])) // Check if the character is a digit
{
push(s, (int)(exp[i] - '0')); // Convert character to
integer
}
else
{
op2 = pop(s);
op1 = pop(s);
switch (exp[i])
{

Instagram mr_faisal_607 Telegram : GUBCA


case '+':
val = op1 + op2;
break;
case '-':
val = op1 - op2;
break;
case '*':
val = op1 * op2;
break;
case '/':
val = op1 / op2;
break;
case '%':
val = op1 % op2;
break;
}
push(s, val);
}
i++;
}
return pop(s);
}

void push(int s[], int x)


{
if (top == MAX - 1) // Check for overflow
{
printf("Overflow..");
}
else
{
top++;
s[top] = x;
}
}

int pop(int s[])


{
int x;
if (top == -1) // Check for underflow
{
printf("\nUnderflow..");
return -1; // Return an error value
}
else
{

Instagram mr_faisal_607 Telegram : GUBCA


x = s[top];
top--;
return x;
}
}

Instagram mr_faisal_607 Telegram : GUBCA


3.Write a program to implement conversion of infix
expression into postfix expression (parentheses and non
parentheses).
#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=='(')
return 0;
else if(x=='+' || x=='-')
return 1;
else if(x=='*' || x=='/')
return 2;
}
int main()
{
char exp[100];
char *e,x;
clrscr();
printf("Enter the expression:");
scanf("%s",exp);
printf("\n");
e=exp;

while(*e!='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e=='(')
push(*e);
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else

Instagram mr_faisal_607 Telegram : GUBCA


{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
getch();
}

Instagram mr_faisal_607 Telegram : GUBCA


4.Write a program to implement recursion.
#include<stdio.h>
#include<conio.h>
int s[20];
int top=0;
void push(int x);
int pop();

int main()
{
int i;
long fact=1;
int n;
clrscr();

printf("\nEnter N= ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
push(i);
}
for(i=1;i<=n;i++)
{
fact=(long)fact*pop();
}

printf("\nFactorial of %d = %d",n,fact);
getch();
return 0;
}
void push(int x)
{
top++;
s[top]=x;
}
int pop()
{
int x;
x=s[top];
top=top-1;
return (x);
}

Instagram mr_faisal_607 Telegram : GUBCA


5.Write a program to reverse the string using the stack.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100
int top=-1;
int item;
char s[MAX];
void push_char(char itme);
char popchar(void);

void main()
{
char str[MAX];
int i;
clrscr();
printf("Input a string:");
scanf("%[^\n]s",str);
for(i=0;i<strlen(str);i++)
push_char(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=popchar();

printf("\nReversed string is : %s",str);


getch();
}
void push_char(char item)
{
//check for full
if(top==MAX-1)
{
printf("\nStack is full!!!!!\n");
return;
}
else{
top=top+1;
s[top]=item;
}
}

char popchar()
{
if(top==-1)
{
printf("\nStack is Empty!!!\n");
}
else{
item=s[top];
top=top-1;
return item;
}

Instagram mr_faisal_607 Telegram : GUBCA


Unit 4
Queue
1. Write a program to implement Simple Queue operations
using Array and Linked List.
• ENQUEUE
• DEQUEUE
• Traversal (display)
Array
#include<stdio.h>
#include<conio.h>
int n,q[10],f=0,r=0;
void main()
{
int ch;
void insert();
void display();
void delete();

clrscr();
printf("\nEnter the size of Queue:");
scanf("%d",&n);
do{
clrscr();
printf("\n1..Insert");
printf("\n2..Delete");
printf("\n3..Dislay");
printf("\n4..Exit");
printf("\nEnter your choice::");
scanf("%d",&ch);

switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);

Instagram mr_faisal_607 Telegram : GUBCA


}
getch();
}while(ch!=4);
}
void insert()
{
if(r>=n)
{
printf("\n...Overflow.......");
}
else{
r++;
printf("\nEnter the element:");
scanf("%d",&q[r]);
if(f==0)
f=1;
}
}
void delete()
{
if(f==0)
{
printf("\n....Underflow.......");
}
else{
int y;
y=q[f];

if(f==r)
f=r=0;
else
f++;
printf("\n%d is deleted",y);
}
}
void display()
{
if(f==0)
{
printf("\n......Underflow..........");
}
else{
int i;
printf("\n");
for(i=f;i<=r;i++)
{
printf("%d\t",q[i]);
}
} }

Instagram mr_faisal_607 Telegram : GUBCA


Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node {
int data;
struct node *next;
};

struct queue {
struct node *f;
struct node *r;
};

void create(struct queue *q) {


q->f = NULL;
q->r = NULL;
}

struct queue *insert(struct queue *q, int val) {


struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = val;

if (q->f == NULL) {
q->f = ptr;
q->r = ptr;
q->f->next = q->r->next = NULL;
} else {
q->r->next = ptr;
q->r = ptr;
q->r->next = NULL;
}
return q;
}

struct queue *delet(struct queue *q) {


struct node *ptr;
ptr = q->f;

if (q->f == NULL) {
printf("Deletion not possible\n");
} else {
q->f = q->f->next;
printf("%d is deleted\n", ptr->data);
free(ptr);
}
return q;
}

void display(struct queue *q) {


struct node *ptr;

Instagram mr_faisal_607 Telegram : GUBCA


ptr = q->f;

if (ptr == NULL) {
printf("Queue is empty\n");
} else {
while (ptr != q->r) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
}
}

void main() {

int choice, value;


struct queue *q = (struct queue *)malloc(sizeof(struct queue));
create(q);
clrscr();
do {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
q = insert(q, value);
break;
case 2:
q = delet(q);
break;
case 3:
display(q);
break;
case 4:
printf("Quitting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
getch();
}

Instagram mr_faisal_607 Telegram : GUBCA


2. Write a program to implement Circular Queue operations
Using Array.
• ENQUEUE
• DQUEUE
• Traversal (display)
#include<stdio.h>
#include<conio.h>
int q[5],front=-1,rear=-1,max=5;

void enqueue();
void dequeue();
void display();

void main()
{

int op,val;
clrscr();
do
{
printf("\n1:Enqueue");
printf("\n2:Dqueue");
printf("\n3:Display");
printf("\n4:Exit");

printf("\nEnter your choice:");


scanf("%d",&op);

switch (op)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
}
} while (op!=4);
getch();
}

void enqueue()

Instagram mr_faisal_607 Telegram : GUBCA


{
int n;
printf("\nEnter value:");
scanf("%d",&n);

if(front==0 && rear==max)


{
printf("\n.......Overflow..........");
}
else if(front==-1 && rear==-1)
{
front=rear=0;
q[rear]=n;
}
else if (rear==max-1 && front!=0)
{
rear=0;
q[rear]=n;
}
else
{
rear++;
q[rear]=n;
}

}
void dequeue()
{
int val;
if(front==-1)
{
printf("\n......underflow......");
}

val=q[front];
if(front==rear)
{
front=rear=-1;
}
else{
if(front==max-1)
{
front=0;
}
else
{
front++;
}
}
printf("\nValue is delete:%d",val);
}
void display()
{
int i;
if(front!=-1 && rear!=-1)

Instagram mr_faisal_607 Telegram : GUBCA


{
if(front<rear)
{
for(i=front;i<=rear;i++)
{
printf("\n%d",q[i]);
}
}
}
else{
for(i=0;i<=rear;i++)
{
printf("\n%d",q[i]);
}
for(i=front;i<max;i++)
{
printf("\n%d",q[i]);
}

}
}

Instagram mr_faisal_607 Telegram : GUBCA


3. Write a program to implement following operations on
Binary Search Tree using Linked List.
• Creation
• Insertion
• Traversal( In-order, Pre-order, Post-order)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct tree
{
int info;
struct tree *left;
struct tree *right;
};

struct tree * insert(struct tree *,int);


void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);

int main()
{
struct tree * root;
int ch,val;
root=NULL;
clrscr();
do{
printf("\n1:Insert");
printf("\n2:In-order");
printf("\n3:pre-order");
printf("\n4:post-order");
printf("\nEnter choice: ");
scanf("%d",&ch);

switch (ch)
{
case 1:
printf("\nEnter new element: ");
scanf("%d",&val);
root=insert(root,val);
printf("\n");
inorder(root);
break;
case 2:
printf("\nInorder traversal of tree is..");

Instagram mr_faisal_607 Telegram : GUBCA


inorder(root);
break;
case 3:
printf("\nPreorder traversal of tree is..");
preorder(root);
break;
case 4:
printf("Postorder traversal of tree is..");
postorder(root);
break;
case 5:
break;
}
}while(ch!=5);
return 0;
}

struct tree *insert(struct tree *root, int x) {


if (!root) {
root = (struct tree *)malloc(sizeof(struct tree));
root->info = x;
root->left = NULL;
root->right = NULL;
return root;
}

if (root->info > x) {
root->left = insert(root->left, x);
}
else
{
if(root->info < x)
root->right = insert(root->right, x);
}
return root;
}

void inorder(struct tree *root) {


if (root!=NULL) {
inorder(root->left);
printf("%d ", root->info);
inorder(root->right);
}
}

void preorder(struct tree *root) {


if (root!=NULL) {
printf("%d ", root->info);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct tree *root) {

Instagram mr_faisal_607 Telegram : GUBCA


if (root!=NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->info);
}
}

Instagram mr_faisal_607 Telegram : GUBCA

Potrebbero piacerti anche