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

Similar to labb123456789123456789123456789123456789

C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
Srinivas Reddy Amedapu
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
Srinivas Reddy Amedapu
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu
 
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 lab
C labC lab
Pnno
PnnoPnno
Cpds lab
Cpds labCpds lab
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
Mahmud Hasan Tanvir
 
C-programs
C-programsC-programs
C-programs
SSGMCE SHEGAON
 
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
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
Koshy Geoji
 
ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
SaraPIC
SaraPICSaraPIC
SaraPIC
Sara Sahu
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
Ashishchinu
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
SANTOSH RATH
 
C file
C fileC file
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
C programms
C programmsC programms
C programms
Mukund Gandrakota
 
C Programming lab
C Programming labC Programming lab
C Programming lab
Vikram Nandini
 

Similar to labb123456789123456789123456789123456789 (20)

C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C lab
C labC lab
C lab
 
Pnno
PnnoPnno
Pnno
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
C-programs
C-programsC-programs
C-programs
 
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
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
C file
C fileC file
C file
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
C programms
C programmsC programms
C programms
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 

Recently uploaded

VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...
VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...
VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...
juligarg18
 
Comparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.com
Comparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.comComparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.com
Comparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.com
df2608021
 
Girls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in City
Girls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in CityGirls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in City
Girls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in City
margaretblush
 
Certified Quality Engineer CQE .pdf
Certified Quality Engineer CQE       .pdfCertified Quality Engineer CQE       .pdf
Certified Quality Engineer CQE .pdf
GAFM ACADEMY
 
communication skills paper 2019 - Bangalore North university.
communication skills paper 2019 - Bangalore North university.communication skills paper 2019 - Bangalore North university.
communication skills paper 2019 - Bangalore North university.
GoravSingh14
 
The Chartered Facilities Manager.PREVIEW.pdf
The Chartered Facilities Manager.PREVIEW.pdfThe Chartered Facilities Manager.PREVIEW.pdf
The Chartered Facilities Manager.PREVIEW.pdf
GAFM ACADEMY
 
Overview of Tourism and Hospitality Organizations
Overview of Tourism and Hospitality OrganizationsOverview of Tourism and Hospitality Organizations
Overview of Tourism and Hospitality Organizations
Md Shaifullar Rabbi
 
How to Plan and Scope Facility Management Projects?.PREVIEW.pdf
How to Plan and Scope Facility Management Projects?.PREVIEW.pdfHow to Plan and Scope Facility Management Projects?.PREVIEW.pdf
How to Plan and Scope Facility Management Projects?.PREVIEW.pdf
GAFM ACADEMY
 
Punjab Daanish Schools & Center Of Excellence
Punjab Daanish Schools & Center Of ExcellencePunjab Daanish Schools & Center Of Excellence
Punjab Daanish Schools & Center Of Excellence
blog
 
Guide for a Winning Interview - July 8, 2024
Guide for a Winning Interview -  July 8, 2024Guide for a Winning Interview -  July 8, 2024
Guide for a Winning Interview - July 8, 2024
Bruce Bennett
 
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
gargj3720
 
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
ng462694
 
0724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-010724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-01
Thomas GIRARD BDes
 
Pablo Huston – The Untold Story of John Huston Adopted Son.pdf
Pablo Huston – The Untold Story of John Huston Adopted Son.pdfPablo Huston – The Untold Story of John Huston Adopted Son.pdf
Pablo Huston – The Untold Story of John Huston Adopted Son.pdf
Janice Moton
 
Biography and career history of Matthew Herzberger.pdf
Biography and career history of Matthew Herzberger.pdfBiography and career history of Matthew Herzberger.pdf
Biography and career history of Matthew Herzberger.pdf
Matthew Herzberger
 
Internship Session by Radu Matei at NHL Stenden University of Applied Science...
Internship Session by Radu Matei at NHL Stenden University of Applied Science...Internship Session by Radu Matei at NHL Stenden University of Applied Science...
Internship Session by Radu Matei at NHL Stenden University of Applied Science...
Radu Matei
 
SSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptx
SSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptxSSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptx
SSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptx
Vijay Kumar
 
0724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-010724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-01
Thomas GIRARD BDes
 
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
gargj3720
 
140. Reviewer Certificate in BP International
140. Reviewer Certificate in BP International140. Reviewer Certificate in BP International
140. Reviewer Certificate in BP International
Manu Mitra
 

Recently uploaded (20)

VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...
VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...
VVIP Girls Call Hyderabad 000XX00000 Provide Best And Top Girl Service And No...
 
Comparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.com
Comparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.comComparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.com
Comparing ChatGPT 3.5 vs 4 vs 4o_ What's New and Improved-ashokveda.com
 
Girls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in City
Girls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in CityGirls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in City
Girls Call Oshiwara 9910780858 Provide Best And Top Girl Service And No1 in City
 
Certified Quality Engineer CQE .pdf
Certified Quality Engineer CQE       .pdfCertified Quality Engineer CQE       .pdf
Certified Quality Engineer CQE .pdf
 
communication skills paper 2019 - Bangalore North university.
communication skills paper 2019 - Bangalore North university.communication skills paper 2019 - Bangalore North university.
communication skills paper 2019 - Bangalore North university.
 
The Chartered Facilities Manager.PREVIEW.pdf
The Chartered Facilities Manager.PREVIEW.pdfThe Chartered Facilities Manager.PREVIEW.pdf
The Chartered Facilities Manager.PREVIEW.pdf
 
Overview of Tourism and Hospitality Organizations
Overview of Tourism and Hospitality OrganizationsOverview of Tourism and Hospitality Organizations
Overview of Tourism and Hospitality Organizations
 
How to Plan and Scope Facility Management Projects?.PREVIEW.pdf
How to Plan and Scope Facility Management Projects?.PREVIEW.pdfHow to Plan and Scope Facility Management Projects?.PREVIEW.pdf
How to Plan and Scope Facility Management Projects?.PREVIEW.pdf
 
Punjab Daanish Schools & Center Of Excellence
Punjab Daanish Schools & Center Of ExcellencePunjab Daanish Schools & Center Of Excellence
Punjab Daanish Schools & Center Of Excellence
 
Guide for a Winning Interview - July 8, 2024
Guide for a Winning Interview -  July 8, 2024Guide for a Winning Interview -  July 8, 2024
Guide for a Winning Interview - July 8, 2024
 
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
 
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
 
0724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-010724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-01
 
Pablo Huston – The Untold Story of John Huston Adopted Son.pdf
Pablo Huston – The Untold Story of John Huston Adopted Son.pdfPablo Huston – The Untold Story of John Huston Adopted Son.pdf
Pablo Huston – The Untold Story of John Huston Adopted Son.pdf
 
Biography and career history of Matthew Herzberger.pdf
Biography and career history of Matthew Herzberger.pdfBiography and career history of Matthew Herzberger.pdf
Biography and career history of Matthew Herzberger.pdf
 
Internship Session by Radu Matei at NHL Stenden University of Applied Science...
Internship Session by Radu Matei at NHL Stenden University of Applied Science...Internship Session by Radu Matei at NHL Stenden University of Applied Science...
Internship Session by Radu Matei at NHL Stenden University of Applied Science...
 
SSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptx
SSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptxSSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptx
SSC CGL 2024 Notification Released: Apply Now For 17,727 Vacancies.pptx
 
0724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-010724.curriculumvitaeandresume_scholarandauthor-01
0724.curriculumvitaeandresume_scholarandauthor-01
 
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
Girls Call Ahmedabad 000XX00000 Provide Best And Top Girl Service And No1 in ...
 
140. Reviewer Certificate in BP International
140. Reviewer Certificate in BP International140. Reviewer Certificate in BP International
140. Reviewer Certificate in BP International
 

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