SlideShare a Scribd company logo
Data Structures using C Lab Manual
Prof. K. Adisesha 1
Bengaluru North University
II Sem BCA (NEP)
Data Structure Using C Programs
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
Data Structures using C Lab Manual
Prof. K. Adisesha 2
// C Program To Find GCD of Two Number Using Recursion
#include <stdio.h>
int GCD(int x, int y);
int main()
{
int a, b;
// Asking for Input
printf("Enter Two Positive Integers: n");
scanf("%dn %d", &a, &b);
printf("GCD of %d and %d is %d.", a, b, GCD(a, b));
return 0;
}
int GCD(int x, int y)
{
if( y != 0)
return GCD(y, x % y);
else
return x;
}
Data Structures using C Lab Manual
Prof. K. Adisesha 3
// C Program to Print Pascal Triangle
#include < stdio.h >
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal trianglen");
scanf("%d", & n);
for (i = 0; i < n; i++) {
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0; c <= i; c++)
printf("%ld ", factorial(i) / (factorial(c) * factorial(i - c)));
printf("n");
}
return 0;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
}
Data Structures using C Lab Manual
Prof. K. Adisesha 4
// C Program to Compute fibonacci numbers using recursion method
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Enter total termsn");
scanf("%d",&n);
printf("Fibonacci seriesn");
for ( c = 1 ; c <= n ; c++ )
{
printf("%dn", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Data Structures using C Lab Manual
Prof. K. Adisesha 5
//C program for Tower of Hanoi using Recursion
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
Data Structures using C Lab Manual
Prof. K. Adisesha 6
//C program to find the largest and smallest element in an array
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("nThe smallest element is %d",small);
return 0;
}
Output:
How many elements:5
Enter the Array:1 8 12 4 6
The largest element is 12
The smallest element is 1
Data Structures using C Lab Manual
Prof. K. Adisesha 7
//C Program to Write Odd and Even Numbers into Different Files
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fp1,*fp2;
int c,i;
clrscr();
fp=fopen("data.txt","w");
printf("Enter the numbers");
for(i=0;i<10;i++)
{
scanf("%d",&c);
putw(c,fp);
}
fclose(fp);
fp=fopen("data.txt","r");
fp1=fopen("even.txt","w");
fp2=fopen("odd.txt","w");
while((c=getw(fp))!=EOF)
{
if(c%2==0)
putw(c,fp1);
else
putw(c,fp2);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
fp1=fopen("even.txt","r");
while((c=getw(fp1))!=EOF)
printf("File-1 Elements : %4d",c);
printf("nn");
fp2=fopen("odd.txt","r");
while((c=getw(fp2))!=EOF)
printf("File-2 Elements : %4d",c);
fcloseall();
}
Output:
Enter the numbers: 1,2,3,4,5,6,7,8,9,10
File-1 Elements: 2 4 6 8 10
File-2 Elements: 1 3 5 7 9
Data Structures using C Lab Manual
Prof. K. Adisesha 8
Write a C program to store record of Student Details in a file using structure.
#include<stdio.h>
struct stud
{
int rno;
float per;
char name[20],add[20];
}s;
int main()
{
FILE *fp;
fp=fopen("student.txt","w");
printf("Enter record of student:nn");
printf("nEnter student number : ");
scanf("%d",&s.rno);
printf("nEnter name of student: ");
scanf("%s",s.name);
printf("nEnter student address : ");
scanf("%s",s.add);
printf("nEnter percentage of student : ");
scanf("%f",&s.per);
fprintf(fp,"%dn%sn%sn%f",s.rno,s.name,s.add,s.per);
printf("nRecord stored in file...");
fclose(fp);
return 0;
}
Output:
Data Structures using C Lab Manual
Prof. K. Adisesha 9
//C program to sort city names in alphabetical order
#include<stdio.h>
#include<string.h>
main(){
int i,j,n;
char str[100][100],s[100];
printf("Enter number of City names :n");
scanf("%d",&n);
printf("Enter City 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(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("nThe sorted order of names are:n");
for(i=0;i<n;i++){
printf("%sn",str[i]);
}
}
Output
Enter number of City names:5
Enter City names in any order:
Mumbai
Chennai
Delhi
Bangalore
Agra
The sorted order of names are:
Agra
Bangalore
Chennai
Delhi
Mumbai
Data Structures using C Lab Manual
Prof. K. Adisesha 10
C program to sort the given list using selection sort technique
#include<stdio.h>
int main()
{
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// Loop to get the elements stored in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);
// Logic of selection sort algorithm
for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Data Structures using C Lab Manual
Prof. K. Adisesha 11
//C program to sort the given list using bubble sort technique
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:n");
for (c = 0; c < n; c++)
printf("%dn", array[c]);
return 0;
}
Data Structures using C Lab Manual
Prof. K. Adisesha 12
// C Program to sort an array in ascending order using Insertion Sort
#include <stdio.h>
int main()
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:n");
for (i = 0; i <= n - 1; i++)
{
printf("%dn", arr[i]);
}
return 0;
}
Data Structures using C Lab Manual
Prof. K. Adisesha 13
// C Program to Perform Quick Sort on a set of Entries from a File using Recursion
#include <stdio.h>
void quicksort (int [], int, int);
int main()
{
int list[50];
int size, i;
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements to be sorted:n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sortn");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("n");
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
Data Structures using C Lab Manual
Prof. K. Adisesha 14
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}
Data Structures using C Lab Manual
Prof. K. Adisesha 15
//C Program to Input Few Numbers & Perform Merge Sort on them using Recursion
#include <stdio.h>
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
Data Structures using C Lab Manual
Prof. K. Adisesha 16
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
Data Structures using C Lab Manual
Prof. K. Adisesha 17
// C Program to implement Linear Search Algorithm recursively
#include<stdio.h>
int Linear_search(int arr[], int Search_ele, int n)
{
int i;
static int temp=0;
if(n>0)
{
i=n-1;
if(arr[i]==Search_ele)
temp=1;
Linear_search(arr,Search_ele,i);
}
return temp;
}
int main()
{
int n,j;
printf("Enter your array size:");
scanf("%d",&n);
int arr[n];
printf("Enter the Array Element:");
for(j=0;j<n;j++)
{
scanf("%d",&arr[j]);
}
int Search_ele;
printf("Enter the search element:");
scanf("%d",&Search_ele);
if(Linear_search(arr,Search_ele,n)==1)
printf("Element found....");
else
printf("Element not found....");
return 0;
}
Data Structures using C Lab Manual
Prof. K. Adisesha 18
//C Program to Perform Binary Search using Recursion
#include<stdio.h>
#include<stdlib.h>
int binsearch(int[], int, int, int);
int main() {
int num, i, key, position;
int low, high, list[10];
printf("nEnter the total number of elements");
scanf("%d", &num);
printf("nEnter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf("nEnter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("nNumber present at %d", (position + 1));
}
else
printf("n The number is not present in the list");
return (0);
}
// Binary search function for binary search
int binsearch(int a[], int x, int low, int high) {
int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (x == a[mid]) {
return (mid);
}
else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
}
else {
binsearch(a, x, mid + 1, high);
}
}
Data Structures using C Lab Manual
Prof. K. Adisesha 19
// C program to implement stack. Stack is a LIFO data structure.
#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATIONn");
while (option)
{
printf ("------------------------------------------n");
printf (" 1 --> PUSH n");
printf (" 2 --> POP n");
printf (" 3 --> DISPLAY n");
printf (" 4 --> EXIT n");
printf ("------------------------------------------n");
printf ("Enter your choicen");
scanf ("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
Data Structures using C Lab Manual
Prof. K. Adisesha 20
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?n");
scanf ("%d", &option);
}
}
/* Function to add an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Fulln");
return;
}
else
{
printf ("Enter the element to be pushedn");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* Function to delete an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Emptyn");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
Data Structures using C Lab Manual
Prof. K. Adisesha 21
{
printf ("Stack is emptyn");
return;
}
else
{
printf ("n The status of the stack is n");
for (i = s.top; i >= 0; i--)
{
printf ("%dn", s.stk[i]);
}
}
printf ("n");
}
Data Structures using C Lab Manual
Prof. K. Adisesha 22
//C Program to Convert Infix to Postfix using Stack
#include<stdio.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;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("n");
e = exp;
while(*e != '0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
Data Structures using C Lab Manual
Prof. K. Adisesha 23
{
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());
}return 0;
}
Output:
Enter the expression: a+b*c
a b c * +
Data Structures using C Lab Manual
Prof. K. Adisesha 24
// C Program to Implement a Queue using an Array
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow n");
else
Data Structures using C Lab Manual
Prof. K. Adisesha 25
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow n");
return ;
}
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
} /* End of display() */
Data Structures using C Lab Manual
Prof. K. Adisesha 26
// C program to create a linked list and display the elements in the list
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data itemn");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?n");
scanf("%d", &choice);
}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("n status of the linked list isn");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULLn");
printf("No. of nodes in the list = %dn", count); }
Data Structures using C Lab Manual
Prof. K. Adisesha 27
//C Program to perform pre order Tree Traversal
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data) {
struct node* node = (struct node*) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* Given a binary tree, print its nodes according to the
"bottom-up" postorder traversal. */
void printPostorder(struct node* node) {
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
printf("%d ", node->data);
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct node* node) {
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
Data Structures using C Lab Manual
Prof. K. Adisesha 28
/* then print the data of node */
printf("%d ", node->data);
/* now recur on right child */
printInorder(node->right);
}
/* Given a binary tree, print its nodes in inorder*/
void printPreorder(struct node* node) {
if (node == NULL)
return;
/* first print data of node */
printf("%d ", node->data);
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
/* Driver program to test above functions*/
int main() {
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("n Preorder traversal of binary tree is n");
printPreorder(root);
printf("n Inorder traversal of binary tree is n");
printInorder(root);
printf("n Postorder traversal of binary tree is n");
printPostorder(root);
getchar();
return 0;
}

More Related Content

PDF
PYTHON PROGRAMMING NOTES.pdf
PDF
Data Structure using C
DOC
Sql queries with answers
DOC
C lab-programs
PDF
C Programming Storage classes, Recursion
DOCX
Data Structures Using C Practical File
PPT
MYSQL Aggregate Functions
PPTX
Constructors in java
PYTHON PROGRAMMING NOTES.pdf
Data Structure using C
Sql queries with answers
C lab-programs
C Programming Storage classes, Recursion
Data Structures Using C Practical File
MYSQL Aggregate Functions
Constructors in java

What's hot (20)

PPT
JDBC
PPTX
Stack and Queue by M.Gomathi Lecturer
PDF
Set methods in python
PPTX
Arrays in c
PPTX
Java Stack Data Structure.pptx
PDF
Modern c++ (C++ 11/14)
PPTX
Introduction to array and string
PPT
PPTX
linked list using c
PDF
Python programming : Control statements
PDF
System Software /Operating System Lab Report
PPT
16717 functions in C++
 
PPT
Packages in java
PPTX
Pointer in c
PDF
Infix to Prefix (Conversion, Evaluation, Code)
PPT
Java Input Output and File Handling
PPTX
PPTX
C# Loops
PPTX
Data structures
PDF
Let us c chapter 4 solution
JDBC
Stack and Queue by M.Gomathi Lecturer
Set methods in python
Arrays in c
Java Stack Data Structure.pptx
Modern c++ (C++ 11/14)
Introduction to array and string
linked list using c
Python programming : Control statements
System Software /Operating System Lab Report
16717 functions in C++
 
Packages in java
Pointer in c
Infix to Prefix (Conversion, Evaluation, Code)
Java Input Output and File Handling
C# Loops
Data structures
Let us c chapter 4 solution
Ad

Similar to DSC program.pdf (20)

PDF
Lab program 1234567891234567891234567891
PDF
Complete Lab 123456789123456789123456789
PDF
lab.123456789123456789123456789123456789
 
PDF
Zoro123456789123456789123456789123456789
 
PDF
labb123456789123456789123456789123456789
 
DOCX
Data Structure in C (Lab Programs)
DOCX
ADA FILE
PDF
C lab programs
PDF
C lab programs
PDF
Common problems solving using c
PPTX
C programm.pptx
DOC
Basic c programs updated on 31.8.2020
DOCX
Write a program to check a given number is prime or not
PDF
DATA STRUCTURE USING C & C++
PDF
DSU C&C++ Practical File Diploma
PPTX
Examples sandhiya class'
PPT
array.ppt
PPTX
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
PPTX
21CS32 DS Module 1 PPT.pptx
Lab program 1234567891234567891234567891
Complete Lab 123456789123456789123456789
lab.123456789123456789123456789123456789
 
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
 
Data Structure in C (Lab Programs)
ADA FILE
C lab programs
C lab programs
Common problems solving using c
C programm.pptx
Basic c programs updated on 31.8.2020
Write a program to check a given number is prime or not
DATA STRUCTURE USING C & C++
DSU C&C++ Practical File Diploma
Examples sandhiya class'
array.ppt
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
21CS32 DS Module 1 PPT.pptx
Ad

More from Prof. Dr. K. Adisesha (20)

PDF
MACHINE LEARNING Notes by Dr. K. Adisesha
PDF
Probabilistic and Stochastic Models Unit-3-Adi.pdf
PDF
Genetic Algorithm in Machine Learning PPT by-Adi
PDF
Unsupervised Machine Learning PPT Adi.pdf
PDF
Supervised Machine Learning PPT by K. Adisesha
PDF
Introduction to Machine Learning PPT by K. Adisesha
PPSX
Design and Analysis of Algorithms ppt by K. Adi
PPSX
Data Structure using C by Dr. K Adisesha .ppsx
PDF
Operating System-4 "File Management" by Adi.pdf
PDF
Operating System-3 "Memory Management" by Adi.pdf
PDF
Operating System Concepts Part-1 by_Adi.pdf
PDF
Operating System-2_Process Managementby_Adi.pdf
PDF
Software Engineering notes by K. Adisesha.pdf
PDF
Software Engineering-Unit 1 by Adisesha.pdf
PDF
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
PDF
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
PDF
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
PDF
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
PDF
Computer Networks Notes by -Dr. K. Adisesha
PDF
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
MACHINE LEARNING Notes by Dr. K. Adisesha
Probabilistic and Stochastic Models Unit-3-Adi.pdf
Genetic Algorithm in Machine Learning PPT by-Adi
Unsupervised Machine Learning PPT Adi.pdf
Supervised Machine Learning PPT by K. Adisesha
Introduction to Machine Learning PPT by K. Adisesha
Design and Analysis of Algorithms ppt by K. Adi
Data Structure using C by Dr. K Adisesha .ppsx
Operating System-4 "File Management" by Adi.pdf
Operating System-3 "Memory Management" by Adi.pdf
Operating System Concepts Part-1 by_Adi.pdf
Operating System-2_Process Managementby_Adi.pdf
Software Engineering notes by K. Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Computer Networks Notes by -Dr. K. Adisesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPT
Project quality management in manufacturing
PPTX
additive manufacturing of ss316l using mig welding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
composite construction of structures.pdf
PPTX
Welding lecture in detail for understanding
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Strings in CPP - Strings in C++ are sequences of characters used to store and...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Project quality management in manufacturing
additive manufacturing of ss316l using mig welding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
composite construction of structures.pdf
Welding lecture in detail for understanding
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Arduino robotics embedded978-1-4302-3184-4.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mechanical Engineering MATERIALS Selection
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx

DSC program.pdf

  • 1. Data Structures using C Lab Manual Prof. K. Adisesha 1 Bengaluru North University II Sem BCA (NEP) Data Structure Using C Programs 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. Data Structures using C Lab Manual Prof. K. Adisesha 2 // C Program To Find GCD of Two Number Using Recursion #include <stdio.h> int GCD(int x, int y); int main() { int a, b; // Asking for Input printf("Enter Two Positive Integers: n"); scanf("%dn %d", &a, &b); printf("GCD of %d and %d is %d.", a, b, GCD(a, b)); return 0; } int GCD(int x, int y) { if( y != 0) return GCD(y, x % y); else return x; }
  • 3. Data Structures using C Lab Manual Prof. K. Adisesha 3 // C Program to Print Pascal Triangle #include < stdio.h > long factorial(int); int main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal trianglen"); scanf("%d", & n); for (i = 0; i < n; i++) { for (c = 0; c <= (n - i - 2); c++) printf(" "); for (c = 0; c <= i; c++) printf("%ld ", factorial(i) / (factorial(c) * factorial(i - c))); printf("n"); } return 0; } long factorial(int n) { int c; long result = 1; for (c = 1; c <= n; c++) result = result * c; return result; }
  • 4. Data Structures using C Lab Manual Prof. K. Adisesha 4 // C Program to Compute fibonacci numbers using recursion method #include<stdio.h> int Fibonacci(int); int main() { int n, i = 0, c; printf("Enter total termsn"); scanf("%d",&n); printf("Fibonacci seriesn"); for ( c = 1 ; c <= n ; c++ ) { printf("%dn", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }
  • 5. Data Structures using C Lab Manual Prof. K. Adisesha 5 //C program for Tower of Hanoi using Recursion #include <stdio.h> void towers(int, char, char, char); int main() { int num; printf("Enter the number of disks : "); scanf("%d", &num); printf("The sequence of moves involved in the Tower of Hanoi are :n"); towers(num, 'A', 'C', 'B'); return 0; } void towers(int num, char frompeg, char topeg, char auxpeg) { if (num == 1) { printf("n Move disk 1 from peg %c to peg %c", frompeg, topeg); return; } towers(num - 1, frompeg, auxpeg, topeg); printf("n Move disk %d from peg %c to peg %c", num, frompeg, topeg); towers(num - 1, auxpeg, topeg, frompeg); }
  • 6. Data Structures using C Lab Manual Prof. K. Adisesha 6 //C program to find the largest and smallest element in an array #include<stdio.h> int main() { int a[50],i,n,large,small; printf("How many elements:"); scanf("%d",&n); printf("Enter the Array:"); for(i=0;i<n;++i) scanf("%d",&a[i]); large=small=a[0]; for(i=1;i<n;++i) { if(a[i]>large) large=a[i]; if(a[i]<small) small=a[i]; } printf("The largest element is %d",large); printf("nThe smallest element is %d",small); return 0; } Output: How many elements:5 Enter the Array:1 8 12 4 6 The largest element is 12 The smallest element is 1
  • 7. Data Structures using C Lab Manual Prof. K. Adisesha 7 //C Program to Write Odd and Even Numbers into Different Files #include<stdio.h> #include<conio.h> void main() { FILE *fp,*fp1,*fp2; int c,i; clrscr(); fp=fopen("data.txt","w"); printf("Enter the numbers"); for(i=0;i<10;i++) { scanf("%d",&c); putw(c,fp); } fclose(fp); fp=fopen("data.txt","r"); fp1=fopen("even.txt","w"); fp2=fopen("odd.txt","w"); while((c=getw(fp))!=EOF) { if(c%2==0) putw(c,fp1); else putw(c,fp2); } fclose(fp); fclose(fp1); fclose(fp2); fp1=fopen("even.txt","r"); while((c=getw(fp1))!=EOF) printf("File-1 Elements : %4d",c); printf("nn"); fp2=fopen("odd.txt","r"); while((c=getw(fp2))!=EOF) printf("File-2 Elements : %4d",c); fcloseall(); } Output: Enter the numbers: 1,2,3,4,5,6,7,8,9,10 File-1 Elements: 2 4 6 8 10 File-2 Elements: 1 3 5 7 9
  • 8. Data Structures using C Lab Manual Prof. K. Adisesha 8 Write a C program to store record of Student Details in a file using structure. #include<stdio.h> struct stud { int rno; float per; char name[20],add[20]; }s; int main() { FILE *fp; fp=fopen("student.txt","w"); printf("Enter record of student:nn"); printf("nEnter student number : "); scanf("%d",&s.rno); printf("nEnter name of student: "); scanf("%s",s.name); printf("nEnter student address : "); scanf("%s",s.add); printf("nEnter percentage of student : "); scanf("%f",&s.per); fprintf(fp,"%dn%sn%sn%f",s.rno,s.name,s.add,s.per); printf("nRecord stored in file..."); fclose(fp); return 0; } Output:
  • 9. Data Structures using C Lab Manual Prof. K. Adisesha 9 //C program to sort city names in alphabetical order #include<stdio.h> #include<string.h> main(){ int i,j,n; char str[100][100],s[100]; printf("Enter number of City names :n"); scanf("%d",&n); printf("Enter City 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(s,str[i]); strcpy(str[i],str[j]); strcpy(str[j],s); } } } printf("nThe sorted order of names are:n"); for(i=0;i<n;i++){ printf("%sn",str[i]); } } Output Enter number of City names:5 Enter City names in any order: Mumbai Chennai Delhi Bangalore Agra The sorted order of names are: Agra Bangalore Chennai Delhi Mumbai
  • 10. Data Structures using C Lab Manual Prof. K. Adisesha 10 C program to sort the given list using selection sort technique #include<stdio.h> int main() { int i, j, count, temp, number[25]; printf("How many numbers u are going to enter?: "); scanf("%d",&count); printf("Enter %d elements: ", count); // Loop to get the elements stored in array for(i=0;i<count;i++) scanf("%d",&number[i]); // Logic of selection sort algorithm for(i=0;i<count;i++){ for(j=i+1;j<count;j++){ if(number[i]>number[j]){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } } printf("Sorted elements: "); for(i=0;i<count;i++) printf(" %d",number[i]); return 0; }
  • 11. Data Structures using C Lab Manual Prof. K. Adisesha 11 //C program to sort the given list using bubble sort technique #include <stdio.h> int main() { int array[100], n, c, d, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < n - 1; c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:n"); for (c = 0; c < n; c++) printf("%dn", array[c]); return 0; }
  • 12. Data Structures using C Lab Manual Prof. K. Adisesha 12 // C Program to sort an array in ascending order using Insertion Sort #include <stdio.h> int main() { int n, i, j, temp; int arr[64]; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 1 ; i <= n - 1; i++) { j = i; while ( j > 0 && arr[j-1] > arr[j]) { temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; j--; } } printf("Sorted list in ascending order:n"); for (i = 0; i <= n - 1; i++) { printf("%dn", arr[i]); } return 0; }
  • 13. Data Structures using C Lab Manual Prof. K. Adisesha 13 // C Program to Perform Quick Sort on a set of Entries from a File using Recursion #include <stdio.h> void quicksort (int [], int, int); int main() { int list[50]; int size, i; printf("Enter the number of elements: "); scanf("%d", &size); printf("Enter the elements to be sorted:n"); for (i = 0; i < size; i++) { scanf("%d", &list[i]); } quicksort(list, 0, size - 1); printf("After applying quick sortn"); for (i = 0; i < size; i++) { printf("%d ", list[i]); } printf("n"); return 0; } void quicksort(int list[], int low, int high) { int pivot, i, j, temp; if (low < high) { pivot = low; i = low; j = high; while (i < j) { while (list[i] <= list[pivot] && i <= high) { i++; } while (list[j] > list[pivot] && j >= low) { j--; } if (i < j) { temp = list[i];
  • 14. Data Structures using C Lab Manual Prof. K. Adisesha 14 list[i] = list[j]; list[j] = temp; } } temp = list[j]; list[j] = list[pivot]; list[pivot] = temp; quicksort(list, low, j - 1); quicksort(list, j + 1, high); } }
  • 15. Data Structures using C Lab Manual Prof. K. Adisesha 15 //C Program to Input Few Numbers & Perform Merge Sort on them using Recursion #include <stdio.h> void mergeSort(int [], int, int, int); void partition(int [],int, int); int main() { int list[50]; int i, size; printf("Enter total number of elements:"); scanf("%d", &size); printf("Enter the elements:n"); for(i = 0; i < size; i++) { scanf("%d", &list[i]); } partition(list, 0, size - 1); printf("After merge sort:n"); for(i = 0;i < size; i++) { printf("%d ",list[i]); } return 0; } void partition(int list[],int low,int high) { int mid; if(low < high) { mid = (low + high) / 2; partition(list, low, mid); partition(list, mid + 1, high); mergeSort(list, low, mid, high); } } void mergeSort(int list[],int low,int mid,int high) { int i, mi, k, lo, temp[50]; lo = low; i = low; mi = mid + 1; while ((lo <= mid) && (mi <= high))
  • 16. Data Structures using C Lab Manual Prof. K. Adisesha 16 { if (list[lo] <= list[mi]) { temp[i] = list[lo]; lo++; } else { temp[i] = list[mi]; mi++; } i++; } if (lo > mid) { for (k = mi; k <= high; k++) { temp[i] = list[k]; i++; } } else { for (k = lo; k <= mid; k++) { temp[i] = list[k]; i++; } } for (k = low; k <= high; k++) { list[k] = temp[k]; } }
  • 17. Data Structures using C Lab Manual Prof. K. Adisesha 17 // C Program to implement Linear Search Algorithm recursively #include<stdio.h> int Linear_search(int arr[], int Search_ele, int n) { int i; static int temp=0; if(n>0) { i=n-1; if(arr[i]==Search_ele) temp=1; Linear_search(arr,Search_ele,i); } return temp; } int main() { int n,j; printf("Enter your array size:"); scanf("%d",&n); int arr[n]; printf("Enter the Array Element:"); for(j=0;j<n;j++) { scanf("%d",&arr[j]); } int Search_ele; printf("Enter the search element:"); scanf("%d",&Search_ele); if(Linear_search(arr,Search_ele,n)==1) printf("Element found...."); else printf("Element not found...."); return 0; }
  • 18. Data Structures using C Lab Manual Prof. K. Adisesha 18 //C Program to Perform Binary Search using Recursion #include<stdio.h> #include<stdlib.h> int binsearch(int[], int, int, int); int main() { int num, i, key, position; int low, high, list[10]; printf("nEnter the total number of elements"); scanf("%d", &num); printf("nEnter the elements of list :"); for (i = 0; i < num; i++) { scanf("%d", &list[i]); } low = 0; high = num - 1; printf("nEnter element to be searched : "); scanf("%d", &key); position = binsearch(list, key, low, high); if (position != -1) { printf("nNumber present at %d", (position + 1)); } else printf("n The number is not present in the list"); return (0); } // Binary search function for binary search int binsearch(int a[], int x, int low, int high) { int mid; if (low > high) return -1; mid = (low + high) / 2; if (x == a[mid]) { return (mid); } else if (x < a[mid]) { binsearch(a, x, low, mid - 1); } else { binsearch(a, x, mid + 1, high); } }
  • 19. Data Structures using C Lab Manual Prof. K. Adisesha 19 // C program to implement stack. Stack is a LIFO data structure. #include <stdio.h> #define MAXSIZE 5 struct stack { int stk[MAXSIZE]; int top; }; typedef struct stack STACK; STACK s; void push(void); int pop(void); void display(void); void main () { int choice; int option = 1; s.top = -1; printf ("STACK OPERATIONn"); while (option) { printf ("------------------------------------------n"); printf (" 1 --> PUSH n"); printf (" 2 --> POP n"); printf (" 3 --> DISPLAY n"); printf (" 4 --> EXIT n"); printf ("------------------------------------------n"); printf ("Enter your choicen"); scanf ("%d", &choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: return;
  • 20. Data Structures using C Lab Manual Prof. K. Adisesha 20 } fflush (stdin); printf ("Do you want to continue(Type 0 or 1)?n"); scanf ("%d", &option); } } /* Function to add an element to the stack */ void push () { int num; if (s.top == (MAXSIZE - 1)) { printf ("Stack is Fulln"); return; } else { printf ("Enter the element to be pushedn"); scanf ("%d", &num); s.top = s.top + 1; s.stk[s.top] = num; } return; } /* Function to delete an element from the stack */ int pop () { int num; if (s.top == - 1) { printf ("Stack is Emptyn"); return (s.top); } else { num = s.stk[s.top]; printf ("poped element is = %dn", s.stk[s.top]); s.top = s.top - 1; } return(num); } /* Function to display the status of the stack */ void display () { int i; if (s.top == -1)
  • 21. Data Structures using C Lab Manual Prof. K. Adisesha 21 { printf ("Stack is emptyn"); return; } else { printf ("n The status of the stack is n"); for (i = s.top; i >= 0; i--) { printf ("%dn", s.stk[i]); } } printf ("n"); }
  • 22. Data Structures using C Lab Manual Prof. K. Adisesha 22 //C Program to Convert Infix to Postfix using Stack #include<stdio.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; } int main() { char exp[100]; char *e, x; printf("Enter the expression : "); scanf("%s",exp); printf("n"); e = exp; while(*e != '0') { if(isalnum(*e)) printf("%c ",*e); else if(*e == '(') push(*e); else if(*e == ')')
  • 23. Data Structures using C Lab Manual Prof. K. Adisesha 23 { 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()); }return 0; } Output: Enter the expression: a+b*c a b c * +
  • 24. Data Structures using C Lab Manual Prof. K. Adisesha 24 // C Program to Implement a Queue using an Array #include <stdio.h> #define MAX 50 void insert(); void delete(); void display(); int queue_array[MAX]; int rear = - 1; int front = - 1; main() { int choice; while (1) { printf("1.Insert element to queue n"); printf("2.Delete element from queue n"); printf("3.Display all elements of queue n"); printf("4.Quit n"); printf("Enter your choice : "); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice n"); } /* End of switch */ } /* End of while */ } /* End of main() */ void insert() { int add_item; if (rear == MAX - 1) printf("Queue Overflow n"); else
  • 25. Data Structures using C Lab Manual Prof. K. Adisesha 25 { if (front == - 1) /*If queue is initially empty */ front = 0; printf("Inset the element in queue : "); scanf("%d", &add_item); rear = rear + 1; queue_array[rear] = add_item; } } /* End of insert() */ void delete() { if (front == - 1 || front > rear) { printf("Queue Underflow n"); return ; } else { printf("Element deleted from queue is : %dn", queue_array[front]); front = front + 1; } } /* End of delete() */ void display() { int i; if (front == - 1) printf("Queue is empty n"); else { printf("Queue is : n"); for (i = front; i <= rear; i++) printf("%d ", queue_array[i]); printf("n"); } } /* End of display() */
  • 26. Data Structures using C Lab Manual Prof. K. Adisesha 26 // C program to create a linked list and display the elements in the list #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main() { struct node { int num; struct node *ptr; }; typedef struct node NODE; NODE *head, *first, *temp = 0; int count = 0; int choice = 1; first = 0; while (choice) { head = (NODE *)malloc(sizeof(NODE)); printf("Enter the data itemn"); scanf("%d", &head-> num); if (first != 0) { temp->ptr = head; temp = head; } else { first = temp = head; } fflush(stdin); printf("Do you want to continue(Type 0 or 1)?n"); scanf("%d", &choice); } temp->ptr = 0; /* reset temp to the beginning */ temp = first; printf("n status of the linked list isn"); while (temp != 0) { printf("%d=>", temp->num); count++; temp = temp -> ptr; } printf("NULLn"); printf("No. of nodes in the list = %dn", count); }
  • 27. Data Structures using C Lab Manual Prof. K. Adisesha 27 //C Program to perform pre order Tree Traversal #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { int data; struct node* left; struct node* right; }; /* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return (node); } /* Given a binary tree, print its nodes according to the "bottom-up" postorder traversal. */ void printPostorder(struct node* node) { if (node == NULL) return; // first recur on left subtree printPostorder(node->left); // then recur on right subtree printPostorder(node->right); // now deal with the node printf("%d ", node->data); } /* Given a binary tree, print its nodes in inorder*/ void printInorder(struct node* node) { if (node == NULL) return; /* first recur on left child */ printInorder(node->left);
  • 28. Data Structures using C Lab Manual Prof. K. Adisesha 28 /* then print the data of node */ printf("%d ", node->data); /* now recur on right child */ printInorder(node->right); } /* Given a binary tree, print its nodes in inorder*/ void printPreorder(struct node* node) { if (node == NULL) return; /* first print data of node */ printf("%d ", node->data); /* then recur on left sutree */ printPreorder(node->left); /* now recur on right subtree */ printPreorder(node->right); } /* Driver program to test above functions*/ int main() { struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); printf("n Preorder traversal of binary tree is n"); printPreorder(root); printf("n Inorder traversal of binary tree is n"); printInorder(root); printf("n Postorder traversal of binary tree is n"); printPostorder(root); getchar(); return 0; }