Programms
Programms
using bubble sort and then apply binary search high = mid - 1;
program in C else if (keynum > array[mid])
low = mid + 1;
#include <stdio.h> } while (keynum != array[mid] && low <= high);
void main() if (keynum == array[mid])
{ {
int array[10]; printf("SEARCH SUCCESSFUL \n");
int i, j, num, temp, keynum; }
int low, mid, high; else
printf("Enter the value of num \n"); {
scanf("%d", &num); printf("SEARCH FAILED \n");
printf("Enter the elements one by one \n"); }
for (i = 0; i < num; i++) }
{
scanf("%d", &array[i]); Program 2: Write a C Program to Implementing
} the operations of a Queue using Array
printf("Input array elements \n");
for (i = 0; i < num; i++) #include <stdio.h>
{
printf("%d\n", array[i]); #define MAX 50
}
/* Bubble sorting begins */ void insert();
for (i = 0; i < num; i++) void delete();
{ void display();
for (j = 0; j < (num - i - 1); j++) int queue_array[MAX];
{ int rear = - 1;
if (array[j] > array[j + 1]) int front = - 1;
{ main()
temp = array[j]; {
array[j] = array[j + 1]; int choice;
array[j + 1] = temp; while (1)
} {
} printf("1.Insert element to queue \n");
} printf("2.Delete element from queue \n");
printf("Sorted array is...\n"); printf("3.Display all elements of queue \n");
for (i = 0; i < num; i++) printf("4.Quit \n");
{ printf("Enter your choice : ");
printf("%d\n", array[i]); scanf("%d", &choice);
} switch (choice)
printf("Enter the element to be searched \n"); {
scanf("%d", &keynum); case 1:
/* Binary searching begins */ insert();
low = 1; break;
high = num; case 2:
do delete();
{ break;
mid = (low + high) / 2; case 3:
display(); {
break; printf("Queue is : \n");
case 4: for (i = front; i <= rear; i++)
exit(1); printf("%d ", queue_array[i]);
default: printf("\n");
printf("Wrong choice \n"); }
} /* End of switch */ } /* End of display() */
} /* End of while */
} /* End of main() */ Program 3: Write a C Program to Implementing
the operations of a Priority Queue using Array
void insert()
{ #include <stdio.h>
int add_item; #include <stdlib.h>
if (rear == MAX - 1)
printf("Queue Overflow \n"); #define MAX 5
else
{ void insert_by_priority(int);
if (front == - 1) void delete_by_priority(int);
/*If queue is initially empty */ void create();
front = 0; void check(int);
printf("Inset the element in queue : "); void display_pqueue();
scanf("%d", &add_item);
rear = rear + 1; int pri_que[MAX];
queue_array[rear] = add_item; int front, rear;
}
} /* End of insert() */ void main()
{
void delete() int n, ch;
{
if (front == - 1 || front > rear) printf("\n1 - Insert an element into queue");
{ printf("\n2 - Delete an element from queue");
printf("Queue Underflow \n"); printf("\n3 - Display queue elements");
return ; printf("\n4 - Exit");
}
else create();
{
printf("Element deleted from queue is : %d\n", while (1)
queue_array[front]); {
front = front + 1; printf("\nEnter your choice : ");
} scanf("%d", &ch);
} /* End of delete() */
switch (ch)
void display() {
{ case 1:
int i; printf("\nEnter value to be inserted : ");
if (front == - 1) scanf("%d",&n);
printf("Queue is empty \n"); insert_by_priority(n);
else break;
case 2:
printf("\nEnter value to delete : "); for (i = 0; i <= rear; i++)
scanf("%d",&n); {
delete_by_priority(n); if (data >= pri_que[i])
break; {
case 3: for (j = rear + 1; j > i; j--)
display_pqueue(); {
break; pri_que[j] = pri_que[j - 1];
case 4: }
exit(0); pri_que[i] = data;
default: return;
printf("\nChoice is incorrect, Enter a correct }
choice"); }
} pri_que[i] = data;
} }
}
/* Function to delete an element from queue */
/* Function to create an empty priority queue */ void delete_by_priority(int data)
void create() {
{ int i;
front = rear = -1;
} if ((front==-1) && (rear==-1))
{
/* Function to insert value into priority queue */ printf("\nQueue is empty no elements to delete");
void insert_by_priority(int data) return;
{ }
if (rear >= MAX - 1)
{ for (i = 0; i <= rear; i++)
printf("\nQueue overflow no more elements can {
be inserted"); if (data == pri_que[i])
return; {
} for (; i < rear; i++)
if ((front == -1) && (rear == -1)) {
{ pri_que[i] = pri_que[i + 1];
front++; }
rear++;
pri_que[rear] = data; pri_que[i] = -99;
return; rear--;
}
else if (rear == -1)
check(data); front = -1;
rear++; return;
} }
}
/* Function to check priority and place element */ printf("\n%d not found in queue to delete", data);
void check(int data) }
{
int i,j; /* Function to display queue elements */
void display_pqueue() if (rear == MAX - 1)
{ rear = 0;
if ((front == -1) && (rear == -1)) else
{ rear = rear + 1;
printf("\nQueue is empty"); }
return; cqueue_arr[rear] = item ;
} }
for (; front <= rear; front++) /*
{ * Delete from Circular Queue
printf(" %d ", pri_que[front]); */
} void del()
front = 0; {
} if (front == -1)
{
Program 4: Write a C Program to Implementing printf("Queue Underflow\n");
the operations of a Circular Queue using Array return ;
}
/* printf("Element deleted from queue is :
* C Program to Implement Circular Queue %d",cqueue_arr[front]);
*/ if (front == rear)
#include <iostream> {
#define MAX 5 front = -1;
int [MAX]; rear = -1;
rear = front = -1; }
void insert(int); else
void del(); {
void display(); if (front == MAX - 1)
struct Circular_Queue front = 0;
{ else
front = front + 1;
int *cqueue_arr; }
int front, rear; }
} /*
void insert(int item) * Display Circular Queue
{ */
if ((front == 0 && rear == MAX-1) || (front == void display()
rear+1)) {
{ int front_pos = front, rear_pos = rear;
printf("Queue Overflow \n"); if (front == -1)
return; {
} printf("Queue is empty\n");
if (front == -1) return;
{ }
front = 0; printf("Queue elements :\n");
rear = 0; if (front_pos <= rear_pos)
} {
else while (front_pos <= rear_pos)
{ {
printf(cqueue_arr[front_pos]<<" "; cq.display();
front_pos++; break;
} case 4:
} break;
else default:
{ printf("Wrong choice\n");
while (front_pos <= MAX - 1) }/*End of switch*/
{ }
printf(“%d”,cqueue_arr[front_pos]); while(choice != 4);
front_pos++; return 0;
} }
front_pos = 0;
while (front_pos <= rear_pos) Program 5 : Write a C Program to Implementing
{ the operations of a Stack using Array
printf(“%d”,cqueue_arr[front_pos]);
front_pos++; #include <stdio.h>
} #include <stdlib.h>
}
printf(“\n”); #define MAX 5
} #define EMPTY "Stack is Empty"
}; #define OVERFLOW "Stack Overflow"
/* #define ISOVERFLOW top >= MAX - 1
* Main /*Macro to find whether the stack is full*/
*/ #define ISEMPTY top == -1 /*Macro to find
int main() whether the stack is empty*/
{
int choice, item; void push(int);
Circular_Queue cq; void pop();
do void display();
{ void stacksize();
printf("1.Insert\n"); void destroy();
printf("2.Delete\n"); void stackfull();
printf("3.Display\n");
printf("4.Quit\n"); int top = -1;
printf("Enter your choice : "); int stack[MAX];
printf(“%d”,choice);
switch(choice) void main()
{ {
case 1: int choice, value;
printf("Input the element for insertion in queue :
"); while (1)
printf(“%d”,item); {
cq.insert(item); printf("Enter Your choice:\n");
break; printf("1.PUSH\n2.POP\n3.DISPLAY\n4.STAC
case 2: KSIZE\n5.DESTROY\n6.SATCKFULL
cq.del(); CHECK\n7.EXIT");
break; scanf("%d", &choice);
case 3: switch (choice)
{ printf(EMPTY);
case 1: return;
printf("enter the value to be pushed on to the }
stack"); printf("the popped element is %d", stack[top]);
scanf("%d", &value); top--;
push(value); }
continue;
case 2: /*Function to display all the elements in the
pop(); stack*/
continue;
case 3: void display()
display(); {
continue; int temp = top;
case 4:
stacksize(); if (ISEMPTY)
continue; {
case 5: printf(EMPTY);
destroy(); return;
continue; }
case 6: while (temp + 1)
stackfull(); {
continue; printf("%d\n", stack[temp]);
case 7: temp--;
exit(0); }
default: }
printf("YOU HAVE ENTERD A WRONG
CHOICE"); /* Function to check whether the stack is full
} using macro */
} void stackfull()
} {
int temp = top, count = 0;
/*Function to add an element into the stack*/
void push(int value) if (ISEMPTY)
{ {
if (ISOVERFLOW) printf(EMPTY);
{ return;
printf(OVERFLOW); }
return; while (temp + 1)
} {
top++; printf("%d\n", stack[temp]);
stack[top] = value; temp--;
} count++;
}
/*Function to delete an element from the stack*/ if (count >= MAX)
void pop() {
{ printf("Stack is full");
if (ISEMPTY) }
{ else
{ }
printf("there are %d more spaces in the stack",
(MAX-count)); int pop()
} {
} return stack[top--];
}
/* Function to return the size of the stack */
void stacksize() int main()
{ {
int temp = top, count = 0; char exp[20];
if (ISEMPTY) char *e;
{ int n1,n2,n3,num;
printf(EMPTY); printf("Enter the expression :: ");
return; scanf("%s",exp);
} e = exp;
while (temp + 1) while(*e != '\0')
{ {
temp--; if(isdigit(*e))
count++; {
} num = *e - 48;
printf("the size of the stack is %d\n", count); push(num);
} }
else
/* Function to delete all the elements in the stack {
*/ n1 = pop();
n2 = pop();
void destroy() switch(*e)
{ {
if (ISEMPTY) case '+':
{ {
printf("nothing is there to destroy"); n3 = n1 + n2;
} break;
while (top != -1) }
{ case '-':
pop(); {
} n3 = n2 - n1;
} break;
}
Program 6: C Program to Evaluate POSTFIX case '*':
Expression Using Stack {
n3 = n1 * n2;
#include<stdio.h> break;
int stack[20]; }
int top = -1; case '/':
{
void push(int x) n3 = n2 / n1;
{ break;
stack[++top] = x; }
} scanf("%s",exp);
push(n3); printf("\n");
} e = exp;
e++;
} while(*e != '\0')
printf("\nThe result of expression %s = {
%d\n\n",exp,pop()); if(isalnum(*e))
return 0; printf("%c ",*e);
} else if(*e == '(')
push(*e);
Program 7: C Program to Convert Infix to Postfix else if(*e == ')')
using Stack {
while((x = pop()) != '(')
#include<stdio.h> printf("%c ", x);
#include<ctype.h> }
else
char stack[100]; {
int top = -1; while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
void push(char x) push(*e);
{ }
stack[++top] = x; e++;
} }
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");