SlideShare a Scribd company logo
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 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));
}
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 %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);
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 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.
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 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.
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 ("%sn", 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 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]);
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 ("%dt", 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 = %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);
}
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 ("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++;
}
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=%dn arr=%dn", temp[i], arr[m]);
m++;
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);
}
}
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=%dn temp=%dn ",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 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();
}
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 arrayn");
else
printf ("Item found at index %dn", 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 ("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!!");
}
}
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 ("%dn", 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 Overflown");
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 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)
{
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 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();
}
}
}
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 memoryn");
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 deleten");
return first;
}
temp = first;
first = first->link;
printf ("The item deleted is %dn", 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 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);
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 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");
}
}
}
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 ("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;
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 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);
}
}
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("%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
5.displayn 6.exitn");
printf ("enter your choicen");
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 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);
}
}
}

More Related Content

Similar to Complete Lab 123456789123456789123456789

Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
SANTOSH RATH
 
Data struture lab
Data struture labData struture lab
Data struture lab
krishnamurthy Murthy.Tt
 
Pnno
PnnoPnno
Operating system labs
Operating system labsOperating system labs
Operating system labs
bhaktisagar4
 
Mech nacp lab
Mech nacp labMech nacp lab
Mech nacp lab
Vivek Kumar Sinha
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
sandeep kumbhkar
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
Cpds lab
Cpds labCpds lab
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
C
CC
Hargun
HargunHargun
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
C programms
C programmsC programms
C programms
Mukund Gandrakota
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
Murali Kummitha
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
Murali Kummitha
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
Murali Kummitha
 
C-programs
C-programsC-programs
C-programs
SSGMCE SHEGAON
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
RSathyaPriyaCSEKIOT
 

Similar to Complete Lab 123456789123456789123456789 (20)

Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Pnno
PnnoPnno
Pnno
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
Mech nacp lab
Mech nacp labMech nacp lab
Mech nacp lab
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C Programming
C ProgrammingC Programming
C Programming
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
C
CC
C
 
Hargun
HargunHargun
Hargun
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
C programms
C programmsC programms
C programms
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
 
C-programs
C-programsC-programs
C-programs
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
 

Recently uploaded

Tríptico Water Engineers future nowa.pdf
Tríptico Water Engineers future nowa.pdfTríptico Water Engineers future nowa.pdf
Tríptico Water Engineers future nowa.pdf
JoseEvelyMartinezCar
 
Preventing Dementia - The Basics Millions of results on Google…
Preventing Dementia - The Basics Millions of results on Google…Preventing Dementia - The Basics Millions of results on Google…
Preventing Dementia - The Basics Millions of results on Google…
bilalpakweb
 
Bond Diploma Degree
Bond Diploma Degree Bond Diploma Degree
Bond Diploma Degree
eahbo
 
Common Types of Dementia 8F who notes progressive memory loss
Common Types of Dementia 8F who notes progressive memory lossCommon Types of Dementia 8F who notes progressive memory loss
Common Types of Dementia 8F who notes progressive memory loss
bilalpakweb
 
Presentation 1 on ghgvsvghhg hjsjjbsh .pdf
Presentation 1 on ghgvsvghhg hjsjjbsh .pdfPresentation 1 on ghgvsvghhg hjsjjbsh .pdf
Presentation 1 on ghgvsvghhg hjsjjbsh .pdf
keithlunduka
 
University of Wollongong degree offer diploma Transcript
University of Wollongong degree offer diploma TranscriptUniversity of Wollongong degree offer diploma Transcript
University of Wollongong degree offer diploma Transcript
ufozf
 
Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...
Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...
Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...
parulpk4011
 
phyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdf
phyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdfphyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdf
phyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdf
parareseller1
 
Domes Of Bijapur A Historical Photo show
Domes Of Bijapur A Historical Photo showDomes Of Bijapur A Historical Photo show
Domes Of Bijapur A Historical Photo show
Sc Pattar
 
MBE Style ooooooooooooooooo WPS Office.pptx
MBE Style ooooooooooooooooo WPS Office.pptxMBE Style ooooooooooooooooo WPS Office.pptx
MBE Style ooooooooooooooooo WPS Office.pptx
DanielaMamani45
 
Girls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in City
dalmeetsingh09090
 
#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad
#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad
#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad
Authors Click Publicer
 
Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...
Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...
Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...
jealousviolet
 
Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...
Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...
Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...
bobhany3646
 
Gandhi and Poetry Satchidanandan MA ENGL
Gandhi and Poetry Satchidanandan MA ENGLGandhi and Poetry Satchidanandan MA ENGL
Gandhi and Poetry Satchidanandan MA ENGL
juhixerox5857
 
Arquitectura en EUROPA - Adriánn Díaz 30.118.599.pptx
Arquitectura en EUROPA - Adriánn Díaz 30.118.599.pptxArquitectura en EUROPA - Adriánn Díaz 30.118.599.pptx
Arquitectura en EUROPA - Adriánn Díaz 30.118.599.pptx
AdriannDiaz
 
Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...
Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...
Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...
bhumivarma35300
 
phyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdf
phyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdfphyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdf
phyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdf
parareseller1
 
Girls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad Available
Girls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad AvailableGirls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad Available
Girls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad Available
ashiklo9823
 
Paparan Persiapan menjelang tanding2018
Paparan Persiapan menjelang  tanding2018Paparan Persiapan menjelang  tanding2018
Paparan Persiapan menjelang tanding2018
MeirizalUsra1
 

Recently uploaded (20)

Tríptico Water Engineers future nowa.pdf
Tríptico Water Engineers future nowa.pdfTríptico Water Engineers future nowa.pdf
Tríptico Water Engineers future nowa.pdf
 
Preventing Dementia - The Basics Millions of results on Google…
Preventing Dementia - The Basics Millions of results on Google…Preventing Dementia - The Basics Millions of results on Google…
Preventing Dementia - The Basics Millions of results on Google…
 
Bond Diploma Degree
Bond Diploma Degree Bond Diploma Degree
Bond Diploma Degree
 
Common Types of Dementia 8F who notes progressive memory loss
Common Types of Dementia 8F who notes progressive memory lossCommon Types of Dementia 8F who notes progressive memory loss
Common Types of Dementia 8F who notes progressive memory loss
 
Presentation 1 on ghgvsvghhg hjsjjbsh .pdf
Presentation 1 on ghgvsvghhg hjsjjbsh .pdfPresentation 1 on ghgvsvghhg hjsjjbsh .pdf
Presentation 1 on ghgvsvghhg hjsjjbsh .pdf
 
University of Wollongong degree offer diploma Transcript
University of Wollongong degree offer diploma TranscriptUniversity of Wollongong degree offer diploma Transcript
University of Wollongong degree offer diploma Transcript
 
Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...
Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...
Girls Call Bhubaneswar 000XX00000 Provide Best And Top Girl Service And No1 i...
 
phyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdf
phyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdfphyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdf
phyto atomy Satyabhama Shantanu Cucumber Body Butter 250g by Phyto Atomy.pdf
 
Domes Of Bijapur A Historical Photo show
Domes Of Bijapur A Historical Photo showDomes Of Bijapur A Historical Photo show
Domes Of Bijapur A Historical Photo show
 
MBE Style ooooooooooooooooo WPS Office.pptx
MBE Style ooooooooooooooooo WPS Office.pptxMBE Style ooooooooooooooooo WPS Office.pptx
MBE Style ooooooooooooooooo WPS Office.pptx
 
Girls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in CityGirls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in City
Girls Call Dehradun 000XX00000 Provide Best And Top Girl Service And No1 in City
 
#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad
#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad
#Aviation #Flight #Aircraft #Aeroplane #Indigo #Airport #Helipad
 
Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...
Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...
Premium Girls Call Mumbai 9910780858 Provide Best And Top Girl Service And No...
 
Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...
Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...
Girls call Service Kolkata 000XX00000 Provide Best And Top Girl Service And N...
 
Gandhi and Poetry Satchidanandan MA ENGL
Gandhi and Poetry Satchidanandan MA ENGLGandhi and Poetry Satchidanandan MA ENGL
Gandhi and Poetry Satchidanandan MA ENGL
 
Arquitectura en EUROPA - Adriánn Díaz 30.118.599.pptx
Arquitectura en EUROPA - Adriánn Díaz 30.118.599.pptxArquitectura en EUROPA - Adriánn Díaz 30.118.599.pptx
Arquitectura en EUROPA - Adriánn Díaz 30.118.599.pptx
 
Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...
Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...
Pune Girls call Service 000XX00000 Provide Best And Top Girl Service And No1 ...
 
phyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdf
phyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdfphyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdf
phyto atomy Cyra Camel Milk Handmade Glycerine Soap 100 gm by Phyto Atomy.pdf
 
Girls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad Available
Girls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad AvailableGirls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad Available
Girls Call Hyderabad 0000000000 Pooja Best High Class Hyderabad Available
 
Paparan Persiapan menjelang tanding2018
Paparan Persiapan menjelang  tanding2018Paparan Persiapan menjelang  tanding2018
Paparan Persiapan menjelang tanding2018
 

Complete Lab 123456789123456789123456789

  • 1. 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.
  • 2. 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
  • 3. 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; }
  • 4. 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(); }
  • 5. 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 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)); }
  • 6. 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 %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);
  • 7. 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);
  • 8. 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 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.
  • 9. 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 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.
  • 10. 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 ("%sn", str[i]); } getch();
  • 11. 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]); }
  • 12. 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 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]);
  • 13. 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(); }
  • 14. 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 ("%dt", x[i]); getch(); }
  • 15. 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 = %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); }
  • 16. 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); } }
  • 17. 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(); }
  • 18. 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 ("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++; }
  • 19. 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=%dn arr=%dn", temp[i], arr[m]); m++; 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); } }
  • 20. 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=%dn temp=%dn ",arr[k], temp[k]); } }
  • 21. 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 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(); }
  • 22. 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 arrayn"); else printf ("Item found at index %dn", index); getch();
  • 23. 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 ("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!!"); } }
  • 24. 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;
  • 25. 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 ("%dn", stack[i]);
  • 26. 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 }
  • 27. 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(); }
  • 28. 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 Overflown"); return; } rear = rear + 1; q[rear] = item; } //Function to delete an item int DeleteQ() { if (front > rear) return -1; return q[front++]; }
  • 29. 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 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) {
  • 30. 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 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(); } } }
  • 31. 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 memoryn"); exit(0); } return first; } void freenode (NODE first) { free(first); }
  • 32. 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 deleten"); return first; } temp = first; first = first->link; printf ("The item deleted is %dn", temp->info); freenode(temp); return first; }
  • 33. 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 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);
  • 34. 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 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"); } } }
  • 35. 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 ("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;
  • 36. 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 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); } }
  • 37. 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("%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 5.displayn 6.exitn"); printf ("enter your choicen"); scanf ("%d", &choice);
  • 38. 162 Asst. Prof. Raghu Ram P S, De Paul College Mysore Data Structures Using C II Sem BCA 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); } } }