32
PROGRAM 1 Write a program to define an integer array of 10 elements and display it. #include<stdio.h> #include<conio.h> int main() { int i=0, arr[10]; clrscr(); // enter the elements for(i=0;i<10;i++) { printf("\n arr[%d] = ", i); scanf("%d",&arr[i]); } printf("\n The array elements are "); for(i=0;i<10;i++) printf("arr[%d] = %d\t", i, arr[i]); return 0; } Data Structures Using C (MSBTE), 1/e Reema Thareja © Oxford University Press 2013. All rights reserved

Solutions to Laboratory Exercises MSBTE

Embed Size (px)

DESCRIPTION

solution to basic c programs

Citation preview

Page 1: Solutions to Laboratory Exercises MSBTE

PROGRAM 1

Write a program to define an integer array of 10 elements and display it.

#include<stdio.h>

#include<conio.h>

int main()

{

int i=0, arr[10];

clrscr();

// enter the elements

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

{

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

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

}

printf("\n The array elements are ");

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

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

return 0;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 2: Solutions to Laboratory Exercises MSBTE

PROGRAM 2

Write a program to search a number in an array of 10 elements.

#include<stdio.h>

#include<conio.h>

int arr[10], n;

void read_arr(void);

void print_arr(void);

void read_arr()

{ int i;

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

scanf(“%d”, &n);

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

{

printf(“\n Arr[%d] = : “, i);

scanf(“%d”, &arr[i]);

}

}

void print_arr()

{ int i;

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

printf(“\n Arr[%d] = %d”, i, arr[i]);

}

void linear_search()

{ int i, num, pos, found=0;

printf(“\n Enter the number that has to be searched : “);

scanf(“%d”, &num);

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

{

if(arr[i] == num)

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 3: Solutions to Laboratory Exercises MSBTE

{

found =1;

pos=i;

printf(“\n %d is found in the array at position = %d”, num,

i);

break;

}

}

if (found == 0)

printf(“\n %d DOES NOT EXIST in the array”);

}

void binary_search()

{ int beg, end, num, mid, found=0;

printf(“\n Enter the number that has to be searched : “);

scanf(“%d”, &num);

beg = 0, end = n-1;

while(beg<=end)

{

mid = (beg + end)/2;

if (arr[mid] == num)

{

printf(“\n %d is present in the array at position = %d”,

num, mid);

found =1;

break;

}

if (arr[mid]>num)

end = mid-1;

else if (arr[mid] < num)

beg = mid+1;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 4: Solutions to Laboratory Exercises MSBTE

if ( beg > end && found == 0)

printf(“\n %d DOES NOT EXIST IN THE ARRAY”);

}

int main()

{ int i,num, option;

clrscr();

read_arr();

print_arr();

printf(“\n\n *****MENU*****”);

printf(“\n 1. Linear Search”);

printf(“\n 2. Binary Search”);

printf(“\n 3. EXIT”);

printf(“\n*********************”);

do

{

printf(“\n\n Enter your option : “);

scanf(“%d”, &option);

switch(option)

{

case 1: linear_search();

break;

case 2: binary_search();

break;

}

}while(option!=3);

getch();

return 0;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 5: Solutions to Laboratory Exercises MSBTE

PROGRAM 3

Write a program to perform insertion & deletion operation on an array.

#include<stdio.h>

#include<conio.h>

int arr[10], n;

void read_arr(void);

void ins_arr(void);

void del_arr(void);

void print_arr(void);

void read_arr()

{

int i;

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

scanf("%d", &n);

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

{

printf("\n Arr[%d] = : ", i);

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

}

}

void ins_arr()

{ int i, num, pos;

printf("\n Enter the number to be inserted : ");

scanf("%d", &num);

printf("\nEnter the position at which the number has to be added

: ");

scanf("%d", &pos);

for(i=n;i>=pos;i--)

arr[i+1] = arr[i];

arr[pos] = num;

n++;

printf("\n The array after insertion of %d is : ", num);

print_arr();

}

void del_arr()

{ int i, pos;

printf("\n Enter the position from which the number has to be

deleted : ");

scanf("%d", &pos);

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

arr[i] = arr[i+1];

n--;

printf("\n The array after deletion is : ");

print_arr();

}

void print_arr()

{ int i;

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

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 6: Solutions to Laboratory Exercises MSBTE

printf("\n Arr[%d] = %d", i, arr[i]);

}

int main()

{

int i, n, num, pos;

clrscr();

read_arr();

ins_arr();

del_arr();

getch();

return 0;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 7: Solutions to Laboratory Exercises MSBTE

PROGRAM 4

Write a program to implement selection, insertion & bubble sort.

#include<stdio.h>

#include<conio.h>

int arr[10], n;

void read_arr(void);

void print_arr(void);

void bubble_sort(void);

void selection_sort(void);

void insertion_sort(void);

int smallest(int k);

void read_arr()

{ int i;

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

scanf("%d", &n);

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

{

printf("\n Arr[%d] = : ", i);

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

}

}

void print_arr()

{ int i;

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

printf("\n Arr[%d] = %d", i, arr[i]);

}

void bubble_sort()

{ int i,j, temp;

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

{

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

{

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

{

temp = arr [j];

arr [j] = arr [j+1];

arr [j+1] = temp;

}

}

}

print_arr();

}

void insertion_sort()

{

int i, k, j, temp;

for(k=1;k<n;k++)

{

temp = arr[k];

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 8: Solutions to Laboratory Exercises MSBTE

j = k-1;

while((temp < arr[j]) && (j>=0))

{

arr[j+1] = arr[j];

j--;

}

arr[j+1] = temp;

}

print_arr();

}

void selection_sort()

{ int k, pos, temp;

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

{

pos = smallest(k);

temp = arr[k];

arr[k] = arr[pos];

arr[pos] = temp;

}

print_arr();

}

int smallest(int k)

{

int pos = k, small=arr[k], i;

for(i=k+1;i<n;i++)

{

if(arr[i]< small)

{

small = arr[i];

pos = i;

}

}

return pos;

}

int main()

{ int i,num, found=0, option;

clrscr();

read_arr();

print_arr();

printf("\n\n *****MENU*****");

printf("\n 1. Bubble Sort");

printf("\n 2. Insertion Sort");

printf("\n 3. Selection Sort");

printf("\n 4. EXIT");

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

do

{

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

{

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 9: Solutions to Laboratory Exercises MSBTE

case 1: bubble_sort();

break;

case 2: insertion_sort();

break;

case 3: selection_sort();

break;

}

}while(option!=4);

getch();

return 0;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 10: Solutions to Laboratory Exercises MSBTE

PROGRAM 5

Write a program to implement stack operation using array.

#include<stdio.h>

#include<conio.h>

#define MAX 10

int st[MAX], top=-1;

void push(int st[], int val);

int pop(int st[]);

int peep(int st[]);

void display(int st[]);

main()

{

int val, option;

clrscr();

do

{

printf("\n *****MAIN MENU*****");

printf("\n 1. PUSH");

printf("\n 2. POP");

printf("\n 3. PEEK");

printf("\n 4. DISPLAY");

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

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

{

case 1:

printf("\n ENter the number to be pushed on to the

stack : ");

scanf("%d", &val);

push(st, val);

break;

case 2:

val = pop(st);

printf("\n The value deleted from the stack is : %d",

val);

break;

case 3:

val = peek(st);

printf("\n The value stored at the top of the stack is

:

%d", val);

break;

case 4:

display(st);

break;

}

}

while(option != 5);

getch();

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 11: Solutions to Laboratory Exercises MSBTE

return 0;

}

void push(int st[], int val)

{

if(top == MAX-1)

{

printf("\n STACK OVERFLOW");

}

else

{

top++;

st[top] = val;

}

}

int pop(int st[])

{

int val;

if(top == -1)

{

printf("\n STACK UNDERFLOW");

return -1;

}

else

{

val = st[top];

top--;

return val;

}

}

void display(int st[])

{

int i;

if(top == -1)

printf("\n STACK IS EMPTY");

else

{

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

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

}

}

int peep(int st[])

{

If(TOP == NULL)

{

printf("\n STACK IS EMPTY");

return -1;

}

else

return (st[top]);

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 12: Solutions to Laboratory Exercises MSBTE

PROGRAM 6

Write a program to implement queue operation using array.

#include<stdio.h>

#include<conio.h>

#define MAX 10

int queue[MAX];

int front = -1, rear = -1;

void insert(void);

int delete_element(void);

int peek(void);

void display(void);

main()

{

int option, val;

clrscr();

do

{

printf("\n\n ***** MAIN MENU *****");

printf("\n 1. Insert an element");

printf("\n 2. Delete an element");

printf("\n 3. Peek");

printf("\n 4. Display the queue");

printf("\n 5. EXIT");

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

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

{

case 1:

insert();

break;

case 2:

val = delete_element();

printf("\n The number that was deleted is : %d", val);

break;

case 3:

val = peek();

printf("\n The first value in the queue is : %d",

val);

break;

case 4:

display();

break;

}

}

while(option != 5);

getch();

return 0;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 13: Solutions to Laboratory Exercises MSBTE

void insert()

{

int num;

printf("\n Enter the number to be inserted in the queue : ");

scanf("%d", &num);

if(rear == MAX-1)

printf("\n OVERFLOW");

if(front == -1 && rear == -1)

front = rear = 0;

else

rear++;

queue[rear] = num;

}

int delete_element()

{

int val;

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

{

printf("\n UNDERFLOW");

return -1;

}

else

{

front++;

val = queue[front];

return val;

}

}

int peek()

{

return queue[front];

}

void display()

{

int i;

printf("\n");

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

printf("\t %d", queue[i]);

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 14: Solutions to Laboratory Exercises MSBTE

PROGRAM 7

Write a program to create singly linked list.

#include<stdio.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

};

struct node *start = NULL;

struct node *create_ll(struct node *);

struct node *display(struct node *);

struct node *insert_beg(struct node *);

struct node *insert_end(struct node *);

struct node *insert_before(struct node *start);

struct node *insert_after(struct node *start);

struct node *insert_sorted(struct node *start);

struct node *delete_beg(struct node *);

struct node *delete_end(struct node *);

struct node *delete_node(struct node *start);

struct node *delete_after(struct node *start);

struct node *delete_sorted(struct node *start);

struct node *delete_list(struct node *start);

struct node *sort_list(struct node *start);

main()

{

int option;

clrscr();

do

{

printf("\n\n *****MAIN MENU *****");

printf("\n 1: Create a List");

printf("\n 2: Display the list");

printf("\n 3: Add a node in the beginning");

printf("\n 4: Add a node at the end");

printf("\n 5: Add a node before a given node");

printf("\n 6: Add a node after a given node");

printf("\n 7. Add a node in a sorted linked list");

printf("\n 8: Delete a node from the beginning");

printf("\n 9: Delete a node from the end");

printf("\n 10: Delete a node a given node");

printf("\n 11: Delete a node before a given node");

printf("\n 12. Delete a node from a sorted linked list");

printf("\n 13: Delete the entire list");

printf("\n 14: Sort the list");

printf("\n 15: EXIT");

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

printf("\n\n ENter your option : ");

scanf("%d", &option);

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 15: Solutions to Laboratory Exercises MSBTE

switch(option)

{

case 1:

start = create_ll(start);

printf("\n LINKED LIST CREATED");

break;

case 2:

start = display(start);

break;

case 3:

start = insert_beg(start);

break;

case 4:

start = insert_end(start);

break;

case 5:

start = insert_before(start);

break;

case 6:

start = insert_after(start);

break;

case 7:

start = insert_sorted(start);

break;

case 8:

start = delete_beg(start);

break;

case 9:

start = delete_end(start);

break;

case 10:

start = delete_node(start);

break;

case 11:

start = delete_after(start);

break;

case 12:

start = delete_sorted(start);

break;

case 13:

start = delete_list(start);

printf("\n List is EMPTY");

break;

case 14:

start = sort_list(start);

break;

}

}

while(option !=15);

getch();

return 0;

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 16: Solutions to Laboratory Exercises MSBTE

}

struct node *create_ll(struct node *start)

{

struct node *new_node;

int num;

printf("\n Enter -1 to end");

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

scanf("%d", &num);

while(num!=-1)

{

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

new_node->data=num;

if(start==NULL) // if the linked list does not exist

{

new_node->next = NULL;

start = new_node;

}

else // if the linked list already exists

{

new_node->next = start;

start = new_node;

}

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

scanf("%d", &num);

}

return start;

}

struct node *display(struct node *start)

{

struct node *ptr;

ptr = start;

printf("\n");

while(ptr != NULL) // while all the nodes have not been processed

{

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

ptr = ptr->next;

}

return start;

}

struct node *insert_beg(struct node *start)

{

struct node *new_node;

int num;

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

scanf("%d", &num);

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

new_node->data = num;

new_node->next = start;

start = new_node;

return start;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 17: Solutions to Laboratory Exercises MSBTE

struct node *insert_end(struct node *start)

{

struct node *ptr, *new_node;

int num;

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

scanf("%d", &num);

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

new_node->data = num;

ptr = start;

while(ptr->next != NULL)

ptr = ptr->next;

ptr->next = new_node;

new_node->next = NULL;

return start;

}

struct node *insert_before(struct node *start)

{

struct node *new_node, *ptr, *preptr;

int num, val;

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

scanf("%d", &num);

printf("\n Enter the value before which the data has to be

inserted : ");

scanf("%d", &val);

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

new_node->data = num;

ptr = start;

while(ptr->data != val)

{

preptr = ptr;

ptr = ptr->next;

}

new_node->next = ptr;

preptr->next = new_node;

return start;

}

struct node *insert_after(struct node *start)

{

struct node *new_node, *ptr, *preptr;

int num, val;

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

scanf("%d", &num);

printf("\n Enter the value after which the data has to be

inserted : ");

scanf("%d", &val);

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

new_node->data = num;

ptr = start;

while(preptr->data != val)

{

preptr = ptr;

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 18: Solutions to Laboratory Exercises MSBTE

ptr = ptr->next;

}

new_node->next = ptr;

preptr->next=new_node;

return start;

}

struct node *insert_sorted(struct node *start)

{

struct node *new_node, *ptr, *preptr;

int num;

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

scanf("%d", &num);

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

new_node->data = num;

ptr = start;

while(ptr->data < num)

{

preptr = ptr;

ptr = ptr->next;

if(ptr == NULL)

break;

}

if(ptr == NULL)

{

Preptr->next = new_node;

new_node->next = NULL;

}

else

{

new_node->next = ptr;

preptr->next = new_node;

}

return start;

}

struct node *delete_beg(struct node *start)

{

struct node *ptr;

ptr = start;

start = start->next;

free(ptr);

return start;

}

struct node *delete_end(struct node *start)

{

struct node *ptr, *preptr;

ptr = start;

while(ptr->next != NULL)

{

preptr = ptr;

ptr = ptr->next;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 19: Solutions to Laboratory Exercises MSBTE

Preptr->next = NULL;

free(ptr);

return start;

}

struct node *delete_node(struct node *start)

{

struct node *ptr, *preptr;

int val;

printf("\n Enter the value of the node which has to be deleted :

");

scanf("%d", &val);

ptr = start;

if(ptr->data == val)

{

start = delete_beg(start);

return start;

}

else

{

while(ptr->data != val)

{

preptr = ptr;

ptr = ptr->next;

}

Preptr->next = ptr->next;

free(ptr);

return start;

}

}

struct node *delete_after(struct node *start)

{

struct node *ptr, *preptr;

int val;

printf("\n Enter the value after which the node has to deleted :

");

scanf("%d", &val);

ptr = start;

while(preptr->data != val)

{

preptr = ptr;

ptr = ptr->next;

}

Preptr->next=ptr->next;

free(ptr);

return start;

}

struct node *delete_sorted(struct node *start)

{

struct node *ptr, *preptr;

int val;

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 20: Solutions to Laboratory Exercises MSBTE

printf("\n Enter the value of the node which has to be deleted :

");

scanf("%d", &val);

ptr = start;

while(ptr->data != val)

{

preptr = ptr;

ptr = ptr->next;

}

Preptr->next = ptr->next;

free(ptr);

return start;

}

struct node *delete_list(struct node *start)

{

// Delete every node in the list, starting from the first node

struct node *ptr;

ptr=start;

while(ptr->next != NULL)

{

printf("\n %d is to be deleted next", ptr->data);

start = delete_beg(ptr);

ptr = ptr->next;

}

return start;

}

struct node *sort_list(struct node *start)

{

// To sort the data in the linked list

struct node *ptr1, *ptr2;

int temp;

ptr1 = start; // Start from the first node

while(ptr1->next != NULL)

{

// Compare the values and swap if necessary

ptr2 = ptr1->next;

while(ptr2 != NULL)

{

if(ptr1->data > ptr2->data)

{

temp = ptr1->data;

ptr1->data = ptr2->data;

ptr2->data = temp;

}

ptr2 = ptr2->next;

}

ptr1 = ptr1->next;

}

return start;

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 21: Solutions to Laboratory Exercises MSBTE

PROGRAM 8

Write a program to create binary tree.

#include<stdio.h>

#include<conio.h>

struct node

{

int data;

struct node *left;

struct node *right;

};

struct node *tree;

void create_tree(struct node *);

struct node *insertElement(struct node *, int);

void preorderTraversal(struct node *);

void inorderTraversal(struct node *);

void postorderTraversal(struct node *);

struct node *findSmallestElelemt(struct node *);

struct node *findLargestElelemt(struct node *);

struct node *deleteElement(struct node *, int);

struct node *mirrorImage(struct node *);

int totalNodes(struct node *);

int totalExternalNodes(struct node *);

int totalInternalNodes(struct node *);

int Height(struct node *);

struct node *deleteTree(struct node *);

main()

{

int option, val;

struct node *ptr;

create_tree(tree);

clrscr();

do

{

printf("\n ********** MAIN MENU ************** \n");

printf("\n 1. Insert Element");

printf("\n 2. Preorder Traversal");

printf("\n 3. Inorder Traversal");

printf("\n 4. Postorder Traversal");

printf("\n 5. Find the smallest element");

printf("\n 6. Find the largest element");

printf("\n 7. Delete an element");

printf("\n 8. Count the total number of nodes");

printf("\n 9. Count the total number of external nodes");

printf("\n 10. Count the total number of internal nodes");

printf("\n 11. Determine the height of the tree");

printf("\n 12. Find the mirror image of the tree");

printf("\n 13. Delete the tree");

printf("\n 14. Exit");

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 22: Solutions to Laboratory Exercises MSBTE

printf("\n\n***********************************************

***");

printf("\n\n Enter your option : ");

scanf("%d", &option);

switch(option)

{

case 1:

printf("\n Enter the value of the new node: ");

scanf("%d", &val);

tree = insertElement(tree, val);

break;

case 2:

printf("\n The elements of the tree are : \n");

preorderTraversal(tree);

break;

case 3:

printf("\n The elements of the tree are : \n");

inorderTraversal(tree);

break;

case 4:

printf("\n The elements of the tree are : \n");

postorderTraversal(tree);

break;

case 5:

ptr = findSmallestElelemt(tree);

printf("\n The smallest element in the tree is :

%d", ptr->data);

break;

case 6:

ptr = findLargestElelemt(tree);

printf("\n The smallest element in the tree is :

%d", ptr->data);

break;

case 7:

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

scanf("%d", &val);

tree = deleteElement(tree, val);

break;

case 8:

printf("\n Total number of nodes in the tree is =

%d", totalNodes(tree));

break;

case 9:

printf("\n Total number of external nodes in the

tree is = %d", totalExternalNodes(tree));

break;

case 10:

printf("\n Total number of internal nodes in the

tree is = %d", totalInternalNodes(tree));

break;

case 11:

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 23: Solutions to Laboratory Exercises MSBTE

printf("\n The height of the binary search tree

is = %d", Height(tree));

break;

case 12:

tree = mirrorImage(tree);

break;

case 13:

tree = deleteTree(tree);

break;

}

}

while(option!=14);

getch();

return 0;

}

void create_tree(struct node *tree)

{

tree = NULL;

}

struct node *insertElement( struct node *tree, int val)

{

// create a new node

struct node *ptr, *nodeptr, *parentptr;

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

ptr->data = val;

ptr->left = NULL;

ptr->right = NULL;

if(tree==NULL) // if the tree does not exist

{

tree=ptr;

tree->left=NULL;

tree->right=NULL;

}

else // if the tree already exists

{

parentptr=NULL;

nodeptr=tree; // find the position for the new node

while(nodeptr!=NULL)

{

parentptr=nodeptr; // insert the element

if(val<nodeptr->data)

nodeptr=nodeptr->left;

else

nodeptr = nodeptr->right;

}

if(val<parentptr->data)

parentptr->left = ptr;

else

parentptr->right = ptr;

}

return tree;

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 24: Solutions to Laboratory Exercises MSBTE

}

void preorderTraversal(struct node *tree)

{

if(tree != NULL)

{

printf("%d\t", tree->data); // first print the data

preorderTraversal(tree->left); // traverse the left subtree

preorderTraversal(tree->right); // traverse the right

subtree

}

}

void inorderTraversal(struct node *tree)

{

if(tree != NULL)

{

inorderTraversal(tree->left);

printf("%d\t", tree->data);

inorderTraversal(tree->right);

}

}

void postorderTraversal(struct node *tree)

{

if(tree != NULL)

{

postorderTraversal(tree->left);

postorderTraversal(tree->right);

printf("%d\t", tree->data);

}

}

struct node *findSmallestElelemt(struct node *tree)

{

if( (tree == NULL) || (tree->left == NULL))

return tree;

else

return findSmallestElelemt(tree->left);

}

struct node *findLargestElelemt(struct node *tree)

{

if( (tree == NULL) || (tree->right == NULL))

return tree;

else

return findLargestElelemt(tree->right);

}

struct node *deleteElement(struct node *tree, int val)

{

struct node *ptr;

if(tree==NULL)

printf("\n %d is not present in the tree", val);

else if(val<tree->data)

deleteElement(tree->left, val);

else if(val>tree->data)

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 25: Solutions to Laboratory Exercises MSBTE

deleteElement(tree->right, val);

else

{

if(tree->left && tree->right)

{

ptr = findLargestElelemt(tree->left);

tree->data = ptr->data;

deleteElement(tree->left, ptr->data);

}

else

{

ptr=tree;

if(tree->left==NULL && tree->right==NULL)

tree=NULL;

else if(tree->left!=NULL)

tree=tree->left;

else

tree=tree->right;

free(ptr);

}

}

return tree;

}

int totalNodes(struct node *tree)

{

if(tree==NULL)

return 0;

else

return( totalNodes(tree->left) + totalNodes(tree->right) + 1);

}

int totalExternalNodes(struct node *tree)

{

if(tree==NULL)

return 0;

else if((tree->left==NULL) && (tree->right==NULL))

return 1;

else

return (totalExternalNodes(tree->left) +

totalExternalNodes(tree->right));

}

int totalInternalNodes(struct node *tree)

{

if( (tree==NULL) || ((tree->left==NULL) && (tree->right==NULL)))

return 0;

else

return (totalInternalNodes(tree->left) +

totalInternalNodes(tree->right) + 1);

}

int Height(struct node *tree)

{

int leftheight, rightheight;

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 26: Solutions to Laboratory Exercises MSBTE

if(tree==NULL)

return 0;

else

{

leftheight = Height(tree->left);

rightheight = Height(tree->right);

if(leftheight > rightheight)

return (leftheight + 1);

else

return (rightheight + 1);

}

}

struct node *mirrorImage(struct node *tree)

{

struct node *ptr;

if(tree!=NULL)

{

mirrorImage(tree->left);

mirrorImage(tree->right);

ptr=tree->left;

ptr->left = ptr->right;

tree->right = ptr;

}

}

struct node *deleteTree(struct node *tree)

{

if(tree!=NULL)

{

deleteTree(tree->left);

deleteTree(tree->right);

free(tree);

}

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 27: Solutions to Laboratory Exercises MSBTE

PROGRAM 9

Write a program to create a graph of ‘n’ vertices using an adjacency list.

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

typedef struct node

{

char vertex;

struct node *next;

};

void displayGraph(struct node *adj[], int no_of_nodes);

void deleteGraph(struct node *adj[], int no_of_nodes);

void readGraph(struct node *adj[], int no_of_nodes);

main()

{

struct node *Adj[10];

int no_of_nodes, i;

clrscr();

printf("\n Enter the number of nodes in G : ");

scanf("%d", &no_of_nodes);

for(i=0;i<=no_of_nodes;i++) // initialize the adjacency list

Adj[i] = NULL;

readGraph(Adj, no_of_nodes);

printf("\n The graph is : ");

displayGraph(Adj, no_of_nodes);

deleteGraph(Adj, no_of_nodes);

getch();

return 0;

}

void readGraph(struct node *Adj[], int no_of_nodes)

{

struct node *new_node, *last;

int i, j, n, val;

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

{

// for each node in the graph

last = NULL;

printf("\n Enter the number of neighbours of %d : ", i);

scanf("%d", &n);

for( j=1;j<=n;j++)

{

printf("\n Enter the %dth neighbour of %d : j, i);

scanf("%d", &val);

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

new_node->vertex = val;

new_node->next = NULL;

if (Adj[i] == NULL) // build the adjacency list

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 28: Solutions to Laboratory Exercises MSBTE

Adj[i] = new_node;

else // add the new node at the

// end of the adjacency list

last->next = new_node;

last = new_node;

}

}

}

void printGraph ( struct node *Adj[], int no_of_nodes)

{

struct node *ptr;

int i;

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

{

ptr = Adj[i];

printf("\n The neighbours of node %d are : ", i);

while(ptr != NULL)

{

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

ptr = ptr->next;

}

}

}

void deleteGraph ( struct node *Adj[], int no_of_nodes)

{

// delete the nodes of the graph

int i;

struct node *temp, *ptr;

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

{

ptr = Adj[i];

while(ptr!=NULL)

{

temp = ptr;

ptr = ptr->next;

free(temp);

}

Adj[i] = NULL;

}

}

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 29: Solutions to Laboratory Exercises MSBTE

PROGRAM 10

Write a program to search an element using hashing techniques.

#include<stdio.h>

#include<conio.h>

int ht[10], i, found = 0, key;

void insert_val();

void search_val();

void delete_val();

void display();

main()

{

int option;

clrscr();

for ( i = 0;i < 10;i++ ) //to initialise every element as '-1'

ht[i] = -1;

do

{

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

printf( "\n MENU \n1.Insert \n2.Search \n3.Delete

\n4.Display \n5.Exit" );

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

scanf( "%d", &option);

switch (option)

{

case 1:

insert_val();

break;

case 2:

search_val();

break;

case 3:

delete_val();

break;

case 4:

display();

break;

default:

printf( "\nInvalid choice entry!!!\n" );

break;

}

}while (option!=5);

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 30: Solutions to Laboratory Exercises MSBTE

getch();

return 0;

}

void insert_val()

{

int val, f = 0;

printf( "\nEnter the element to be inserted : " );

scanf( "%d", &val );

key = ( val % 10 ) - 1;

if ( ht[key] == -1 )

{

ht[key] = val;

}

else

{

if ( key < 9 )

{

for ( i = key + 1;i < 10;i++ )

{

if ( ht[i] == -1 )

{

ht[i] = val;

break;

}

}

}

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

{

if ( ht[i] == -1 )

{

ht[i] = val;

break;

}

}

}

}

void display()

{

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

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

}

void search_val()

{

int val, flag = 0;

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 31: Solutions to Laboratory Exercises MSBTE

printf( "\nEnter the element to be searched :: " );

scanf( "%d", &val );

key = ( val % 10 ) - 1;

if ( ht[ key ] == val )

flag = 1;

else

{

for ( i = key + 1;i < 10;i++ )

{

if(ht[i] == val)

{

flag = 1;

key = i;

break;

}

}

}

if ( flag == 0 )

{

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

{

if ( ht[ i ] == val )

{

flag = 1;

key = i;

break;

}

}

}

if ( flag == 1 )

{

found=1;

printf("\n The item searched was found in the hash table at

position %d !", key + 1 );

}

else

{

key = -1;

printf( "\n The item searched was not found in the hash

table" );

}

}

void delete_val()

{

search_val();

if (found==1)

{

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved

Page 32: Solutions to Laboratory Exercises MSBTE

if ( key != -1 )

{

printf( "\nThe element deleted is %d ", ht[ key ] );

ht[ key ] = -1;

}

}

}

PROGRAM 11

Design two mini projects based on above programs.

Please refer to the ‘Projects’ uploaded separately on students web resources for this book.

Data Structures Using C (MSBTE), 1/e Reema Thareja

© Oxford University Press 2013. All rights reserved