lab manuallab manual course : cs1243 data structures lab class : b.sc computer science semester : 2...

Post on 20-Jan-2021

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

LAB MANUAL

Course : CS1243 Data Structures Lab

Class : B.Sc Computer Science

Semester : 2

Prepared By : Nidhi S

COLLEGE OF APPLIED SCIENCE

PERISSERY

CS 1243: Data Structures Lab

P a g e 2 | 52

TABLE OF CONTENTS

1. Array – Insertion …………………………………………………………..03

2. Array – Deletion …………………………………………………………...05

3. Largest and smallest ……………………………………………………….07

4. Linear search ………………………………………………………………09

5. Binary search ………………………………………………………………11

6. Bubble sort ………………………………………………………………...13

7. Insertion sort ……………………………………………………………….15

8. Selection sort ………………………………………………………………17

9. Stack using array …………………………………………………………..19

10. Infix to postfix ……………………………………………………………..22

11. Queue using array ………………………………………………………….24

12. Singly linked list – Insertion ………………………………………………27

13. Singly linked list – Deletion ……………………………………………….31

14. Singly linked list – Search …………………………………………………35

15. Singly linked list – Count the nodes ……………………………………….37

16. Singly linked list – Traversal ………………………………………………39

17. Stack using linked list ……………………………………………………...41

18. Queue using linked list …………………………………………………….44

19. Binary search tree – Traversal ……………………………………………..47

20. Binary search tree – Search ………………………………………………..50

CS 1243: Data Structures Lab

P a g e 3 | 52

1. ARRAY - INSERTION

Aim:

To write a c program to insert an item into an array.

Program:

#include <stdio.h>

void main()

{

int a[20], size, pos, item, i;

clrscr();

printf("\nEnter the size of array:\t");

scanf("%d", &size);

printf("\nEnter %d element:\n", size);

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

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

printf("Enter the new item for insertion and its position in array:\n");

scanf("%d%d", &item, &pos);

if(pos >= 0 && pos < size)

{

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

a[i] = a[i - 1];

a[pos] = item;

size++;

printf("\nArray after insertion:");

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

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

}

else

printf(“Insertion not possible!!!”);

getch();

}

CS 1243: Data Structures Lab

P a g e 4 | 52

Output:

CS 1243: Data Structures Lab

P a g e 5 | 52

2. ARRAY - DELETION

Aim:

To write a c program to delete an item from an array.

Program:

#include<stdio.h>

void main()

{

int a[10], size, pos, i;

clrscr();

printf("\nEnter the size of array:\t");

scanf("%d", &size);

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

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

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

printf("Enter the position(<%d) of the element to be deleted:\t",size);

scanf("%d", &pos);

if (pos >= 0 && pos < size)

{

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

a[i] = a[i+1];

size--;

printf("Array after deletion:");

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

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

}

else

printf("Deletion not possible!!!");

getch();

}

CS 1243: Data Structures Lab

P a g e 6 | 52

Output:

CS 1243: Data Structures Lab

P a g e 7 | 52

3. LARGEST AND SMALLEST

Aim:

To write a c program to find the largest and smallest elements in the given array.

Program:

#include<stdio.h>

void main()

{

int a[10], size, i, small, large;

clrscr();

printf("\nEnter the size of array:\t");

scanf("%d", &size);

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

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

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

small=large=a[0];

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

{

if (a[i] < small)

small = a[i];

if (a[i] > large)

large = a[i];

}

printf("Largest = %d\nSmallest = %d",large,small);

getch();

}

CS 1243: Data Structures Lab

P a g e 8 | 52

Output

CS 1243: Data Structures Lab

P a g e 9 | 52

4. LINEAR SEARCH

Aim:

To write a c program to search an element in array using linear search.

Program:

#include<stdio.h>

void main()

{

int arr[10], key, i, n, pos = -1;

clrscr();

printf("\nEnter the size of array : ");

scanf("%d", &n);

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

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

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

printf("Enter the number that has to be searched : ");

scanf("%d", &key);

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

{

if(arr[i] == key)

{

pos=i;

printf("%d is found at index %d", key,i);

break;

}

}

if (pos == -1)

printf("%d not found in the array", key);

getch();

}

CS 1243: Data Structures Lab

P a g e 10 | 52

Output:

CS 1243: Data Structures Lab

P a g e 11 | 52

5. BINARY SEARCH

Aim:

To write a c program to search an element in sorted array using binary search.

Program:

#include<stdio.h>

void main()

{

int arr[10], key, i, n, beg, end, mid, pos=-1;

clrscr();

printf("\nEnter the size of array: ");

scanf("%d", &n);

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

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

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

printf("Enter the search key: ");

scanf("%d", &key);

beg = 0, end = n-1;

while(beg<=end)

{

mid = (beg + end)/2;

if (arr[mid] == key)

{

printf("\n%d is present at position %d", key, mid);

pos=mid;

break;

}

else if (arr[mid]>key)

end = mid-1;

else

beg = mid+1;

}

CS 1243: Data Structures Lab

P a g e 12 | 52

if (pos == -1)

printf("\n%d not found", key);

getch();

}

Output:

CS 1243: Data Structures Lab

P a g e 13 | 52

6. BUBBLE SORT

Aim:

To write a c program to sort an array using bubble sort.

Program:

#include<stdio.h>

void main()

{

int nums[10], size, temp, i=0, j;

clrscr();

printf("\nEnter the size of array: ");

scanf("%d",&size);

printf("\nEnter the numbers: \n");

while(i < size)

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

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

{

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

{

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

{

temp = nums[j];

nums[j]=nums[j+1];

nums[j+1] = temp;

}

}

}

printf("\nSorted list is \t");

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

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

getch();

}

CS 1243: Data Structures Lab

P a g e 14 | 52

Output:

CS 1243: Data Structures Lab

P a g e 15 | 52

7. INSERTION SORT

Aim:

To write a c program to sort an array using insertion sort.

Program:

#include<stdio.h>

void main()

{

int nums[10], size, small, i, j;

clrscr();

printf("\nEnter the size of array: ");

scanf("%d",&size);

printf("\nEnter the numbers: \n");

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

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

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

{

small = nums[i];

j = i - 1;

while(j >= 0 && small < nums[j])

{

nums[j+1] = nums[j];

j=j-1;

}

nums[j+1] = small;

}

printf("\nSorted list is");

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

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

getch();

}

CS 1243: Data Structures Lab

P a g e 16 | 52

Output:

CS 1243: Data Structures Lab

P a g e 17 | 52

8. SELECTION SORT

Aim:

To write a c program to sort an array using selection sort.

Program:

#include<stdio.h>

void main()

{

int a[10], n, i, j, min, temp;

clrscr();

printf("\nEnter the size of array: ");

scanf("%d",&n);

printf("\nEnter %d Elements: \n",n);

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

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

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

{

min = i;

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

if (a[min] > a[j])

min = j;

if (min != i)

{

temp = a[i];

a[i] = a[min];

a[min] = temp;

}

}

printf("\n The Sorted array in ascending order:");

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

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

getch();

}

CS 1243: Data Structures Lab

P a g e 18 | 52

Output:

CS 1243: Data Structures Lab

P a g e 19 | 52

9. STACK USING ARRAY

Aim:

To write a c program to implement stack using array.

Program:

#include<stdio.h>

void main()

{

int stack[10], i, size, top=-1, item, choice;

clrscr();

printf("Enter size of stack: ");

scanf("%d",&size);

do

{

printf("Menu - Stack operations");

printf("\t1.Display\t2.Push\t3.Pop\t4.Exit\t");

scanf("%d",&choice);

switch(choice)

{

case 1:

if (top == -1)

printf("EMPTY STACK!!!\n");

else

{

printf("The elements on the array are :");

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

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

printf("\n");

}

break;

case 2:

if (top == size-1)

printf("Stack is full!!!\n");

CS 1243: Data Structures Lab

P a g e 20 | 52

else

{

printf("Enter new item: ");

scanf("%d",&item);

stack[++top] = item;

printf("Inserted %d.\n",stack[top]);

}

break;

case 3:

if(top == -1)

printf("Stack is empty!!!\n");

else

{

item = stack[top--];

printf("Item deleted: %d\n",item);

}

break;

case 4:

exit(0);

default:

printf("Invalid choice!!!\n");

break;

}

}while(choice!=4);

getch();

}

CS 1243: Data Structures Lab

P a g e 21 | 52

Output:

CS 1243: Data Structures Lab

P a g e 22 | 52

10. INFIX TO POSTFIX

Aim:

To write a c program to convert an infix expression to postfix expression using stack.

Program:

#include<stdio.h>

#include <ctype.h>

int pr(char s)

{

if(s == '^')

return(3);

else if(s == '*' || s == '/')

return(2);

else if(s == '+' || s == '-')

return(1);

else

return(0);

}

void main()

{

char in[50], stack[50], post[50], ch;

int top = -1, i = 0, k = 0;

printf("\nEnter Infix Expression : ");

scanf("%s",in);

stack[++top] = '#';

while( (ch=in[i++]) != '\0')

{

if( ch == '(')

stack[++top] = ch;

else if(isalnum(ch))

post[k++] = ch;

else if( ch == ')')

{

CS 1243: Data Structures Lab

P a g e 23 | 52

while( stack[top] != '(')

post[k++] = stack[top--];

top--;

}

else

{

while( pr(stack[top]) >= pr(ch) )

post[k++] = stack[top--];

stack[++top] = ch;

}

}

while( stack[top] != '#')

post[k++] = stack[top--];

post[k] = '\0';

printf("\nPostfix Expression = %s\n",post);

getch();

}

Output:

CS 1243: Data Structures Lab

P a g e 24 | 52

11. QUEUE USING ARRAY

Aim:

To write a c program to implement queue using array.

Program:

#include<stdio.h>

void main()

{

int queue[20], front=-1, rear=-1, val, size, choice, i;

clrscr();

printf("Enter the size of the Queue: ");

scanf("%d",&size);

do

{

printf("Queue");

printf("\t1.Insert\t2.Delete\t3.Display\t4. Quit: ");

scanf("%d",&choice);

switch(choice)

{

case 1:

if (rear == size-1)

printf("Queue is full!!!!\n");

else

{

printf("Enter the value to be added: ");

scanf("%d",&val);

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

front = front + 1;

rear = rear + 1;

queue[rear] = val;

printf("Inserted %d\n",queue[rear]);

}

break;

CS 1243: Data Structures Lab

P a g e 25 | 52

case 2:

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

printf("Queue is empty!!!\n");

else

{

val = queue[front];

front = front+1;

printf("Deleted %d\n",val);

}

break;

case 3:

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

printf("Queue is empty!!!\n");

else

{

printf("The content of queue are:");

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

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

printf("\n");

}

break;

case 4:

exit(0);

default:

printf("Invalid choice!!!\n");

break;

}

}

while(choice!=4);

getch();

}

CS 1243: Data Structures Lab

P a g e 26 | 52

Output:

CS 1243: Data Structures Lab

P a g e 27 | 52

12. SINGLY LINKED LIST - INSERTION

Aim:

To write a c program to illustrate the insertion operation in a singly linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

struct node *temp,*newnode,*start=NULL;

void create()

{

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

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

scanf("%d",&newnode->data);

newnode->next =NULL;

}

void display()

{

temp=start;

while(temp!=NULL)

{

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

temp=temp->next ;

}

printf("NULL");

}

void main()

{

int choice,key,flag;

CS 1243: Data Structures Lab

P a g e 28 | 52

clrscr();

while(1)

{

printf("\n1.Beginning 2.End 3.After 4.Exit:\t");

scanf("%d",&choice);

switch(choice)

{

case 1:

create();

if(start==NULL)

start=newnode;

else

{

newnode->next=start;

start=newnode;

}

display();

break;

case 2:

create();

if(start==NULL)

start=newnode;

else

{

temp=start;

while(temp->next !=NULL)

temp=temp->next ;

temp->next =newnode;

}

display();

break;

case 3:

CS 1243: Data Structures Lab

P a g e 29 | 52

printf("Enter the key after which new node to be

inserted:\t");

scanf("%d",&key);

flag=0;

temp=start;

while(temp!=NULL)

{

if(temp->data==key)

{

create();

newnode->next=temp->next;

temp->next=newnode;

flag=1;

display();

break;

}

else

temp=temp->next;

}

if(flag==0)

printf("Key not found!!!\n");

break;

case 4:

exit(0);

default:

printf("Wrong Choice\n");

break;

}

}

}

CS 1243: Data Structures Lab

P a g e 30 | 52

Output:

CS 1243: Data Structures Lab

P a g e 31 | 52

13. SINGLY LINKED LIST - DELETION

Aim:

To write a c program to illustrate the deletion operation in a singly linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

} *start=NULL, *temp, *newnode, *ptr;

void create()

{

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

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

scanf("%d",&newnode->data);

newnode->next=start;

start = newnode;

}

void display()

{

temp=start;

while(temp!=NULL)

{

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

temp=temp->next ;

}

printf("NULL\n");

}

void main()

{

int choice, n, i, key;

CS 1243: Data Structures Lab

P a g e 32 | 52

clrscr();

printf("How many nodes do you want?\t");

scanf("%d",&n);

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

create();

display();

while(1)

{

printf("Delete 1.Beginning 2.End 3.Node 4.Exit: ");

scanf("%d",&choice);

switch(choice)

{

case 1:

if(start != NULL)

{

temp=start;

start=start->next ;

free(temp);

}

display();

break;

case 2:

temp = start;

if (start->next == NULL)

start=NULL;

else

{

while(temp->next!=NULL)

{

ptr=temp;

temp=temp->next;

}

ptr->next=NULL;

CS 1243: Data Structures Lab

P a g e 33 | 52

}

free(temp);

display();

break;

case 3:

printf("Enter node to be deleted:\t");

scanf("%d",&key);

temp=start;

while(temp != NULL && temp->data != key)

{

ptr = temp;

temp = temp->next;

}

if(temp->data == key)

{

if(temp == start)

start = start->next;

else

ptr->next = temp->next;

free(temp);

}

else

printf("Key not found!!!\n");

display();

break;

case 4:

exit(0);

default:

printf("Wrong Choice\n");

break;

}

}

}

CS 1243: Data Structures Lab

P a g e 34 | 52

Output:

CS 1243: Data Structures Lab

P a g e 35 | 52

14. SINGLY LINKED LIST - SEARCH

Aim:

To write a c program to demonstrate the search operation in a singly linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

} *start=NULL, *temp, *newnode;

void create()

{

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

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

scanf("%d",&newnode->data);

newnode->next=start;

start = newnode;

}

void main()

{

int n, i, key;

clrscr();

printf("How many nodes do you want?\t");

scanf("%d",&n);

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

create();

temp=start;

while(temp!=NULL)

{

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

temp=temp->next ;

CS 1243: Data Structures Lab

P a g e 36 | 52

}

printf("NULL\n");

printf("Enter node to be searched:\t");

scanf("%d",&key);

temp=start;

i = 0;

while(temp != NULL && temp->data != key)

{

i++;

temp = temp->next;

}

if(temp->data == key)

printf("Node with data %d found at position %d.",key,i);

else

printf("Key not found!!!\n");

getch();

}

Output:

CS 1243: Data Structures Lab

P a g e 37 | 52

15. SINGLY LINKED LIST – COUNT THE NODES

Aim:

To write a c program to count the total number of nodes in a singly linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

} *start=NULL, *temp, *newnode;

void create()

{

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

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

scanf("%d",&newnode->data);

newnode->next=start;

start = newnode;

}

void main()

{

int choice=1, n, i;

clrscr();

while(choice==1)

{

create();

printf("\nPress 1 to CONTINUE and press any other key to STOP : ");

scanf("%d",&choice);

}

i=0;

for(temp=start;temp != NULL;temp=temp->next)

{

CS 1243: Data Structures Lab

P a g e 38 | 52

i++;

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

}

printf("NULL\n");

printf("No: of nodes in this singly linked list is %d\n", i);

getch();

}

Output:

CS 1243: Data Structures Lab

P a g e 39 | 52

16. SINGLY LINKED LIST – TRAVERSAL

Aim:

To write a c program to illustrate the traversal operation in a singly linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

} *start=NULL, *temp, *newnode;

void create()

{

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

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

scanf("%d",&newnode->data);

newnode->next=start;

start = newnode;

}

void traversal()

{

temp=start;

while(temp!=NULL)

{

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

temp=temp->next ;

}

printf("NULL");

}

void main()

{

int n, i;

CS 1243: Data Structures Lab

P a g e 40 | 52

clrscr();

printf("How many nodes do you want?\t");

scanf("%d",&n);

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

create();

printf("Singly linked list : ");

traversal();

getch();

}

Output:

CS 1243: Data Structures Lab

P a g e 41 | 52

17. STACK USING LINKED LIST

Aim:

To write a c program to implement stack using linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

};

void main()

{

struct node *top=NULL,*temp,*new_node;

int choice;

clrscr();

do

{

printf("Stack operations 1.Display 2.Push 3.Pop 4.Exit: ");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("The elements in the stack : ");

temp=top;

while(temp!=NULL)

{

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

temp=temp->next;

}

printf("NULL\n");

break;

case 2:

CS 1243: Data Structures Lab

P a g e 42 | 52

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

printf("Enter new item: ");

scanf("%d",&new_node->data);

new_node->next=NULL;

if(top==NULL)

top=new_node;

else

{

new_node->next=top;

top=new_node;

}

printf("Inserted : %d\n",top->data);

break;

case 3:

if(top==NULL)

printf("Stack is empty!!!\n");

else

{

temp=top;

top=top->next;

printf("Deleted: %d\n",temp->data);

free(temp);

}

break;

case 4:

exit(0);

default:

printf("Invalid choice!!!\n");

break;

}

}while(choice!=4);

getch();

}

CS 1243: Data Structures Lab

P a g e 43 | 52

Output:

CS 1243: Data Structures Lab

P a g e 44 | 52

18. QUEUE USING LINKED LIST

Aim:

To write a c program to implement queue using linked list.

Program:

#include<stdio.h>

#include<stdlib.h>

struct node {

int data;

struct node *next;

};

void main()

{

struct node *front=NULL, *rear=NULL,*new_node,*temp;

int choice;

clrscr();

do

{

printf("Queue operations 1.Insert 2.Delete 3.Display 4.Quit : ");

scanf("%d",&choice);

switch(choice)

{

case 1:

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

printf("Enter the value to be added: ");

scanf("%d",&new_node->data);

new_node->next=NULL;

if(rear==NULL)

front=rear=new_node;

else

{

rear->next=new_node;

rear=new_node;

CS 1243: Data Structures Lab

P a g e 45 | 52

}

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

break;

case 2:

if(front==NULL)

printf("Queue is empty!!!\n");

else

{

temp=front;

front=front->next;

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

free(temp);

}

break;

case 3:

printf("Queue : ");

for(temp=front;temp!=NULL;temp=temp->next)

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

printf("NULL\n");

break;

case 4:

exit(0);

default:

printf("Invalid choice!!!\n");

break;

}

}

while(choice!=4);

getch();

}

CS 1243: Data Structures Lab

P a g e 46 | 52

Output:

CS 1243: Data Structures Lab

P a g e 47 | 52

19. BINARY SEARCH TREE – TRAVERSAL

Aim:

To write a c program to illustrate the traversal operation in binary search tree.

Program:

#include <stdio.h>

#include <stdlib.h>

struct BST {

int data;

struct BST *left, *right;

} *root = NULL, *temp;

void insert(struct BST *root, struct BST *temp)

{

if (root == NULL)

root = temp;

else if (temp->data < root->data)

{

if (root->left != NULL)

insert(root->left, temp);

else

root->left = temp;

}

else if (temp->data > root->data)

{

if (root->right != NULL)

insert(root->right, temp);

else

root->right = temp;

}

else

printf("Duplicate node --> %d\n",temp->data);

}

void preorder(struct BST *root)

CS 1243: Data Structures Lab

P a g e 48 | 52

{

if (root != NULL)

{

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

preorder(root->left);

preorder(root->right);

}

}

void postorder(struct BST *root)

{

if (root != NULL)

{

postorder(root->left);

postorder(root->right);

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

}

}

void inorder(struct BST *root)

{

if (root != NULL)

{

inorder(root->left);

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

inorder(root->right);

}

}

void main()

{

int ch;

clrscr();

printf("Enter the number of nodes you want : ");

scanf("%d", &ch);

printf("Enter %d nodes data :", ch);

CS 1243: Data Structures Lab

P a g e 49 | 52

for(;ch!=0;ch--)

{

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

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

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

if (root == NULL)

root = temp;

else

insert(root, temp);

}

printf("\nPreorder Traversal: ");

preorder(root);

printf("\nInorder Traversal: ");

inorder(root);

printf("\nPostorder Traversal: ");

postorder(root);

getch();

}

Output:

CS 1243: Data Structures Lab

P a g e 50 | 52

20. BINARY SEARCH TREE – SEARCH

Aim:

To write a c program to demonstrate the search operation in binary search tree.

Program:

#include <stdio.h>

#include <stdlib.h>

struct BST {

int data;

struct BST *left, *right;

} *root = NULL, *temp;

void insert(struct BST *root, struct BST *temp)

{

if (root == NULL)

root = temp;

else if (temp->data < root->data)

{

if (root->left != NULL)

insert(root->left, temp);

else

root->left = temp;

}

else if (temp->data > root->data)

{

if (root->right != NULL)

insert(root->right, temp);

else

root->right = temp;

}

else

printf("Duplicate node --> %d\n",temp->data);

}

void search(struct BST *root, int key)

CS 1243: Data Structures Lab

P a g e 51 | 52

{

int flag = 0;

temp = root;

while(temp != NULL)

{

if (key == temp->data)

{

printf("Success...\n");

flag=1;

break;

}

else if (key < temp->data)

temp = temp->left;

else

temp = temp->right;

}

if (flag == 0)

printf("Search failed!!!\n");

}

void main()

{

int ch, key;

clrscr();

printf("Enter the number of nodes you want : ");

scanf("%d", &ch);

printf("Enter %d nodes data :", ch);

for(;ch!=0;ch--)

{

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

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

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

if (root == NULL)

root = temp;

CS 1243: Data Structures Lab

P a g e 52 | 52

else

insert(root, temp);

}

printf("Enter the search key: \n");

scanf("%d", &key);

search(root, key);

getch();

}

Output:

top related