Sei sulla pagina 1di 140

ANNAMALAI

UNIVERSITY

FACULTY OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

MASTER OF COMPUTER APPLICATION

I SEMESTER

MCAP 107 PROGRAMMING LAB II


DATA STRUCTURE USING - C

MCA1710 Programming Lab- II (Data Structures Using C)


List of Experiments
1. RECURSION IN C
a) Factorial numbers
b) Fibonacci series
2. Implementation of Stack Using Array
3. Implementation of Queue Using Array
4 . Conversion of Infix to Postfix
5. Single Linked list
6. Doubly Linked List
7. Implementation of Stack Using Linked List
8. Implementation of Queue Using Linked List
9. Binary Tree Traversal
10. Binary Search Tree
11. SORTING TECHNIQUES
a) Bubble Sort
b) Merge Sort
c) QuickSort
d) Radix Sort
12. SEARCHING TECHNIQUES
a) Linear Search
b) Binary Search

Lab Incharge

(Dr.J.Sasikala)

1.Recursion in C
a) Factorial numbers
Aim

Write a C program to implement Factorial of a


number using Data Structures
Algorithm

(i) Start the program


(ii) Read the number n
(iii)[Initialize]
i=1, fact=1
(iv)Repeat step 4 through 6 until i=n
(v)fact=fact*i
(vi) i=i+1
(vii) Print fact
(viii)Stop the program.
Program
#include<stdio.h>
#include<conio.h>
int main()
{

int n,i,fact=1;
printf("Enter any number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact = fact * i;
printf("Factorial value of %d = %d",n,fact);
return 0;
}
Sample output
Enter a number: 5
Factorial expansion phase:
5
4
3
2
1
Factorial wrapup phase:
1
2
6

24
120
The factorial of number 5 is 120.

Result
Thus the program was successfully executed
and the output was verified.

b) Fibonacci series
Aim
Write a C Program to implement Fibonacci
Series using Data Structures
Algorithm
(i)Start the program
(ii)Declare variables n,i=0,j=1,temp,k.
(iii)Input and read the limit of the Fibonacci
series,n.
(iv)If n==1 then
1)Print i and j.
(v)Else
1).Print i and j.

2).Initialize k as 2.
3).while(k<n) repeat steps a to e.
a)Find temp= i+j.
b)Print temp.
c)Assign i=j.
d)Assign j=temp.
e)Increment k by 1.
4).End while.
(vi) End if.
(vii) Stop the program
Program
#include<stdio.h>

#include<conio.h>
void main()
{
int n,i=0,j=1,temp,k;
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("fibonacci series\n");
if(n==1)
{
printf("%d\n%d\n",i,j);
}

else
{
printf("%d\n%d\n",i,j);
for(k=2;k<n;k++)
{
temp=i+j;
printf("%d\n",temp);
i=j;
j=temp;
}
}
getch();

Output
Enter the limit:13
Fibonacci Series
0
1
1
2
3
5
8

13
21
34
55
89
144

Result
Thus the program was successfully executed
and the output was verified.

2. IMPLEMENTATION OF STACK USING

ARRAY

Aim
To write a C program to perform the
implementation of stack using array.
Algorithm
Step 1: Start the program
Step 2: Initialize the array variable st[100] for
storing elements and declare the required
variables.
Step 3: Define a function to push , pop and display
the data item of the queue.
Step 4: For push() function
Get the new elements
Create the top is greater than array
size,display stack is overflow.
else
Insert the new element and increment the top
pointer.

Step 5: For pop() function


If the Stack is empty then display stack is
underflow.
else
Decrement the top pointer.
Step 6: For display() function
Apply the loop for to display the stack
elements.
Step 7: Stop the program.
Program
//stack using array
#include<stdio.h>
#include<conio.h>
int stack[100],option,n,top,x,i;
void push();
void pop();
void display();

void main()
{
clrscr();
top=-1;
printf(\n enter the size of STACK[MAX=100]:);
scanf(%d,&n);
printf(\n\t\t STACK OPERATION);
printf(\n\t\t~~~~~~~~~~~~~~~~~);
printf(\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.
EXIT);
do
{
//clrscr();
printf(\n enter UR option:);
scanf(%d,&option);
switch(option)
{
case 1:{push();break;}

case 2:{pop();break;}
case 3:{display();break;}
case 4:{printf(\n\tUR ON EXIT);break;}
default:{printf{\n\t enter only 1,2,3,4);
}
getch();
}
while(option!=4);
}
void push()
{
if(top>=n-1)
{
printf(\n\t\tSTACK is over flow);
getch();
}
else
{

printf(enter a value to be pushed:);


scanf(%d,&x);
top++;
stack[top]=x;
//printf(\n\t\t%d is pushed\n\n\n,x);
//display();
}
}
void pop()
{
if(top<=-1)
{
printf(\n\t\tstack is under flow);
}
else
{
printf(\n\t\t the popped elements is
%d,stack[top]);

top--;
//display();
}
}
void display()
{
if(top>=0)
{
printf(\n the elements in STACK);
printf(\n~~~~~~~~~~~~~~~~~~~);
for(i=top;i>=0;;i--)
printf(\n\n%d,stack[i]);
printf(\n\n\n\n press any key to continue);
}
else
{
printf(\n the STACK is empty);
}

}
Sample output:
Enter the size of STACK[MAX=100]: 3
STACK OPERATION
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter ur option: 1
Enter a value to be pushed:22
Enter ur option: 1
Enter a value to be pushed:33
Enter ur option: 1
Enter a value to be pushed:44
Enter ur option: 1
STACK is over flow
Enter ur option:3
The elements in STACK

44
33
22
Press any key to continue
Enter ur option: 2
The popped elements is 44
Enter ur option: 2
The popped elements is 33
Enter ur option: 2
The popped elements is 22
Enter ur option: 2
Stack is under flow
Enter ur option: 3
The STACK is empty
Enter ur option: 4
UR ON EXIT

Result:
Thus the program was successfully
executed and the output was verified.

3.IMPLEMENTATION OF QUEUE USING


ARRAY
Aim
To write a C program to perform the
implementation of queue using array
Algorithm
Step 1: start the program
Step 2: initialize the array variable a() for
storing elements and declare the required variable
Step 3: Define a function to insert , delete
display the dataitems for data
Step 4: For insert() function
*get the new elements
*create the rear is greater than array
size display queue isoverflow.
Else

Insert the new element & increment the top


pointer
Step 5: For delete() function
If the queue is empty than display queue is
over flow
Else
Decrement the top pointer
Step 6: For display() function
Apply the loop for to display queue
elements.
Program
//queue using array
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int q[n],ch=1,front=0,rear=0,i,j=1,x=n;

clrscr();
printf(\n\t\tQueue using array);
printf(\n1.Insertion);
printf(\n2.Deletion);
printf(\n3.Display);
printf(\n4.Exit);
while(ch)
{
printf(\n Enter UR choice);
scanf(%d,&ch);
switch(ch)
{
case 1:
if(rear= =x)
printf(\n Queue full);
else
{
printf(\n enter no%d:,j++);

scanf(%d,&q[rear++]);
}
break;
case 2:
if(front= =rear)
{
printf(\n Queue is empty);
}
else
{
printf(\ n Deleted elements is %d,q[front++]);
x++;
}
break;
case 3:
printf(\n Queue elements);
if(front==rear)
printf(\n queue is empty);

else
{
for(i=front;i<rear;i++)
{
printf(%d,q[i]);
printf(\t);
}
break;
case 4:
exit();
default:
exit();
}
}
}
getch();
}

Sample output
Queue using array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter ur choice 1
Enter no1: 22
Enter ur choice 1
Enter no1: 33
Enter ur choice 1
Enter no1: 11
Enter ur choice 3
Queue elements

22

Enter ur choice 2
Deleted elements is 11
Enter ur choice 2
Queue is empty

33

11

Enter ur choice 3
Queue elements
Queue is empty
Enter ur choice 4
Ur on exit..

Result
Thus the program was successfully
executed and the output was verified

4. CONVERSION OF INFIX TO POSTFIX


EXPRESSION
Aim
To convert given infix expression to its postfix
form
Algorithm
Step 1: get the infix expression in a character
array
Step 2: scan the expression character wise from
left to right 3
Step 3: if the character is an operand add it to the
character array. If the character is an operator,
push it into stack .while pushing check the priority
of the current operator (o/p) inside the stack
according to the assumed priority given below.

Operator
X
*/
+-

priority
0
2
1

Step 4: if the I/P priority is less than o/p priority,


pop up the operator from the stack and add it to the
character array until,an operator having less.
Step 5: priority then the I/P priority is reached and
push the current operator into the stack
Step 6: if the character is an c,push it into the
stack if it is a ) pop up the character from the
stack upto opening paranthesis and it to the
character array.
Step 7: if the stack is not empty after the scanning
is over, pop up the remaining operators from the
stack till pop=0 is reached and add it to the
character array.
Step 8: print the character array which are now
contains the postfix form of the given infix
expression.

Program
/*infix to postfix*/

#include<stdio.h>
#include<conio.h>
#include<type.h>
int isp(char);
int icp(char);
void convert();
char str[50];
void main()
{
clrscr();
printf(\n\t\tINFIX
CONVERSION);

TO

POSTFIX

printf(\n Enter the infix expression);


Printf(\n Enter the infix expression add # as last
character);
scanf(%s,&str);
convert();
getch();

}
int isp(char c)
{
int pr;
switch(c)
{
case&:
pr=-1;
break;
case):
pr=0;
break;
case*:
pr=3;
break;
case/:
pr=2;
break;

case+:
pr=1;
break;
case-:
pr=1;
break;
case(:
pr=0;
break;
}
return(pr);
}
int icp(char c)
{
int pr;
switch(c)
{
case ):

pr=0;
break;
case ^:
pr=3;
break;
case *:
pr=2;
break;
case /:
pr=2;
break;
case +:
pr=1;
break;
case -:
pr=1;
break;
case (:

pr=4;
break;
}
return(pr);
}
void convert();
{
int i=0,top=0;
char item,st[50];
st[0]=&;
printf(\n The postfix expression:);
while(str[i]!=#)
{
item=str[i];
if(isalpha(item))
printf(%c,item);
else
{

if(item= =));
{
while(st[top]!=();
{
printf(%c,st[top]);
top--;
}
top--;
}
else
{
while(isp(st[top])>=icp(item))
{
pritnf(%c,st[top]);
top--;
}
top++;
st[top]=item;

}
}
i++;
}
while(top>=1)
{
printf(%c,st[top]);
top--;
}
}
Sample output
INFIX TO POSTFIX CONVERSION
Enter the infix expression
Enter the infix expression add # at last character
(a+b)*c+(d-a)#
The postfix expression: ab+c*da-+

Result
Thus the program was successfully executed and
the output was verified.

5. SINGLE LINKED LIST


Aim
To create a c program to perform create,
insert, and delete operations in single linked list.
Algorithm
Step 1: define a structure to represent the node of
the linked list. It contains a data field and apointer
field which points to the next node.
Step 2: declare a struct to get the data item and
get the position of the node to be added or selected
in the linked list.
Step 3: define a function to create, insert, delete
and print the data items of the linked list.
*for create() function
*create a new node to get the value
*get the value and assign the value
into node
*create the another node and get the
value link the previous node

*repeat the process until the value


getting becomes zero.
Step 4: for insert() function
*get the data item and the position of
the new node to be inserted in the linked list.
*travel the list to locate
predecessor and successor nodes.

the

Step 5: for delete() function


*get the position of the node to be
deleted
*travel the list to locate the
predecessor and successor nodes of the deleting
node
*adjust the link fields of deleting node
and its predecessor and successor.
Step 6: for disp() function
*traverse the list and print the values
as data item in each node until the pointer node
becomes null(i.e., till the end of the list).
Step 7: Stop the program.

Program
/*SINGLE LINKED LIST*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int info;
struct stack *next;
}
*p,*root,*newn;
int ch=1,val;
void main()
{
void create();
void insert();
void del();
void disp();

clrscr();
printf(\n\t\tLINEAR LINKED LIST );
printf(\n1.create\n2.insert\n3.delete\n4.display);
while(ch)
{
printf(\n Enter ur choice :);
scanf(%d,&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;

case 4:
disp();
break;
default:
exit(0);
}
}
getch();
}
void create()
{
newn=malloc(sizeof(struct stack));
printf(\nenter val:);
scanf(%d,&val);
newn->info=val;
newn->next=null;
root=newn;
p=root;

printf(\nEnter val:);
scanf(%d,&val);
while(val!=0)
{
newn=malloc(sizeof(struct stack));
newn->info=val;
p=root;
if(val<=p->info)
{
newn->next=p;
root=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
else
{
while((p->next!=null)&&(p->next->info<=val))
{

p=p->next;
}
newn->next=p->next;
p->next=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
}
}
void insert()
{
int val;
newn=malloc(sizeof(struct stack));
printf(\nEnter val:);
scanf(%d,&val);
newn->info=val;
p=root;
if(val<=p->info)

{
newn->next=p;
root=newn;
}
else
{
while((p->next!=NULL)&&(p->next->info<=val))
{
p=p->next;
}
newn->next=p->next;
p->next=newn;
}
}
void del()
{
struct stack*f;
int val;

printf(\nEnter val:);
scanf(%d,&val);
p=root;
if(val==root->info)
{
root=root->next;
free(p);
}
else
{
while(p->next!=NULL)
{
if(p->next!=NULL)
{
f=p->next;
p->next=f->next;
free(f);
if(p->next= =NULL)

{
p=root;
}
break;
}
p=p->next;
}
if(p->next==NULL)
{
printf(\n Value not found);
}
}
}
void disp()
{
p=root;
if(root!=NULL)
{

printf(\nThe elements are:);


printf(top<-);
for(p=root;p!=NULL;p=p->next)
{
printf(%d,p->info);
printf(<-);
}
printf(NULL);
}
else
printf(\nList is empty);
}
Sample output
LINEAR LINKED LIST
1.create
2.insert
3.delete

4.display
Enter ur choice: 1
Enter val: 22
Enter val: 33
Enter val: 44
Enter val: 0
Enter ur choice: 4
The elements are: top <- 22 <- 33 <- 44 <- NULL
Enter ur choice:2
Enter val:36
Enter ur choice: 4
The elements are: top <- 22 <- 33 <- 36 <- 44 <NULL
Enter ur choice:3
Enter val:33
Enter ur choice:4
The elements are: top <- 22 <- 36 <- 44 <- NULL
Enter ur choice:5

Result
Thus the program was successfully executed
and the output was verified

6. DOUBLY LINKED LIST


Aim
Write a C program to perform create,insert
and delete operations in doubly linked list.
Algorithm
Step 1: define a structure to represent the node of
the linked list. It contains a data field and apointer
field which points to the next node.
Step 2: declare a struct to get the data item and
get the position of the node to be added or selected
in the linked list.
Step 3: define a function to create, insert, delete
and print the data items of the linked list.
Step 4: For create() function
*create a new node to get the value
*get the value and assign the value
into node
*update the left and right pointer

*create the another node and get the


value link the previous node and get the value and
update the left and right pointer.
*repeat the process until the value
getting becomes zero.
Step 5: for insert() function
*get the data item and the position of
the new node to be inserted in the linked list.
*traverse the list to locate
predecessor and successor nodes.

the

*adjust the link fields of a new node


and its predecessor and successor node.
Step 6: for delete() function
*get the position of the node to be
deleted
*traverse the list to locate the
predecessor and successor nodes of the deleting
nodes.
*adjust the link fields of deleting node
and its predecessor and successor.

Step 7: for disp() function


*traverse the list and print the values
as data item in each node until the pointer node
becomes null(i.e., till the end of the list).
Step 8: Stop the program.
Program
/*DOUBLY LINKED LIST*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>
struct stack
{
struct stack *left;
int info;
struct stack *right;
}

*p,*root,*newn;
int ch,val;
void main()
{
void create();
void insert();
void del();
void disp();
clrscr();
printf(\n\t\tDOUBLY LINKED LIST );
printf(\n1.create\n2.insert\n3.delete\n4.display);
do
{
printf(\n Enter ur choice :);
scanf(%d,&ch);
switch(ch)
{
case 1:

create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
disp();
break;
default:
exit(0);
}
}
while(ch<=4);
getch();
}

void create()
{
newn=malloc(sizeof(struct stack));
printf(\nEnter val:);
scanf(%d,&val);
newn->info=val;
newn->right=NULL;
root=newn;
p=root;
printf(\nEnter val:);
scanf(%d,&val);
while(val!=0)
{
newn=malloc(sizeof(struct stack));
newn->info=val;
p=root;
if(val<=p->info)
{

newn->right=root;
root->left=newn;
newn->left=null;
root=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
else
{
while((p->right!=NULL)&&(p->right>info<=val))
{
p=p->right;
}
newn->right=p->right;
newn->left=p;
p->right->left=newn;
p->right=newn;

printf(\nEnter val:);
scanf(%d,&val);
}
}
}
void insert()
{
int val;
newn=malloc(sizeof(struct stack));
printf(\nenter val:);
scanf(%d,&val);
newn->info=val;
p=root;
if(val<=p->info)
{
newn->right=p;
newn->left=NULL;
p->left=newn;

root=newn;
}
else
{
while((p->right!=NULL)&&(p->right>info<=val))
{
p=p->right;
}
newn->right=p->right;
newn->left=p;
p->right->left=newn;
p->right=newn;
}
}
void del()
{
struct stack*f;

int val;
printf(\nenter val:);
scanf(%d,&val);
p=root;
if(val==root->info)
{
root=root->right;
free(p);
}
else
{
while(p->right!=NULL)
{
if(p->right->info= =val)
{
f=p->right;
p->right=f->right;
f->right->left=f->left;

free(f);
if(p->right= =NULL)
{
p=root;
}
break;
}
p=p->right;
}
if(p->right==NULL)
{
printf(\n value not found);
}
}
}
void disp()
{
p=root;

if(root!=NULL)
{
printf(\nThe elements are:);
printf(TOP<-);
for(p=root;p!=NULL;p=p->right)
{
printf(%d,p->info);
printf(->);
printf(<-);
}
printf(NULL);
}
else
printf(\nList is empty);
}
Sample output
DOUBLY LINKED LIST
1.create

2.insert
3.delete
4.display
Enter ur choice: 1
Enter val: 22
Enter val: 44
Enter val: 33
Enter val: 0
Enter ur choice: 4
The elements are: top <- 22 -><- 33-> <- 44-> <NULL
Enter ur choice:2
Enter val:11
Enter ur choice: 4
The elements are: top <-11-><- 22-> <- 33-> <44-> <- NULL
Enter ur choice:3
Enter val:33

Enter ur choice:4
The elements are: top <- 11-> <- 22-> <- 44 -><NULL
Enter ur choice:5

Result
Thus the program was successfully executed
and the output was verified.

7. IMPLEMENTATION OF STACK USING


LINKED LIST
Aim
Write a C program for stack using linked list.
Algorithm
Step 1: start the program
Step 2: Initialize the structure stack
Step 3: Initialize the global variable
Step 4: The operation for stack are:
(i). push
(ii).pop
(iii).display
(iv).exit
Step 5: If choice=1
Push()function
Create a newnode using structure
Enter the value to be inserted to the stack

Assign newnode->next=top and


top=newnode.
Step 6: if choice=2
Pop() function
If top is equal to null, then the stack is
underflow
Else assign p=top and top=p->next
Print the p value
Free the value of p.
Step 7: if choice=3
Display() function
If top is equal to null then stack
underflow.
Else using loop from top to null print the
values.
Step 8: if choice =4
Call the exit function.
Step 9: Stop the program.

Program
//STACK USING LINKED LIST
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int data;
struct stack *next;
}
*p,*top,*newn;
int ch=1;
void main()
{
void push();
void pop();
void display();
clrscr();

printf(\n\t\t STACK USING LINKED LIST);


printf(\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~);
printf(\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\
n);
while(ch)
{
printf(enter ur option:);
scanf(%d,&ch);
switch(ch)
{
case 1: push();break;
case 2: pop();break;
case 3: display(); break;
case 4: exit(0);
default:exit(0);
}
}
getch();

}
void push()
{
printf(Enter ur data);
newn=malloc(sizeof(struct stack));
scanf(%d,&newn->data);
newn->next=top;
top=newn;
}
void pop()
{
if(top= =NULL)
{
printf(stack is under flow);
}
else
{
p=top;

top=p->next;
printf(the popped elements is %d,p->data);
free(p);
}
printf(\n);
}
void display()
{
if(top= =NULL)
{
printf(stack is empty\n);
}
else
{
printf(the stack elements are:);
for(p=top;p!=NULL;p=p->next)
printf(\t%d,p->data);
printf(\n);

}
}
Sample output
STACK USING LINKED LIST
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter ur option: 1
Enter ur data 55
Enter ur option: 1
Enter ur data 35
Enter ur option: 1
Enter ur data 25
Enter ur option: 1
Enter ur data 45
Enter ur option: 3

The stack elements are: 45 25 35 55


Enter ur option: 2
The popped elements is 45
Enter ur option: 2
The popped elements is 25
Enter ur option: 2
The popped elements is 35
Enter ur option: 2
The popped elements is 55
Enter ur option: 2
Stack is underflow
Enter ur option: 3
Stack is empty
Enter ur option: 4

Result
Thus the program was successfully executed
and the output was verified.

8. IMPLEMENTATION OF QUEUE USING


LINKED LIST
Aim
Write a C program for queue using linked list.
Algorithm
Step 1: start the program
Step 2: Initialize the structure queue
Step 3: Initialize the global variable
Step 4: The operations for queue are:
(i). insert
(ii).delete
(iii).display
(iv).exit
Step 5: If choice=1
For Insert()function
Get the new node and insert the data.
Set the new node link equal to null

If the rear is equal to null set front is equal


to node and rear to equal node.
Else
Set rear(next) is equal to node and rear
equal to the node.
Step 6: For delete() function
If front is equal to null, display queue is
empty
Else
Decrement the value from front node and
set front(next)=front.
Step 7: Apply the loop condition to display the
queue content
Step 8: Stop the program
Program
//QUEUE USING LINKED LIST
#include<stdio.h>
#include<conio.h>

#include<process.h>
#include<malloc.h>
#define null 0
struct queue
{
int data;
struct queue *next;
}
*p,*front,*rear,*newn;
int ch=1,x;
void main()
{
void insert();
void delete();
void display();
clrscr();
printf(\n\t\t QUEUE USING LINKED LIST);

printf(\n1.Insert\n2.Delete\n3.Display\n4.Exit\n)
;
while(ch)
{
printf(Enter ur choice:);
scanf(%d,&ch);
switch(ch)
{
case 1: insert();break;
case 2: delete();break;
case 3: display(); break;
case 4: exit(0);
default:exit(0);
}
}
getch();
}
void insert()

{
printf(Enter ur data);
newn=malloc(sizeof(struct queue));
scanf(%d,&newn->data);
newn->next=null;
if(rear= =null)
{
front=newn;
rear=newn;
}
else
{
rear->next=newn;
rear=newn;
}
}
void delete()
{

if(front= =NULL)
{
printf(\nQueue is empty\n);
rear=null;
}
else
{
p=front;
x=p->data;
front=p->next;
printf(\n Deleted value is %d\n,x);
free(p);
}
}
void display()
{
if(front= =null)
{

printf(\nqueue is empty\n);
}
else
{
printf(The Queue elements are:);
for(p=front;p!=null;p=p->next)
printf(\t%d,p->data);
printf(\n);
}
}
Sample output
QUEUE USING LINKED LIST
1.Insert
2.Delete
3.Display
4.Exit
Enter ur option: 1

Enter ur data 55
Enter ur option: 1
Enter ur data 33
Enter ur option: 1
Enter ur data 44
Enter ur option: 1
Enter ur data 22
Enter ur option: 3
The queue elements are: 55 33 44 22
Enter ur option: 2
Deleted value is 55
Enter ur option: 2
Deleted value is 33
Enter ur option: 2
Deleted value is 44
Enter ur option: 2
Deleted value is 22
Enter ur option: 2

Queue is empty
Enter ur option: 3
Queue is empty
Enter ur option: 4

Result
Thus the program was successfully executed
and the output was verified.

9. BINARY TREE TRAVERSAL


Aim
To find the inorder, preorder and postorder
traversal of a binary tree.
Algorithm
Step 1: Start the program
Step 2: Initialize the left child and right child to
form a binary tree continue the process until the
left child and right child doesnt exit.
Step 3: get the root node and get its left child and
right child to form a binary tree continue the
process until the left child and right child doesnt
exit.
Step 4: For inorder traversal first traverse the left
child then root node and finally right child.
Step 5: For preorder traversal, first traverse the
root node , then left child and finally child.
Step 6: For postorder traversal, first traverse the
left child, right child and finally the root node
Step 7: Stop the program.

Program
/*Binary Tree Traversal*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
struct node
{
int data;
struct node *right,*left;
}
*root,*p,*q;
struct node *make(int y)
{
struct node *newnode;
newnode=(struct
node));
newnode->data=y;

node

*)malloc(sizeof(struct

newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r, int x)
{
if(r->left!=NULL)
printf(\n Invalid !);
else
r->left=make(x);
}
void right(struct node *r,int x)
{
if(r->right!=NULL)
printf(\n Invalid !);
else
r->right=make(x);
}
void inorder(struct node *r)

{
if(r!=NULL)
{
inorder(r->left);
printf(\t %d,r->data);
inorder(r->right);
}
}
void preorder(struct node *r)
{
if(r!=NULL)
{
printf(\t %d,r->data);
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node *r)

{
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
printf(\t %d,r->data);
}
}
void display(struct node *r, int c, int r1, int w)
{
if(r!=NULL)
{
if(r)
{
gotoxy(c,r1);
printf(%d,r->data);
if(r->left)
display(r->left,c-w,r1+2,w/2);

if(r->right)
display(r->right,c+w,r1+2,w/2);
}
}
}
void main()
{
int no;
int choice;
clrscr();
printf(\n enter the root:);
scanf(%d,&no);
root=make(no);
p=root;
while(1)
{
printf(\n Enter another number:);
scanf(%d,&no);

if(no= =-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf(\n left branch of %d is %d,p->data,no);
left(p,no);
}
else

{
right(p,no);
printf(\n right branch of %d is %d,p->data,no);
}
}
while(1)
{
printf(\n 1.Inorder Traversal
\n 2.Preorder
Traversal\n 3.Postorder Traversal\n 4.Display\n
5.Exit);
printf(\n Enter choice:);
scanf(%d,&choice);
switch(choice)
{
case 1: inorder(root);
break;
case 2: preorder(root);
break;

case 3: postorder(root);
break;
case 4:
{
clrscr();
display(root,40,5,16);
getch();
break;
}
case 5: exit(0);
default:printf(Error ! Invalid Choice);
break;
}
getch();
}
}

Sample output
BINARY TREE TRAVERSAL
Enter the root: 5
Enter another number: 3
Left branch of 5 is 3
Enter another number: 6
Right branch of 5 is 6
Enter another number :2
Left branch of 3 is 2
Enter another number: 8
Right branch of 6 is 8
Enter another number: -1
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice: 4

5
3

1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:1
2

1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:2
5

1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:3
2

1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:5

Result
Thus the program was successfully executed
and the output was verified.

10. BINARY SEARCH TREE


Aim
Write a C program to perform Binary search
tree.
Algorithm
Step 1: Start the program
Step 2: First check the given searching value to
the root node of the tree.
(i).if the root node is equal to the search
node then displayelement found.
Step 3: compare root node and search node.
(i).if searchnode is greater than root node.
(ii).check right-> node
Else
(iii).check left->node
Step 4: Repeat the step 3 process when leaf node
is reached
If it found display the element is found
Else

Display the element not found.


Step 5: Stop the program.
Program
/*Binary Search Tree*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
struct node
{
int data;
struct node *right,*left;
}
*root,*p,*q;
struct node *make(int y)
{
struct node *newnode;

newnode=(struct node *)malloc(sizeof(struct


node));
newnode->data=y;
newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r,int x)
{
if(r->left!=NULL)
printf(\n Invalid !);
else
r->left=make(x);
}
void right(struct node *r,int x)
{
if(r->right!=NULL)
printf(\n Invalid !);
else

r->right=make(x);
}
void search(struct node *r,int t)
{
if(r!=NULL)
{
if(r->data= =t)
printf(Element found);
else if(r->data>t&&(r->left | | r->right!=NULL))
{
search(r->left,t);
}
else if(r->data<t&&(r->left | | r->right!=NULL))
search(r->right,t);
else
printf(Element not found);
}
}

void display(struct node *r, int c,int r1,int w)


{
if(r!=NULL)
{
if(r)
{
gotoxy(c,r1);
printf(%d,r->data);
if(r->left)
display(r->left,c-w,r1+2,w/2);
if(r->right)
display(r->right,c+w,r1+2,w/2);
}
}
}
void main()
{
int no;

int choice;
clrscr();
printf(\n enter the root:);
scanf(%d,& no);
root=make(no);
p=root;
while(1)
{
printf(\n Enter another number:);
scanf(%d,&no);
if(no= =-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)

q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf(\n Left branch of %d is %d,p->data,no);
left(p,no);
}
else
{
right(p,no);
printf(\n Right branch of %d is %d,p->data,no);
}
}
While(1)
{
printf(\n\n\n\n 1.search\n 2.display \n 3.exit);

printf(\n Enter choice:);


scanf(%d,&choice);
switch(choice)
{
case 1:
{
int t;
printf(Enter the element to be searched);
scanf(%d,&t);
search(root,t);
break;
}
case 2:
{
clrscr();
display(root,40,5,16);
getch();
break;

}
case 3: exit(0);
default:printf(error ! invalid choice);
break;
}
getch();
}
}
Sample output:
BINARY SEARCH TREE
Enter the root: 5
Enter another number: 4
Left branch of 5 is 4
Enter another number: 6
Right branch of 5 is 6
Enter another number: 3
Left branch of 4 is 3
Enter another number: -1

1.search
2.display
3.exit
Enter choice: 2
5
4
3
1.search
2.display
3.exit
Enter choice: 1
Enter the element to be searched 3
Element found
1.search
2.display
3.exit
Enter choice: 1

Enter the element to be searched 6


Element found
1.search
2.display
3.exit
Enter choice: 1
Enter the element to be searched 0
Element not found
1.search
2.display
3.exit
Enter choice: 3

Result
Thus the program was successfully executed
and the output was verified.

11(a).BUBBLE SORT
Aim
Write a C program to start the given set of
numbers using bubble sort technique.
Algorithm
Step 1: start the program.
Step 2: get the inputs for the array.
Step 3: bubble sort the smallest element to place
from sorting of the array by using a nested for loop
and swap the element in position i,j-1. If they are
out of orders.
Step 4: print the values of sorted elements.
Step 5: Stop the program.
Program
//Bubble sort
#include<stdio.h>
#include<conio.h>
void bubble (int a [],int n)

{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}//end for 1.
}//end function.
void main()
{
int a[100],n,i;

clrscr();
printf(\n\n Enter integer value for total no.s of
elements to be sorted:);
scanf(%d,&n);
for(i=0;i<=n-1;i++)
{
printf(\n\n Enter integer value for element
no.%d:,i+1);
scanf(%d,&a[i]);
}
bubble(a,n);
printf(\n\n Finally sorted array is:);
for(i=0;i<=n-1;i++)
printf(%3d,a[i]);
getch();
}// end program.

Sample output
BUBBLE SORT
Enter integer value for total no.s of elements to be
sorted: 6
Enter integer value for element no.1: 88
Enter integer value for element no.2: -8
Enter integer value for element no.3: -66
Enter integer value for element no.4: 10
Enter integer value for element no.5: 77
Enter integer value for element no.6: 20
Finally sorted array is: -66 -8 10 20 77 88

Result
Thus the program was successfully executed
and the output was verified.

11(b).MERGE SORT
Aim
To sort the given set of numbers using merge
sort technique.
Algorithm
Step 1: start the program.
Step 2: get the input values of 2 arrays as a [m]
and b[n] and n as its array size.
Step 3: sort the contents of array a[] and b[].
Step 4: in merge()
(i). it contains two sorted arrays a[m]
and b[n] into one.
(ii). Scan both arrays from left to right.
(iii).compare a[1] with b[1] if a[1] put
a[1] as a[1] as compare a[2] with b[1]
and so on. Else put b[1] as c[1] and
compare a[1] with b[2] and so on.
(iv).repeat step until any one array
becomes empty.

(v).check a array becomes empty, store


all the remaining array elements in the
same order in the C array.
(vi)Besides are store all the remaining a
array elements in the same order in the
C array.
Step 5: Stop the program.
Program
//merge sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],b[100],c[100],i,j,n;
clrscr();
printf(Enter the array size\t);
scanf(%d,&n);

printf(\n Enter the %d values \n,n);


for(i=0;i<n;i++)
scanf(%d,&a[i]);
printf(\n Enter the another %d values \n,n);
for(i=0;i<n;i++)
scanf(%d,&b[i]);
msort(a,n);
msort(b,n);
merge(a,b,n,c);
msort(c,(n*2));
printf(\nThe sorted values are:);
for(i=0;i<(n+n);i++)
printf(\n%d,c[i]);
getch();
}
msort(int a[],int n)
{
int i,j,t;

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
return 0;
}
merge(int a[],int b[],int n,int c[])
{
int i,j,t,k=0;
for(i=0;i<n;i++)

{
{
if(a[i]>b[i])
{
c[k]=b[i];
k++;
c[k]=a[i];
k++;
}
else
{
c[k]=a[i];
k++;
c[k]=b[i];
k++;
}
}
}

return c;
}
Sample output
MERGE SORT
Enter the array size 5
Enter the 5 values
55 11 22 66 44
Enter the another 5 values
21 07 59 88 99
The sorted values are:
7
11
21
22
44
55
59

66
88
99

Result
Thus the program was successfully executed
and the output was verified.

11(c). QUICK SORT


Aim
Write a C program to sort the given set of
number using quicksort technique.
Algorithm
Step 1: Start the program.
Step 2: Get the inputs for the array.
Step 3: For a given array A[p:n],if R>n; then
partition of the given array into two non-empty sub
arrays
(i).then the pivot element is q then q is
equal to (p+n)/2. p is a starting point (size of the
array) and n is end point (size of the array).
(ii).A[p:q] and A[q+1:n],such that every
key in A[p:q] is less than or equal to every key in
A[q+1:n].
Step 4: The two sub arrays are sorted by
recursive calls to quick sort
Step 5:print the values to be sorted elements
Step 6: Stop the program.

Program
//Quick sort
#include<stdio.h>
#include<stdlib.h>
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int choose_pivot(int i,int j)
{
return((i+j)/2);
}
void quicksort(int list[],int m,int n)
{
int key,i,j,k;

if(m<n)
{
k=choose_pivot(m,n);
swap(&list[m],&list[k]);
key=list[m];
i=m+1;
j=n;
while(i<=j)
{
while((i<=n)&&(list[i]<=key))
i++;
while((j>=m)&&(list[j]>key))
j--;
if(i<j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
//recursively sort the lesser list

quick sort(list,m,j-1);
quick sort(list,j+1,n);
}
}
void printlist(int list[],int n)
{
int i;
for(i=0;i<n;i++)
printf(%d\t,list[i]);
}
void main()
{
int list[100],n;
int i=0;
clrscr();
printf(enter the maximum number of elements to
be sorted);
scanf(%d,&n);

for(i=0;i<n;i++)
{
scanf(%d,&list[i]);
}
printf(The list before sorting is:\n);
printlist(list,n);
quicksort(list,0,n-1);
printf(\n The list after sorting using quicksort
algorithm:\n);
printlist(list,n);
getch();
}
Sample output
QUICK SORT
Enter the maximum number of elements to be
sorted 5

50
11
88
77
22
The list before sorting is:
50

11

88 77 22

The list after sorting using quicksort algorithm:


11

22

50 77

88

Result
Thus the program was successfully executed
and the output was verified.

11(d). RADIX SORT


Aim:
Write a C program to implement Radix
Sort Using Data Structures.
Algorithm:
i) Start the program.
ii) Get the values.
iii) Values of the Actual Digits in the
positional representations of the numbers being
sorted.
iv) Start at the most-significant digit and
advance through the least-significant digits as long
as the corresponding digits in the two numbers
match.
v) Using decimal base , the numbers can be
partitioned into ten groups based on their most
significant digit.

vi) Each number in the order in which it


appears in the file and place it into one of ten
queues.
vii) Restore each queue to the original file
starting with the queue of numbers with a 0 digit
and ending with the queue of numbers with a 9
digit.
viii) Stop the program.
Program
#include<stdio.h>
#include<conio.h>
radix_sort(int array[], int n);
void main()
{
int array[100],n,i;
clrscr();
printf("Enter the number of elements to be sorted:
");
scanf("%d",&n);
printf("\n Enter the elements to be sorted: \n");
for(i =0;i<n;i++ )
{

printf("\tArray[%d] = ",i);
scanf("%d",&array[i]);
}
printf("\n Array Before Radix Sort:"); //Array
Before Radix Sort
for(i =0;i<n;i++)
{
printf("%8d", array[i]);
}
printf("\n");
radix_sort(array,n);
printf("\n Array After Radix Sort: "); //Array
After Radix Sort
for(i =0;i<n;i++)
{
printf("%8d", array[i]);
}
printf("\n");
getch();
}
radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];

int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0; i<n; i++)
{
if(arr[i] > large)
{
large = arr[i];
}
while(large > 0)
{
num++;
large = large/10;
}
for(passes=0; passes<num; passes++)
{
for(k=0;k<10;k++)
{
buck[k] = 0;
}
for(i=0;i<n;i++)
{

l = ((arr[i]/div)%10);
bucket[l][buck[l]++] = arr[i];
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<buck[k];j++)
{
arr[i++] = bucket[k][j];
}
}
div*=10;
}
}
}
Sample output
Enter the number of elements to be sorted: 7
Enter the elements to be sorted:
Array[0]=777
Array[1]=269
Array[2]=158

Array[3]=341
Array[4]=265
Array[5]=989
Array[6]=506
Array before radix sort: 777 269 158 341 265 989
506
Array after radix sort: 158 265 269 341 506 777
989

Result
Thus the program was successfully executed
and the output was verified.

12(a).Linear Search
Aim
To develop a C program to implement linear
search.
Algorithm
Step 1: Get the array elements.
Step 2: Get the element to search.
Step 3: Apply for loop differentiate the array
element and apply the below condition in for loop.
(i).check the array element is equal to
search element then display element found
Else
Display element not found.
Step 4: Stop the program.
Program
//Linear search
#include<stdio.h>

#include<conio.h>
void main()
{
int i,n,item,a[20];
clrscr();
printf(\n Enter no of elements=);
scanf(%d,&n);
printf(\n Enter %d elements=,n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
printf(\n Enter the element to be search=);
scanf(%d,&item);
for(i=0;i<n;i++)
{
if(a[i]= =item)
{

printf(\n\t%d is present at position %d,a[i],i+1);


break;
}
}
if(i= =n)
printf(\n Element is not present);
getch();
}
Sample output
LINEAR SEARCH
Enter no of elements =5
Enter 5 elements = 55 44 66 33 22
Enter the element to be search= 33
33 is present at position 4
Enter no of elements = 5
Enter 5 elements = 55 44 66 33 22
Enter the element to be search = 11
Element is not present

Result
Thus the program was successfully executed and
the output was verified.

12(b).BINARY SEARCH
Aim
To develop a C program to implement binary
search.
Algorithm
Step 1: Get the array elements.
Step 2: Get the elements to search.
Step 3: Sort the array element using any sorting
techniques.
Step 4: Get the mid value by (low+high)/2.
Step 5: Compare the search element with that mid
elements.
Step 6: Check the given value is less than the mid
element, then find mid=(low+mid-1)/2 else, find
mid=(mid+1+high/2).
Step 7: Repeat the steps 4 to 6 until the element is
found or high<low.
Step 8: Check the element is not present,display
,element not found,else displayelement found.

Step 9: Stop the program.


Program
//Binary search
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,n,item,a[100],b;
clrscr();
printf(\n Enter no of elements=);
scanf(%d,&n);
printf(\n Enter %d elements=,n);
for(i=1;i<=n;i++)
{
scanf(%d,&a[i]);
}
sort(a,n);

printf(\n The sorted values are :\n);


for(i=0;i<=n;i++)
{
printf(\n%d,a[i]);
}
printf(\n Enter the element to be search=);
scanf(%d,&item);
b=(n/2);
if(a[b]= =item)
{
printf(\n\tElement found);
}
else if(a[b]>item)
{
for(i=1;i<=(b+1);i++)
{
if(a[i]= =item&&i!=(b+1))
{

printf(\n\tElement found);break;
}
if(i= =(b+1))
printf(\nElement not found);
}
}
else if(a[b]<item)
{
for(i=b;i<=(n+1);i++)
{
if(a[i]= =item&&(i!=(n+1)))
{
printf(\n\tElement found);break;
}
If(i= =(n+1))
printf(\nElement not found);
}
}

else
printf(\n\tElement not found);
getch();
}
sort(int a[],int n)
{
int i,j,t;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}

}
return 0;
}
Sample output
BINARY SEARCH
Enter no of elements = 5
Enter 5 elements = 22 11 55 44 33
The sorted values are:
11
22
33
44
55
Enter the element to be search= 33
Element found
Enter no of elements = 5
Enter 5 elements = 22 11 55 44 33

The sorted values are: 24


Element not found.

Result
Thus the program was successfully executed
and the output was verified.

Potrebbero piacerti anche