SlideShare a Scribd company logo
135
LAB PROGRAMS
Course Code: CAC04P
Course Credits: 02
Course Title: Data Structures Lab
Hours/Week: 04
Total Contact Hours: 52
Exam Marks: 25
Formative Assessment Marks: 25
Exam Duration: 03 Hours
Part A
1
2
3
4
5
6
7
8
9
1
. Program to find GCD using recursive function
. Program to display Pascal Triangle using binomial function
. Program to generate n Fibonacci numbers using recursive function.
. Program to implement Towers of Hanoi.
. Program to implement dynamic array, find smallest and largest element of the array.
. Program to create two files to store even and odd numbers.
. Program to create a file to store student records.
. Program to read the names of cities and arrange them alphabetically.
. Program to sort the given list using selection sort technique.
0. Program to sort the given list using bubble sort technique.
Part B
1
2
3
4
5
6
7
8
9
1
. Program to sort the given list using insertion sort technique.
. Program to sort the given list using quick sort technique.
. Program to sort the given list using merge sort technique.
. Program to search an element using linear search technique.
. Program to search an element using recursive binary search technique.
. Program to implement Stack.
. Program to convert an infix expression to postfix.
. Program to implement simple queue.
. Program to implement linear linked list.
0.Program to display traversal of a tree.
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
136
Evaluation Scheme for Lab Examination
Assessment Criteria Marks
02
Program – 1 from Part B Flowchart / Algorithm
Writing the Program 04
Execution and Formatting
Flowchart / Algorithm
Writing the Program
04
Program – 2 from Part B 02
04
Execution and Formatting 04
Viva Voice based on C Programming 02
Practical Record 03
Total 25
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
137
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;
}
Data Structures Using C II Sem BCA
Seshadripuram degree college mysore
138
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();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
139
3. Program to generate “n” Fibonacci numbers using recursive function.
#
#
include<stdio.h>
include<conio.h>
int Fibonacci(int);
void main()
{
int n, i = 0, c;
clrscr();
printf ("enter the numbern");
scanf ("%d", &n);
printf ("Fibonacci seriesn");
for (c = 1 ; c <= n ; c++)
{
printf ("%dn", 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));
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
140
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 %cn", 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 disksn");
scanf ("%d", &n);
tower (n, 'a', 'b', 'c');
printf ("n total number of disc moves =%dn", count);
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
141
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);
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
142
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 aren");
fprintf (fpo,"odd numbers aren");
for (i=1; i<=100; i++)
{
if (i % 2 == 0)
fprintf (fpe, "%dn", i);
else
fprintf(fpo, "%dn", 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.
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
143
7. Program to create a file and store student records
#
#
include<stdio.h>
include<conio.h>
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 existsn");
return;
}
printf ("Enter the namen");
gets(name);
fprintf (fptr, "Name = %sn", name);
printf ("enter agen");
scanf ("%d", &age);
fprintf (fptr, "Age = %dn", age);
printf ("enter salaryn");
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.
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
144
8. Program to read the names of cities and arrange them alphabetically.
#
#
#
include<stdio.h>
include<conio.h>
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 ("%sn", str[i]);
}
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
135
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]);
}
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
136
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 sortn");
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 sortingn");
for (i = 0 ; i <n ; i++ )
printf ("%dt", a[i]);
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
137
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();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
138
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 ("%dt", x[i]);
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
139
void quicksort (int x[10],int first, int last)
{
int pivot, j, temp, i;
printf ("first = %dn, last = %dn", first, last);
if (first < last)
{
pivot = first;
i = first;
j = last;
printf ("pivot = %dn", pivot);
printf ("i = %dn", i);
printf ("j = %dn", 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);
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
140
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);
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
141
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();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
142
void partition (int arr[], int low, int high)
{
int mid;
printf ("nt partition low=%dn high=%dn ", low, high);
if (low < high)
{
mid = (low + high) / 2;
printf ("mid=%dn", 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 ("nt merge low=%dn mid=%dn high=%dn", 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=%dn arr=%dn", arr[i], arr[l]);
l++;
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
143
else
{
temp[i] = arr[m];
printf ("arr=%dn arr=%dn", temp[i], arr[m]);
m++;
}
i++;
printf ("i=%dn", i);
}
if (l > mid)
{
For (k=m; k<=high; k++)
{
temp[i] = arr[k];
printf ("temp=%dn arr=%dn", temp[i], arr[k]);
i++;
printf ("i=%dn", i);
}
}
else
{
for (k=l; k<=mid; k++)
{
temp[i] = arr[k];
printf ("temp=%dn arr=%dn ",temp[i], arr[k]);
i++;
printf ("i=%dn ", i);
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
144
for (k=low; k<=high; k++)
{
arr[k] = temp[k];
printf ("arr=%dn temp=%dn ",arr[k], temp[k]);
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
145
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 arrayn");
scanf ("%d", &n);
printf ("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf ("%d", &array[c]);
printf ("Enter the number to searchn");
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();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
146
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 arrayn");
else
printf ("Item found at index %dn", index);
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
147
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 ("nn1.Pushn2.Popn3.Displayn4.Exit");
printf ("nnEnter 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!!");
}
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
148
void push()
{
int val;
if (top == MAX-1)
{
printf ("nStack is full!!");
}
else
{
printf ("nEnter element to push:");
scanf ("%d", &val);
top = top + 1;
stack[top]=val;
}
}
void pop()
{
if (top == -1)
{
printf ("nStack is empty!!");
}
else
{
printf ("nDeleted element is %d", stack[top]);
top = top - 1;
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
149
void display()
{
int i;
if (top == -1)
{
printf ("nStack is empty!!");
}
else
{
printf ("nStack is...n");
for(i = top; i >= 0; --i)
printf ("%dn", stack[i]);
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
150
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
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
151
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 == ')')
{
while ((x = pop()) != '(')
printf ("%c ", x);
}
else
{
while (priority(stack[top]) >= priority(*e))
printf ("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
getch();
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
152
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 Overflown");
return;
}
rear = rear + 1;
q[rear] = item;
}
//Function to delete an item
int DeleteQ()
{
if (front > rear)
return -1;
return q[front++];
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
153
//Function to display an item
void Display()
{
int i;
//if queue is empty
if (front > rear)
{
printf ("Queue is emptyn");
return;
}
printf ("Contents of the queuen");
for (i=front; i<=rear; i++)
{
printf("%dn", q[i]);
}
}
void main()
{
front= 0;
rear= -1;
clrscr();
for(;;)
{
printf ("1.Insert 2.Delete 3. Display 4.Exitn");
printf ("Enter the choicen");
scanf ("%d",&choice);
switch(choice)
{
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
154
case 1:
printf ("enter the item to be insertedn");
scanf ("%d", &item);
InsertQ();
break;
case 2:
item = DeleteQ();
if (item == -1)
printf ("Queue is emptyn");
else
printf ("item deleted = %dn", item);
break;
case 3:
Display();
break;
case 4: exit(0);
default:
//exit(0);
getch();
}
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
155
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 memoryn");
exit(0);
}
return first;
}
void freenode (NODE first)
{
free(first);
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
156
// 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 deleten");
return first;
}
temp = first;
first = first->link;
printf ("The item deleted is %dn", temp->info);
freenode(temp);
return first;
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
157
// C function to display the elements
void display (NODE first)
{
NODE temp;
if (first == NULL)
{
printf ("List is emptyn");
return;
}
printf ("The contents of singly linked listn");
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 frontt 2:Delete frontt 3.Displayt 4.Exitn");
printf ("Enter the choicen");
scanf ("%d", &choice);
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
158
switch(choice)
{
case 1: printf ("Enter the item to be insertedn");
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 choicen");
}
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
159
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 ("errorn");
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;
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
160
prev = NULL;
cur = root;
while (cur != NULL)
{
prev = cur;
if (item == cur->info)
{
printf ("duplicants not allowedn");
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 ("%dt", root->info);
preorder (root->llink);
preorder (root->rlink);
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
161
void postorder (NODE root)
{
if (root != NULL)
{
postorder (root->llink);
postorder (root->rlink);
printf("%dt", root->info);
}
}
void inorder (NODE root)
{
if (root != NULL)
{
inorder (root->llink);
printf ("%dt", root->info);
inorder (root->rlink);
}
}
void main()
{
NODE root;
int choice, item;
root=NULL;
clrscr();
for(;;)
{
printf ("n 1. create n 2.traverse inordern 3.traversae preordern 4.traverse postordern
.displayn 6.exitn");
printf ("enter your choicen");
scanf ("%d", &choice);
5
Seshadripuram degree college mysore Data Structures Using C II Sem BCA
162
switch(choice)
{
case 1: printf ("enter itemn");
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);
}
}
}
Seshadripuram degree college mysore Data Structures Using C II Sem BCA

More Related Content

PDF
Lab program 1234567891234567891234567891
PDF
Complete Lab 123456789123456789123456789
PDF
lab.123456789123456789123456789123456789
 
PDF
DSC program.pdf
PDF
C and Data structure lab manual ECE (2).pdf
DOCX
Data Structures Using C Practical File
PPTX
21CS32 DS Module 1 PPT.pptx
Lab program 1234567891234567891234567891
Complete Lab 123456789123456789123456789
lab.123456789123456789123456789123456789
 
DSC program.pdf
C and Data structure lab manual ECE (2).pdf
Data Structures Using C Practical File
21CS32 DS Module 1 PPT.pptx

Similar to labb123456789123456789123456789123456789 (20)

PDF
Data Structure using C
PDF
DATA STRUCTURE USING C & C++
PDF
DSU C&C++ Practical File Diploma
DOCX
PDF
C Programming lab
DOCX
Data Structure Project File
PDF
Data Structures Mastery: Sample Paper for Practice"
PDF
Data Structures Notes 2021
DOCX
ADA FILE
PPT
array.ppt
PDF
09 a1ec01 c programming and data structures
PDF
Data Structures Practical File
DOC
Basic c programs updated on 31.8.2020
DOCX
Fuzail_File_C.docx
PDF
C programms
PPTX
PPTX
Data structures and algorithms lab1
PDF
C lab programs
PDF
C lab programs
Data Structure using C
DATA STRUCTURE USING C & C++
DSU C&C++ Practical File Diploma
C Programming lab
Data Structure Project File
Data Structures Mastery: Sample Paper for Practice"
Data Structures Notes 2021
ADA FILE
array.ppt
09 a1ec01 c programming and data structures
Data Structures Practical File
Basic c programs updated on 31.8.2020
Fuzail_File_C.docx
C programms
Data structures and algorithms lab1
C lab programs
C lab programs
Ad

More from Ghh (6)

PDF
mmlab.123456789123456789123456789123456789
 
PDF
lab.123456789123456789123456789123456789
 
PDF
lab.123456789123456789123456789123456789
 
PDF
lab.123456789123456789123456789123456789
 
PDF
lab.123456789123456789123456789123456789
 
PDF
Zoro123456789123456789123456789123456789
 
mmlab.123456789123456789123456789123456789
 
lab.123456789123456789123456789123456789
 
lab.123456789123456789123456789123456789
 
lab.123456789123456789123456789123456789
 
lab.123456789123456789123456789123456789
 
Zoro123456789123456789123456789123456789
 
Ad

Recently uploaded (20)

PPTX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
PPTX
The Stock at arrangement the stock and product.pptx
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
DOCX
mcsp232projectguidelinesjan2023 (1).docx
PDF
Josh Gao Strength to Strength Book Summary
PPTX
Discovering the LMA Course by Tim Han.pptx
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PDF
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
PPTX
Definition and Relation of Food Science( Lecture1).pptx
PPTX
E-Commerce____Intermediate_Presentation.pptx
PPTX
Surgical thesis protocol formation ppt.pptx
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
PPTX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
PPTX
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
PPTX
Your Guide to a Winning Interview Aug 2025.
PPTX
Principles of Inheritance and variation class 12.pptx
PPTX
_+✅+JANUARY+2025+MONTHLY+CA.pptx current affairs
PPTX
Overview Planner of Soft Skills in a single ppt
PPTX
1751884730-Visual Basic -Unitj CS B.pptx
PPTX
CORE 1 HOUSEKEEPING TOURISM SECTOR POWERPOINT
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
The Stock at arrangement the stock and product.pptx
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
mcsp232projectguidelinesjan2023 (1).docx
Josh Gao Strength to Strength Book Summary
Discovering the LMA Course by Tim Han.pptx
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
Definition and Relation of Food Science( Lecture1).pptx
E-Commerce____Intermediate_Presentation.pptx
Surgical thesis protocol formation ppt.pptx
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
Your Guide to a Winning Interview Aug 2025.
Principles of Inheritance and variation class 12.pptx
_+✅+JANUARY+2025+MONTHLY+CA.pptx current affairs
Overview Planner of Soft Skills in a single ppt
1751884730-Visual Basic -Unitj CS B.pptx
CORE 1 HOUSEKEEPING TOURISM SECTOR POWERPOINT

labb123456789123456789123456789123456789

  • 1. 135 LAB PROGRAMS Course Code: CAC04P Course Credits: 02 Course Title: Data Structures Lab Hours/Week: 04 Total Contact Hours: 52 Exam Marks: 25 Formative Assessment Marks: 25 Exam Duration: 03 Hours Part A 1 2 3 4 5 6 7 8 9 1 . Program to find GCD using recursive function . Program to display Pascal Triangle using binomial function . Program to generate n Fibonacci numbers using recursive function. . Program to implement Towers of Hanoi. . Program to implement dynamic array, find smallest and largest element of the array. . Program to create two files to store even and odd numbers. . Program to create a file to store student records. . Program to read the names of cities and arrange them alphabetically. . Program to sort the given list using selection sort technique. 0. Program to sort the given list using bubble sort technique. Part B 1 2 3 4 5 6 7 8 9 1 . Program to sort the given list using insertion sort technique. . Program to sort the given list using quick sort technique. . Program to sort the given list using merge sort technique. . Program to search an element using linear search technique. . Program to search an element using recursive binary search technique. . Program to implement Stack. . Program to convert an infix expression to postfix. . Program to implement simple queue. . Program to implement linear linked list. 0.Program to display traversal of a tree. Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 2. 136 Evaluation Scheme for Lab Examination Assessment Criteria Marks 02 Program – 1 from Part B Flowchart / Algorithm Writing the Program 04 Execution and Formatting Flowchart / Algorithm Writing the Program 04 Program – 2 from Part B 02 04 Execution and Formatting 04 Viva Voice based on C Programming 02 Practical Record 03 Total 25 Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 3. 137 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; } Data Structures Using C II Sem BCA Seshadripuram degree college mysore
  • 4. 138 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(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 5. 139 3. Program to generate “n” Fibonacci numbers using recursive function. # # include<stdio.h> include<conio.h> int Fibonacci(int); void main() { int n, i = 0, c; clrscr(); printf ("enter the numbern"); scanf ("%d", &n); printf ("Fibonacci seriesn"); for (c = 1 ; c <= n ; c++) { printf ("%dn", 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)); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 6. 140 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 %cn", 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 disksn"); scanf ("%d", &n); tower (n, 'a', 'b', 'c'); printf ("n total number of disc moves =%dn", count); getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 7. 141 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); getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 8. 142 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 aren"); fprintf (fpo,"odd numbers aren"); for (i=1; i<=100; i++) { if (i % 2 == 0) fprintf (fpe, "%dn", i); else fprintf(fpo, "%dn", 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. Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 9. 143 7. Program to create a file and store student records # # include<stdio.h> include<conio.h> 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 existsn"); return; } printf ("Enter the namen"); gets(name); fprintf (fptr, "Name = %sn", name); printf ("enter agen"); scanf ("%d", &age); fprintf (fptr, "Age = %dn", age); printf ("enter salaryn"); 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. Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 10. 144 8. Program to read the names of cities and arrange them alphabetically. # # # include<stdio.h> include<conio.h> 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 ("%sn", str[i]); } getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 11. 135 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]); } getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 12. 136 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 sortn"); 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 sortingn"); for (i = 0 ; i <n ; i++ ) printf ("%dt", a[i]); getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 13. 137 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(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 14. 138 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 ("%dt", x[i]); getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 15. 139 void quicksort (int x[10],int first, int last) { int pivot, j, temp, i; printf ("first = %dn, last = %dn", first, last); if (first < last) { pivot = first; i = first; j = last; printf ("pivot = %dn", pivot); printf ("i = %dn", i); printf ("j = %dn", 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); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 16. 140 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); } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 17. 141 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(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 18. 142 void partition (int arr[], int low, int high) { int mid; printf ("nt partition low=%dn high=%dn ", low, high); if (low < high) { mid = (low + high) / 2; printf ("mid=%dn", 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 ("nt merge low=%dn mid=%dn high=%dn", 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=%dn arr=%dn", arr[i], arr[l]); l++; } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 19. 143 else { temp[i] = arr[m]; printf ("arr=%dn arr=%dn", temp[i], arr[m]); m++; } i++; printf ("i=%dn", i); } if (l > mid) { For (k=m; k<=high; k++) { temp[i] = arr[k]; printf ("temp=%dn arr=%dn", temp[i], arr[k]); i++; printf ("i=%dn", i); } } else { for (k=l; k<=mid; k++) { temp[i] = arr[k]; printf ("temp=%dn arr=%dn ",temp[i], arr[k]); i++; printf ("i=%dn ", i); } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 20. 144 for (k=low; k<=high; k++) { arr[k] = temp[k]; printf ("arr=%dn temp=%dn ",arr[k], temp[k]); } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 21. 145 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 arrayn"); scanf ("%d", &n); printf ("Enter %d integersn", n); for (c = 0; c < n; c++) scanf ("%d", &array[c]); printf ("Enter the number to searchn"); 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(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 22. 146 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 arrayn"); else printf ("Item found at index %dn", index); getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 23. 147 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 ("nn1.Pushn2.Popn3.Displayn4.Exit"); printf ("nnEnter 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!!"); } } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 24. 148 void push() { int val; if (top == MAX-1) { printf ("nStack is full!!"); } else { printf ("nEnter element to push:"); scanf ("%d", &val); top = top + 1; stack[top]=val; } } void pop() { if (top == -1) { printf ("nStack is empty!!"); } else { printf ("nDeleted element is %d", stack[top]); top = top - 1; } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 25. 149 void display() { int i; if (top == -1) { printf ("nStack is empty!!"); } else { printf ("nStack is...n"); for(i = top; i >= 0; --i) printf ("%dn", stack[i]); } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 26. 150 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 } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 27. 151 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 == ')') { while ((x = pop()) != '(') printf ("%c ", x); } else { while (priority(stack[top]) >= priority(*e)) printf ("%c ",pop()); push(*e); } e++; } while(top != -1) { printf("%c ",pop()); } getch(); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 28. 152 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 Overflown"); return; } rear = rear + 1; q[rear] = item; } //Function to delete an item int DeleteQ() { if (front > rear) return -1; return q[front++]; } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 29. 153 //Function to display an item void Display() { int i; //if queue is empty if (front > rear) { printf ("Queue is emptyn"); return; } printf ("Contents of the queuen"); for (i=front; i<=rear; i++) { printf("%dn", q[i]); } } void main() { front= 0; rear= -1; clrscr(); for(;;) { printf ("1.Insert 2.Delete 3. Display 4.Exitn"); printf ("Enter the choicen"); scanf ("%d",&choice); switch(choice) { Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 30. 154 case 1: printf ("enter the item to be insertedn"); scanf ("%d", &item); InsertQ(); break; case 2: item = DeleteQ(); if (item == -1) printf ("Queue is emptyn"); else printf ("item deleted = %dn", item); break; case 3: Display(); break; case 4: exit(0); default: //exit(0); getch(); } } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 31. 155 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 memoryn"); exit(0); } return first; } void freenode (NODE first) { free(first); } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 32. 156 // 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 deleten"); return first; } temp = first; first = first->link; printf ("The item deleted is %dn", temp->info); freenode(temp); return first; } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 33. 157 // C function to display the elements void display (NODE first) { NODE temp; if (first == NULL) { printf ("List is emptyn"); return; } printf ("The contents of singly linked listn"); 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 frontt 2:Delete frontt 3.Displayt 4.Exitn"); printf ("Enter the choicen"); scanf ("%d", &choice); Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 34. 158 switch(choice) { case 1: printf ("Enter the item to be insertedn"); 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 choicen"); } } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 35. 159 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 ("errorn"); 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; Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 36. 160 prev = NULL; cur = root; while (cur != NULL) { prev = cur; if (item == cur->info) { printf ("duplicants not allowedn"); 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 ("%dt", root->info); preorder (root->llink); preorder (root->rlink); } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 37. 161 void postorder (NODE root) { if (root != NULL) { postorder (root->llink); postorder (root->rlink); printf("%dt", root->info); } } void inorder (NODE root) { if (root != NULL) { inorder (root->llink); printf ("%dt", root->info); inorder (root->rlink); } } void main() { NODE root; int choice, item; root=NULL; clrscr(); for(;;) { printf ("n 1. create n 2.traverse inordern 3.traversae preordern 4.traverse postordern .displayn 6.exitn"); printf ("enter your choicen"); scanf ("%d", &choice); 5 Seshadripuram degree college mysore Data Structures Using C II Sem BCA
  • 38. 162 switch(choice) { case 1: printf ("enter itemn"); 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); } } } Seshadripuram degree college mysore Data Structures Using C II Sem BCA