lab.123456789123456789123456789123456789

drdeekshu 33 views 38 slides Jun 05, 2024
Slide 1
Slide 1 of 38
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38

About This Presentation

Hjj


Slide Content

135
Tejaswi S. Asst. Prof., Seshadripuram Degree College Data Structures Using C II Sem BCA



LAB PROGRAMS



Course Code: CAC04P Course Title: Data Structures Lab
Course Credits: 02 Hours/Week: 04
Total Contact Hours: 52 Formative Assessment Marks: 25
Exam Marks: 25 Exam Duration: 03 Hours

Part A
1. Program to find GCD using recursive function
2. Program to display Pascal Triangle using binomial function
3. Program to generate n Fibonacci numbers using recursive function.
4. Program to implement Towers of Hanoi.
5. Program to implement dynamic array, find smallest and largest element of the array.
6. Program to create two files to store even and odd numbers.
7. Program to create a file to store student records.
8. Program to read the names of cities and arrange them alphabetically.
9. Program to sort the given list using selection sort technique.
10. Program to sort the given list using bubble sort technique.
Part B
1. Program to sort the given list using insertion sort technique.
2. Program to sort the given list using quick sort technique.
3. Program to sort the given list using merge sort technique.
4. Program to search an element using linear search technique.
5. Program to search an element using recursive binary search technique.
6. Program to implement Stack.
7. Program to convert an infix expression to postfix.
8. Program to implement simple queue.
9. Program to implement linear linked list.
10. Program to display traversal of a tree.

136
Tejaswi S. Asst. Prof., Seshadripuram Degree College Data Structures Using C II Sem BCA



Evaluation Scheme for Lab Examination

Assessment Criteria Marks
Program – 1 from Part B Flowchart / Algorithm 02
Writing the Program 04
Execution and Formatting 04
Program – 2 from Part B Flowchart / Algorithm 02
Writing the Program 04
Execution and Formatting 04
Viva Voice based on C Programming 02
Practical Record 03
Total 25

137
Tejaswi S. Asst. Prof., Seshadripuram Degree College Data Structures Using C II Sem BCA



1. Program to find GCD using recursive function


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

int hcf (int n1, int n2);


void main()
{
int n1, n2;
clrscr();

printf ("Enter two positive integers: ");
scanf ("%d %d", &n1, &n2);

printf ("G.C.D of %d and %d is %d", n1, n2, hcf (n1, n2));
getch();
}


int hcf (int n1, int n2)
{
if (n2!= 0)
return hcf (n2, n1 % n2);


else
return n1;
}

138
Tejaswi S. Asst. Prof., Seshadripuram Degree College Data Structures Using C II Sem BCA



2. Program to display Pascal Triangle using binomial function
#include<stdio.h>
#include<conio.h>

void main()
{
int n, i, j, k;
clrscr();
printf ("Enter the number of rows: ");
scanf ("%d", &n);

for (i=1; i<=n; i++)
{
int coef = 1;


for (k = n - i; k >= 0; k--)


printf(" ");


for (j=1; j<=i; j++)
{
printf ("%d ", coef);
coef = coef * (i – j ) / j;
}
printf("\n");
}
getch();
}

139
3. Program to generate “n” Fibonacci numbers using recursive function.
#include<stdio.h>
#include<conio.h>
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA




int Fibonacci(int);


void main()
{
int n, i = 0, c;
clrscr();
printf ("enter the number\n");
scanf ("%d", &n);
printf ("Fibonacci series\n");


for (c = 1 ; c <= n ; c++)
{
printf ("%d\n", Fibonacci(i));
i++;
}
getch();
}
int Fibonacci (int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (Fibonacci (n – 1) + Fibonacci(n – 2));
}

140
getch();
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



4. Program to implement Tower of Hanoi
#include<stdio.h>
#include<conio.h>
int count=0;
void tower (int n, int source, int temp, int dest)
{
if (n==1)
{
printf ("move disk 1 from %c to %c\n", source, dest);
count++;
return;
}
tower (n-1, source, dest, temp);
printf ("move disk %d from %c to %c \n", n, source, dest);
count++;
tower (n-1, temp, source, dest);
}


void main()
{
int n;
clrscr();

printf ("enter the number of disks\n");
scanf ("%d", &n);

tower (n, 'a', 'b', 'c');


printf ("\n total number of disc moves =%d\n", count);

141
getch();
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



5. Program to implement dynamic array, find smallest and largest element of the
array
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i, n, *data;
clrscr();
printf ("How many elements you want to add");
scanf ("%d", &n);

data = (int*) calloc(n, sizeof(int)); //Allocates memory for n elements
if (data == NULL)
{
printf ("Error!!! Memory is Not Allocated.");
exit(0);
}
printf ("\n");
for (i = 0; i < n; ++i) // Stores the numbers entered by the user
{
printf ("Enter Number %d: ", i + 1);
scanf ("%d", data + i);
}
for (i = 1; i < n; ++i) // Loop to store largest number at address data
{
if (*data < *(data + i))
*data = *(data + i);
}
printf ("\nLargest Element = %d", *data);

142
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



6. Program to create two files to store even and odd numbers
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpe, *fpo;
int i;
clrscr();

fpe = fopen ("even.txt", "w");
fpo = fopen ("odd.txt", "w");


fprintf (fpe,"even numbers are\n");
fprintf (fpo,"odd numbers are\n");

for (i=1; i<=100; i++)
{
if (i % 2 == 0)
fprintf (fpe, "%d\n", i);


else
fprintf(fpo, "%d\n", i);
}
fclose (fpo);
fclose (fpe);
getch();
}
OUTPUT: In Bin file you can find two files namely EVEN.TXT and ODD.TXT and in these
text file you can see the even and odd numbers till 100 respectively.

143
7. Program to create a file and store student records
#include<stdio.h>
#include<conio.h>
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA


void main()
{
FILE *fptr;
char name [20];
int age, salary;
clrscr();
//open for writing
fptr = fopen ("emp.txt", "w");
if (fptr == NULL)
{
printf ("File does not exists\n");
return;
}
printf ("Enter the name\n");
gets(name);
fprintf (fptr, "Name = %s\n", name);
printf ("enter age\n");
scanf ("%d", &age);
fprintf (fptr, "Age = %d\n", age);


printf ("enter salary\n");
scanf ("%d", &salary);
fprintf (fptr, "Salary = %d", salary);


fclose(fptr);
getch();
}
OUTPUT: In Bin file you can find a file namely EMP.TXT and in this text file you can find
the details what you have given in program.

144
8. Program to read the names of cities and arrange them alphabetically.
#include<stdio.h>
#include<conio.h>
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA


#include<string.h>
void main()
{
int i, j, n;
char str[100][100], temp[100];
clrscr();
printf ("Enter number of names :\n");
scanf ("%d", &n);
printf ("Enter names in any order:\n");
for (i=0; i<n; i++)
{
scanf ("%s", str[i]);
}
for (i=0; i<n; i++)
{
for (j=i+1; j<n; j++)
{
if(strcmp(str[i], str[j]) > 0)
{
strcpy (temp, str[i]);
strcpy (str[i], str[j]);
strcpy (str[j], temp);
}
}
}
printf("\nThe sorted order of names are:\n");
for (i=0; i<n; i++)
{
printf ("%s\n", str[i]);
}
getch();

135
getch();
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



9. Program to sort the given list using Selection Sort technique
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, temp, a[20], pos;
clrscr();
printf ("Enter total elements:\n");
scanf ("%d", &n);
printf ("Enter %d elements:\n", n);
for (i=0; i<n; i++)
{
scanf ("%d", &a[i]);
}
for (i=0; i<n; i++)
{
pos = i;
for (j=i+1; j<n; j++)
{
if(a[j] < a[pos])
{
pos = j;
}
}
temp = a[i];
a[i] = a[pos];
a[pos] = temp;
}
printf ("After sorting ");
for (i=0; i<n; i++)
{
printf (" %d", a[i]);
}

136
getch();
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



10. Program to sort the given list using Bubble Sort technique
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, temp, a[20];
clrscr();
printf ("Enter the no. of elements to be sorted: \n");
scanf ("%d", &n);
//input the elements to sort
printf ("Enter the number values you want to sort\n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
for (i=1; i<n; i++)
{
for (j=0; j<n-i; j++)
{
if (a[j] >= a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
//printing the sorted elements
printf ("Array after sorting\n");
for (i = 0 ; i <n ; i++ )
printf ("%d\t", a[i]);

137
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



Part B
1. Program to sort the given list using Insertion Sort technique
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n, temp, a[20];
clrscr();
printf ("Enter total elements: ");
scanf ("%d", &n);

printf("Enter %d elements: ",n);
for (i=0; i<n; i++)
{
scanf ("%d", &a[i]);
}
for (i=1; i<n; i++)
{
temp = a[i];
for (j = i; j > 0 && temp < a[j-1]; j--)
{
a[j] = a[j-1];
}
a[j] = temp;
}
printf ("After sorting: ");
for (i=0; i<n; i++)
printf (" %d", a[i]);
getch();
}

138
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



2. Program to sort the given list using Quick Sort technique
#include<stdio.h>
#include<conio.h>
void quicksort (int [10], int, int);


void main()
{
int x[20], size, i;
clrscr();

printf ("Enter size of the array:\n");
scanf ("%d", &size);

printf ("Enter %d elements", size);
for (i=0; i<size; i++)
scanf ("%d", &x[i]);
quicksort(x, 0, size-1);
printf ("Sorted elements:\n");
for (i=0; i<size; i++)
printf ("%d\t", x[i]);


getch();
}

139
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



void quicksort (int x[10],int first, int last)
{
int pivot, j, temp, i;
printf ("first = %d\n, last = %d\n", first, last);


if (first < last)
{
pivot = first;
i = first;
j = last;


printf ("pivot = %d\n", pivot);
printf ("i = %d\n", i);
printf ("j = %d\n", j);


while (i < j)
{
while(x[i] <= x[pivot] && i < last)
{
i++;
printf("\n \t %d %d %d", i, pivot, last);
}


while(x[j] > x[pivot])
{
j--;
printf("\n \t %d %d ", j, pivot);
}

140
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



if(i < j)
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
temp = x[pivot];
x[pivot] = x[j];
x[j] = temp;

printf ("\n \t %d %d %d", j, pivot, temp);


quicksort (x, first, j-1);
quicksort (x, j+1, last);
}
}

141
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



3. Program to sort the given list using Merge Sort technique

#include<stdio.h>
#include<conio.h>
#define MAX 50

void mergeSort (int arr[], int low, int mid, int high);
void partition(int arr[], int low, int high);

void main()
{
int merge[MAX], i, n;
clrscr();

printf ("Enter the total number of elements:\n");
scanf ("%d", &n);

printf ("Enter the elements which to be sort:\n");
for (i=0; i <n; i++)
{
scanf ("%d", &merge[i]);
}
partition (merge, 0, n-1);
printf ("After merge sorting elements are:\n ");
for (i=0; i<n; i++)
{
printf ("%d ", merge[i]);
}


getch();
}

142
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



void partition (int arr[], int low, int high)
{
int mid;
printf ("\n\t partition low=%d\n high=%d\n ", low, high);
if (low < high)
{
mid = (low + high) / 2;
printf ("mid=%d\n", mid);

partition (arr, low, mid);
partition (arr, mid+1, high);
mergeSort (arr, low, mid, high);
}
}
void mergeSort (int arr[], int low, int mid, int high)
{
int i, m, k, l, temp[MAX];


printf ("\n\t merge low=%d\n mid=%d\n high=%d\n", low, mid, high);
l = low;
i = low;
m = mid + 1;


while((l<=mid) && (m<=high))
{
if (arr[l] <= arr[m])
{
temp[i] = arr[l];
printf ("arr=%d\n arr=%d\n", arr[i], arr[l]);
l++;
}

143
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



else
{




}
i++;



temp[i] = arr[m];
printf ("arr=%d\n arr=%d\n", temp[i], arr[m]);
m++;
printf ("i=%d\n", i);
}


if (l > mid)
{
For (k=m; k<=high; k++)
{
temp[i] = arr[k];
printf ("temp=%d\n arr=%d\n", temp[i], arr[k]);
i++;
printf ("i=%d\n", i);
}
}


else
{



for (k=l; k<=mid; k++)
{
temp[i] = arr[k];
printf ("temp=%d\n arr=%d\n ",temp[i], arr[k]);
i++;
printf ("i=%d\n ", i);
}
}

144
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



for (k=low; k<=high; k++)
{
arr[k] = temp[k];
printf ("arr=%d\n temp=%d\n ",arr[k], temp[k]);
}
}

145
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



4. Program to search an element using Linear Search technique
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], search, c, n;
clrscr();

printf ("Enter the number of elements in array\n");
scanf ("%d", &n);

printf ("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf ("%d", &array[c]);


printf ("Enter the number to search\n");
scanf ("%d", &search);

for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf ("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf ("%d is not present in array.\n", search);
getch();
}

146
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



5. Program to search an element using recursive Binary Search technique
#include<stdio.h>
#include<conio.h>
int bs (int arr[], int lo, int hi, int item)
{
int mid;
if (lo > hi)
return -1;
mid = (lo + hi) / 2;
if (arr[mid] == item)
return mid;
else if (arr[mid] > item)
bs (arr, lo, mid - 1, item);
else if (arr[mid] < item)
bs (arr, mid + 1, hi, item);
}
void main()
{
int arr[] = { 10, 21, 23, 46, 75 };
int index = 0;
int item = 0;
clrscr();
printf ("Enter item to search: ");
scanf ("%d", &item);
index = bs (arr, 0, 5, item);
if (index == -1)
printf ("Item not found in array\n");
else
printf ("Item found at index %d\n", index);
getch();

147
}
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



6. Program to implement Stack
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 5 //Maximum number of elements that can be stored
int top= -1, stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
while(1) //infinite loop, will end when choice will be 4
{
printf ("\n*** Stack Menu ***");
printf ("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf ("\n\nEnter your choice(1-4):");
scanf ("%d", &ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf ("\nWrong Choice!!");
}
}

148
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



void push()
{
int val;
if (top == MAX-1)
{


}
else
{






}
}
printf ("\nStack is full!!");




printf ("\nEnter element to push:");
scanf ("%d", &val);
top = top + 1;
stack[top]=val;
void pop()
{
if (top == -1)
{


}
else
{



}
}
printf ("\nStack is empty!!");




printf ("\nDeleted element is %d", stack[top]);
top = top - 1;

149
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



void display()
{
int i;
if (top == -1)
{


}
else
{




}
}
printf ("\nStack is empty!!");




printf ("\nStack is...\n");
for(i = top; i >= 0; --i)
printf ("%d\n", stack[i]);

150
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



7. Program to convert an infix expression to postfix
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
char stack [100];
int top = -1;

void push (char x)
{
stack[++top] = x;
}
char pop ()
{
if (top == -1)
return -1;
else
return stack[top--];
}
int priority (char x)
{
if (x == '(')
return 0;

if (x == '+' || x == '-')
return 1;

if (x == '*' || x == '/')
return 2;

return 0; //this statement is to come out of the loop
}

151
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



void main()
{
char exp [100];
char *e, x;
clrscr();
printf ("Enter the expression: ");
scanf ("%s", exp);
e = exp;
while (*e != '\0')
{
if (isalnum(*e))
printf ("%c ",*e);
else if (*e == '(')
push(*e);
else if (*e == ')')
{



}
else
{




}
e++;
}
while ((x = pop()) != '(')
printf ("%c ", x);




while (priority(stack[top]) >= priority(*e))
printf ("%c ",pop());
push(*e);
while(top != -1)
{
printf("%c ",pop());
}
getch();
}

152
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



8. Program to implement Simple Queue
#include<stdio.h>
#include<conio.h>
#define que_size 5

int choice, item, front, rear, q[10];


//Function to insert an item
void InsertQ()
{
if (rear == que_size-1)
{
printf ("Queue Overflow\n");
return;
}
rear = rear + 1;
q[rear] = item;
}


//Function to delete an item
int DeleteQ()
{
if (front > rear)
return -1;

return q[front++];
}

153
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



//Function to display an item
void Display()
{
int i;
//if queue is empty
if (front > rear)
{
printf ("Queue is empty\n");
return;
}


printf ("Contents of the queue\n");
for (i=front; i<=rear; i++)
{
printf("%d\n", q[i]);
}
}


void main()
{
front= 0;
rear= -1;
clrscr();

for(;;)
{



printf ("1.Insert 2.Delete 3. Display 4.Exit\n");
printf ("Enter the choice\n");
scanf ("%d",&choice);
switch(choice)
{

154
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



case 1:
printf ("enter the item to be inserted\n");
scanf ("%d", &item);
InsertQ();
break;
case 2:
item = DeleteQ();
if (item == -1)
printf ("Queue is empty\n");


else
printf ("item deleted = %d\n", item);
break;
case 3:
Display();
break;

case 4: exit(0);
default:
//exit(0);
getch();
}
}
}

155
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



9. Program to implement Linear Liked List
#include<stdio.h>
#include<alloc.h>
#include<conio.h>

struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;


NODE getnode()
{
NODE first;
first = (NODE) malloc (sizeof (struct node));


if (first == NULL)
{
printf ("Out of memory\n");
exit(0);
}
return first;
}


void freenode (NODE first)
{
free(first);
}

156
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



// C function to insert the element at the front end
NODE insert_front(int item, NODE first)
{
NODE temp;
temp = getnode();
temp->info = item;
temp->link = first;
return temp;
}


// C function to delete the element at the front end
NODE delete_front(NODE first)
{
NODE temp;
if (first == NULL)
{
printf ("List is empty can not delete\n");
return first;
}
temp = first;
first = first->link;
printf ("The item deleted is %d\n", temp->info);


freenode(temp);
return first;
}

157
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



// C function to display the elements


void display (NODE first)
{
NODE temp;
if (first == NULL)
{
printf ("List is empty\n");
return;
}


printf ("The contents of singly linked list\n");
temp = first;
while(temp != NULL)
{
printf ("\t%d", temp->info);
temp = temp->link;
}
printf("\n");
}


void main()
{
NODE first=NULL;
int choice,item;
clrscr();
for(;;)
{
printf ("1: Insert front\t 2:Delete front\t 3.Display\t 4.Exit\n");
printf ("Enter the choice\n");
scanf ("%d", &choice);

158
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



switch(choice)
{
case 1: printf ("Enter the item to be inserted\n");
scanf ("%d", &item);
first=insert_front(item,first);
break;
case 2: first=delete_front(first);
break;
case 3: display(first);
break;
case 4: exit(0);


default: printf ("wrong choice\n");
}
}
}

159
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



10. Program to display traversal of a Tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *rlink;
struct node *llink;
};
typedef struct node *NODE;
NODE getnode()
{
NODE temp;
temp = (NODE) malloc (sizeof(struct node));
if (temp == NULL)
{
printf ("error\n");
exit(1);
}
return temp;
}
NODE bin_sea_tree(int item, NODE root)//binary search tree
{
NODE prev, cur, temp;
temp = getnode();
temp->info = item;
temp->llink = temp->rlink =NULL;
if (root == NULL)
return temp;

160
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



prev = NULL;
cur = root;
while (cur != NULL)
{
prev = cur;
if (item == cur->info)
{
printf ("duplicants not allowed\n");
free(temp);
return(root);
}
if (item < cur->info)
cur = cur->llink;
else
cur = cur->rlink;
}
if (item < prev->info)
prev->llink = temp;
else
prev->rlink = temp;
return(root);
}
void preorder (NODE root)
{
if (root != NULL)
{
printf ("%d\t", root->info);
preorder (root->llink);
preorder (root->rlink);
}
}

161
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



void postorder (NODE root)
{
if (root != NULL)
{
postorder (root->llink);
postorder (root->rlink);
printf("%d\t", root->info);
}
}
void inorder (NODE root)
{
if (root != NULL)
{
inorder (root->llink);
printf ("%d\t", root->info);
inorder (root->rlink);
}
}


void main()
{
NODE root;
int choice, item;
root=NULL;
clrscr();
for(;;)
{
printf ("\n 1. create \n 2.traverse inorder\n 3.traversae preorder\n 4.traverse postorder\n
5.display\n 6.exit\n");
printf ("enter your choice\n");
scanf ("%d", &choice);

162
Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA



switch(choice)
{
case 1: printf ("enter item\n");
scanf ("%d", &item);
root = bin_sea_tree(item,root);
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: printf("elements in tree are:\n");
inorder(root);
break;


default: exit(0);
}
}
}
Tags