Ques
Ques
Ques
#include <conio.h>
#include <stdio.h>
int main()
clrscr();
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &position);
if(position >= n + 1)
else {
printf("Resultant array:\n");
printf("%d\n", array[c]);
getch();
return 0;
}
OUTPUT
Q6. Create a program to perform the linear search.
#include <conio.h>
#include <stdio.h>
int main()
int A[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
clrscr();
scanf("%d", &search);
if(A[c] == search) {
count++;
break;
if(count == 0)
getch();
return 0;
}
OUTPUT
Q7. Create a program to perform the linear search and perform duplicate elements.
#include <conio.h>
#include <stdio.h>
int main()
clrscr();
scanf("%d", &search);
if(A[c] == search) {
count++;
if(count == 0)
else
getch();
return 0;
}
OUTPUT
Q8. Create a program to perform the binary search.
#include <conio.h>
#include <stdio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &el);
first = 0;
last = n - 1;
first = mid + 1;
break;
} else {
last = mid - 1;
}
if(first > last)
printf("Search unsuccessgul Ement Not Found! %d isn't present in the list\n", el);
getch();
}
OUTPUT
Q9. Create a menu driven program to create a stack using array and perform push (), pop () and display () function.
#include <conio.h>
#include <stdio.h>
int s[5];
void main()
int ans;
void push(int);
int pop();
void display();
clrscr();
do {
printf("Menu\n");
printf("Enteryour choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: {
if(top == 4) {
printf("Stack Overflow\n");
} else {
scanf("%d", &el);
push(el);
break;
case 2: {
if(top == -1) {
printf("Stack under flow\n");
} else {
break;
case 3:
display();
break;
default:
scanf("%d", &ans);
} while(ans != 1);
getch();
void push(int x)
top = top + 1;
s[top] = x;
int pop()
int y;
y = s[top];
top = top - 1;
return y;
void display()
{
int i;
}
OUTPUT
Q10. Create a menu driven program to create a stack using array and perform push(), pop() and display() functions.
#include <conio.h>
#include <stdio.h>
void push(void);
void pop(void);
void display(void);
int main()
clrscr();
top = -1;
scanf("%d", &n);
printf("\n\t--------------------------------");
do {
scanf("%d", &choice);
switch(choice) {
case 1: {
push();
break;
}
case 2: {
pop();
break;
case 3: {
display();
break;
case 4: {
break;
default: {
} while(choice != 4);
getch();
return 0;
void push()
if(top >= n - 1) {
} else {
scanf("%d", &x);
top++;
stack[top] = x;
}
void pop()
} else {
top--;
void display()
if(top >= 0) {
printf("\n%d", stack[i]);
} else {
}
OUTPUT
Q11. Create a menu driven program to create a queue using array and perform add(), delete() and display()
functions.
#include <conio.h>
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
main()
int choice;
clrscr();
while(1) {
printf("4.Quit \n");
scanf("%d", &choice);
switch(choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
getch();
void insert()
int add_item;
if(rear == MAX - 1)
else {
if(front == -1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
void delete()
return;
} else {
front = front + 1;
}
void display()
int i;
if(front == -1)
else {
printf("Queue is : \n");
printf("\n");
}
OUTPUT
Q12.Write a program to create a stack using structure and perform push(), pop() and display() function.
#include <conio.h>
#include <stdio.h>
#define MAXSIZE 5
struct stack {
int stk[MAXSIZE];
int top;
};
STACK s;
void push(void);
int pop(void);
void display(void);
void main()
int choice;
int option = 1;
clrscr();
s.top = -1;
printf("STACK OPERATION\n");
while(option) {
scanf("%d", &choice);
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
void push()
int num;
printf("Stack is Full\n");
return;
} else {
scanf("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
return;
int pop()
int num;
if(s.top == -1) {
printf("Stack is Empty\n");
return (s.top);
} else {
num = s.stk[s.top];
s.top = s.top - 1;
return (num);
void display()
int i;
if(s.top == -1) {
printf("Stack is empty\n");
return;
} else {
printf("%d\n", s.stk[i]);
printf("\n");
}
OUTPUT
Q13.Write a program to create a queue using structure and perform push(), pop() and display() function.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
// stack as 0
stack->capacity = capacity;
stack->top = -1;
return stack;
{
return stack->top == -1;
if(isFull(stack))
return;
stack->array[++stack->top] = item;
if(isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
if(isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
void display()
int a;
a = top;
int main()
Int a, b;
scanf("%d", &choice);
switch(choice) {
case 1: {
push(stack, a);
} break;
case 2: {
pop(stack, b);
} break;
case 3:
display();
break;
case 4:
return;
}
return 0;
}
Q14. Create a menu driven program to create a single link list for adding an element and deleting an element to
/from the begging, end and from any position, traversing the list and searching the element.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
``void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node {
int info;
};
int choice;
clrscr();
while(1) {
printf("---------------------------------------\n");
printf("\n--------------------------------------\n");
scanf("%d", &choice);
switch(choice) {
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
break;
getch();
return 0;
void create()
if(temp == NULL) {
exit(0);
scanf("%d", &temp->info);
temp->next = NULL;
if(start == NULL) {
start = temp;
} else {
ptr = start;
while(ptr->next != NULL) {
ptr = ptr->next;
ptr->next = temp;
void display()
{
struct node* ptr;
if(start == NULL) {
printf("\nList is empty:\n");
return;
} else {
ptr = start;
while(ptr != NULL) {
printf("%d\t", ptr->info);
ptr = ptr->next;
void insert_begin()
if(temp == NULL) {
return;
scanf("%d", &temp->info);
temp->next = NULL;
if(start == NULL) {
start = temp;
} else {
temp->next = start;
start = temp;
void insert_end()
{
if(temp == NULL) {
return;
scanf("%d", &temp->info);
temp->next = NULL;
if(start == NULL) {
start = temp;
} else {
ptr = start;
while(ptr->next != NULL) {
ptr = ptr->next;
ptr->next = temp;
void insert_pos()
int i, pos;
if(temp == NULL) {
return;
scanf("%d", &pos);
scanf("%d", &temp->info);
temp->next = NULL;
if(pos == 0) {
temp->next = start;
start = temp;
} else {
ptr = ptr->next;
if(ptr == NULL) {
return;
temp->next = ptr->next;
ptr->next = temp;
} // end of else
void delete_begin()
if(ptr == NULL) {
printf("\nList is Empty:\n");
return;
} else {
ptr = start;
start = start->next;
free(ptr);
void delete_end()
{
struct node *temp, *ptr;
if(start == NULL) {
printf("\nList is Empty:");
exit(0);
ptr = start;
start = NULL;
free(ptr);
} else {
ptr = start;
while(ptr->next != NULL) {
temp = ptr;
ptr = ptr->next;
temp->next = NULL;
free(ptr);
void delete_pos()
int i, pos;
if(start == NULL) {
exit(0);
} else {
scanf("%d", &pos);
if(pos == 0) {
ptr = start;
start = start->next;
printf("\nThe deleted element is:%d\t", ptr->info);
free(ptr);
} else {
ptr = start;
temp = ptr;
ptr = ptr->next;
if(ptr == NULL) {
return;
temp->next = ptr->next;
free(ptr);
}
Q15. Create a menu driven program to create a double link list for adding an element and deleting an element to
/from the beginning, end and from any position specified by the user, traversing the list from beginning to end as
well as from end to beginning, sorting the list and searching the element.
#include <conio.h>
#include <stdio.h>
struct node {
int data;
void create()
int el;
scanf("%d", &el);
fn->data = el;
fn->prev = NULL;
fn->next = NULL;
start = fn;
last = fn;
void add_at_bg()
temp = start;
new_node->next = temp;
new_node->prev = NULL;
printf(" \nEnter the data to the node atthe begining\n");
scanf("%d", &new_node->data);
temp->prev = new_node;
start = new_node;
void add_at_end()
temp = last;
new_node->next = NULL;
scanf("%d", &new_node->data);
temp->next = new_node;
new_node->prev = temp;
last = new_node;
void display_b_to_e()
if(start == NULL)
printf("Linklist is empty");
else {
temp = start;
while(temp != NULL) {
temp = temp->next;
}
void display_e_to_b()
if(last == NULL)
printf("Linklist is empty");
if(start == last)
printf("%d\n", last->data);
else {
temp = last;
while(temp != NULL) {
temp = temp->prev;
void delete_from_bg()
temp = start;
start = start->next;
start->prev = NULL;
free(temp);
void del_end()
if(last == NULL)
else {
temp = last;
last->prev->next = NULL;
free(temp);
}
void del_pos()
int pos, i;
temp = start;
scanf("%d", &pos);
temp = temp->next;
del_node = temp->next;
if(del_node == last)
del_end();
else {
temp->next = temp->next->next;
temp->next->prev = temp;
free(del_node);
void add_pos()
scanf("%d", &pos);
if(pos == 1)
add_at_bg();
else {
temp = start;
scanf("%d", &ele);
new_node->data = ele;
temp = temp->next;
if(temp->next == NULL) {
// add_at_end();
temp->next = new_node;
new_node->prev = temp;
new_node->next = NULL;
} else {
new_node->next = temp->next;
temp->next = new_node;
new_node->next->prev = new_node;
new_node->prev = temp;
void update()
temp = start;
scanf("%d", &pos);
scanf("%d", &up);
if(pos == 1) {
temp = start;
temp->data = up;
} else {
for(i = 0; i < pos - 2; i++)
temp = temp->next;
update_node = temp->next;
update_node->data = up;
void main()
int ch;
// struct node;
clrscr();
do {
printf("Menu\n");
printf("1 to create\n");
printf("11 to exit\n");
scanf("%d", &ch);
switch(ch) {
case 1:
create();
break;
case 2:
add_at_bg();
break;
case 3:
add_at_end();
break;
case 4:
delete_from_bg();
break;
case 5:
del_end();
break;
case 6:
add_pos();
break;
case 7:
del_pos();
break;
case 8:
update();
break;
case 9:
display_b_to_e();
break;
case 10:
display_e_to_b();
break;
case 11:
exit(1);
default:
printf("wrong choice\n");
} while(1);
getch();
}
Q16. Create a menu driven program to create a circular link list and perform the functions like adding an element
and delete elements and display the elements.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
/*
*/
struct node {
int data;
} * head;
/*
*/
void displayList();
int main()
clrscr();
head = NULL;
/*
*/
while(choice != 0) {
printf("============================================\n");
printf("============================================\n");
printf("1. Create List\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
scanf("%d", &choice);
switch(choice) {
case 1:
scanf("%d", &n);
createList(n);
break;
case 2:
displayList();
break;
case 0:
break;
default:
printf("\n\n\n\n\n");
getch();
return 0;
/**
*/
void createList(int n)
{
int i, data;
if(n >= 1) {
/*
*/
scanf("%d", &data);
head->data = data;
head->next = NULL;
prevNode = head;
/*
*/
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode;
prevNode->next = head;
/**
*/
void displayList()
int n = 1;
if(head == NULL) {
printf("List is empty.\n");
} else {
current = head;
do {
current = current->next;
n++;
} while(current != head);
}
Q17. Program to create a binary search tree and perform a functions of addition and deletion of the elements inth
etree.
#include <conio.h>
#include <malloc.h>
#include <stdio.h>
struct node {
int info;
struct node* lchild;
struct node* rchild;
} * root;
if(parent == NULL)
root = tmp;
else if(item < parent->info)
parent->lchild = tmp;
else
parent->rchild = tmp;
} /*End of insert()*/
/*Initialize child*/
if(loc->lchild != NULL) /*item to be deleted has lchild */
child = loc->lchild;
else /*item to be deleted has rchild */
child = loc->rchild;
suc->lchild = loc->lchild;
suc->rchild = loc->rchild;
} /*End of case_c()*/
main()
{
int choice, num;
clrscr();
root = NULL;
while(1) {
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Postorder Traversal\n");
printf("6.Display\n");
printf("7.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the number to be inserted : ");
scanf("%d", &num);
insert(num);
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d", &num);
del(num);
break;
case 3:
display(root);
break;
default:
printf("Wrong choice\n");
} /*End of switch */
} /*End of while */
getch();
} /*End of main()*/
Q18.Create a program to traverse the tree recursively in inorder, preorder and postorder.
#include <malloc.h>
#include <stdio.h>
struct node {
int info;
} * root;
*loc = NULL;
*par = NULL;
return;
*loc = root;
*par = NULL;
return;
ptr = root->lchild;
else
ptr = root->rchild;
ptrsave = root;
while(ptr != NULL) {
if(item == ptr->info) {
*loc = ptr;
*par = ptrsave;
return;
ptrsave = ptr;
ptr = ptr->lchild;
else
ptr = ptr->rchild;
} /*End of while */
*par = ptrsave;
} /*End of find()*/
if(location != NULL) {
return;
tmp->info = item;
tmp->lchild = NULL;
tmp->rchild = NULL;
if(parent == NULL)
root = tmp;
parent->lchild = tmp;
else
parent->rchild = tmp;
} /*End of insert()*/
root = NULL;
par->lchild = NULL;
else
par->rchild = NULL;
} /*End of case_a()*/
/*Initialize child*/
child = loc->lchild;
child = loc->rchild;
root = child;
par->lchild = child;
par->rchild = child;
} /*End of case_b()*/
ptrsave = loc;
ptr = loc->rchild;
while(ptr->lchild != NULL) {
ptrsave = ptr;
ptr = ptr->lchild;
suc = ptr;
parsuc = ptrsave;
case_a(parsuc, suc);
else
case_b(parsuc, suc);
root = suc;
par->lchild = suc;
else
par->rchild = suc;
suc->lchild = loc->lchild;
suc->rchild = loc->rchild;
} /*End of case_c()*/
if(root == NULL) {
printf("Tree empty");
return 0;
}
find(item, &parent, &location);
if(location == NULL) {
return 0;
case_a(parent, location);
case_b(parent, location);
case_b(parent, location);
case_c(parent, location);
free(location);
} /*End of del()*/
if(root == NULL) {
printf("Tree is empty");
return 0;
if(ptr != NULL) {
preorder(ptr->lchild);
preorder(ptr->rchild);
} /*End of preorder()*/
if(root == NULL) {
printf("Tree is empty");
return;
if(ptr != NULL) {
inorder(ptr->lchild);
inorder(ptr->rchild);
} /*End of inorder()*/
if(root == NULL) {
printf("Tree is empty");
return;
if(ptr != NULL) {
postorder(ptr->lchild);
postorder(ptr->rchild);
} /*End of postorder()*/
int i;
if(ptr != NULL) {
printf("\n");
printf(" ");
printf("%d", ptr->info);
} /*End of if*/
} /*End of display()*/
main()
clrscr();
root = NULL;
while(1) {
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Postorder Traversal\n");
printf("6.Display\n");
printf("7.Quit\n");
scanf("%d", &choice);
switch(choice) {
case 1:
scanf("%d", &num);
insert(num);
break;
case 2:
scanf("%d", &num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root, 1);
break;
case 7:
break;
default:
printf("Wrong choice\n");
} /*End of switch */
} /*End of while */
getch();
} /*End of main()*/
Q19. Create a program to traverse a tree without recursively.
#include<stdio.h>
#include<stdlib.h>
#define bool int
while (!done)
{
/* Reach the left most tNode of the current tNode */
if(current != NULL)
{
/* place pointer to a tree node on the stack before traversing
the node's left subtree */
push(&s, current);
current = current->left;
}
{
if (!isEmpty(s))
{
current = pop(&s);
printf("%d ", current->data);
if(new_tNode == NULL)
{
printf("Stack Overflow \n");
getchar();
exit(0);
}
return(tNode);
}
inOrder(root);
getchar();
return 0;
}
Q22. Create a program for bubble sort.
#include <conio.h>
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j;
int i;
printf("\n");
clrscr();
bubbleSort(arr, n);
printArray(arr, n);
getch();
return 0;
}
Q23.Create a program for insertion sort.
#include <conio.h>
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j;
int i;
printf("\n");
int main()
{
clrscr();
bubbleSort(arr, n);
printArray(arr, n);
getch();
return 0;
}
Q25.Create a program for selection sort.
#include <conio.h>
#include <stdio.h>
int main()
clrscr();
scanf("%d", &n);
scanf("%d", &array[c]);
position = c;
position = d;
if(position != c) {
swap = array[c];
array[c] = array[position];
array[position] = swap;
printf("%d\n", array[c]);
getch();
return 0;
}
Q26. Create a program for merging sort.
#include <conio.h>
#include <stdio.h>
int main()
{
int a[100], aux[100], n, i, d, swap;
clrscr();
merge_sort(0, n - 1, a, aux);
#include <conio.h>
#include <stdio.h>
char stack[20];
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;
return 1;
return 2;
main()
char exp[20];
char *e, x;
clrscr();
printf("Enter the expression :: ");
scanf("%s", exp);
e = exp;
while(*e != '\0') {
if(isalnum(*e))
printf("%c", *e);
push(*e);
printf("%c", x);
} else {
printf("%c", pop());
push(*e);
e++;
while(top != -1) {
printf("%c", pop());
getch();
#include <conio>
#include <stdio.h>
int stack[20];
void push(int x)
stack[++top] = x;
}
int pop()
return stack[top--];
int main()
char exp[20];
char* e;
clrscr();
scanf("%s", exp);
e = exp;
while(*e != '\0') {
if(isdigit(*e)) {
num = *e - 48;
push(num);
} else {
n1 = pop();
n2 = pop();
switch(*e) {
case '+': {
n3 = n1 + n2;
break;
case '-': {
n3 = n2 - n1;
break;
case '*': {
n3 = n1 * n2;
break;
case '/': {
n3 = n2 / n1;
break;
push(n3);
e++;
getch();
return 0;