You are on page 1of 20

1.

Menu driven program to perform various operations on the


array.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[20];
int m,n,p,val,i,j,key,pos;
/*Function Prototype*/
void create();
void display();
void insert();
void del();
int main()
{
int choice;
clrscr();
do{
printf("\n\n--------Menu-----------\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:
display();
break;
case 3:
insert();
break;

case 4:
del();
break;

case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=5);
return 0;
}
void create() //creating an array
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}//end of create()
void display() //displaying an array elements
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
}//end of display()
void insert() //inserting an element in to an array
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}//end of insert()

void del() //deleting an array element


{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
}//end of delete()
2. Menu driven program of various searching techniques.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int No2 = 0;
int Scan(int a[100]);
void Lsearch(int a[100],int);
void Bsearch(int a[100],int);
void main()
{
int No1 = 0;
int a[20];
clrscr();
do
{
printf("\n\nPlease Select One Of The Following Options ::");
printf("\n1. Linear Searching");
printf("\n2. Binary Searching");
printf("\n3. Exit ");
printf("\nPlease Enter Your Choice Here ::\t");
scanf("%d",&No1);
switch(No1)
{
case 1: No2 = (Scan(a));
Lsearch(a , No2);
break;
case 2: No2 = (Scan(a));
Bsearch(a , No2);
break;

case 3: exit (0);


break;
default:printf("\n Invalid Choice");
}
}
while(No1 <=3);
getch();
}
int Scan(int a[100])
{
int i;
printf("\nPlease Enter The Limit Of Array A ::\t");
scanf("%d",&No2);
printf("\nPlease Enter The Elements For Array A ::\n");
for(i=0;i<No2;i++)
{
scanf("%d",&a[i]);
}
return (No2);
}
void Lsearch(int a[100],int No2)
{
int i , No3 = 0;
int flag = 0;
printf("\nPlease Enter The Element Which You Want Search ::\t");
scanf("%d",&No3);
for(i=0;i<No2;i++)
{
if(No3 == a[i])
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location With Memory Location :-
(%u)",a[i],(i+1),&a[i]);
}
else
{
printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
}
}
void Bsearch(int a[100],int No2)
{
int i , Ptr , Temp = 0 , No4 = 0;
int Lb = 0 , Mid = 0 , Ub = No2;
int flag = 0;
for(i=1;i<No2;i++)
{
Ptr = 0;
while(Ptr <= (No2 - i))
{
if(a[Ptr] > a[Ptr+1])
{
Temp = a[Ptr];
a[Ptr] = a[Ptr+1];
a[Ptr+1] = Temp;
}
Ptr++;
}
}
printf("\nYour Entered Elements Have Been Sorted Into The Array A ::\n");
for(i=0;i<No2;i++)
{
printf("%d \n",a[i]);
}
Mid = ((Lb + Ub) / 2);
printf("\nPlease Enter The Element Which You Want Search Into The Array A ::\t");
scanf("%d",&No4);
if(No4 == a[Mid])
{
flag = 1;
}
else
{
if(No4 < a[Mid])
{
for(i=Lb;i<Mid;i++)
{
if(No4 == a[i])
{
flag = 1;
break;
}
}
}
else
{
for(i=(Mid+1);i<Ub;i++)
{
if(No4 == a[i])
{
flag = 1;
break;
}
}
}
}
if(flag == 1)
{
printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location With Memory Location :-
(%u)",a[i],(i+1),&a[i]);
}
else
{
printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
}
}
3. Program to sort array using insertion sort algorithm.

include<stdio.h>
#include<conio.h>
int main() {
int i, j, num, temp, arr[20];
clrscr();
printf("Enter total elements: ");
scanf("%d", &num);

printf("Enter %d elements: ", num);


for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}

for (i = 1; i < num; i++) {


temp = arr[i];
j = i - 1;
while ((temp < arr[j]) && (j >= 0)) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}

printf("After Sorting: ");


for (i = 0; i < num; i++) {
printf("%d", arr[i]);
}

return 0;
}
4. Program to sort array using selection sort algorithm.

#include <stdio.h>
#include<conio.h>
int main()
{
int array[20], n, c, d, position, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1 ) ; c++ )


{
position = c;

for ( d = c + 1 ; d < n ; d++ )


{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}
5. Program to sort array using bubble sort algorithm.

/* Bubble sort code */

#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}
6. Program to perform operation on stacks.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define max_size 5
int stack[max_size],top=-1;
/*------ Function Prototype------------*/
void push();
void pop();
void display();

int main()
{
int choice;
do{
//printf("\n");
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;

case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);
return 0;
}
void push() //Inserting element in to the stack
{
int item;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);}}}
7. Program to implement a circular queue.

#include<stdio.h>
#include<conio.h>
#define SIZE 5

void enQueue(int);
void deQueue();
void display();

int cQueue[SIZE], front = -1, rear = -1;

void main()
{
int choice, value;
clrscr();
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("\nEnter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nPlease select the correct choice!!!\n");
}
}
}
void enQueue(int value)
{
if((front == 0 && rear == SIZE - 1) || (front == rear+1))
printf("\nCircular Queue is Full! Insertion not possible!!!\n");
else{
if(rear == SIZE-1 && front != 0)
rear = -1;
cQueue[++rear] = value;
printf("\nInsertion Success!!!\n");
if(front == -1)
front = 0;
}
}
void deQueue()
{
if(front == -1 && rear == -1)
printf("\nCircular Queue is Empty! Deletion is not possible!!!\n");
else{
printf("\nDeleted element : %d\n",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}
void display()
{
if(front == -1)
printf("\nCircular Queue is Empty!!!\n");
else{
int i = front;
printf("\nCircular Queue Elements are : \n");
if(front <= rear){
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
else{
while(i <= SIZE - 1)
printf("%d\t", cQueue[i++]);
i = 0;
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
}
}
8. Program to implement stack using linked list.

#include<stdio.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*top = NULL;

void push(int);
void pop();
void display();

void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
9. Program to implement circular queue using linked list.

/*Circular Queue implementation using linked list


*/
#include<stdio.h>
#include<stdlib.h>
#define que struct queue
#define pf printf
#define sf scanf
struct queue{
int info;
struct queue *link;
};
que *front=NULL,*rear=NULL;
int count=0;
void push(int n)
{
que *newnode;
newnode=(struct queue*)malloc(sizeof(struct queue));
newnode->info=n;
newnode->link=NULL;
if(count==0)
front=newnode;
else
rear->link=newnode;
rear=newnode;
rear->link=front;
count++;
}
int pop(void)
{
int n;
que *temp;
if(count==0)
return (-1);
count--;
if(front==rear)
{
n=front->info;
free(front);
front=NULL;
rear=NULL;
}else
{
temp= front ;
n = temp-> info ;
front = front -> link ;
rear -> link = front ;
free ( temp ) ;
}
return n;
}
void display(void)
{
que *temp;
int i;
if(count==0)
pf("Empty");
else
{
temp=front;
for(i=0;i<count;i++)
{
pf("%d ",temp->info);
temp=temp->link;
}
}
pf("\n");
}
int size(void)
{
return count;
}
int main()
{
int n,ch=10;
while(ch!=0)
{
pf("\n What do you want to do??\n");
pf("1.Push\n");
pf("2.Pop\n");
pf("3.SizeOfQueue\n");
pf("4.Display\n");
pf("0.EXIT\n");
sf("%d",&ch);
switch(ch)
{
case 1:
{
pf("What no. do you want to push in queue\n");
sf("%d",&n);
push(n);
break;
}
case 2:
{
n=pop();
if(n==-1)
pf("Queue is empty\n");
else
pf("Number poped from queue is %d\n",n);
break;
}
case 3:
{
n=size();
pf("Size of queue is %d\n",n);
break;
}
case 4:
{
pf("Queue is -->> ");
display();
}
case 0:
break;
default:
pf("Wrong Choice\n");
break;
}
}
return 0;
}
10. Program of binary search using recursion.

#include<stdio.h>
#include<conio.h>
int main(){

int a[10],i,n,m,c=0,l,u,mid;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements in ascending order: ");


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

printf("Enter the number to be search: ");


scanf("%d",&m);

l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");

return 0;
}

You might also like