C PROGRAMMING AND DATA STRUCTURES lab MANUAL
C PROGRAMMING AND DATA STRUCTURES lab MANUAL
Ex.No. Date Name of the Experiment Page Date of Marks Staff Remarks
No. completion Awarded Signature
1. Statements, expressions, 6
decision making and iterative
statements
2. Functions and Arrays 12
3. Implement C programs 18
using Pointers and
Structures
4. Implement C programs using 22
Files
5. Development of real time 27
C applications
6. Array implementation of List 30
ADT
7. Array implementation of Stack 38
and Queue ADTs
8. Linked list 48
implementation of List,
Stack and Queue ADTs
9. Applications of List, Stack 60
and Queue ADTs
10. Implementation of Binary 66
Trees and operations of
Binary Trees
11. Implementation of Binary 73
Search Trees
12. Implementation of searching 79
techniques
13. Implementation of 81
Sorting algorithms :
Insertion Sort, Quick Sort,
Merge Sort
14. Implementation of Hashing – 86
any two collision techniques
4
Experiment Score /10
EX.No:1 Date
of Completion Additional
Credits
AIM
To write a C program to practice of c programming using statements, expressions,
Decision making and iterative statements
ALGORITHM
Step 1: Start
Step 2: Declare variables num1, num2,num3 and sum,average.
Step 3: Read values num1,num2,num3
Step 4: Add num1,num2,num3 and assign the result to sum. sum←num1+num2 +num3
average ← sum/3
Step 5: Display sum and average
Step 6: Stop
PROGRAM
#include<stdio.h>
int main( )
{
int a,b,c;
int sum,average;
printf("Enter any three integers: ");
scanf("%d%d %d",&a,&b,&c);
sum = a+b+c;
average=sum/3
printf("Sum and average of three integers: %d %d",sum,average);
return 0;
}
6
OUTPUT
DECISION MAKING
ALGORITHM
1. Start.
2. Read year.
3. Check whether year is divisible by 4 and 400 and not divisible by 100.
4. If the condition is true then print year is leap year. Otherwise print year is notleap year.
5. Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>void main()
{
int i, year;clrscr();
printf(“Enter the value of N:”);
scanf(“%d”,&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf(“%d is a leap year\n”,year);
else
7
OUTPUT:
ITERATIVE STATEMENTS
ALGORITHM
1. Start
2. Read n, assign sum = 0
3. for i = 1 to n
Calculate sum = sum + 1/(i*i*i)
4. print sum
5. Stop.
PROGRAM
#include<stdio.h>
void main()
{
intn,i,sum=0;
clrscr();
printf(“\n Enter n \t”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
sum=sum+i*i*i;
}
printf(“Sum = %d”,sum);
getch();
8
OUTPUT
Enter n 5
Sum = 225
RESULT
9
Experiment Score /10
EX.No:2 Date
of Completion Additional
Credits
AIM
To write a C program to practice of c programming using functions and arrays statements.
ARRAYS STATEMENTS
ALGORITHM
1. Start.
6. Add the element of A and B in column wise and store the result insum matrix.
8. Stop.
PROGRAM
#include<stdio.h>
void main()
{
int A[3][3],B[3][3],sum[3][3],i,j;
clrscr();
printf(“Enter the elements of matrix A”);for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
12
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Enter the elements of matrix B”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&B[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=A[i][j]+B[i][j];
}
}
printf(“sum of two matrix\n”);for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%3d”,sum[i][j]);
printf(“\n”);
}
}
OUTPUT
Enter the elements of matrix A
101
111
010
Enter the elements of matrix B
011
100
111
Sum of two matrix
112
211
121
13
FUNCTIONS
ALGORITHM
1. Start
2. Read a,b,c
5. Stop.
Max function:
1. Start
4. Return max.
5. Stop.
PROGRAM
#include<stdio.h>
int max(int,int,int);
void main()
{
int a,b,c,d;
clrscr();
printf(“\n Enter three integer values:”);
scanf(“%d %d %d”,&a,&b,&c);
d=max(a,b,c);
printf(“\n Maximum is : %d”,d);
getch();
}
int max(int a, int b,int c)
14
{
int max; if(a>b&&a>c)max=a;
else if(b>c)max=b;
else max=c;
return(max);
}
OUTPUT
Enter three integer values:
56
28
79
Maximum is: 79
RESULT
15
Experiment Score /10
EX.No:3 Date
of Completion Additional
Credits
AIM
To write a C program to implement C programs using Pointers and Structures.
ALGORITHM
1. Start
5. Stop.
PROGRAM
#include<stdio.h>
void main()
{
struct
{
int rollno;
char name[30];
char branch[4];
int marks;
*stud;
18
printf(“\n Enter Rollno:”);
scanf(“%d”, &stud->rollno);
printf(“\n Enter Name :”);
scanf(“%s”,stud->name);
printf(“\n Enter Branch:”);
scanf(“%s”,stud->branch);
printf(“\n Enter Marks:”);
scanf(“%d”,&stud->marks);
19
Experiment Score /10
EX.No:4 Date
of Completion Additional
Credits
ALGORITHM
1. Start
2. Create file pointers
7. Stop.
PROGRAM
#include <stdio.h>
#include <string.h>
Main()
{
FILE *fp;
22
char filename[20];
printf(“\n Enter the filename: “);
fp = fopen(filename, “r”);
if(fp==NULL)
{
exit (1);
}
ch= fgetc(fp);
while(ch!=EOF)
{
if(ch==’\n’);
no_of_lines++;
no_of_characters++;
ch = fgetc(fp);
}
if (no_of_characters >0)
printf(“\n In the file %s, there are %d lines and %d characters”, filename, no_of_lines,
no_of_characters);
else
fclose(fp);
}
23
OUTPUT
RESULT
24
Experiment Score /10
EX.No:5 Date
of Completion Additional
Credits
ALGORITHM
1. Start
2. Define pi as 3.1415
4. Read radius
6. Print area
7. Stop.
PROGRAM
#include <stdio.h>
#define PI 3. 1415
#definecircleArea(r) (PI*r*r)
int main()
{
RESULT
PRACTICE EXCERICES
28
Experiment Score /10
EX.No:6 Date
of Completion Additional
Credits
AIM
To write a C program to Array implementation of List ADT.
ALGORITHM
30
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 10 void
create(); void insert();
void deletion();void
search(); void
display();
int a,b[20], n, p, e, f, i, pos; void
main()
{
//clrscr(); int
ch; char g='y';do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit
\n"); printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();break;
case 2:
deletion();break;
case 3:
search();break;
case 4:
insert(); break;
case 5:
display();
break; case 6:
exit(); break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
31
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
}
void deletion()
{
printf("\n Enter the position u want to delete::");scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion"); for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}
void search()
{
printf("\n Enter the Element to be searched:");scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e); continue;
}
}
}
void insert()
{
printf("\n Enter the position u need to insert::"); scanf("%d", &pos);
if(pos>=n)
32
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;}
OUTPUT:
Main Menu
1. Create
2. Delete
3. Search
4. insert
5. Display
6. Exit
Enter Your Choice : 1
Enter the Number of Nodes : 3
Enter the Element : 1
Enter the Element : 12
Enter the Element : 23
Do you want to continue : Y
Main Menu
1. Create
2. Delete
3. Search
4. insert
5. Display
33
6. Exit
Enter Your Choice : 2
Enter the position you want to delete : 2
The Element after deletion : 1 12
Do you want to continue: Y
Main Menu
1. Create
2. Delete
3. Search
4. insert
5. Display
6. Exit
Enter Your Choice : 3
Enter the Element to be Searched : 12
Value is in the Position : 1
Do you want to continue: Y
Main Menu
1. Create
2. Delete
4. insert
5. Display
6. Exit
Enter Your Choice : 4
Enter the position you need to insert : 1
Enter the element to Insert : 89
The list after insertion : 1 89 12
RESULT
34
Experiment Score /10
EX.No:7 Date
of Completion Additional
Credits
AIM
To write a C program to Array implementation of Stack and Queue ADTs.
ALGORITHM
38
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
PROGRAM:
#include <stdio.h>
#include <stdlib.h> int
stack[6],rev[6];
int top=-1,k=0;
int size; void
push();void
pop();
void display(); int
pali();
void main()
{
int choice,f;
printf("Enter the size for stack\n");
scanf("%d",&size);
printf("1.Push\n2.Pop\n3.Display\n4.Check for
Palindrome\n5.Exit\n");while(1)
{
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:f=pali();
if(f==1)
printf("It's Palindrome\n");
else
printf("It's not a Palindrome\n");
break;
case 5:exit(0); default:printf("Wrong
choice...\n");
}}}
void push() { int
num;
if(top==(size-1))
{
printf("Stack Overflow\n");
}
else {
printf("Enter the number to be pushed\n");
39
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
scanf("%d",&num);
top++; stack[top]=num;
}}void pop() {
int num;
if(top==-1)
{
printf("Stack Underflow\n");
}
else {
num=stack[top];
printf("Popped element is %d\n",num);
top--
;
}}
void display() { int i;
if(top==-1)
{
printf("Stack Underflow\n");
}
else
{
printf("Stack Contents. \n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
rev[k++]=stack[i];
}
}
}
int pali()
{
int i,flag=1;
for(i=top;i>=0;i--)
{
if(stack[i]!=rev[--k])
{
flag=0;
}
}
return flag;
}
OUTPUT:
--------STACK OPERATIONS-----------
1.Push 2.Pop
3. Display
4. Check for Palindrome
40
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
5.Exit
- Enter
your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 2
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 5
Enter your choice: 2
The poped element: 5 Enter
your choice: 3 The stack
elements are:
1
2
1
Enter your choice: 4 It’s a
Palindrome Enter your
choice: 5
ALGORITHM
1. Define a array which stores queue elements
2. The operations on the queue are a)INSERT data into the queue b)DELETE
data out of queue.
3. INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL the input data in the first node in the queue & the link of the
node is NULL.
c. If the TOP is not NULL the link of top points to the new node. TOP pointsto
the node
1. Delete data from queue.
a. If TOP is NULL the queue is empty
b. If TOP is NOT NULL the link of TOP is the current TOP,the
pervious TOP is popped from queue.
2. The queue represented by linked list is traversed to display its content.
41
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define SIZE 5
char CQ[SIZE];
int front=-1, rear=-1; int ch;
void CQ_Insert(); void CQ_Delet();
void CQ_Display();void
main()
{
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
while(1)
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: CQ_Insert();
break;
case 2:CQ_Delet();
break;
case 3:CQ_Display();
break;
case 4: exit(0);
}
}}
void CQ_Insert() { char
ele;
if(front==(rear+1)%SIZE) {
printf("Circular Queue Full\n");
return; }
if(front==-1) front++;
printf("Enter the element to be inserted\n"); scanf("\n%c",&ele);
rear = (rear+1)%SIZE;CQ[rear] =ele; }
void CQ_Delet() { char item; if(front ==-1)
{
printf("Circular Queue Empty\n");
return; }
else if(front == rear) {item=CQ[front];
printf("Deleted element is: %c\n",item);front=
-1;
rear=
-1;
42
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
}
else {
item =CQ[front];
printf("Deleted element is: %c\n",item);front = (front+1)%SIZE; }}
void CQ_Display()
{
int i; if(front==-1)
OUTPUT:
Circular Queue operations
1.insert
2. delete 3.display 4.exit
43
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
RESULT
PRACTICE EXCERICES
44
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
To write a C program Linked list implementation of List, Stack and Queue ADTs.
ALGORITHM
48
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM :
#include <stdio.h>
#include <malloc.h> #include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display()
;void delete();
int count();
typedef struct node DATA_NODE;
OUTPUT
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:1
Enter Element for Insert Linked List : 100
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:1
Enter Element for Insert Linked List :200
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:1
Enter Element for Insert Linked List : 300
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:1
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:1
Enter Element for Insert Linked List : 500
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:3
Display Linked List :
51
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
# 100 # # 200 # # 300 # # 400 # # 500 #
No Of Items In Linked List : 5
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter your option:4
No Of Items In Linked List : 5
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Enter Position for Delete Element : 3
Deleted Successfully
Options
1 : Insert into Linked List 2 : Delete from Linked List 3 : Display Linked
List
4 : Count Linked ListOthers : Exit()
Options
1 : Insert into
Linked List 2 : Delete from Linked List 3 : Display Linked List
4 : Count Linked ListOthers : Exit()
Enter your option:2
No Of Items In Linked List : 4
Invalid PositionOptions
1 : Insert into Linked List
2 : Delete from Linked List 3 : Display Linked List
4 : Count Linked ListOthers : Exit()
Enter your option: 5
52
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
break;
53
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
case 8:
destroy();break;
default :
printf(" Wrong choice, Please enter correct choice ");break;
}
}
}
/* Create empty stack */void create()
{
top = NULL;
}
/* Count stack elements */void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node)); top-
>ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node)); temp->ptr = top;
temp->info = data; top = temp;
}
count++;
}
/* Display stack elements */void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");return;
}
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack"); return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);free(top);
top = top1;count--;
}
/* Return top element */ int topelement()
{
return(top->info);
}
OUTPUT
55
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Enter choice : 2
Enter choice : 6 90 78 56
Enter choice : 7
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int
frontelement();
void enq(int data);
void deq();
void empty(); void display();void create();
void queuesize();int count = 0; void main()
{
int no, ch, e; printf("\n 1 - Enque");printf("\n 2 - Deque");
printf("\n 3 - Front element"); printf("\n 4 - Empty"); printf("\n 5 - Exit");
printf("\n 6 - Display"); printf("\n 7 - Queue size");create();
while (1)
{
printf("\n Enter choice : ");scanf("%d", &ch);
56
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
switch (ch)
{
case 1:
printf("Enter data : ");scanf("%d", &no); enq(no);
break; case 2:
deq();break;
case 3:
e = frontelement(); if (e != 0)
printf("Front element : %d", e);else
printf("\n No front element in Queue as queue is empty"); break;
case 4:
empty();break;
case 5:
exit(0);case 6:
display();break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
rear = temp;
}
count++;
}
OUTPUT:
RESULT
59
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
To write a C program for Applications of List, Stack and Queue ADTs.
ALGORITHM
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#define new_node (struct node*)malloc(sizeof(struct node)) struct node
{
int vertex;
struct node *next;
};
void main()
{
int option;
do
{
printf("\n A Program to represent a Graph by using an Adjacency List \n "); printf("\n 1. Directed
Graph ");
printf("\n 2. Un-Directed Graph "); printf("\n 3. Exit ");
60
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
printf("\n\n Select a proper option : "); scanf("%d", &option);
switch(option)
{
case 1 : dir_graph();
break;
case 2 : undir_graph();
break;
case 3 : exit(0);
}
}
while(1);
}
int dir_graph()
{
struct node *adj_list[10], *p;
int n;
int in_deg, out_deg, i, j;
printf("\n How Many Vertices ? : "); scanf("%d", &n);
for( i = 1 ; i <= n ; i++ ) adj_list[i] = NULL;
read_graph (adj_list, n);
printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ")
;for (i = 1; i <= n ; i++ )
{
in_deg = out_deg = 0; p = adj_list[i];
while( p != NULL )
{
out_deg++; p = p -> next;
}
for ( j = 1 ; j <= n ; j++ )
{
p = adj_list[j]; while( p != NULL )
{
if ( p -> vertex == i ) in_deg++;
p = p -> next;
}
}
printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n", i, in_deg, out_deg, in_deg + out_deg);
}
return;
}
int undir_graph()
{
struct node *adj_list[10], *p;
int deg, i, j, n;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);
for ( i = 1 ; i <= n ; i++ )
adj_list[i] = NULL;
61
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
read_graph(adj_list, n);
printf("\n Vertex \t Degree "); for ( i = 1 ; i <= n ; i++ )
{
deg = 0;
p = adj_list[i]; while( p != NULL )
{
deg++;
p = p -> next;
}
printf("\n\n %5d \t\t %d\n\n", i, deg);
}
return;
}
int read_graph ( struct node *adj_list[10], int n )
{
int i, j; char reply;
struct node *p, *c;
for ( i = 1 ; i <= n ; i++ )
{
for ( j = 1 ; j <= n ; j++ )
{
if ( i == j ) continue;
printf("\n Vertices %d & %d are Adjacent ? (Y/N) :", i, j);
scanf("%c", &reply);
if ( reply == 'y' || reply == 'Y' )
{
c = new_node;c -> vertex = j;
c -> next = NULL;
if ( adj_list[i] == NULL ) adj_list[i] = c;
else
{
p = adj_list[i];
while ( p -> next != NULL ) p = p -> next;
p -> next = c;
}
} } }
return;
}
OUTPUT:
1. Directed Graph
2. Un-Directed Graph
3. Exit
1 2 0 2
2 1 2 3
3 0 1 1
4 1 1 2
63
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PROGRAM:
#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);}
OUTPUT:
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
PROGRAM:
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):"); scanf("%d",&n);
printf("\nEnter Process Burst Time\n");for(i=0;i<n;i++)
{
64
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0; for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
//calculating turnaround time for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];avwt+=wt[i]; avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i; avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
OUTPUT:
RESULT
65
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
To write a C program Implementation of Binary Trees and operations of Binary Trees
ALGORITHM
PROGRAM
#include<stdlib.h>
#include<stdio.h>
struct bin_tree
{
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
insert(&(*tree)->left, val);
}
66
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right); free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
search(&((*tree)->left), val);
}
67
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
68
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
OUTPUT
Pre Order Display 9
4
2
6
15
12
17
In Order Display 2
4
6
9
12
15
17
Post Order Display 2
6
4
12
17
15
9
Searched node=4
RESULT
PRACTICE EXERCISES
1. Write a C program to create the binary search tree and do the deletion operation for all constraints on
the created binary search tree
69
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
To write a C program Implementation of Binary Search Trees.
ALGORITHM
2. Create a tree with pointers for left and right sub tree.
3. Insert an element is by checking the top node and the leaf node and the operation
will be performed.
4. Deleting an element contains searching the tree and deleting the item.
Display the Tree elements
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct BST
{
int data;
struct BST *left; struct BST *right;
}node;
node *create();
void insert(node *,node *);void preorder(node *);
int main()
{
char ch;
node *root=NULL,*temp;
do
{
temp=create(); if(root==NULL)
root=temp;else
73
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
insert(root,temp);
node *create()
{
node *temp; printf("nEnter data:");
temp=(node*)malloc(sizeof(node)); scanf("%d",&temp->data);
temp->left=temp->right=NULL;return temp;
}
void insert(node *root,node *temp)
{
if(temp->data<root->data)
{
if(root->left!=NULL) insert(root->left,temp);
else
root->left=temp;
}
if(temp->data>root->data)
{
if(root->right!=NULL) insert(root->right,temp);
else
root->right=temp;
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("%d ",root->data);preorder(root->left); preorder(root->right);
}
}
OUTPUT:
[com39@localhost ~]$ cd os
[com39@localhost os]$ gcc ex6.c
[com39@localhost os]$
./a.out
74
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
RESULT
PRACTICE EXERCISES
1. Develop a C program for implementing a binary tree data structure and perform inorder, preorder and
postorder traversal
75
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
void main()
{
int array[10];
int i, num, keynum, found = 0;
OUTPUT
RESULT
80
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
ALGORITHM
PROGRAM
#include <stdio.h>
void selection(int [], int, int, int, int);
int main()
{
int list[30], size, temp, i, j;
printf("Enter the size of the list: "); scanf("%d", &size);
printf("Enter the elements in list:\n"); for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
selection(list, 0, 0, size, 1);
printf("The sorted list in ascending order is\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
return 0;
}
81
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i]; list[i] = list[j]; list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}
}
OUTPUT
RESULT
82
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
AIM
ALGORITHM
4. Use the hash function to allocate the spot and to find the index for
record.
5. Repeat the process until collision occurs.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int create(int);
void linear_prob(int[], int, int);
void display (int[]);
void main()
{
int a[MAX],num,key,i; int
ans=1;
printf(" collision handling by linear probing : \n"); for (i=0;i<MAX;i++)
{
a[i] = -1;
}
do
{
86
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
printf("\n Enter the data"); scanf("%4d", &num); key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) "); scanf("%d",&ans);
}while(ans); display(a);
}
int create(int num)
{
int key;
key=num%100; return
key;
}
void linear_prob(int a[MAX], int key, int num)
{
int flag, i, count=0;
flag=0; if(a[key]== -1)
{
a[key] = num;
}
else
{
printf("\nCollision Detected...!!!\n"); i=0;
while(i<MAX)
{
if (a[i]!=-1)
count++; i++;
}
printf("Collision avoided successfully using LINEAR PROBING\n"); if(count == MAX)
{
printf("\n Hash table is full"); display(a);
exit(1);
}
for(i=key+1; i<MAX; i++) if(a[i]
== -1)
{
a[i] = num; flag =1; break;
}
//for(i=0;i<key;i++) i=0;
while((i<key) && (flag==0))
{
if(a[i] == -1)
{
a[i] = num; flag=1; break;
} i++;
}
}
}
void display(int a[MAX])
{
int i,choice;
87
DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
printf("1.Display ALL\n 2.Filtered Display\n"); scanf("%d",&choice);
if(choice==1)
{
printf("\n the hash table is\n"); for(i=0; i<MAX; i++)
printf("\n %d %d ", i, a[i]);
}
else
{
printf("\n the hash table is\n"); for(i=0; i<MAX; i++)
if(a[i]!=-1)
{
printf("\n %d %d ", i, a[i]); continue;
}
}
}
OUTPUT:
Collision Detected...!!!
Collision avoided successfully using LINEAR PROBING
RESULT
89