0% found this document useful (0 votes)
7 views71 pages

Pydah College of Engineering

The document is a lab manual for the Data Structures course at Pydah College of Engineering, detailing various exercises and C programming tasks related to searching, sorting, linked lists, queues, stacks, and binary trees. Each exercise includes specific aims and source code examples for implementing algorithms such as linear search, binary search, bubble sort, quick sort, and more. The manual serves as a practical guide for students to apply theoretical concepts in programming and data structure manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views71 pages

Pydah College of Engineering

The document is a lab manual for the Data Structures course at Pydah College of Engineering, detailing various exercises and C programming tasks related to searching, sorting, linked lists, queues, stacks, and binary trees. Each exercise includes specific aims and source code examples for implementing algorithms such as linear search, binary search, bubble sort, quick sort, and more. The manual serves as a practical guide for students to apply theoretical concepts in programming and data structure manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

PYDAH COLLEGE OF ENGINEERING

(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)


YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

DATA STRUCTURES LAB MANUAL

Department
of
Computer Science & Engineering

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

SYLLABUS

EXERCISE-01 (Searching)
a) Write a C program that use both recursive and Non-recursive functions to perform linear
search for a key value in a given list.

b)Write a C program that use both recursive and Non-recursive functions to perform binary
search for a key value in a given list .

EXERCISE-02(Sorting-1)
a) Write a C program that implement bubble sort ,to sort a given list of integers in ascending
order.

b) Write a C program that implement quick sort, to sort a given list of integers in ascending
order.

c) Write a C program that implement insertion sort, to sort a given list of integers in ascending
order.

EXERCISE-03(Sorting-2)
a) Write a C program that implement a radix sort, to sort the given list of integers in ascending
order.

b) Write a C program that implement a merge sort, to sort a given list of integers in ascending
order.

EXERCISE-04(Singly linked list)


a) Write a C program that uses functions to create a singly linked list.

b) Write a C program that uses functions to perform insertion operation on a singly linked list.

c) Write a C program that uses functions to perform deletion operation on a singly linked list.

d) Write a C program to reverse elements of a single linked list .

EXERCISE-05(queue)
a) Write a C program that implement queues (its operations) using array.

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

b) Write a C program that implement queues (its operations) using linked list.

EXERCISE-06(stack)
a) Write a C program that implements stack(its operations)using array.

b) Write a C program that implements stack(its operations)using linked list.

c) Write a C program that uses stack operations to evaluate the postfix operation.

EXERCISE-07(binary tree)
Write a recursive program for traversing a binary tree in preorder, inorder and postorder.

EXERCISE-08(binary search tree)


a) Write a C program to create a BST.

b) Write a C program to insert a node into a BST.

c) Write a C program to delete a node from a BST.

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

EXERCISE:01

Aim :- Write a C program that use both recursive and Non-recursive functions to perform a
linear search for a key value for a given list.

SOURCE CODE:
##include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
clrscr();
printf(“Enter the array elements in ascending order”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key element\n”);
scanf(“%d”, &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf(“The key element is found at location %d”, mid + 1);
else
printf(“the key element is not found”);
getch();
}

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

Output:
Enter the size of the array 7
Enter the array elements in ascending order 23 45 68 90 100 789 890
Enter the key element 789
The key Element is found at location 6

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:02

Aim:- Write a c program that use both recursive and non recursive binary search for a key value
to a given list.

Source code:

#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
while (start_index <= end_index){
int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
}
return -1;
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}

Output:

Element found at index : 4

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

EXERCISE:02(Sorting-I)
Aim:- Write a c program that implement bubble sort,to sort a given list of integers in
ascending order.

SOURCE CODE:
#include <stdio.h>

void bubble_sort(int arr[], int n) {

int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output:
Sorted array: 11 12 22 25 34 64 90

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:02
Aim:- Write a c program that implement quick sort,to sort a given list of integers in ascending
order.

SOURCE CODE:

#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

}
// Function to print the array
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = { 12, 17, 6, 25, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

Output:-
Sorted array:
1 5 6 12 17 25

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM-03
Aim:- Write a C program that implement insertion sort to sort the given list of integers in
ascending order.

SOURCE CODE:

#include <stdio.h>
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;

while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one pos
tion ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}

void printArr(int a[], int n) /* function to print the array */


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

int main()

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
insert(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);

return 0;
}

Output:

Before sorting array elements are:

12 31 25 8 32 17

After sorting array elements are:

8 12 17 25 31 31

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

EXERCISE-03(SORTING-2)
PROGRAM-01

Aim:- Write a C program to implement radix sort to sort a given list of integers in ascending
order.

SOURCE CODE:

#include <stdio.h>
int getMax(int a[], int n) {
int max = a[0];
for(int i = 1; i<n; i++) {
if(a[i] > max)
max = a[i];
}
return max; //maximum element from the array
}

void countingSort(int a[], int n, int place) // function to implement counting sort
{
int output[n + 1];
int count[10] = {0};

// Calculate count of elements


for (int i = 0; i < n; i++)
count[(a[i] / place) % 10]++;

// Calculate cumulative frequency


for (int i = 1; i < 10; i++)
count[i] += count[i - 1];

// Place the elements in sorted order

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

for (int i = n - 1; i >= 0; i--) {


output[count[(a[i] / place) % 10] - 1] = a[i];
count[(a[i] / place) % 10]--;
}

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


a[i] = output[i];
}
// function to implement radix sort
void radixsort(int a[], int n) {
// get maximum element from array
int max = getMax(a, n);
// Apply counting sort to sort elements based on place value
for (int place = 1; max / place > 0; place *= 10)
countingSort(a, n, place);
}
// function to print array elements
void printArray(int a[], int n) {
for (int i = 0; i < n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
int main() {
int a[] = {181, 289, 390, 121, 145, 736, 514, 888, 122};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a,n);
radixsort(a, n);
printf("After applying Radix sort, the array elements are - \n");
printArray(a, n);
}

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

Output:
Before sorting array elements:

181 289 390 121 145 514 888 122

After sorting array elements:

121 122 145 181 289 390 514 888

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:02
Aim:- Write a C program that implement merge sort to sort a given list of elements in
ascending order.

SOURCE CODE:

/* Function to merge the subarrays of a[] */


void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;

int LeftArray[n1], RightArray[n2]; //temporary arrays

/* copy data to temp arrays */


for (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];

i = 0, /* initial index of first sub-array */


j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */

while (i < n1 && j < n2)


{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}

while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}

Output:
Before sorting array elements:

32 12 84 01 63

After sorting array elements:

01 12 32 63 84

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

EXERCISE:04
PROGRAM:01

Aim:- Write a C program that uses functions to create a singly linked list.

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
// Singly Linked List structure
struct Node {
int data;
struct Node* next;
};

// Global head pointer


struct Node* head = NULL;

// Function to insert a node at the end of the list


void insert(int value) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

// Check if the list is empty


if (head == NULL) {
head = newNode;
} else {
// Traverse to the end of the list
struct Node* current = head;

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

while (current->next != NULL) {


current = current->next;
}
// Link the new node to the last node
current->next = newNode;
}
}

// Function to traverse and print the list


void traverse() {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}

int main() {
// Insert elements into the list
insert(10);
insert(20);
insert(30);
insert(40);

// Traverse and print the list


printf("List: ");
traverse();

return 0;
}

Output:

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

List: 10 20 30 40

PROGRAM:02
Aim:- Write a C program that uses functions to perform insertion operation on a singly linked
list.

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beginsert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}

Output

Enter the item which you want to insert?


12

Node inserted

Press 0 to insert more ?


0

Enter the item which you want to insert?


23

Node inserted

Press 0 to insert more ?


2

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:03
Aim:- Write a C program that uses deletion operation on a singly linked list.

SOURCE CODE:
#include<stdio.h>

#include<stdlib.h>
void create(int);
void begdelete();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\n1.Append List\n2.Delete node\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

begdelete();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}

}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}

}
void begdelete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty");

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the begining ...");
}
}

Output
1.Append List
2.Delete node
3.Exit
4.Enter your choice?1

Enter the item


23

Node inserted

1.Append List
2.Delete node
3.Exit
4.Enter your choice?2

Node deleted from the begining ...

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:04
Aim:- Write a C program to reverse element of a singly linked list.

SOURCE CODE:

#include <stdio.h>
//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};

//Represent the head and tail of the singly linked list


struct node *head, *tail = NULL;

//addNode() will add a new node to the list


void addNode(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty


if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//reverse() will the reverse the order of the list


void reverse(struct node *current) {
//Checks if list is empty
if(head == NULL) {
printf("List is empty\n");
return;
}
else{
//Checks if the next node is null, if yes then prints it.
if(current->next == NULL) {
printf("%d ", current->data);
return;
}
//Recursively calls the reverse function
reverse(current->next);
printf("%d ", current->data);
}
}

//display() will display all the nodes present in the list


void display() {
//Node current will point to head
struct node *current = head;

if(head == NULL) {
printf("List is empty\n");

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);

printf("Original List: \n");


display();

printf("Reversed List: \n");


//Print reversed list
reverse(head);

return 0;
}

Output:
Original List:
1234
Reversed List:
4321

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

EXERCISE:05
PROGRAM:01

Aim:-Write a C program that implements queue(its operations) using array.

SOURCE CODE:
#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;

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

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

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

} /* End of switch */

} /* End of while */

} /* End of main() */

void insert()

. int add_item;

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

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;

} /* End of insert() */

void delete()

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

printf("Queue Underflow \n");

return ;

else

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

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

front = front + 1;

} /* End of delete() */

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

} /* End of display() */

Output:
1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 10

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 15

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 20

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 1

Inset the element in queue : 30

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 2

Element deleted from queue is : 10

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 3

Queue is :

15 20 30

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 4

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:02
Aim:-Write a C program that implement queue using linked list.

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n")
;
printf("\n==========================================================
======\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

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


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

Output:

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
123

***********Main Menu**********

==============================

1.insert an element

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

123

90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2

***********Main Menu**********

==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

Enter your choice ?3

printing values .....

90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?4

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

Exercise:06
Program:01

Aim:- Write a C program that implement stack using linked list.

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

printf("\n Enter your choice \n");


scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

Output:

Enter choice : 1

Enter data : 56

Enter choice : 1

Enter data : 80

Enter choice : 2

Popped value : 80

Enter choice : 3

Top element : 56

Enter choice : 1

Enter data : 78

Enter choice : 1

Enter data : 90

Enter choice : 6

90 78 56

Enter choice : 7

No. of elements in stack : 3

Enter choice : 8

All stack elements destroyed

Enter choice : 4

Stack is empty

Enter choice : 5

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:02
Aim:-Write a C program that uses stack operation to evaluate using array.
SOURCE CODE:
#include<stdio.h>

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

void push(void);

void pop(void);

void display(void);

int main()

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

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

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:

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

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

while(choice!=4);

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

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

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

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

OUTPUT :

perform operations on the stack:

1.push

2.pop

3.show

4.end

Enter your choice:

Enter the element that is inserted to be in stack:10

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM-03
Aim:- Write a C program that uses stack operations to evaluate postfix expressions.

SOURCE CODE:
#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

// Stack implementation

int stack[MAX_SIZE];
int top = -1;
void push(int item) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
top++;
stack[top] = item;
}
int pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
}
int item = stack[top];

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

top--;
return item;
}
int is_operator(char symbol) {
if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/') {
return 1;
}
return 0;
}
int evaluate(char* expression) {
int i = 0;
char symbol = expression[i];
int operand1, operand2, result;
while (symbol != '\0') {
if (symbol >= '0' && symbol <= '9') {
int num = symbol - '0';
push(num);
}
else if (is_operator(symbol)) {
operand2 = pop();
operand1 = pop();
switch(symbol) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
}
push(result);
}
i++;
symbol = expression[i];
}
result = pop();

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

return result;
}
int main() {
char expression[] = "5 6 7 + * 8 -";
int result = evaluate(expression);
printf("Result= %d\n", result);
return 0;
}
OUTPUT:
Result: 57

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

EXERCISE-07
PROGRAM:01

Aim:- Write a c program for traversing a binary tree in preorder, post order, in order.

PREFIX:

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
/*To create a new node*/
struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in preorder*/

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

void traversePreorder(struct node* root)


{
if (root == NULL)
return;
printf(" %d ", root->element);
traversePreorder(root->left);
traversePreorder(root->right);
}

int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);

printf("\n The Preorder traversal of given binary tree is -\n");


traversePreorder(root);
return 0;
}

Output:
The preorder is :

9 7 5 8 14 11 16

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

POST ORDER:

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
struct node {
s
struct node* left;
struct node* right;
};

/*To create a new node*/


struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in postorder*/
void traversePostorder(struct node* root)
{
if (root == NULL)

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

return;
traversePostorder(root->left);
traversePostorder(root->right);
printf(" %d ", root->element);
}
int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);

printf("\n The Postorder traversal of given binary tree is -\n");


traversePostorder(root);
return 0;
}

Output:
15264

52164

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

INORDER:

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
//return a new node with the given
struct node *getNode(int val)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
//inserts nodes in the binary search tree
struct node *insertNode(struct node *root, int val)

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

{
if(root == NULL)
return getNode(val);

if(root->key < val)


root->right = insertNode(root->right,val);

if(root->key > val)


root->left = insertNode(root->left,val);

return root;
}

//inorder traversal of the binary search tree


void inorder(struct node *root)
{
if(root == NULL)
return;

//traverse the left subtree


inorder(root->left);

//visit the root


printf("%d ",root->key);

//traverse the right subtree


inorder(root->right);
}
int main()
{
struct node *root = NULL;
int data;
char ch;

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

/* Do while loop to display various options to select from to decide the input */
do
{
printf("\nSelect one of the operations::");
printf("\n1. To insert a new node in the Binary Tree");
printf("\n2. To display the nodes of the Binary Tree(via Inorder Traversal).\n");
int choice;
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\nInorder Traversal of the Binary Tree::\n");
inorder(root);
break;
default :
printf("Wrong Entry\n");
break;
}

printf("\nDo you want to continue (Type y or n)\n");


scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');

return 0;
}

Output:

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

Inorder traversal of binary tree is:

425136

EXERCISE:08
PROGRAM:01

Aim:- Write a c program to create BST.

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *right_child;
struct node *left_child;
};
struct node* new_node(int x){
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data = x;
temp->left_child = NULL;
temp->right_child = NULL;
return temp;
}

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

struct node* search(struct node * root, int x){


if (root == NULL || root->data == x)
return root;
else if (x > root->data)
return search(root->right_child, x);
else
return search(root->left_child, x);
}
struct node* insert(struct node * root, int x){
if (root == NULL)
return new_node(x);
else if (x > root->data)
root->right_child = insert(root->right_child, x);
else
root -> left_child = insert(root->left_child, x);
return root;
}
struct node* find_minimum(struct node * root) {
if (root == NULL)
return NULL;
else if (root->left_child != NULL)
return find_minimum(root->left_child);
return root;
}
struct node* delete(struct node * root, int x)
{
if (root == NULL)
return NULL;
if (x > root->data)
root->right_child = delete(root->right_child, x);
else if (x < root->data)
root->left_child = delete(root->left_child, x);
else {

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

if (root->left_child == NULL && root->right_child == NULL){


free(root);
return NULL;
}
else if (root->left_child == NULL || root->right_child == NULL){
struct node *temp;
if (root->left_child == NULL)
temp = root->right_child;
else
temp = root->left_child;
free(root);
return temp;
}
else {
struct node *temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, temp->data);
}
}
return root;
}
void inorder(struct node *root){
if (root != NULL)
{
inorder(root->left_child);
printf(" %d ", root->data);
inorder(root->right_child);
}
}
int main() {
struct node *root;
root = new_node(20);
insert(root, 5);

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
root = delete(root, 1);
root = delete(root, 40);
root = delete(root, 45);
root = delete(root, 9);
inorder(root);
printf("\n");
return 0;
}

Output :-

1 5 7 9 12 15 20 25 30 40 42 45

5 7 12 15 20 25 30 42

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:02
Aim:-Write a C program to insert a node into a BST.

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
void insert(int);
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

scanf("%d",&item);
insert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void insert(int item)
{
struct node *ptr, *parentptr , *nodeptr;
ptr = (struct node *) malloc(sizeof (struct node));
if(ptr == NULL)
{
printf("can't insert");
}
else
{
ptr -> data = item;
ptr -> left = NULL;
ptr -> right = NULL;
if(root == NULL)
{
root = ptr;
root -> left = NULL;
root -> right = NULL;
}
else
{
parentptr = NULL;
nodeptr = root;
while(nodeptr != NULL)
{
parentptr = nodeptr;
if(item < nodeptr->data)

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

{
nodeptr = nodeptr -> left;
}
else
{
nodeptr = nodeptr -> right;
}
}
if(item < parentptr -> data)
{
parentptr -> left = ptr;
}
else
{
parentptr -> right = ptr;
}
}
printf("Node Inserted");
}
}

Output
Enter the item which you want to insert?

12

Node Inserted

Press 0 to insert more ?

0Enter the item which you want to insert?

23

Node Inserted

Press 0 to insert more ?

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

PROGRAM:03
Aim:-Write a C program to delete a node from a BST.

SOURCE CODE:
#include<stdio.h>

#include<stdlib.h>

struct node {

int key;

struct node *left, *right;

};

// A utility function to create a new BST node

struct node *newNode(int item) {

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

temp->key = item;

temp->left = temp->right = NULL;

return temp;

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

// A utility function to do inorder traversal of BST

void inorder(struct node *root) {

if (root != NULL) {

inorder(root->left);

printf("%d ", root->key);

inorder(root->right);

/* A utility function to insert a new node with given key in BST */

struct node* insert(struct node* node, int key) {

/* If the tree is empty, return a new node */

if (node == NULL)

return newNode(key);

/* Otherwise, recur down the tree */

if (key < node->key)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

/* return the (unchanged) node pointer */

return node;

/* Given a non-empty binary search tree, return the node with minimum

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

key value found in that tree. Note that the entire tree does not

need to be searched. */

struct node * minValueNode(struct node* node) {

struct node* current = node;

/* loop down to find the leftmost leaf */

while (current->left != NULL)

current = current->left;

return current;

/* Given a binary search tree and a key, this function deletes the key

and returns the new root */

struct node* deleteNode(struct node* root, int key) {

// base case

if (root == NULL)

return root;

// If the key to be deleted is smaller than the root's key,

// then it lies in left subtree

if (key < root->key)

root->left = deleteNode(root->left, key);

// If the key to be deleted is greater than the root's key,

// then it lies in right subtree

else if (key > root->key)

root->right = deleteNode(root->right, key);

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

// if key is same as root's key, then This is the node

// to be deleted

else {

// node with only one child or no child

if (root->left == NULL) {

struct node *temp = root->right;

free(root);

return temp;

} else if (root->right == NULL) {

struct node *temp = root->left;

free(root);

return temp;

// node with two children: Get the inorder successor (smallest

// in the right subtree)

struct node* temp = minValueNode(root->right);

// Copy the inorder successor's content to this node

root->key = temp->key;

// Delete the inorder successor

root->right = deleteNode(root->right, temp->key);

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

return root;

// Driver Program to test above functions

int main() {

/* Let us create following BST

50

/ \

30 70

/ \ / \

20 40 60 80 */

struct node *root = NULL;

root = insert(root, 50);

root = insert(root, 30);

root = insert(root, 20);

root = insert(root, 40);

root = insert(root, 70);

root = insert(root, 60);

root = insert(root, 80);

printf("Inorder traversal of the given tree \n");

inorder(root);

printf("\nDelete 20\n");

root = deleteNode(root, 20);

printf("Inorder traversal of the modified tree \n");

inorder(root);

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in


PYDAH COLLEGE OF ENGINEERING
(Approved by AICTE, New Delhi and Affiliated to JNTUK, Kakinada)
YANAM ROAD, PATAVALA KAKINADA, 533461, E.G.Dist ,

printf("\nDelete 30\n");

root = deleteNode(root, 30);

printf("Inorder traversal of the modified tree \n");

inorder(root);

printf("\nDelete 50\n");

root = deleteNode(root, 50);

printf("Inorder traversal of the modified tree \n");

inorder(root);

return 0;}

Output:
$ gcc DeleteBST.c

$ ./a.out

Inorder traversal of the given tree

20 30 40 50 60 70 80

Delete 20

Inorder traversal of the modified tree

30 40 50 60 70 80

Delete 30

Inorder traversal of the modified tree

40 50 60 70 80

Delete 50

Inorder traversal of the modified tree

40 60 70 80

Telephone:0884-2315333 Email id: [email protected] website:www.pydah.edu.in

You might also like