You are on page 1of 78

Q5. Create a program to delete an element at a particular location in an array defined by the user.

#include <conio.h>

#include <stdio.h>

int main()

int array[100], position, c, n;

clrscr();

printf("Enter number of elements in array\n");

scanf("%d", &n);

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

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

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

printf("Enter the location where you wish to delete element\n");

scanf("%d", &position);

if(position >= n + 1)

printf("Deletion not possible.\n");

else {

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

array[c] = array[c + 1];

printf("Resultant array:\n");

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

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 search, c, n, count = 0;

int A[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

clrscr();

printf("\nEnter a number to search\n");

scanf("%d", &search);

for(c = 0; c < 10; c++) {

if(A[c] == search) {

printf("%d is present at location %d.\n", search, c + 1);

count++;

break;

if(count == 0)

printf("%d isn't present in the array.\n", search);

for(c = 0; c < 10; c++) {

printf(" %d ", A[c]);

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()

int search, c, n, count = 0;

int A[10] = { 1, 2, 3, 2, 5, 2, 7,2, 9, 2 };

clrscr();

printf("\nEnter a number to search:\n");

scanf("%d", &search);

for(c = 0; c < 10; c++) {

if(A[c] == search) {

printf("%d is present at location %d.\n", search, c + 1);

count++;

if(count == 0)

printf("%d isn't present in the array.\n", search);

else

printf("%d is present %d times in the array.\n", search, count);

for(c = 0; c < 10; c++) {

printf(" %d ", A[c]);

getch();

return 0;

}
OUTPUT
Q8. Create a program to perform the binary search.

#include <conio.h>

#include <stdio.h>

void main()

int c, first, last, mid, n, el, array[50];

clrscr();

printf("Enter the number of elements(Max=50)\n");

scanf("%d", &n);

printf("Enter the elements in assending order\n");

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

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

printf("Enter the value to find");

scanf("%d", &el);

first = 0;

last = n - 1;

mid = (first + last) / 2;

while(first <= last) {

if(el > array[mid]) {

first = mid + 1;

} else if(array[mid] == el) {

printf("%d found at location %d\n", el, mid + 1);

break;

} else {

last = mid - 1;

mid = (first + last) / 2;

}
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 top = -1;

int s[5];

void main()

int ch, el;

int ans;

void push(int);

int pop();

void display();

clrscr();

do {

printf("Menu\n");

printf("press 1 for push()\n");

printf("press 2 for pop[()\n");

printf("press 3 for display()\n");

printf("Enteryour choice\n");

scanf("%d", &ch);

switch(ch) {

case 1: {

if(top == 4) {

printf("Stack Overflow\n");

} else {

printf("Enter the element to be pushed \n");

scanf("%d", &el);

push(el);

break;

case 2: {

if(top == -1) {
printf("Stack under flow\n");

} else {

printf("The deleted elemnet is %d \n", pop());

break;

case 3:

display();

break;

default:

printf("wrong choice \n");

printf("\n Do you want to continue 0 for yes and 1 for no \n");

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;

printf("The elements in the stack are: \n");

for(i = 0; i <= top; i++)

printf(" %d ", s[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>

int stack[100], choice, n, top, x, i;

void push(void);

void pop(void);

void display(void);

int main()

clrscr();

top = -1;

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d", &n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t--------------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do {

printf("\n Enter the Choice:");

scanf("%d", &choice);

switch(choice) {

case 1: {

push();

break;
}

case 2: {

pop();

break;

case 3: {

display();

break;

case 4: {

printf("\n\t EXIT POINT ");

break;

default: {

printf("\n\t Please Enter a Valid Choice(1/2/3/4)");

} while(choice != 4);

getch();

return 0;

void push()

if(top >= n - 1) {

printf("\n\tSTACK is over flow");

} else {

printf(" Enter a value to be pushed:");

scanf("%d", &x);

top++;

stack[top] = x;

}
void pop()

if(top <= -1) {

printf("\n\t Stack is under flow");

} else {

printf("\n\t The popped elements is %d", stack[top]);

top--;

void display()

if(top >= 0) {

printf("\n The elements in STACK \n");

for(i = top; i >= 0; i--)

printf("\n%d", stack[i]);

printf("\n Press Next Choice");

} else {

printf("\n The STACK is empty");

}
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];

int rear = -1;

int front = -1;

main()

int choice;

clrscr();

while(1) {

printf("1.Insert element to queue \n");

printf("2.Delete element from queue \n");

printf("3.Display all elements of queue \n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice) {

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:
exit(1);

default:

printf("Wrong choice \n");

getch();

void insert()

int add_item;

if(rear == MAX - 1)

printf("Queue Overflow \n");

else {

if(front == -1)

/*If queue is initially empty */

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

void delete()

if(front == -1 || front > rear) {

printf("Queue Underflow \n");

return;

} else {

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

}
void display()

int i;

if(front == -1)

printf("Queue is empty \n");

else {

printf("Queue is : \n");

for(i = front; i <= rear; i++)

printf("%d ", queue_array[i]);

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;

};

typedef struct stack STACK;

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) {

printf(" 1 --> PUSH \n");

printf(" 2 --> POP \n");

printf(" 3 --> DISPLAY \n");

printf(" 4 --> EXIT \n");

printf("Enter your choice\n");

scanf("%d", &choice);

switch(choice) {

case 1:
push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

return;

/* Function to add an element to the stack */

void push()

int num;

if(s.top == (MAXSIZE - 1)) {

printf("Stack is Full\n");

return;

} else {

printf("Enter the element to be pushed\n");

scanf("%d", &num);

s.top = s.top + 1;

s.stk[s.top] = num;

return;

/* Function to delete an element from the stack */

int pop()

int num;

if(s.top == -1) {

printf("Stack is Empty\n");
return (s.top);

} else {

num = s.stk[s.top];

printf("poped element is = %dn", s.stk[s.top]);

s.top = s.top - 1;

return (num);

/* Function to display the status of the stack */

void display()

int i;

if(s.top == -1) {

printf("Stack is empty\n");

return;

} else {

printf("\n The status of the stack is \n");

for(i = s.top; i >= 0; i--) {

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.

// C program for array implementation of stack

#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

// A structure to represent a stack

struct Stack {

int top;

unsigned capacity;

int* array;

};

// function to create a stack of given capacity. It initializes size of

// stack as 0

struct Stack* createStack(unsigned capacity)

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (int*)malloc(stack->capacity * sizeof(int));

return stack;

// Stack is full when top is equal to the last index

int isFull(struct Stack* stack)

return stack->top == stack->capacity - 1;

// Stack is empty when top is equal to -1

int isEmpty(struct Stack* stack)

{
return stack->top == -1;

// Function to add an item to stack. It increases top by 1

void push(struct Stack* stack, int item)

if(isFull(stack))

return;

stack->array[++stack->top] = item;

printf("%d pushed to stack\n", item);

// Function to remove an item from stack. It decreases top by 1

int pop(struct Stack* stack)

if(isEmpty(stack))

return INT_MIN;

return stack->array[stack->top--];

// Function to return the top from stack without removing it

int peek(struct Stack* stack)

if(isEmpty(stack))

return INT_MIN;

return stack->array[stack->top];

void display()

int a;

a = top;

for(int i = 0; i < a; i++)

printf(" %d " & array[i]);


}

// Driver program to test above functions

int main()

Int a, b;

struct Stack* stack = createStack(100);

printf(" 1 --> PUSH \n");

printf(" 2 --> POP \n");

printf(" 3 --> DISPLAY \n");

printf(" 4 --> EXIT \n");

printf("Enter your choice\n");

scanf("%d", &choice);

switch(choice) {

case 1: {

printf(“Enter the value to insert queue :”);

scanf(“% d”, &a);

push(stack, a);

} break;

case 2: {

printf(“Enter the value to insert queue :”);

scanf(“% d”, &b);

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;

struct node* next;

};

struct node* start = NULL;

int main() // main() starts

int choice;

clrscr();

while(1) {

printf("\n***SINGLE LINKED LIST OPERATIONS:****\n");

printf("\n MENU \n");

printf("---------------------------------------\n");

printf("\n 1.Create \n");

printf("\n 2.Display \n");

printf("\n 3.Insert at the beginning \n");

printf("\n 4.Insert at the end \n");

printf("\n 5.Insert at specified position \n");


printf("\n 6.Delete from beginning \n");

printf("\n 7.Delete from the end \n");

printf("\n 8.Delete from specified position \n");

printf("\n 9.Exit \n");

printf("\n--------------------------------------\n");

printf("Enter your choice:\t");

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:

printf("\n Wrong Choice:\n");

break;

getch();

return 0;

void create()

struct node *temp, *ptr;

temp = (struct node*)malloc(sizeof(struct node));

if(temp == NULL) {

printf("\nOut of Memory Space:\n");

exit(0);

printf("\nEnter the data value for the node:\t");

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;

printf("\nThe List elements are:\n");

while(ptr != NULL) {

printf("%d\t", ptr->info);

ptr = ptr->next;

void insert_begin()

struct node* temp;

temp = (struct node*)malloc(sizeof(struct node));

if(temp == NULL) {

printf("\nOut of Memory Space:\n");

return;

printf("\nEnter the data value for the node:\t");

scanf("%d", &temp->info);

temp->next = NULL;

if(start == NULL) {

start = temp;

} else {

temp->next = start;

start = temp;

void insert_end()
{

struct node *temp, *ptr;

temp = (struct node*)malloc(sizeof(struct node));

if(temp == NULL) {

printf("\nOut of Memory Space:\n");

return;

printf("\nEnter the data value for the node:\t");

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()

struct node *ptr, *temp;

int i, pos;

temp = (struct node*)malloc(sizeof(struct node));

if(temp == NULL) {

printf("\nOut of Memory Space:\n");

return;

printf("\nEnter the position for the new node to be inserted:\t");

scanf("%d", &pos);

printf("\nEnter the data value of the node:\t");

scanf("%d", &temp->info);
temp->next = NULL;

if(pos == 0) {

temp->next = start;

start = temp;

} else {

for(i = 0, ptr = start; i < pos - 1; i++) {

ptr = ptr->next;

if(ptr == NULL) {

printf("\nPosition not found:[Handle with care]\n");

return;

temp->next = ptr->next;

ptr->next = temp;

} // end of else

void delete_begin()

struct node* ptr;

if(ptr == NULL) {

printf("\nList is Empty:\n");

return;

} else {

ptr = start;

start = start->next;

printf("\nThe deleted element is :%d\t", ptr->info);

free(ptr);

void delete_end()

{
struct node *temp, *ptr;

if(start == NULL) {

printf("\nList is Empty:");

exit(0);

} else if(start->next == NULL) {

ptr = start;

start = NULL;

printf("\nThe deleted element is:%d\t", ptr->info);

free(ptr);

} else {

ptr = start;

while(ptr->next != NULL) {

temp = ptr;

ptr = ptr->next;

temp->next = NULL;

printf("\nThe deleted element is:%d\t", ptr->info);

free(ptr);

void delete_pos()

int i, pos;

struct node *temp, *ptr;

if(start == NULL) {

printf("\nThe List is Empty:\n");

exit(0);

} else {

printf("\nEnter the position of the node to be deleted:\t");

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;

for(i = 0; i < pos; i++) {

temp = ptr;

ptr = ptr->next;

if(ptr == NULL) {

printf("\nPosition not Found:\n");

return;

temp->next = ptr->next;

printf("\nThe deleted element is:%d\t", ptr->info);

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 {

struct node* prev;

int data;

struct node* next;

} * start, *last, *new_node, *temp, *fn;

void create()

int el;

// struct node * new_node;

fn = (struct node*)malloc(sizeof(struct node));

printf("\n Enterthe value of node");

scanf("%d", &el);

fn->data = el;

fn->prev = NULL;

fn->next = NULL;

start = fn;

last = fn;

void add_at_bg()

struct node* new_node;

new_node = (struct node*)malloc(sizeof(struct node));

temp = (struct node*)malloc(sizeof(struct node));

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()

struct node* new_node;

new_node = (struct node*)malloc(sizeof(struct node));

temp = (struct node*)malloc(sizeof(struct node));

temp = last;

new_node->next = NULL;

printf(" \nEnter the data to the node at the end\n");

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 {

printf("\n The Linklist from begining to end:\n");

temp = start;

while(temp != NULL) {

printf(" %d ", temp->data);

temp = temp->next;

}
void display_e_to_b()

if(last == NULL)

printf("Linklist is empty");

if(start == last)

printf("%d\n", last->data);

else {

printf("\n The Linklist in reverse order :\n");

temp = last;

while(temp != NULL) {

printf(" %d ", temp->data);

temp = temp->prev;

void delete_from_bg()

temp = start;

printf("\nDeleted element is %d", start->data);

start = start->next;

start->prev = NULL;

free(temp);

void del_end()

if(last == NULL)

printf("Link list is empty\n");

else {

temp = last;

last->prev->next = NULL;

printf("deleted value is %d \n", temp->data);

free(temp);
}

void del_pos()

struct node* del_node;

int pos, i;

temp = start;

del_node = (struct node*)malloc(sizeof(struct node));

printf("enter the pos from where you want to delete an element\n");

scanf("%d", &pos);

for(i = 0; i < pos - 2; i++)

temp = temp->next;

del_node = temp->next;

if(del_node == last)

del_end();

else {

temp->next = temp->next->next;

temp->next->prev = temp;

printf("the deleted value is->%d", del_node->data);

free(del_node);

void add_pos()

struct node* new_node;

int pos, i, ele;

new_node = (struct node*)malloc(sizeof(struct node));

printf("enter the pos from where you want to add an element\n");

scanf("%d", &pos);

if(pos == 1)

add_at_bg();

else {
temp = start;

printf("enter the element for the new node\n");

scanf("%d", &ele);

new_node->data = ele;

for(i = 0; i < pos - 2; i++)

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()

struct node* update_node;

int pos, i, up;

temp = start;

// update_node=(struct node*)malloc(sizeof(struct node));

printf("enter the pos from where you want to update an element\n");

scanf("%d", &pos);

printf("enter the data to be updated\n");

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("2 to add at front\n");

printf("3 to add at end\n");

printf("4 to delete from front\n");

printf("5 to delete from end\n");

printf("6 to add to a particular position\n");

printf("7 to delete from a particular position\n");

printf("8 to update a particular position\n");

printf("9 to traverse and display from beg to end\n");

printf("10 to traverse and display from end to begining\n");

printf("11 to exit\n");

printf("enter your choice\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>

/*

* Basic structure of Node

*/

struct node {

int data;

struct node* next;

} * head;

/*

* Functions used in this program

*/

void createList(int n);

void displayList();

int main()

int n, data, choice = 1;

clrscr();

head = NULL;

/*

* Run forever until user chooses 0

*/

while(choice != 0) {

printf("============================================\n");

printf("CIRCULAR LINKED LIST PROGRAM\n");

printf("============================================\n");
printf("1. Create List\n");

printf("2. Display list\n");

printf("0. Exit\n");

printf("--------------------------------------------\n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter the total number of nodes in list: ");

scanf("%d", &n);

createList(n);

break;

case 2:

displayList();

break;

case 0:

break;

default:

printf("Error! Invalid choice. Please choose between 0-2");

printf("\n\n\n\n\n");

getch();

return 0;

/**

* Creates a circular linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)
{

int i, data;

struct node *prevNode, *newNode;

if(n >= 1) {

/*

* Creates and links the head node

*/

head = (struct node*)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");

scanf("%d", &data);

head->data = data;

head->next = NULL;

prevNode = head;

/*

* Creates and links rest of the n-1 nodes

*/

for(i = 2; i <= n; i++) {

newNode = (struct node*)malloc(sizeof(struct node));

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

newNode->next = NULL;

// Link the previous node with newly created node

prevNode->next = newNode;

// Move the previous node ahead


prevNode = newNode;

// Link the last node with first node

prevNode->next = head;

printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display the content of the list

*/

void displayList()

struct node* current;

int n = 1;

if(head == NULL) {

printf("List is empty.\n");

} else {

current = head;

printf("DATA IN THE LIST:\n");

do {

printf("Data %d = %d\n", n, current->data);

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;

void find(int item, struct node** par, struct node** loc)


{
struct node *ptr, *ptrsave;

if(root == NULL) /*tree empty*/


{
*loc = NULL;
*par = NULL;
return;
}
if(item == root->info) /*item is at root*/
{
*loc = root;
*par = NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item < root->info)
ptr = root->lchild;
else
ptr = root->rchild;
ptrsave = root;
while(ptr != NULL) {
if(item == ptr->info) {
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if(item < ptr->info)
ptr = ptr->lchild;
else
ptr = ptr->rchild;
} /*End of while */
*loc = NULL; /*item not found*/
*par = ptrsave;
} /*End of find()*/

void insert(int item)


{
struct node *tmp, *parent, *location;
find(item, &parent, &location);
if(location != NULL) {
printf("Item already present");
return;
}

tmp = (struct node*)malloc(sizeof(struct node));


tmp->info = item;
tmp->lchild = NULL;
tmp->rchild = NULL;

if(parent == NULL)
root = tmp;
else if(item < parent->info)
parent->lchild = tmp;
else
parent->rchild = tmp;
} /*End of insert()*/

void case_a(struct node* par, struct node* loc)


{
if(par == NULL) /*item to be deleted is root node*/
root = NULL;
else if(loc == par->lchild)
par->lchild = NULL;
else
par->rchild = NULL;
} /*End of case_a()*/

void case_b(struct node* par, struct node* loc)


{
struct node* child;

/*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;

if(par == NULL) /*Item to be deleted is root node*/


root = child;
else if(loc == par->lchild) /*item is lchild of its parent*/
par->lchild = child;
else /*item is rchild of its parent*/
par->rchild = child;
} /*End of case_b()*/

void case_c(struct node* par, struct node* loc)


{
struct node *ptr, *ptrsave, *suc, *parsuc;

/*Find inorder successor and its parent*/


ptrsave = loc;
ptr = loc->rchild;
while(ptr->lchild != NULL) {
ptrsave = ptr;
ptr = ptr->lchild;
}
suc = ptr;
parsuc = ptrsave;

if(suc->lchild == NULL && suc->rchild == NULL)


case_a(parsuc, suc);
else
case_b(parsuc, suc);

if(par == NULL) /*if item to be deleted is root node */


root = suc;
else if(loc == par->lchild)
par->lchild = suc;
else
par->rchild = suc;

suc->lchild = loc->lchild;
suc->rchild = loc->rchild;
} /*End of case_c()*/

int del(int item)


{
struct node *parent, *location;
if(root == NULL) {
printf("Tree empty");
return 0;
}

find(item, &parent, &location);


if(location == NULL) {
printf("Item not present in tree");
return 0;
}

if(location->lchild == NULL && location->rchild == NULL)


case_a(parent, location);
if(location->lchild != NULL && location->rchild == NULL)
case_b(parent, location);
if(location->lchild == NULL && location->rchild != NULL)
case_b(parent, location);
if(location->lchild != NULL && location->rchild != NULL)
case_c(parent, location);
free(location);
} /*End of del()*/

void display(struct node* ptr, int level)


{
int i;
if(ptr != NULL) {
display(ptr->rchild, level + 1);
printf("\n");
for(i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level + 1);
} /*End of if*/
} /*End of display()*/

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;

struct node* lchild;

struct node* rchild;

} * root;

void find(int item, struct node** par, struct node** loc)

struct node *ptr, *ptrsave;

if(root == NULL) /*tree empty*/

*loc = NULL;

*par = NULL;

return;

if(item == root->info) /*item is at root*/

*loc = root;

*par = NULL;

return;

/*Initialize ptr and ptrsave*/

if(item < root->info)

ptr = root->lchild;

else

ptr = root->rchild;

ptrsave = root;

while(ptr != NULL) {
if(item == ptr->info) {

*loc = ptr;

*par = ptrsave;

return;

ptrsave = ptr;

if(item < ptr->info)

ptr = ptr->lchild;

else

ptr = ptr->rchild;

} /*End of while */

*loc = NULL; /*item not found*/

*par = ptrsave;

} /*End of find()*/

void insert(int item)

struct node *tmp, *parent, *location;

find(item, &parent, &location);

if(location != NULL) {

printf("Item already present");

return;

tmp = (struct node*)malloc(sizeof(struct node));

tmp->info = item;

tmp->lchild = NULL;

tmp->rchild = NULL;

if(parent == NULL)

root = tmp;

else if(item < parent->info)

parent->lchild = tmp;

else
parent->rchild = tmp;

} /*End of insert()*/

void case_a(struct node* par, struct node* loc)

if(par == NULL) /*item to be deleted is root node*/

root = NULL;

else if(loc == par->lchild)

par->lchild = NULL;

else

par->rchild = NULL;

} /*End of case_a()*/

void case_b(struct node* par, struct node* loc)

struct node* child;

/*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;

if(par == NULL) /*Item to be deleted is root node*/

root = child;

else if(loc == par->lchild) /*item is lchild of its parent*/

par->lchild = child;

else /*item is rchild of its parent*/

par->rchild = child;

} /*End of case_b()*/

void case_c(struct node* par, struct node* loc)

struct node *ptr, *ptrsave, *suc, *parsuc;


/*Find inorder successor and its parent*/

ptrsave = loc;

ptr = loc->rchild;

while(ptr->lchild != NULL) {

ptrsave = ptr;

ptr = ptr->lchild;

suc = ptr;

parsuc = ptrsave;

if(suc->lchild == NULL && suc->rchild == NULL)

case_a(parsuc, suc);

else

case_b(parsuc, suc);

if(par == NULL) /*if item to be deleted is root node */

root = suc;

else if(loc == par->lchild)

par->lchild = suc;

else

par->rchild = suc;

suc->lchild = loc->lchild;

suc->rchild = loc->rchild;

} /*End of case_c()*/

int del(int item)

struct node *parent, *location;

if(root == NULL) {

printf("Tree empty");

return 0;

}
find(item, &parent, &location);

if(location == NULL) {

printf("Item not present in tree");

return 0;

if(location->lchild == NULL && location->rchild == NULL)

case_a(parent, location);

if(location->lchild != NULL && location->rchild == NULL)

case_b(parent, location);

if(location->lchild == NULL && location->rchild != NULL)

case_b(parent, location);

if(location->lchild != NULL && location->rchild != NULL)

case_c(parent, location);

free(location);

} /*End of del()*/

int preorder(struct node* ptr)

if(root == NULL) {

printf("Tree is empty");

return 0;

if(ptr != NULL) {

printf("%d ", ptr->info);

preorder(ptr->lchild);

preorder(ptr->rchild);

} /*End of preorder()*/

void inorder(struct node* ptr)

if(root == NULL) {

printf("Tree is empty");
return;

if(ptr != NULL) {

inorder(ptr->lchild);

printf("%d ", ptr->info);

inorder(ptr->rchild);

} /*End of inorder()*/

void postorder(struct node* ptr)

if(root == NULL) {

printf("Tree is empty");

return;

if(ptr != NULL) {

postorder(ptr->lchild);

postorder(ptr->rchild);

printf("%d ", ptr->info);

} /*End of postorder()*/

void display(struct node* ptr, int level)

int i;

if(ptr != NULL) {

display(ptr->rchild, level + 1);

printf("\n");

for(i = 0; i < level; i++)

printf(" ");

printf("%d", ptr->info);

display(ptr->lchild, level + 1);

} /*End of if*/

} /*End of display()*/
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:

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

/* A binary tree tNode has data, pointer to left child


and a pointer to right child */
struct tNode
{
int data;
struct tNode* left;
struct tNode* right;
};

/* Structure of a stack node. Linked List implementation is used for


stack. A stack node contains a pointer to tree node and a pointer to
next stack node */
struct sNode
{
struct tNode *t;
struct sNode *next;
};

/* Stack related functions */


void push(struct sNode** top_ref, struct tNode *t);
struct tNode *pop(struct sNode** top_ref);
bool isEmpty(struct sNode *top);

/* Iterative function for inorder tree traversal */


void inOrder(struct tNode *root)
{
/* set current to root of binary tree */
struct tNode *current = root;
struct sNode *s = NULL; /* Initialize stack s */
bool done = 0;

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;
}

/* backtrack from the empty subtree and visit the tNode


at the top of the stack; however, if the stack is empty,
you are done */
else

{
if (!isEmpty(s))
{
current = pop(&s);
printf("%d ", current->data);

/* we have visited the node and its left subtree.


Now, it's right subtree's turn */
current = current->right;
}
else
done = 1;
}
} /* end of while */
}
/* UTILITY FUNCTIONS */
/* Function to push an item to sNode*/
void push(struct sNode** top_ref, struct tNode *t)
{
/* allocate tNode */
struct sNode* new_tNode =
(struct sNode*) malloc(sizeof(struct sNode));

if(new_tNode == NULL)
{
printf("Stack Overflow \n");
getchar();
exit(0);
}

/* put in the data */


new_tNode->t = t;

/* link the old list off the new tNode */


new_tNode->next = (*top_ref);

/* move the head to point to the new tNode */


(*top_ref) = new_tNode;
}

/* The function returns true if stack is empty, otherwise false */


bool isEmpty(struct sNode *top)
{
return (top == NULL)? 1 : 0;
}

/* Function to pop an item from stack*/


struct tNode *pop(struct sNode** top_ref)
{
struct tNode *res;
struct sNode *top;

/*If sNode is empty then error */


if(isEmpty(*top_ref))
{
printf("Stack Underflow \n");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->t;
*top_ref = top->next;
free(top);
return res;
}
}

/* Helper function that allocates a new tNode with the


given data and NULL left and right pointers. */
struct tNode* newtNode(int data)
{
struct tNode* tNode = (struct tNode*)
malloc(sizeof(struct tNode));
tNode->data = data;
tNode->left = NULL;
tNode->right = NULL;

return(tNode);
}

/* Driver program to test above functions*/


int main()
{

struct tNode *root = newtNode(1);


root->left = newtNode(2);
root->right = newtNode(3);
root->left->left = newtNode(4);
root->left->right = newtNode(5);

inOrder(root);

getchar();
return 0;
}
Q22. Create a program for bubble sort.

#include <conio.h>

#include <stdio.h>

void swap(int* xp, int* yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

int i, j;

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

// Last i elements are already in place

for(j = 0; j < n - i - 1; j++)

if(arr[j] > arr[j + 1])

swap(&arr[j], &arr[j + 1]);

/* Function to print an array */

void printArray(int arr[], int size)

int i;

for(i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

// Driver program to test above functions


int main()

int arr[] = { 64, 34, 25, 12, 22, 11, 90 };

int n = sizeof(arr) / sizeof(arr[0]);

clrscr();

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

getch();

return 0;

}
Q23.Create a program for insertion sort.

#include <conio.h>

#include <stdio.h>

void swap(int* xp, int* yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

int i, j;

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

// Last i elements are already in place

for(j = 0; j < n - i - 1; j++)

if(arr[j] > arr[j + 1])

swap(&arr[j], &arr[j + 1]);

/* Function to print an array */

void printArray(int arr[], int size)

int i;

for(i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

// Driver program to test above functions

int main()
{

int arr[] = { 64, 34, 25, 12, 22, 11, 90 };

int n = sizeof(arr) / sizeof(arr[0]);

clrscr();

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

getch();

return 0;

}
Q25.Create a program for selection sort.

#include <conio.h>

#include <stdio.h>

int main()

int array[100], 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]);

getch();

return 0;

}
Q26. Create a program for merging sort.

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

// function to sort the subsection a[i .. j] of the array a[]


void merge_sort(int i, int j, int a[], int aux[])
{
if(j <= i) {
return; // the subsection is empty or a single element
}
int mid = (i + j) / 2;

// left sub-array is a[i .. mid]


// right sub-array is a[mid + 1 .. j]

merge_sort(i, mid, a, aux); // sort the left sub-array recursively


merge_sort(mid + 1, j, a, aux); // sort the right sub-array recursively

int pointer_left = i; // pointer_left points to the beginning of the left sub-array


int pointer_right = mid + 1; // pointer_right points to the beginning of the right sub-array
int k; // k is the loop counter

// we loop from i to j to fill each element of the final merged array


for(k = i; k <= j; k++) {
if(pointer_left == mid + 1) { // left pointer has reached the limit
aux[k] = a[pointer_right];
pointer_right++;
} else if(pointer_right == j + 1) { // right pointer has reached the limit
aux[k] = a[pointer_left];
pointer_left++;
} else if(a[pointer_left] < a[pointer_right]) { // pointer left points to smaller element
aux[k] = a[pointer_left];
pointer_left++;
} else { // pointer right points to smaller element
aux[k] = a[pointer_right];
pointer_right++;
}
}

for(k = i; k <= j; k++) { // copy the elements from aux[] to a[]


a[k] = aux[k];
}
}

int main()
{
int a[100], aux[100], n, i, d, swap;
clrscr();

printf("Enter number of elements in the array:\n");


scanf("%d", &n);

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

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


scanf("%d", &a[i]);

merge_sort(0, n - 1, a, aux);

printf("Printing the sorted array:\n");

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


printf("%d\n", a[i]);
getch();
return 0;
}
Q27.Create a program to convert postfix to infix.

#include <conio.h>

#include <stdio.h>

char stack[20];

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;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

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);

else if(*e == '(')

push(*e);

else if(*e == ')') {

while((x = pop()) != '(')

printf("%c", x);

} else {

while(priority(stack[top]) >= priority(*e))

printf("%c", pop());

push(*e);

e++;

while(top != -1) {

printf("%c", pop());

getch();

Q28.Create a program for evaluation of postfix.

#include <conio>

#include <stdio.h>

int stack[20];

int top = -1;

void push(int x)

stack[++top] = x;
}

int pop()

return stack[top--];

int main()

char exp[20];

char* e;

int n1, n2, n3, num;

clrscr();

printf("Enter the expression :: ");

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++;

printf("\nThe result of expression %s = %d\n\n", exp, pop());

getch();

return 0;

You might also like