You are on page 1of 73

1

IT 2205 DATA STRUCTURES LAB

EX.NO. DESCRIPTION PAGE NO.


1 – A Implement singly linked lists
1–B Implement doubly linked lists
Represent a polynomial as a linked list and write functions
2
for polynomial addition
Implement stack and use it to convert infix to postfix
3
expression
Implement a double-ended queue (dequeue) where
4 insertion and deletion operations are possible at both the
ends.

5
Implement an expression tree. Produce its pre-order, in-
k /
6
order, and post-order traversals
Implement binary search tree
. t
7
8
Implement insertion in AVL trees
Implement priority queue using binary heaps b e
9
t
Implement hashing with open addressing u
10
s e
Implement Prim's algorithm using priority queues to find
MST of an Undirected graph

/ c
BEYOND THE SYLLABUS
1
/
Implement Radix Sort.
:
t p
h t

http://csetube.weebly.com/
2

1-A SINGLY LINKED LIST


AIM:

To write a program to implement singly linked list.

ALGORITHM:

1. Start
2. Creation: Get the number of elements, and create the nodes having structures
DATA  LINK and store the element in Data field, link them together to form a
linked list.
3. Insertion: Get the number to be inserted and create a new node store the value in
DATA field. And insert the node in the required position.
/
4. Deletion: Get the number to be deleted. Search the list from the beginning and
k
locate the node then delete the node.
. t
5. Display: Display all the nodes in the list.
b e
6. Stop.

t u
s e
/ c
: /
t p
h t

http://csetube.weebly.com/
3

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
typedef struct list
{
int no;
struct list *next;
}LIST;
LIST *p,*t,*h,*y,*ptr,*pt;
void create( void );
void insert( void );
void delet( void );
void display ( void );
int j,pos,k=1,count;
void main() k /
{
int n,i = 1,opt; .t
clrscr();
b e
p = NULL;
printf("%d",sizeof(LIST));
t u
printf( "Enter the no of nodes :\n " );
scanf( "%d",&n ); s e
count = n;
while( i <= n) / c
{
: /
create();
i++;
t p
}
h t
printf("\nEnter your option:\n");
printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit\n");
do
{
scanf("%d",&opt);
switch( opt )
{
case 1:
insert();
count++;
break;
case 2:
delet();
count--;
if ( count == 0 )

http://csetube.weebly.com/
4

{
printf("\n List is empty\n");
}
break;
case 3:
printf("List elements are:\n");
display();
break;
}
printf("\nEnter your option \n");
}while( opt != 4 );
getch();
}
void create ( )
{
if( p == NULL )
{
p = ( LIST * ) malloc ( sizeof ( LIST ) ); k /
printf( "Enter the element:\n" );
scanf( "%d",&p->no ); .t
p->next = NULL;
b e
h = p;
}
t u
else
{ s e
t = ( LIST * ) malloc (sizeof( LIST ));
printf( "\nEnter the element" ); / c
scanf( "%d",&t->no );
: /
t->next = NULL;
p->next = t;
t p
p = t;
} h t
}
void insert()
{
t=h;
p = ( LIST * ) malloc ( sizeof(LIST) );
printf("Enter the element to be insrted:\n");
scanf("%d",&p->no);
printf("Enter the position to insert:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = p;
h->next = t;
}

http://csetube.weebly.com/
5

else
{
for(j=1;j<(pos-1);j++)
t = t->next;
p->next = t->next;
t->next = p;
t=p;
}
}
void delet()
{
//t=h;
printf("Enter the position to delete:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = h->next ;
} k /
else
{ .t
t = h;
b e
for(j=1;j<(pos-1);j++)
t = t->next;
t u
pt=t->next->next;
free(t->next); s e
t->next= pt;
} / c
}
: /
void display()
{
t p
t = h;
h t
while( t->next != NULL )
{
printf("\t%d",t->no);
t = t->next;
}
printf( "\t %d\t",t->no );
}

http://csetube.weebly.com/
6

OUTPUT :

Enter the no of nodes : 3


Enter the element:1
Enter the element 2
Enter the element 3
Enter your option:
1.Insert 2.Delete 3.Display 4.Exit
3
List elements are:
1 2 3
Enter your option 1
Enter the element to be insrted:
12
Enter the position to insert: 1
Enter your option 3
List elements are:
12 1 2 3 k /
Enter your option 1
Enter the element to be insrted: .t
13
b e
Enter the position to insert: 3
Enter your option 1
t u
Enter the element to be insrted:
14 s e
Enter the position to insert:6
Enter your option 3 / c
List elements are:
: /
12 1 13 2
t
Enter your option 2 p
3 14

h t
Enter the position to delete:1
Enter your option 3
List elements are:
1 13 2 3 14
Enter your option 2
Enter the position to delete:3
Enter your option 3
List elements are:
1 13 3 14
Enter your option 2
Enter the position to delete:4
Enter your option 3
List elements are:
1 13 3
Enter your option

http://csetube.weebly.com/
7

1-B DOUBLY LINKED LIST


AIM:

To write a program to implement doubly linked list with Insert, Delete and
Display operations.

ALGORITHM:

1. Start
2. Creation: Get the number of elements to create the list. Then create the node
having the structure BLINK  DATA  FLINK and store the elements in Data
field. Link them together to form a doubly linked list.
/
3. Insertion: Get the number to be Inserted, create a new node to store the value.
k
Search the list and insert the node in its right position.
. t
e
4. Deletion: Get the number to be deleted. Search the list from the beginning and try
b
t u
to locate node p with DATA. If found then delete the node.
5. FLINK P’s previous node to P’s Next node. BLINK P’s Next node to P’s

s e
Previous node else display “Data not Found”.

/ c
6. Display: Display all the nodes in the list.
7. Stop.
: /
t p
h t

http://csetube.weebly.com/
8

PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0

typedef struct list


{
int no;
struct list *next;
struct list *pre;
}LIST;

LIST *p,*t,*h;
void create( void );
void insert( void ); k /
void delet( void );
.t
void display ( void );
int j,pos,k=1,count;
b e
void main()
t u
{
int n,i = 1,opt; s e
clrscr();
/ c
p = NULL;

: /
printf( "Enter the no of nodes :\n " );
scanf( "%d",&n );
count = n;
t p
while( i <= n)
{ h t
create();
i++;
}
printf("\nEnter your option:\n");
printf("1.Insert \t 2.Delete \t 3.Display \t 4.Exit\n");
do
{
scanf("%d",&opt);
switch( opt )
{
case 1:
insert();
count++;
break;

http://csetube.weebly.com/
9

case 2:
delet();
count--;
if ( count == 0 )
{
printf("\n List is empty\n");
}
break;
case 3:
printf("List elements are:\n");
display();
break;
}
printf("\nEnter your option \n");
}while( opt != 4 );
getch();
}
k /
void create ( )
.t
{
if( p == NULL )
b e
{
p = ( LIST * ) malloc ( sizeof ( LIST ) );
t u
printf( "Enter the element:\n" );
scanf( "%d",&p->no ); s e
p->next = NULL;
p->pre = NULL; / c
h = p;
: /
}
else
t p
{
h t
t = ( LIST * ) malloc (sizeof( LIST ));
printf( "\nEnter the element" );
scanf( "%d",&t->no );
t->next = NULL;
p->next = t;
t->pre = p;
p = t;
}
}
void insert()
{
t=h;
p = ( LIST * ) malloc ( sizeof(LIST) );
printf("Enter the element to be insrted:\n");
scanf("%d",&p->no);

http://csetube.weebly.com/
10

printf("Enter the position to insert:\n");


scanf( "%d",&pos );
if( pos == 1 )
{
h = p;
h->next = t;
t->pre = h;
h->pre = NULL;
}
else
{
for(j=1;j<(pos-1);j++)
t = t->next;
p->next = t->next;
t->next = p;
p->pre = t;
}
} k /
void delet()
{ .t
printf("Enter the position to delete:\n");
b e
scanf( "%d",&pos );
if( pos == 1 )
t u
{
h = h->next ; s e
h->pre = NULL;
} / c
else
: /
{
t = h;
t p
t = t->next; h t
for(j=1;j<(pos-1);j++)

t->next = t->next->next;
t->next->pre = t;
free( t->next );
}
}
void display()
{
t = h;
while( t->next != NULL )
{printf("%d\n",t->no);
t = t->next;
}
printf( "%d",t->no );

http://csetube.weebly.com/
11

OUTPUT:

Enter the no of nodes: 3


Enter the element3
Enter your option:
1.Insert 2.Delete 3.Display 4.Exit
3
List elements are:
1 2 3
Enter your option 1
Enter the element to be insrted:22
Enter the position to insert:1
Enter your option 3
List elements are:
22 1 2 3 k /
Enter your option 1
Enter the element to be insrted: .t
11
b e
Enter the position to insert:5
Enter your option 3
t u
List elements are:
22 1 2 3 11 s e
Enter your option
2 / c
:
Enter the position to delete:/
1
Enter your option
t p
3
h t
List elements are:
1 2 3 11
Enter your option 2
Enter your option 4

http://csetube.weebly.com/
12

2. REPRESENT A POLYNOMIAL AS A LINKED LIST AND WRITE


FUNCTIONS FOR POLYNOMIAL ADDITION

AIM:

To develop a program to represent a polynomial as a linked list and write


functions for polynomial addition.

ALGORITHM:

1. Start.
2. Create a Linked lists used to represent and manipulate polynomials.
3. A polynomial can be represented as
P(X) = anxne+an-1xn-1e+…+a1x1e +a
Where ai – nonzero coefficients,0<i<n k /
ei – exponent . t
b e
4. Each node in the polynomial is considered as a node .Each node contains three fields
Node Structure
t u
Coefficient Exponent Link
s e
/ c
: /
Coefficient Field - Which holds the coefficient of a term

p
Exponent Field - Which holds the exponent value of that term
t
h t
Link Field - Address of the next term in the polynomial
5. P and Q are two polynomials.P & Q can be represented as linked list.
6. P:5x2+6x+7
7. Q:3x3+4x2+x
8. The resultant polynomial can be represented like this. R:3x3+9x2+7x+7
9. Stop.

http://csetube.weebly.com/
13

PROGRAM

# include <stdio.h>
# include <malloc.h>

struct node
{
float coef;
int expo;
struct node *link;
};
struct node *poly_add(struct node *,struct node *);
struct node *enter(struct node *);
struct node *insert(struct node *,float,int);

main( )
{
struct node *p1_start,*p2_start,*p3_start; k /
p1_start=NULL;
p2_start=NULL; .t
p3_start=NULL;
b e
printf("Polynomial 1 :\n");
p1_start=enter(p1_start);
t u
printf("Polynomial 2 :\n");
p2_start=enter(p2_start); s e
p3_start=poly_add(p1_start,p2_start);
printf("Polynomial 1 is : "); / c
display(p1_start);
: /
display(p2_start);
t p
printf("Polynomial 2 is : ");

h t
printf("Added polynomial is : ");
display(p3_start);
}/*End of main()*/

struct node *enter(struct node *start)


{
int i,n,ex;
float co;
printf("How many terms u want to enter : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coeficient for term %d : ",i);
scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);

http://csetube.weebly.com/
14

start=insert(start,co,ex);
}
return start;
}/*End of enter()*/

struct node *insert(struct node *start,float co,int ex)


{
struct node *ptr,*tmp;
tmp= malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*list empty or exp greater than first one */
if(start==NULL || ex>start->expo)
{
tmp->link=start;
start=tmp;
}
else k /
{
ptr=start; .t
while(ptr->link!=NULL && ptr->link->expo>ex)
b e
ptr=ptr->link;
tmp->link=ptr->link;
t u
ptr->link=tmp;
s e
if(ptr->link==NULL) /*item to be added in the end */
tmp->link=NULL;
} / c
return start;
: /
}/*End of insert()*/
t p
{ h t
struct node *poly_add(struct node *p1,struct node *p2)

struct node *p3_start,*p3,*tmp;


p3_start=NULL;
if(p1==NULL && p2==NULL)
return p3_start;
while(p1!=NULL && p2!=NULL )
{
tmp=malloc(sizeof(struct node));
if(p3_start==NULL)
{
p3_start=tmp;
p3=p3_start;
}
else
{

http://csetube.weebly.com/
15

p3->link=tmp;
p3=p3->link;
}
if(p1->expo > p2->expo)
{
tmp->coef=p1->coef;
tmp->expo=p1->expo;
p1=p1->link;
}
else
if(p2->expo > p1->expo)
{
tmp->coef=p2->coef;
tmp->expo=p2->expo;
p2=p2->link;
}
else
if(p1->expo == p2->expo) k /
{
tmp->coef=p1->coef + p2->coef; .t
tmp->expo=p1->expo;
b e
p1=p1->link;
p2=p2->link;
t u
}
}/*End of while*/ s e
while(p1!=NULL)
{ / c
: /
tmp=malloc(sizeof(struct node));
tmp->coef=p1->coef;
t
tmp->expo=p1->expo; p
{ h t
if (p3_start==NULL) /*poly 2 is empty*/

p3_start=tmp;
p3=p3_start;
}
else
{
p3->link=tmp;
p3=p3->link;
}
p1=p1->link;
}/*End of while */
while(p2!=NULL)
{
tmp=malloc(sizeof(struct node));
tmp->coef=p2->coef;

http://csetube.weebly.com/
16

tmp->expo=p2->expo;
if (p3_start==NULL) /*poly 1 is empty*/
{
p3_start=tmp;
p3=p3_start;
}
else
{
p3->link=tmp;
p3=p3->link;
}
p2=p2->link;
}/*End of while*/
p3->link=NULL;
return p3_start;
}/*End of poly_add() */

display(struct node *ptr) k /


{
if(ptr==NULL) .t
{
b e
printf("Empty\n");
return;
t u
}
while(ptr!=NULL) s e
{
/ c
printf("(%.1fx^%d) + ", ptr->coef,ptr->expo);
ptr=ptr->link;
: /
}
t p
printf("\b\b \n"); /* \b\b to erase the last + sign */
t
}/*End of display()*/
h

http://csetube.weebly.com/
17

OUTPUT

Polynomial 1 :
How many terms u want to enter : 3
Enter coeficient for term 1 : 5
Enter exponent for term 1 : 2
Enter coeficient for term 2 : 3
Enter exponent for term 2 : 1
Enter coeficient for term 3 : 1
Enter exponent for term 3 : 0

Polynomial 2 :
How many terms u want to enter : 3
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 3
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 2
Enter coeficient for term 3 : 4 k /
Enter exponent for term 3 : 1
. t
Polynomial 1 is : (5.0x^2) + (3.0x^1) + (1.0x^0)
b e
Polynomial 2 is : (3.0x^3) + (4.0x^2) + (4.0x^1)
t u
e
Added polynomial is : (3.0x^3) + (9.0x^2) + (7.0x^1) + (1.0x^0)
s
/ c
: /
t p
h t

http://csetube.weebly.com/
18

3. IMPLEMENT STACK AND USE IT TO CONVERT INFIX TO


POSTFIX EXPRESSION

AIM:

To write a program to implement stack and use it to convert infid to postfix


expression.

ALGORITHM:

1. Start.
2. Create a stack to store operand and operator.
3. In Postfix notation the operator follows the two operands and in the infix notation
the operator is in between the two operands.
k /
4. Consider the sum of A and B. Apply the operator “+” to the operands A and B

. t
and write the sum as A+B is INFIX. + AB is PREFIX. AB+ is POSTFIX

b e
5. Get an Infix Expression as input and evaluate it by first converting it to postfix
and then evaluating the postfix expression.
t u
e
6. The expressions with in innermost parenthesis must first be converted to postfix
s
c
so that they can be treated as single operands. In this way Parentheses can be
/
/
successively eliminated until the entire expression is converted.
:
t p
7. The last pair of parentheses to be opened with in a group of parentheses encloses
the first expression with in that group to be transformed. This last-in first-out

h t
immediately suggests the use of Stack. Precedence plays an important role in the
transforming infix to postfix.
8. Stop.

http://csetube.weebly.com/
19

PROGRAM

#include<stdio.h>
#include<string.h>
#include<math.h>
#define Blank ' '
#define Tab '\t'
#define MAX 50

long int pop ();


long int eval_post();
char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;

main()
{
long int value; char k /
choice='y';
while(choice == 'y') .t
{
b e
top = 0;
printf("Enter infix : ");
t u
fflush(stdin);
gets(infix); s e
infix_to_postfix();
printf("Postfix : %s\n",postfix); / c
value=eval_post();
: /
t p
printf("Value of expression : %ld\n",value);
printf("Want to continue(y/n) : ");

} h t
scanf("%c",&choice);

}/*End of main()*/

infix_to_postfix()
{
int i,p=0,type,precedence,len;
char next ;
stack[top]='#';
len=strlen(infix);
infix[len]='#';
for(i=0; infix[i]!='#';i++)
{
if( !white_space(infix[i]))
{
switch(infix[i])

http://csetube.weebly.com/
20

{
case '(':
push(infix[i]);
break;
case ')':
while((next = pop()) != '(')
postfix[p++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
precedence = prec(infix[i]);
while(stack[top]!='#' && precedence<= prec(stack[top]))
postfix[p++] = pop();
push(infix[i]); k /
break;
default: /*if an operand comes */ . t
postfix[p++] = infix[i];
b e
}/*End of switch */
}/*End of if */
t u
}
while(stack[top]!='#') s e
postfix[p++] = pop();
/ c
postfix[p] = '\0' ; /*End postfix with'\0' to make it a string*/

:
}/*End of infix_to_postfix()*//
p
/* This function returns the precedence of the operator */
t
{ h t
prec(char symbol )

switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}/*End of switch*/

http://csetube.weebly.com/
21

}/*End of prec()*/

push(long int symbol)


{
if(top > MAX)
{
printf("Stack overflow\n");
exit(1);
}
else
{ top=top+1;
stack[top] = symbol;
}
}/*End of push()*/
long int pop()
{
if (top == -1 )
{ k /
printf("Stack underflow \n");
exit(2); .t
}
b e
else
return (stack[top--]);
t u
}/*End of pop()*/
s e
white_space(char symbol)
{ / c
: /
if( symbol == Blank || symbol == Tab || symbol == '\0')
return 1;
else
t p
return 0;
h t
}/*End of white_space()*/

long int eval_post()


{
long int a,b,temp,result,len;
int i; len=strlen(postfix);
postfix[len]='#';
for(i=0;postfix[i]!='#';i++)
{
if(postfix[i]<='9' && postfix[i]>='0')
push( postfix[i]-48 );
else
{

http://csetube.weebly.com/
22

a=pop();
b=pop();
switch(postfix[i])
{
case '+':
temp=b+a; break;
case '-':
temp=b-a;break;
case '*':
temp=b*a;break;
case '/':
temp=b/a;break;
case '%':
temp=b%a;break;
case '^':
temp=pow(b,a);
}/*End of switch */ push(temp); }/*End of else*/ }/*End of for */
result=pop(); return result; }/*End of eval_post */ k /
. t
OUTPUT:
b e
Enter infix : (a+b)
t u
Postfix : ab+ s e
/ c
Enter infix : (a+b)*c/d+f

: /
Postfix : ab+c*d/f+
t p
h t

http://csetube.weebly.com/
23

4. IMPLEMENT A DOUBLE-ENDED QUEUE (DEQUEUE) WHERE


INSERTION AND DELETION OPERATIONS ARE POSSIBLE AT
BOTH THE ENDS

AIM :

To write a program to implement a double ended queue (DEQUEUE) where


insertion and deletion operations are possible at both the ends.

ALGORITHM:

1. Start.
2. The operations that can be performed on a dequeue are as follows
Insert at front
Insert at rear
Delete from front
Delete from rear
k /
. t
3. After inserting the element B at front end, the dequeue will look like this
BC D E
b e
front rear
t u
e
4. After inserting the element F at rear end,the dequeue will look like this
s
BC D E F
front / c rear

: /
5. We delete an element from front which has to be B.

p
tt
C D E F
front rear
h
6. We delete an element from rear which has to be F.
C D E

front rear
7. Stop.

http://csetube.weebly.com/
24

PROGRAM :

# include<stdio.h>
# define MAX 5
int deque_arr[MAX];
int left = -1;
int right = -1;

main()
{
int choice;
printf("1.Input restricted dequeue\n");
printf("2.Output restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : k /
input_que();
break; .t
case 2:
b e
output_que();
break;
t u
default:
printf("Wrong choice\n"); s e
}/*End of switch*/
}/*End of main()*/ / c
: /
input_que()
{
t p
int choice;
while(1) h t
{
printf("1.Insert at right\n");
printf("2.Delete from left\n");
printf("3.Delete from right\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:

http://csetube.weebly.com/
25

delete_left();
break; case 3:
delete_right();
break;
case 4:
display_queue();
break;
case 5:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of input_que() */

output_que()
{
int choice; k /
while(1)
{ .t
printf("1.Insert at right\n");
b e
printf("2.Insert at left\n");
printf("3.Delete from left\n");
t u
printf("4.Display\n");
printf("5.Quit\n"); se
printf("Enter your choice : ");
scanf("%d",&choice); / c
switch(choice)
: /
{
case 1:
t p
insert_right();
break; h t
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display_queue();
break;
case 5:
exit();
default:
printf("Wrong choice\n");

http://csetube.weebly.com/
26

}/*End of switch*/
}/*End of while*/
}/*End of output_que() */

insert_right()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{
printf("Queue Overflow\n");
return;
}
if (left == -1) /* if queue is initially empty */
{
left = 0;
right = 0;
}
else k /
if(right == MAX-1) /*right is at last position of queue */
right = 0; .t
else
b e
right = right+1;
t
printf("Input the element for adding in queue : ");u
scanf("%d", &added_item);
deque_arr[right] = added_item ; s e
}/*End of insert_right()*/
/ c
insert_left()
: /
{
int added_item;
t p
{ h t
if((left == 0 && right == MAX-1) || (left == right+1))

printf("Queue Overflow \n");


return;
}
if (left == -1)/*If queue is initially empty*/
{
left = 0;
right = 0;
} else
if(left== 0)
left=MAX-1;
else
left=left-1;
printf("Input the element for adding in queue : ");

http://csetube.weebly.com/
27

scanf("%d", &added_item);
deque_arr[left] = added_item ;
}/*End of insert_left()*/

delete_left()
{
if (left == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[left]);
if(left == right) /*Queue has only one element */
{
left = -1;
right=-1;
}
else k /
if(left == MAX-1)
left = 0; . t
else
b e
left = left+1;
}/*End of delete_left()*/
t u
delete_right() s e
{
if (left == -1) / c
{
: /
return ;
t p
printf("Queue Underflow\n");

}
h t
printf("Element deleted from queue is : %d\n",deque_arr[right]);
if(left == right) /*queue has only one element*/
{
left = -1;
right=-1;
}
else
if(right == 0)
right=MAX-1;
else
right=right-1;
}/*End of delete_right() */

display_queue()
{

http://csetube.weebly.com/
28

int front_pos = left,rear_pos = right;


if(left == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= MAX-1) k /
{
printf("%d ",deque_arr[front_pos]); .t
front_pos++;
b e
}
front_pos = 0;
t u
while(front_pos <= rear_pos)
{ se
printf("%d ",deque_arr[front_pos]);
front_pos++; / c
}
: /
}/*End of else */
printf("\n");
t p
t
}/*End of display_queue() */
h

http://csetube.weebly.com/
29

OUTPUT:

1.Input restricted dequeue


2.Output restricted dequeue
Enter your choice : 1

1.Insert at right
2.Delete from left
3.Delete from right
4.Display
5.Quit
Enter your choice : 1

Input the element for adding in queue : 10

1.Insert at right
2.Delete from left k /
3.Delete from right
4.Display .t
5.Quit
b e
Enter your choice : 1
t u
Input the element for adding in queue : 20
s e
1.Insert at right
2.Delete from left / c
3.Delete from right
: /
4.Display
5.Quit
t p
t
Enter your choice : 1
h
Input the element for adding in queue : 30

1.Insert at right
2.Delete from left
3.Delete from right
4.Display
5.Quit
Enter your choice : 4

Queue elements :
10 20 30

1.Insert at right
2.Delete from left

http://csetube.weebly.com/
30

3.Delete from right


4.Display
5.Quit
Enter your choice : 2

Element deleted from queue is : 10


1.Insert at right
2.Delete from left
3.Delete from right
4.Display
5.Quit
Enter your choice : 4

Queue elements :
20 30

1.Insert at right
2.Delete from left k /
3.Delete from right
4.Display .t
5.Quit
b e
Enter your choice : 3
t u
Element deleted from queue is : 30
se
1.Insert at right
2.Delete from left / c
3.Delete from right
: /
4.Display
5.Quit
t p
t
Enter your choice : 4
h
Queue elements :
20

1.Insert at right
2.Delete from left
3.Delete from right
4.Display
5.Quit
Enter your choice : 3

Element deleted from queue is : 20

1.Insert at right
2.Delete from left

http://csetube.weebly.com/
31

3.Delete from right


4.Display
5.Quit
Enter your choice : 3

Queue Underflow

1.Insert at right
2.Delete from left
3.Delete from right
4.Display
5.Quit
Enter your choice :5

k /
.t
b e
t u
se
/ c
: /
t p
h t

http://csetube.weebly.com/
32

5. IMPLEMENT AN EXPRESSION TREE. PRODUCE ITS PRE-


ORDER, IN-ORDER, AND POST- ORDER TRAVERSALS

AIM:

To develop a program to implement an expression tree, that produce its pre-order,


in-order and post-order traversals.

ALGORITHM:

1. Start.
2. Create a binary tree with the nodes partitioned into three disjoint subsets. The
first subset consists of a single element called the root of the tree.the other two

k /
subsets are themselves binary trees called the left and right sub trees of the

. t
original tree.A left or right sub tree can be empty.Each element of a binary tree is
called node of the tree.
b e
u
3. A common operation in the binary tree is to traverse that is to pass through the
t
e
tree enumerating each nodes once.The traversal of the binary tree can be done in
s
/ c
three ways they are preorder,inorder,postorder traversal.
4. To traverse a nonempty binary tree preorder we perform the following three

: /
operations(as depth-first order)

t p
o

o h t
Visit the root
Traverse the left subtree in preorder
o Traverse the right subtree in preorder

5. To traverse a nonempty binary tree inorder we perform the following three


operations(as symmetric order)

o Traverse the left subtree in inorder


o Visit the root
o Traverse the right subtree in inorder

http://csetube.weebly.com/
33

6. To traverse a nonempty binary tree postorder we perform the following three


operations

o Traverse the left subtree in postorder


o Traverse the right subtree in postorder
o Visit the root

7. Stop.

k /
. t
b e
t u
s e
/ c
: /
t p
h t

http://csetube.weebly.com/
34

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<alloc.h>
#define size 20
typedef struct node
{
char data;
struct node *left;
struct node *right;
}
btree;
/*stack stores the operand nodes of the tree*/
btree *stack[size];
int top;
k /
void main()
{ .t
btree *root;
b e
char exp[80];/*exp stores postfix expression*/
btree *create(char exp[80]);
t u
void inorder(btree *root);
void preorder(btree *root); s e
void postorder(btree *root);
clrscr(); / c
: /
printf("\n enter the postfix expression:\n");
scanf("%s",exp);
t p
top=-1;/*Initialize the stack*/

h t
root=create(exp);
printf("\n The tree is created.....\n");
printf("\n Inorder traversal: \n\n");
inorder(root);
printf("\n Preorder traversal: \n\n");
preorder(root);
printf("\n Postorder traversal: \n\n");
postorder(root);
getch();
}

btree *create(char exp[])


{
btree *temp;
int pos;
char ch;

http://csetube.weebly.com/
35

void push(btree*);
btree *pop();
pos=0;
ch=exp[pos];
while(ch!='\0')
{
/*create new node*/
temp=((btree*)malloc(sizeof(btree)));
temp->left=temp->right=NULL;
temp->data=ch;
if(isalpha(ch))
push(temp);
else if(ch=='+' ||ch=='-' || ch=='*' || ch=='/')
{
temp->right=pop();
temp->left=pop();
push(temp);
} k /
else
printf("\n Invalid char Expression\n"); .t
pos++;
b e
ch=exp[pos];
}
t u
temp=pop();
return(temp); s e
}
/ c
void push(btree *Node)
: /
{
if(top+1 >=size)
t p
top++; h t
printf("Error:Stack is full\n");

stack[top]=Node;
}

btree* pop()
{
btree *Node;
if(top==-1)
printf("\nerror: stack is empty..\n");
Node =stack[top];
top--;
return(Node);
}

void inorder(btree *root)

http://csetube.weebly.com/
36

{
btree *temp;
temp=root;
if(temp!=NULL)
{
inorder(temp->left);
printf("%c",temp->data);
inorder(temp->right);
}
}

void preorder(btree *root)


{
btree *temp;
temp=root;
if(temp!=NULL)
{
printf("%c",temp->data); k /
preorder(temp->left);
preorder(temp->right); .t
}
b e
}
t u
void postorder(btree *root)
{ se
btree *temp;
temp=root; / c
if(temp!=NULL)
: /
{
t
postorder(temp->left); p
h t
postorder(temp->right);
printf("%c",temp->data);
}
}

http://csetube.weebly.com/
37

OUTPUT:

Enter the postfix expression:


ab+cd-*

The tree is created.....

Inorder traversal:
a+b*c-d

Preorder traversal:
*+ab-cd

Postorder traversal:
ab+cd-*

k /
.t
b e
t u
se
/ c
: /
t p
h t

http://csetube.weebly.com/
38

6. IMPLEMENT BINARY SEARCH TREE

AIM :

To write a program to create binary search tree and perform insertion and search
operations on it.

ALGORITHM:

1. Start
2. Create a binary tree using linked representation with nodes having the structure
LEFTDATARIGHT. Build the tree in such a way that values at each left is
less and values at each right is greater.
3. Insert: Get the value to be inserted. Create a new node and set the data field to X.
then set the left and right links to NULL. k /
. t
4. Compare X with root node data in X <root continue search in left subtree. If

b e
X=root, then display “Duplicate data”, if X>root then search in right subtree.
Then insert the node in its right position.
t u
5. Display: Display all the data in the tree.
s e
6. Stop.
/ c
: /
t p
h t

http://csetube.weebly.com/
39

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct search
{
int element;
struct search* left;
struct search *right;
};
struct search *t;
struct search *makeempty(struct search *);
struct search *findmin(struct search *);
struct search *findmax(struct search *);
void inorder(struct search *);
struct search *insert(int,struct search *); k /
struct search *delet(int,struct search *);
int retrieve(struct search *); . t
b e
void main()
{
t u
int choice,element;
clrscr(); s e
printf(“\n\t\t tree”);
t=makeempty(NULL); / c
: /
printf(“\n Operations on tree”);

do
t p
printf("\n 1.Findmin \t 2.Findmax \t 3.Insert \t 4.Delete \t 5.Exit \n");

{
h t
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Minimum:%d",retrieve(findmin(t)));
break;
case 2:
printf("\n Maximum:%d",retrieve(findmax(t)));
break;
case 3:
printf("\n Enter an element to insert:");
scanf("%d",&element);
t=insert(element,t);
inorder(t);

http://csetube.weebly.com/
40

break;
case 4:
printf("\nEnter the element to delete:");
scanf("%d",&element);
t=delet(element,t);
inorder(t);
break;
case 5:
exit(0);
}
}while(choice!=6);
getch();
}

struct search *makeempty(struct search *t)


{
if(t!=NULL)
{ k /
makeempty(t->left);
makeempty(t->right); .t
free(t);
b e
}
return(0);
t u
}
s e
struct search *findmin(struct search *t)
{ if(t==NULL) / c
return(NULL);
: /
else if(t->left==NULL)
return(t);
t p
else
h t
return(findmin(t->left));
}
struct search *findmax(struct search *t)
{
if(t!=NULL)
while(t->right!=NULL)
t=t->right;
return(t);
}

struct search *insert(int x,struct search *t)


{
if(t==NULL)

http://csetube.weebly.com/
41

t=(struct search *)malloc(sizeof(struct search));


if(t==NULL)
{
exit(1);
}
else
{
t->element=x;
t->left=t->right=NULL;
}
}
else
{
if(x<t->element)
t->left=insert(x,t->left);
else if(x>t->element)
t->right=insert(x,t->right);
} k /
return(t);
} .t
struct search *delete(int x, struct search *t)
b e
{
if(t==NULL)
t u
printf("\nElement not found");
else if(x<t->element) s e
t->left=delet(x,t->left);
else if(x>t->element) / c
t->right=delet(x,t->right);
: /
{
t p
else if(t->left && t->right)

h t
tmp=findmin(t->right);
t->element=tmp->element;
t->right=delet(t->element,t->right);
}
else
{
tmp=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tmp);
}

int retrieve(struct search *p)


{

http://csetube.weebly.com/
42

return(p->element);
}

void inorder(struct search *t)


{
if(t!=NULL)
{
inorder(t->left);
printf("\t %d\t",t->element);
inorder(t->right);
}
}

k /
.t
b e
t u
se
/ c
: /
t p
h t

http://csetube.weebly.com/
43

OUTPUT:

Enter an element to insert:89


63 89
Enter your choice:3
Enter an element to insert:52
52 63 89
Enter your choice:3
Enter an element to insert:95
52 63 89 95
Enter your choice1
Minimum :52
Enter your choice2
Maximum:95
Enter your choice4
Enter an element to delete:52
63 89
Enter your choice:5
95
k /
.t
b e
t u
se
/ c
: /
t p
h t

http://csetube.weebly.com/
44

7. IMPLEMENT INSERTION IN AVL TREES

AIM:

To develop a program to implement the insertion on AVL Trees.

ALGORITHM:

1. Start.
2. Declare the node with leftlink, rightlink, data and height of node.
3. Enter the number of elements to be inserted.
4. While inserting each element the height of each node will be checked.
5. If the height difference of left and right node is equal to 2 for an node then the
height is unbalanced at the node.
k /
. t
6. The present node while inserting a new node at left sub tree then perform rotation
with left child otherwise rotation with right chile.
b e
u
7. Height is unbalanced at Grand Parent node while inserting a new node at right
t
s e
subtree of parent node then perform double rotation with left.
8. Height is unbalanced at grandparent node while inserting a new node then

/ c
perform double rotation with right.

: /
9. To view the tree perform traversal on tree.
10. Stop.
t p
h t

http://csetube.weebly.com/
45

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1

typedef struct Node


{
int data;
int BF;
struct Node *left;
struct Node *right;
}node;
node *root;
node *create(node *root,int data,int *current);
node *remove(node *root,int data,int *current); k /
node *find_succ(node *temp,node *root,int *current);
node *right_rotation(node *root,int *current); .t
node *left_rotation(node *root,int *current);
b e
void display(node *root);
t u
node *insert(int data,int *current)
{ s e
root=create(root,data,current);
return root; / c
}
: /
t p
node *create(struct Node *root,int data,int *current)
{
h t
node *temp1,*temp2;
if(root==NULL)
{
root= new node;
root->data=data;
root->left=NULL;
root->right=NULL;
root->BF=0;
*current=TRUE;
return(root);
}
if(data<root->data)
{
root->left=create(root->left,data,current);
if(*current)

http://csetube.weebly.com/
46

{
switch(root->BF)
{
case 1 :
temp1=root->left;
if(temp1->BF==1)
{
printf("\n\n Single Rotation : R Rotation");
root->left=temp1->right;
temp1->right=root;
root->BF=0;
root=temp1;
}
else
{
printf("\n Double rotation : LR rotation");
temp2=temp1->right;
temp1->right=temp2->left; k /
temp2->left=temp1;
root->left=temp2->right; .t
temp2->right=root;
b e
if(temp2->BF==1)
root->BF=-1;
t u
else
root->BF=0; s e
if(temp2->BF==-1)
temp1->BF=1; / c
else
: /
temp1->BF=0;
root=temp2;
t p
}
root->BF=0; h t
*current=FALSE;
break;
case 0:
root->BF=1;
break;
case -1:
root->BF=0;
*current=FALSE;
}}}
if(data > root->data)
{
root->right=create(root->right,data,current);
if(*current!=NULL)
{

http://csetube.weebly.com/
47

switch(root->BF)
{
case 1:
root->BF=0;
*current=FALSE;
break;
case 0:
root->BF=-1;
break;
case -1:
temp1=root->right;
if(temp1->BF==-1)
{
printf("\n\n\n single rotation : L Rotation");
root->right=temp1->left;
temp1->left=root;
root->BF=0;
root=temp1; k /
}
else .t
{
b e
printf("\n Double rotation : RL rotation");
temp2=temp1->left;
t u
temp1->left=temp2->right;
temp2->right=temp1; s e
root->right=temp2->left;
temp2->left=root; / c
if(temp2->BF==-1)
: /
root->BF=1;
else
t p
root->BF=0;
h
if(temp2->BF==1)
t
temp1->BF=-1;
else
temp1->BF=0;
root=temp2;
}
root->BF=0;
*current=FALSE;
}
}
}
return(root);
}

void display(node *root)

http://csetube.weebly.com/
48

{
if(root!=NULL)
{
display(root->left);
printf(" %d",root->data);
display(root->right);
}
}

node *remove(node *root,int data,int *current)


{
node *temp;
if(root->data==13)
printf(" %d",root->data);
if(root==NULL)
{
printf("\n No Such data");
return (root); k /
}
else .t
{
b e
if(data<root->data)
{
t u
root->left=remove(root->left,data,current);
if(*current) s e
root=right_rotation(root,current);
} / c
else
: /
{
t
if(data>root->data) p
{
h t
root->right=remove(root->right,data,current);
if(*current)
root=left_rotation(root,current);
}
else
{
temp=root;
if(temp->right==NULL)
{
root=temp->left;
*current=TRUE;
delete(temp);
}
else
{

http://csetube.weebly.com/
49

if(temp->left==NULL)
{
root=temp->right;
*current=TRUE;
delete(temp);
}
else
{
temp->right=find_succ(temp->right,temp,current);
if(*current)
root=left_rotation(root,current);
}
}
}
}
}
return(root);
} k /
node *find_succ(node *succ,node *temp,int *current)
{ .t
node *temp1=succ;
b e
if(succ->left!=NULL)
{
t u
if(*current) s e
succ->left=find_succ(succ->left,temp,current);

succ=right_rotation(succ,current);
} / c
else
: /
{
temp1=succ;
t p
h t
temp->data=succ->data;
succ=succ->right;
delete(temp1);
*current = TRUE;
}
return(succ);
}
node *right_rotation(node *root,int *current)
{
node *temp1,*temp2;
switch(root->BF)
{
case 1:
root->BF=0;
break;
case 0:

http://csetube.weebly.com/
50

root->BF=-1;
*current=FALSE;
break;
case -1:
temp1=root->right;
if(temp1->BF<=0)
{
printf("\n Single Rotation : L rotation");
root->right=temp1->left;
temp1->left=root;
if(temp1->BF==0)
{
root->BF=-1;
temp1->BF=1;
*current=FALSE;
}
else
{ k /
root->BF=temp1->BF=0;
} .t
root=temp1;
b e
}
else
t u
{
s
printf("\n Double Rotation : RL Rotation");
e
temp2=temp1->left;
temp1->left=temp2->right; / c
temp2->right=temp1;
: /
root->right=temp2->left;
temp2->left=root;
t p
root->BF=1; h t
if(temp2->BF==-1)

else
root->BF=0;
if(temp2->BF==-1)
root->BF=1;
else
root->BF=0;
if(temp2->BF==1)
temp1->BF=-1;
else
temp1->BF=0;
root=temp2;
temp2->BF=0;
}
}

http://csetube.weebly.com/
51

return (root);
}
node *left_rotation(node *root,int *current)
{
node *temp1,*temp2;
switch(root->BF)
{
case -1:
root->BF=0;
break;
case 0:
root->BF=1;
*current=FALSE;
break; case 1:
temp1=root->left;
if(temp1->BF>=0)
{
printf("\n Single Rotation R Rotation"); k /
root->left=temp1->right;
temp1->right=root; .t
if(temp1->BF==0)
b e
{
root->BF=1;
t u
temp1->BF=-1;
*current=FALSE; s e
}
else / c
{
: /
root->BF=temp1->BF=0;
}
t p
root=temp1;
} h t
else
{
printf("\n Double Rotatuion : LR Rotation");
temp2=temp1->right;
temp1->right=temp2->left;
temp2->left=temp1;
root->left=temp2->right;
temp2->right=root;
if(temp2->BF==1)
root->BF=-1;
else
root->BF=0;
if(temp2->BF==-1)

http://csetube.weebly.com/
52

temp1->BF=1;
else
temp1->BF=0;
root=temp2;
temp2->BF=0;
}
}
return root;
}
void main()
{
node *root=NULL;
int current; clrscr();
root=insert(40,&current);
printf("\n\t\t");
display(root);
root=insert(50,&current);
printf("\n\t\t"); k /
display(root);
root=insert(70,&current); .t
printf("\n\t\t");
b e
display(root);
root=insert(30,&current);
t u
printf("\n\t\t");
display(root); s e
root=insert(20,&current);
printf("\n\t\t"); / c
display(root);
: /
root=insert(45,&current);
printf("\n\t\t");
t p
display(root);
h t
root=insert(25,&current);
printf("\n\t\t");
display(root);
root=insert(10,&current);
printf("\n\t\t");
display(root);
root=insert(5,&current);
printf("\n\t\t");
display(root);
printf("\n\n\n Final AVL tree is : \n");
display(root);
}

http://csetube.weebly.com/
53

OUTPUT

40
50
Inserting 70
Single rotation : L Rotation
40 50 70
Inserting 30
30 40 50 70
Inserting 20
Single rotation : r Rotation
20 30 40 50 70
Inserting 45
Double rotation : lr Rotation
20 30 40 45 50 70
Inserting 25 k /
Double rotation : lr Rotation
20 25 30 40 45 50 70 .t
Inserting 10
b e
10 20 25 30 40 45 50 70
Inserting 5
t u
Single Rotation : r Rotation
5 10 20 25 30 40 45 50 70 s e
Final AVL Tree is:
5 10 20 25 30 40 45 50 70 / c
: /
AVL tree after deletion of a node 20:
5 10 25 30 40 45 50 70
t p
AVL tree after deletion of a node 45:

h t 5 10 25 30 40 50 70

http://csetube.weebly.com/
54

8. IMPLEMENT PRIORITY QUEUE USING BINARY HEAPS

AIM:

To develop a program to implement the priority queue using Binary Heaps.

ALGORITHM:

1. Start.
2. Definition: An abstract data type to efficiently support finding the item
with the highest priority across a series of operations. The basic operations are:
insert, find-minimum (or maximum), and delete-minimum (or maximum). Some
implementations also efficiently support join two priority queues (meld), delete an
arbitrary item, and increase the priority of a item (decrease-key).
k /
3.
t
Formal Definition: The operations new(), insert(v, PQ), find-minimum or

.
min(PQ), and delete-minimum or dm(PQ) may be defined with axiomatic
semantics as follows.
b e
4. new() returns a priority queue
t u
5. min(insert(v, new())) = v
s e
6. c
dm(insert(v, new())) = new()
/
7.
/
min(insert(v, insert(w, PQ))) = if priority(v) < priority(min(insert(w, PQ)))
:
8. t p
then v else min(insert(w, PQ))
dm(insert(v, insert(w, PQ))) = if priority(v) < priority(min(insert(w, PQ)))

h t
then insert(w, PQ) else insert(v, dm(insert(w, PQ))) where PQ is a priority queue,
v and w are items, and priority(v) is the priority of item v.
9. Stop.

http://csetube.weebly.com/
55

PROGRAM

#include<stdio.h>
#include<conio.h>
#define SIZE 5

void main(void)
{
int rear,front,que[SIZE],choice;int Qfull(int rear),Qempty(int rear,int front);
int insert(int que[SIZE],int rear,int front);
int delet(int que[SIZE],int front);
void display(int que[SIZE],int rear,int front);
char ans;
clrscr();
front=0;
rear=-1;
do
{ k /
clrscr();
printf("\n\t\t Priority Queue \n"); . t
printf("\n Main Menu");
b e
printf("\n 1.Insert\n2.Delete\n3.Display");
printf("\n Enter Your Choice : ");
t u
scanf("%d",&choice);
switch(choice) s e
{
case 1 : if(Qfull(rear)) / c
printf("\n Queue if FUll");
: /
else
t p
rear=insert(que,rear,front);
break;
case 2: h t
if(Qempty(rear,front))
printf("\n Cannot delete elements");
else
front=delet(que,front);
break;
case 3:if(Qempty(rear,front))
printf("\n eue is empty");
else
display(que,rear,front);
break;
default:printf("\n Wrong Choice");
break;
}
printf("\n Do you Want to continue?");

http://csetube.weebly.com/
56

ans=getche();
}
while(ans=='Y'||ans=='y');
getch();
}

int insert(int que[SIZE],int rear,int front)


{
int item,j;
printf("\n Enter the Elements : ");
scanf("%d",&item);
if(front == -1)
front++;
j=rear;
while(j>=0 && item<que[j])
{
que[j+1]=que[j];
j--; k /
}
que[j+1]=item; .t
rear=rear+1;
b e
return rear;
}
t u
int Qfull(int rear)
{ s e
if(rear==SIZE-1)
return 1; / c
else
: /
return 0;
}
t p
h t
int delet(int que[SIZE],int front)
{
int item;
item=que[front];
printf("\n The item deleted is %d",item);
front++;
return front;
}
Qempty(int rear,int front)
{
if((front==-1) || (front>rear))
return 1;
else
return 0;
}

http://csetube.weebly.com/
57

void display(int que[SIZE],int rear,int front)


{
int i;
printf("\n The Queue is : ");
for(i=front;i<=rear;i++)
printf(" %d",que[i]);
}

k /
.t
b e
t u
s e
/ c
: /
t p
h t

http://csetube.weebly.com/
58

OUTPUT

Priority Queue

Main Menu
1.Insert
2.Delete
3.Display
Enter Your Choice : 1

Enter the Elements : 50


Do you Want to continue?

Priority Queue
Main Menu
1.Insert
2.Delete
3.Display k /
Enter Your Choice : 1
Enter the Elements : 10 .t
b e
Do you Want to continue?
t u
Priority Queue
Main Menu se
1.Insert
2.Delete / c
3.Display
: /
Enter Your Choice : 1
t p
h t
Enter the Elements : 20
Do you Want to continue?

Priority Queue
Main Menu
1.Insert
2.Delete
3.Display
Enter Your Choice : 3

The Queue is : 10 20 50
Do you Want to continue?

http://csetube.weebly.com/
59

9. IMPLEMENT HASHING WITH OPEN ADDRESSING

AIM :

To develop a program to implement Hashing with open addressing.

ALGORITHM:

Open addressing hashing works on the same principle as other types of hashing; that
is, it uses a hashing function h and the key of a datum x to map x to h[key(x)], and the
values are s tored in a table. The difference is in what is stored in the table. Instead of
maintaining a pointer to a linked list, the table contains the actual values of key(x).
Implementation
The table is T[0..m-1] where m is the table size, and the input is x_1,...,x_n. The has
function h is given.

INSERT:
k /
. t
We apply our hash function to the first value: the last digit is 9, so 19 goes in slot 9.

b e
Next is 33: this goes in slot 3. Now we come to a problem: the next number, 43 should
also go into slot 3. We have no linked lists, so where does it go? In open addressing, a

t u
value that is slated for an occupied slot goes into the next empty slot. 43 then goes into
slot 4. 53: slot 3 is filled, go to slot 4. Slot 4 is filled, go to slot 5. Slot 5 is empty, so 53
goes into slot 5.
s e
/ c
The table is usually made circular, so that a value meant to be inserted into the

: /
last slot (which is occupied) is sent around to the front of the list. So if we were to insert
99 into the table, we see that slot 9 is occupied, and 99 is sent to slot 0, which is empty.

t p
Note that if we have more values entered than slots, we can run out of room in the table,
and be unable to make any more insertions. For this reason, the load factor a in open
t
addressing can never exceed 1.0 .
h
DELETE:

Say we want to delete 43. First we go to slot 3: It's occupied, but not by 43. So we go on
to the next occupied slot. We continue to do this until we find either the number we're
looking for (success), an empty slot or we arrive back where we started. In this case, the
very next slot is occupied by 43, so we remove it and mark the slot as deleted. (We'll see
why in a moment.)

Probing and the Probe Sequence

Probing is simply another word for the sequence we used for our search and insert
routines. We "probe" the table until we find an available slot. Due to the (usually) circular
nature of the table we have to limit the number of slots the algorithm will examine. Since
there are only m slots in the table, we limit the algorithm to m comparisons.

http://csetube.weebly.com/
60

The probe sequence is the permutation of 0,1,...,m-1 used for searching for an available
slot when inserting an element. For a key k the sequence is denoted by:

h(k,0), h(k,1), ..., h(k,m-1)

Thus, h(k,0) is what we used to call h(k). The probe sequence used in the simple
introductory example is simply

h(k), h(k) + 1, ..., h(k) + m - 1

all mod m (to force the circular inspection of the array). Clearly, this is a permutation, but
not a very good one (we'll see why in a moment).

Linear Probing:

k /
This is more or less self-explanatory. In linear probing, the slots in the has table are
probed in a linear fashion, in order (ie. check slot 4, then slot 5, then 6, then 7,...etc). We
express linear probing as
. t
h(k,i)=[h(k) + c*i] mod m
b e
t u
where c is an integer. While the slots are probed in a linear fashion, they are not

s e
necessarily probed in exact order. Note that we use mod m to account for the (usually)
circular nature of the table. One of the problems with linear probing is called primary

/ c
clustering. In this, "blocks" of occupied slots tend to accumulate; the larger a block is, the
faster it grows, by the principles of probability. This increases the average search time in
the table.
: /
t p
h t

http://csetube.weebly.com/
61

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10

void main()
{
int a[MAX],num,key,i;
char ans;
int create(int);
void linear_prob(int [],int,int),display(int []);
clrscr();
printf("\n Collision Handling By Linaer Probling");
for(i=0;i<MAX;i++)
a[i]=-1;
do k /
{
printf("\n Enter the Number "); .t
scanf("%d",&num);
b e
key=create(num);
linear_prob(a,key,num);
t u
printf("\n Do U Wish to Contiue?(Y/N");
ans=getch(); s e
}
while(ans=='y'); / c
display(a);
: /
getch();
}
t p
h t
int create(int num)
{
int key;
key=num%10;
return key;
}

void linear_prob(int a[MAX],int key,int num)


{
int flag,i,count=0;
void display(int a[]);
flag=0;
if(a[key]==-1)
a[key]=num;
else

http://csetube.weebly.com/
62

{ i=0;
while(i<MAX)
{
if(a[i]!=-1)
count++;
i++;
}
if(count==MAX)
{
printf("\n\n Hash Table is Fu;;");
display(a);
getch();
exit(1);
}
for(i=key+1;i<MAX;i++)
if(a[i]==-1)
{
a[i]=num; k /
flag=1;
break; .t
}
b e
for(i=0;i<key&&flag==0;i++)
if(a[i]==-1)
t u
{
a[i]=num; se
flag=1;
break; / c
}
: /
}
}
t p
h t
void display(int a[MAX])
{
int i;
printf("\n\n The HAsh Table is....\n");
for(i=0;i<MAX;i++)
printf("\n %d %d",i,a[i]);
}

http://csetube.weebly.com/
63

OUTPUT

Collision Handling By Linaer Probling


Enter the Number 131
Do U Wish to Contiue?(Y/N

Enter the Number


21

Do U Wish to Contiue?(Y/N
Enter the Number
3

Do U Wish to Contiue?(Y/N
Enter the Number
4

Do U Wish to Contiue?(Y/N k /
Enter the Number 5
.t
Do U Wish to Contiue?(Y/N
b e
Enter the Number 8
t u
Do U Wish to Contiue?(Y/N
Enter the Number 9 s e
Do U Wish to Contiue?(Y/N / c
Enter the Number 18
: /
t p
Do U Wish to Contiue?(Y/N

0 18 h t
The HAsh Table is....

1 131
2 21
3 3
4 4
5 5
6 -1
7 -1
8 8
9 9

http://csetube.weebly.com/
64

10. IMPLEMENT PRIM'S ALGORITHM USING PRIORITY QUEUES TO


FIND MST OF AN UNDIRECTED GRAPH

AIM :

To develop a program to implement the Prim’s Algorithm using Priority Queues


to find MST of an Undirected Graph.

ALGORITHM :

Prim's algorithm is an algorithm in graph theory that finds a minimum spanning tree
for a connected weighted graph. This means it finds a subset of the edges that forms a
tree that includes every vertex, where the total weight of all the edges in the tree is
minimized. The algorithm was developed in 1930 by Czech mathematician Vojtěch

/
Jarník and later independently by computer scientist Robert C. Prim in 1957 and
k
t
rediscovered by Edsger Dijkstra in 1959. Therefore it is sometimes called the DJP
.
e
algorithm, the Jarník algorithm, or the Prim-Jarník algorithm.
b
t u
The algorithm continuously increases the size of a tree starting with a single vertex until
it spans all the vertices.
s e
/ c
/
1. Input: A connected weighted graph with vertices V and edges E.
:
p
2. Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V,
t
Enew = {}
h t
3. Repeat until Vnew = V:
Choose edge (u,v) with minimal weight such that u is in Vnew and v is not (if
there are multiple edges with the same weight, choose arbitrarily but
consistently)
Add v to Vnew, add (u, v) to Enew
4. Output: Vnew and Enew describe a minimal spanning tree

http://csetube.weebly.com/
65

PROGRAM

#include<stdio.h>
#define MAX 10
#define TEMP 0
#define PERM 1
#define FALSE 0
#define TRUE 1
#define infinity 9999

struct node
{
int predecessor;
int dist; /*Distance from predecessor */
int status;
};

struct edge k /
{
int u; .t
int v;
b e
};
int adj[MAX][MAX];
t u
int n;
s e
main()
/ c
{
int i,j;
: /
int path[MAX];
t
int wt_tree,count; p
h t
struct edge tree[MAX];
create_graph();
printf("Adjacency matrix is :\n");
display();
count = maketree(tree,&wt_tree);
printf("Weight of spanning tree is : %d\n", wt_tree);
printf("Edges to be included in spanning tree are : \n");
for(i=1;i<=count;i++)
{
printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
}/*End of main()*/

create_graph()
{

http://csetube.weebly.com/
66

int i,max_edges,origin,destin,wt;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else k /
{
adj[origin][destin]=wt; .t
adj[destin][origin]=wt;
b e
}
}/*End of for*/
t u
if(i<n-1)
{ s e
exit(1); / c
printf("Spanning tree is not possible\n");

}
: /
}/*End of create_graph()*/
t p
display()
{ h t
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
}
}/*End of display()*/

int maketree(struct edge tree[MAX],int *weight)


{
struct node state[MAX];
int i,k,min,count,current,newdist;
int m;

http://csetube.weebly.com/
67

int u1,v1;
*weight=0;
/*Make all nodes temporary*/
for(i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].dist = infinity;
state[i].status = TEMP;
}
/*Make first node permanent*/
state[1].predecessor=0;
state[1].dist = 0;
state[1].status = PERM;
/*Start from first node*/
current=1;
count=0; /*count represents number of nodes in tree */

{ k /
while( all_perm(state) != TRUE ) /*Loop till all the nodes become PERM*/

for(i=1;i<=n;i++)
. t
{

b e
if ( adj[current][i] > 0 && state[i].status == TEMP )
{
t u
if( adj[current][i] < state[i].dist )
{
s e
state[i].predecessor = current;

} / c
state[i].dist = adj[current][i];

}
: /
}/*End of for*/
t p
/*Search for temporary node with minimum distance

h t
and make it current node*/
min=infinity;
for(i=1;i<=n;i++)
{
if(state[i].status == TEMP && state[i].dist < min)
{
min = state[i].dist;
current=i;
}
}/*End of for*/
state[current].status=PERM;
/*Insert this edge(u1,v1) into the tree */
u1=state[current].predecessor;
v1=current;
count++;
tree[count].u=u1;

http://csetube.weebly.com/
68

tree[count].v=v1;
/*Add wt on this edge to weight of tree */
*weight=*weight+adj[u1][v1];
}/*End of while*/
return (count);
}/*End of maketree()*/
/*This function returns TRUE if all nodes are permanent*/

int all_perm(struct node state[MAX] )


{
int i;
for(i=1;i<=n;i++)
if( state[i].status == TEMP )
return FALSE;
return TRUE;
}/*End of all_perm()*/

k /
.t
b e
t u
s e
/ c
: /
t p
h t

http://csetube.weebly.com/
69

OUTPUT

Enter number of vertices : 5


Enter edge 1(0 0 to quit) : 0 1
Enter weight for this edge : 10
Invalid edge!
Enter edge 1(0 0 to quit) : 1 2
Enter weight for this edge : 10
Enter edge 2(0 0 to quit) : 2 3
Enter weight for this edge : 1
Enter edge 3(0 0 to quit) : 2 4
Enter weight for this edge : 6
Enter edge 4(0 0 to quit) : 3 4 k /
Enter weight for this edge : 2
.t
Enter edge 5(0 0 to quit) : 3 5
b e
Enter weight for this edge : 7
t u
Enter edge 6(0 0 to quit) : 4 5
s e
Enter weight for this edge : 3
Enter edge 7(0 0 to quit) : 5 1 / c
:
Enter weight for this edge : 5
/
t p
Enter edge 8(0 0 to quit) : 0 0

h t
Adjacency matrix is :
0 10 0 0 5
10 0 1 6 0
0 1 0 2 7
0 6 2 0 3
5 0 7 3 0
Weight of spanning tree is : 11
Edges to be included in spanning tree are :
1->5
5->4
4->3
3->2

http://csetube.weebly.com/
70

RADIX SORT

AIM:
To develop a program to implement Radix Sort.
ALGORITHM:
Radix sort is one of the linear sorting algorithms for integers. It functions by sorting
the input numbers on each digit, for each of the digits in the numbers. However, the
process adopted by this sort method is somewhat counterintuitive, in the sense that the
numbers are sorted on the least-significant digit first, followed by the second-least
significant digit and so on till the most significant digit.
To appreciate Radix Sort, consider the following analogy: Suppose that we wish to
sort a deck of 52 playing cards (the different suits can be given suitable values, for

k /
example 1 for Diamonds, 2 for Clubs, 3 for Hearts and 4 for Spades). The 'natural' thing
t
to do would be to first sort the cards according to suits, then sort each of the four seperate
.
e
piles, and finally combine the four in order. This approach, however, has an inherent
b
u
disadvantage. When each of the piles is being sorted, the other piles have to be kept aside
t
s e
and kept track of. If, instead, we follow the 'counterintuitive' aproach of first sorting the
cards by value, this problem is eliminated. After the first step, the four seperate piles are

/ c
combined in order and then sorted by suit. If a stable sorting algorithm (i.e. one which

: /
resolves a tie by keeping the number obtained first in the input as the first in the output) it
p
can be easily seen that correct final results are obtained.
t
h t
As has been mentioned, the sorting of numbers proceeds by sorting the least
significant to most significant digit. For sorting each of these digit groups, a stable sorting
algorithm is needed. Also, the elements in this group to be sorted are in the fixed range of
0 to 9. Both of these characteristics point towards the use of Counting Sort as the sorting
algorithm of choice for sorting on each digit (If you haven't read the description on
Counting Sort already, please do so now).
The time complexity of the algorithm is as follows: Suppose that the n input numbers
have maximum k digits. Then the Counting Sort procedure is called a total of k times.
Counting Sort is a linear, or O(n) algorithm. So the entire Radix Sort procedure takes
O(kn) time. If the numbers are of finite size, the algorithm runs in O(n) asymptotic time.

http://csetube.weebly.com/
71

PROGRAM

#define NUMELTS 100


# include<stdio.h>
#include<conio.h>
#include<math.h>
void radixsort(int a[],int);

void main()
{
int n,a[20],i;
clrscr();
printf("enter the number :");
scanf("%d",&n);
printf(" ENTER THE DATA -");
for(i=0;i<n;i++)
{ k /
printf("%d. ",i+1);
scanf("%d",&a[i]); .t
}
b e
radixsort(a,n);
getch();
t u
}
s e
void radixsort(int a[],int n)
{ / c
: /
int rear[10],front[10],first,p,q,exp,k,i,y,j;
struct
{
t p
int info;
int next; h t
}node[NUMELTS];
for(i=0;i<n-1;i++)
{
node[i].info=a[i];
node[i].next=i+1;
}
node[n-1].info=a[n-1];
node[n-1].next=-1;
first=0;
for(k=1;k<=2;k++) //consider only 2 digit number
{
for(i=0;i<10;i++)
{
front[i]=-1;

http://csetube.weebly.com/
72

rear[i]=-1;
}
while(first!=-1)
{ p=first;
first=node[first].next;
y=node[p].info;
exp=pow(10,k-1);
j=(y/exp)%10;
q=rear[j];
if(q==-1)
front[j]=p;
else
node[q].next=p;
rear[j]=p;
}
for(j=0;j<10&&front[j]==-1;j++)
;
first=front[j]; k /
while(j<=9)
{ .t
for(i=j+1;i<10&&front[i]==-1;i++)
b e
;
if(i<=9)
t u
{ p=i;
node[rear[j]].next=front[i]; s e
}
/ c
}
j=i;

: /
}
t p
node[rear[p]].next=-1;

h t
//copy into original array
for(i=0;i<n;i++)
{
a[i]=node[first].info;
first=node[first].next;}
clrscr();
textcolor(YELLOW);
cprintf(" DATA AFTER SORTING:");
for(i=0;i<n;i++)
printf(" %d.%d",i+1,a[i]);
}

http://csetube.weebly.com/
73

OUTPUT

enter the number :9

ENTER THE DATA -1. 90


2. 76
3. 12
4. 34
5. 98
6. 55
7. 43
8. 4
9. 78 k /
. t
b e
DATA AFTER SORTING: 1.4 2.12 3.34 4.43 5.55 6.76 7.78 8.90 9.98

t u
s e
/ c
: /
t p
h t

http://csetube.weebly.com/

You might also like