IT DS Lab Record
IT DS Lab Record
Date :
ARRAY IMPLEMENTATION OF LIST ADT
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#define size 10
void create()
{
int i;
printf("\nEnter the number of elements to be inserted: "); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dth element",i); scanf("%d",&a[i]);
}}
void display()
{
int i;
printf("\nThe elements in the arraylist are: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}}
void insert_first()
{
int i,x;
printf("\nEnter the elements to be inserted: "); scanf("%d",&x);
for(i=n-1;i>=0;i--)
a[i+1]=a[i];
a[0]=x;
}
void insert_middle()
{
int i,x;
printf("\nEnter the data: "); scanf("%d",&x);
printf("\nEnter the position of the element to be inserted: "); scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=x;
n=n+1;
}
void insert_last()
{
int x;
printf("\nEnter the element to be inserted: "); scanf("%d",&x);
a[n]=x;
n++;
}
void delete_first()
{
int i;
for(i=0;i<n-1;i++)
a[i]=a[i+1];
n--;
}
void delete_middle()
{
int i;
printf("\nEnter the position of the element to be deleted: "); scanf("%d",&pos);
for(i=pos;i<n-1;i++)
a[i]=a[i+1];
n--;
}
void delete_last()
{
a[n]=0; n--;
}
void main()
{ int ch;
do
{
printf("\n\n LIST IMPLEMENTATION USING ARRAY");
printf("\n ***************************************");
printf("\n\n 1.Creation 2.Display 3.Insert First 4.Insert Middle ");
printf("\n5.Insert Last 6.Delete First 7.Delete Middle 8.Delete Last 9.Exit ");
printf("\n\n\n Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_first();
break;
case 4:
insert_middle();
break;
case 5:
insert_last();
break;
case 6:
delete_first();
break;
case 7:
delete_middle();
break;
case 8:
delete_last();
break;
case 9:
exit(0);
break;
default:
printf("\n Enter your choice");
printf("\n Enter any key to continue");
}
}
while(ch!=0);
getch();
}
OUTPUT:
LIST IMPLEMENTATION USING ARRAY
1. Creation 2. Display 3. Insert First 4. Insert Middle 5. Insert Last 6. Delete First
7. Delete Middle 8. Delete Last 9.Exit
Enter your Choice: 1
Enter the number of elements to be inserted in an array: 5
Enter the 0th element: 1
Enter the 1st element: 2
Enter the 2nd element: 3
Enter the 3rd element: 4
Enter the 4th element: 5
LIST IMPLEMENTATION USING ARRAY
1. Creation 2. Display 3. Insert First 4. Insert Middle 5. Insert Last 6. Delete First
7. Delete Middle 8. Delete Last 9.Exit
Enter your Choice: 2
The elements in the array list are:
12345
LIST IMPLEMENTATION USING ARRAY
1. Creation 2. Display 3. Insert First 4. Insert Middle 5. Insert Last 6. Delete First
7. Delete Middle 8. Delete Last 9.Exit
Enter your Choice: 3
Enter the element to be inserted: 6
LIST IMPLEMENTATION USING ARRAY
1. Creation 2. Display 3. Insert First 4. Insert Middle 5. Insert Last 6. Delete First
7. Delete Middle 8. Delete Last 9.Exit
Enter your Choice: 2
The elements in the array list are:
62345
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
AIM: To write a C program to implement Singly List ADT using linked list implementation
ALGORITHM:
The following operations are performed on a Single Linked List
• Insertion
• Deletion
• Display
First, perform the following steps before implementing actual operations.
Step 1 - Include all the header files which are used in the program.
Step 2 - Declare all the user defined functions.
Step 3 - Define a Node structure with two members data and next
Step 4 - Define a Node pointer 'head' and set it to NULL.
Step 5 - Implement the main method by displaying operations menu and make suitable function
calls in the main method to perform user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
printf("Continue?\n");
scanf("%d",&q);
i++;
}
while(q!=0);
temp->next=null;
}
void insert_at_first()
{
struct node *temp;
temp=malloc(sizeof(struct node));
printf("Enter the data to be inserted in that node\n");
scanf("%d",&num);
temp->data=num;
temp->next=head;
head=temp;
}
void insert_at_last()
{
struct node *temp;
temp=head;
printf("Enter the data to be inserted in that node\n");
scanf("%d",&num);
while(temp->next!=null)
temp=temp->next;
temp->next=malloc(sizeof(struct node));
temp->data=num;
temp->next=null;
}
void insert_at_middle()
{
struct node *a,*temp;
a=head;
printf("Enter the position\n");
scanf("%d",&q);
printf("Enter the number to be inserted in that node\n");
scanf("%d",&num);
while(a->data!=q)
a=a->next;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->next=a->next;
a->next=temp;
}
void delete_at_first()
{
struct node *temp;
temp=head;
head=head->next;
free(temp);
}
void delete_at_last()
{
struct node *temp,*a;
temp=head;
while(temp->next!=null)
{
a=temp;
temp=temp->next; }
a->next=null;
free(temp);
}
void delete_at_middle()
{
struct node *temp,*a;
temp=head;
printf("Enter the position\n");
scanf("%d",&q);
while(temp->data!=q)
{
a=temp;
temp=temp->next;
}
a->next=temp->next;
free(temp);
}
void display()
{
struct node *temp;
temp=head;
printf("head->");
while(temp!=null)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("null");
}
void main()
{
int ch,c;
clrscr();
create();
do
{
printf("What operation do you want to do? \n");
printf("1.Insert at first\n2.Insert at last\n3.Insert at middle\n4.delete at first\n5.delete at last\n6.delete
at middle\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_at_first();
break;
case 2:
insert_at_last();
break;
case 3:
insert_at_middle();
break;
case 4:
delete_at_first();
break;
case 5:
delete_at_last();
break;
case 6:
delete_at_middle();
break;
default:
printf("Wrong choice");
}
printf("The modified list is\n")
display();
printf("\ndo you want to continue?\n1.yes\n2.no\n");
scanf("%d",&c);
}
while(c!=2);
getch();
}
OUTPUT:
Node:1 1
Node:2 2
Continue?
1
Node:3 3
Continue?
1
Node:4 4
Continue?
1
Node:5 5
Continue?
0
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at firs 5.delete at last 6.delete at middle
1
Enter the data to be inserted in that node
10
The modified list is
head->10->1->2->3->4->5->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 2
Enter the data to be inserted in that node: 19
The modified list is
head->10->1->2->3->4->5->19->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 3
Enter the position: 3
Enter the number to be inserted in that node :30
The modified list is
head->10->1->2->3->30->4->5->19->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at firs 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 4
The modified list is
head->1->2->3->30->4->5->19->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 5
The modified list is
head->1->2->3->30->4->5->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 6
Enter the position: 30
The modified list is
head->1->2->3->4->5->null
do you want to continue?
1. yes
2.no
2
RESULT:
Thus, the C program is executed successfully.
Ex no:
Date:
LINKED LIST IMPLEMENTATION OF DOUBLY LINKED LISTS
AIM:
To write a c program to execute the doubly linked lists
ALGORITHM:
1. Create a structure LINKED_LIST with a data element and two pointers previous and next of
type LINKED_LIST and redefine the structure LINKED_LIST with NODE for easy usage.
2. Create a header node and last node.
3. Accept the user choice Insert, Delete, Reverse and Display.
4. If the Choice is Insert
ii. Read the data.
iii. Find the position in the list for the new data.
iv. Allocate space for new node and store new data in it.
v. Insert the new node in the correct position by updating pointers.
5. If the Choice is Delete
a. Read the data to be deleted from the list.
b. Remove the node containing data by updating the pointers.
6. If the Choice is Reverse
Reverse the list using last node and display the elements.
7. If the choice is Display
Traverse the list and display the elements.
8. Stop
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
struct dnode
{
int data;
struct dnode *prev;
struct dnode *next;
}*head=null;
int num,q,i=2;
void create()
{
struct dnode *temp,*temp1;
head=malloc(sizeof(struct dnode));
printf("Node:1\n");
scanf("%d",&num);
head->data=num;
head->prev=null;
head->next=null;
temp1=head;
do
{
printf("Node:%d",i);
scanf("%d",&num);
temp1->next=malloc(sizeof(struct dnode));
temp=temp1->next;
temp->data=num;
temp->prev=temp1;
temp1=temp1->next;
printf("Continue?\n");
scanf("%d",&q);
i++;
}
while(q!=0);
temp->next=null;}
void insert_at_first()
{
struct dnode *temp;
temp=malloc(sizeof(struct dnode));
printf("Enter the data to be inserted in that node\n");
scanf("%d",&num);
temp->data=num;
head->prev=temp;
temp->next=head;
temp->prev=null;
head=temp;
}
void insert_at_last()
{
struct dnode *temp;
temp=head;
printf("Enter the data to be inserted in that node\n");scanf("%d",&num);
while(temp->next!=null)
temp=temp->next;
temp->next=malloc(sizeof(struct dnode));
temp->next->data=num;
temp->next->prev=temp;
temp->next->next=null;
}
void insert_at_middle()
{
struct dnode *temp,*temp1;
temp=head;
printf("Enter the position\n");scanf("%d",&q);
printf("Enter the number to be inserted in that node\n");scanf("%d",&num);
while(temp->data!=q)
temp=temp->next;
temp1=malloc(sizeof(struct dnode));
temp1->data=num;
temp1->next=temp->next;
temp1->prev=temp;
temp->next=temp1;
temp1->next->prev=temp1;
}
void delete_at_first()
{
struct dnode *temp;
temp=head;
head=head->next;
free(temp);
head->prev=null;
}
void delete_at_last()
{
struct dnode *temp;
temp=head;
while(temp->next!=null)
temp=temp->next;
temp->prev->next=null;
free(temp);
}
void delete_at_middle()
{
struct dnode *temp,*temp1;
temp=head;
printf("Enter the position\n");scanf("%d",&q);
while(temp->data!=q)
temp=temp->next;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
void display()
{
struct dnode *temp;
temp=head;
printf("head->");
while(temp!=null)
{
printf("%d->",temp->data); temp=temp->next;
}
printf("null");
}
void main()
{
int ch,c;
clrscr();
create();
do
{
printf("What operation do you want to do? \n");
printf("1.Insert at first\n2.Insert at last\n3.Insert at middle\n4.delete at first\n5.delete at last\n6.delete
at middle\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_at_first();
break;
case 2:
insert_at_last();
break;
case 3:
insert_at_middle();
break;
case 4:
delete_at_first();
break;
case 5:
delete_at_last();
break;
case 6:
delete_at_middle();
break;
default:
printf("Wrong choice");
}
printf("The modified list is\n");
display();
printf("\ndo you want to continue?\n1.yes\n2.no\n");
scanf("%d",&c);
}
while(c!=2);
getch();
}
OUTPUT:
Node:1 1
Node:2 2
Continue?
1
Node:3 3
Continue?
1
Node:4 4
Continue?
1
Node:5 5
Continue?
0
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
1
Enter the data to be inserted in that node
10
The modified list is
head->10->1->2->3->4->5->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
2
Enter the data to be inserted in that node
19
The modified list is
head->10->1->2->3->4->5->19->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middl 4.delete at first 5.delete at last 6.delete at middle: 3
Enter the position: 3
Enter the number to be inserted in that node: 30
The modified list is
head->10->1->2->3->30->4->5->19->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 4
The modified list is
head->1->2->3->30->4->5->19->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 5
The modified list is
head->1->2->3->30->4->5->null
do you want to continue?
1. yes
2. no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle: 6
Enter the position: 30
The modified list is
head->1->2->3->4->5->null
do you want to continue?
1. yes
2.no
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
AIM:
ALGORITHM:
Addition:
Step 1: Read the coefficient and power of the polynomial equation one and assign it's elements into a
linked list.
step 2: Read the coefficient and power of the polynomial equation two and assign it's elements into a
linked list.
step 3: Sorting the first polynomial in power order wise.
Compare the first node's power with next nodes power, if second node's power is greater than
first Node then swap. Repeat the process until we get the proper order.
Step 4: Sorting the second polynomial in power order wise.
Compare the first node's power with next nodes power, if second node's power is greater than
first Node then swap. Repeat the process until we get the proper order.
Step 5 : Add the two polynomial.
If power part of the two list is equal than add the coefficient
Multiplication
1. Input the multiplicand and multiplier and multiplier
2. Set both the polynomial in descending order of the coefficient
3. Multiply each node of multiplicand with each node of the multiplier (multiplication of the
coefficient part and addition of the exponent part) and add them into a newly formed linked
list in descending order
4. Coefficients having the same exponent value are added up with each other in the list and no
two nodes have the same exponent value.
5. Then the product is to be displayed in a proper way in the form of ax^n+bx^n-1+…
PROGRAM:
#include <stdio.h>
//#include <conio.h>
#include <malloc.h>
struct node
{
int coeff;
int pow;
struct node *next;
};
struct node *start1 = NULL;
struct node *start2 = NULL;
struct node *start3 = NULL;
struct node *start4 = NULL;
struct node *start5 = NULL;
struct node *last3 = NULL;
struct node *create_poly(struct node *);
struct node *display_poly(struct node *);
struct node *add_poly(struct node *, struct node *, struct node *);
struct node *sub_poly(struct node *, struct node *, struct node *);
struct node *mul_poly(struct node *, struct node *, struct node *);
struct node *add_node(struct node *, int, int);
int main()
{
int option;
//clrscr();
do
{
printf("\n******* MAIN MENU ****“**");
printf("\n 1. Enter t”e first po“ynomial");
printf("\n 2. Display”the first “olynomial");
printf("\n 3. Enter t”e second p“lynomial");
printf("\n 4. Display”thesecond“polynomial");
printf("\n 5. Add the”polynomial“");
printf("\n 6. Display”the result“);
printf("\n 7. Subtrac” the polyn“mials");
printf("\n 8. Display”the result“);
printf("\n 9. Multipl” the polyn“mials");
printf("\n 10. Displa” the resul“");
printf("\n 11. EXIT")”
printf("\“\n Enter yo”r option :“");
scanf("%d", &option);“switch(op“io”)
{
case 1: start1 = create_poly(start1);
break;
case 2: start1 = display_poly(start1);
break;
case 3: start2 = create_poly(start2);
break;
case 4: start2 = display_poly(start2);
break;
case 5: start3 = add_poly(start1, start2, start3);
break;
case 6: start3 = display_poly(start3);
break;
case 7: start4 = sub_poly(start1, start2, start4);
break;
case 8: start4 = display_poly(start4);
break;
case 9: start5 = mul_poly(start1,start2,start5);
break;
case 10:start5 = display_poly(start5);
break;
}
}while(option!=11);
getch();
return 0;
}
struct node *create_poly(struct node *start)
{
struct node *new_node, *ptr;
int n, c;
printf("\n Enter the coeff : ");“scanf("%d", &n);
prin“f("\t Ent“r”ts power : ");“scanf("%d", &c);
whil“(n != -1)“{
if(start==NULL)
{
new_node = (struct node *)malloc(sizeof(struct node));
new_node ->coeff = n;
new_node -> pow = c;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
new_node = (struct node *)malloc(sizeof(struct node));
new_node ->coeff = n;
new_node -> pow = c;
new_node -> next = NULL;
ptr -> next = new_node;
}
printf("\n Enter the coeff : ");“scanf("%d", &n);
if(n“== -1)
br“ak”
printf("\t Enter its power : ");“scanf("%d", &c);
}
re“urn start“
}”struct node *display_poly(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\n%d x^%d\t", ptr ->coe“f, ptr ->p”w);
ptr = ptr -> next;
}
return start;
}
struct node *add_poly(struct node *start1, struct node *start2, struct node *start3)
{
struct node *ptr1, *ptr2;
int sum_coeff, c;
ptr1 = start1, ptr2 = start2;
while(ptr1 != NULL && ptr2 != NULL)
{
if(ptr1 -> pow == ptr2 -> pow)
{
sum_coeff = ptr1 ->coeff + ptr2 ->coeff;
start3 = add_node(start3, sum_coeff, ptr1 -> pow);
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
else if(ptr1 -> pow > ptr2 -> pow)
{
start3 = add_node(start3, ptr1 ->coeff, ptr1 -> pow);
ptr1 = ptr1 -> next;
}
else if(ptr1 -> pow < ptr2 -> pow)
{
start3 = add_node(start3, ptr2 ->coeff, ptr2 -> pow);
ptr2 = ptr2 -> next;
}
}
if(ptr1 == NULL)
{
while(ptr2 != NULL)
{
start3 = add_node(start3, ptr2 ->coeff, ptr2 -> pow);
ptr2 = ptr2 -> next;
}
}
if(ptr2 == NULL)
{
while(ptr1 != NULL)
{
start3 = add_node(start3, ptr1 ->coeff, ptr1 -> pow);
ptr1 = ptr1 -> next;
}
}
return start3;
}
struct node *sub_poly(struct node *start1, struct node *start2, struct node *start4)
{
struct node *ptr1, *ptr2;
int sub_coeff, c;
ptr1 = start1, ptr2 = start2;
do
{
if(ptr1 -> pow == ptr2 -> pow)
{
sub_coeff = ptr1 ->coeff - ptr2 ->coeff;
start4 =–add_node(start4, sub_coeff, ptr1 -> pow);
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}
else if(ptr1 -> pow > ptr2 -> pow)
{
start4 = add_node(start4, ptr1 ->coeff, ptr1 -> pow);
ptr1 = ptr1 -> next;
}
else if(ptr1 -> pow < ptr2 -> pow)
{
start4 = add_node(start4, ptr2 ->coeff, ptr2 -> pow);
ptr2 = ptr2 -> next;
}
}while(ptr1 != NULL || ptr2 != NULL);
if(ptr1 == NULL)
{
while(ptr2 != NULL)
{
start4 = add_node(start4, ptr2 ->coeff, ptr2 -> pow);
ptr2 = ptr2 -> next;
}
}
if(ptr2 == NULL)
{
while(ptr1 != NULL)
{
start4 = add_node(start4, ptr1 ->coeff, ptr1 -> pow);
ptr1 = ptr1 -> next;
}
}
return start4;
}
struct node *mul_poly(struct node *start1, struct node *start2, struct node *start5)
{
struct node *ptr1, *ptr2,*ptr3,*ptr4,*temp;
ptr1 = start1, ptr2 = start2;
while(ptr1 != NULL)
{
while(ptr2 != NULL)
{
int c,p;
c=ptr1->coeff * ptr2->coeff;
p=ptr1->pow + ptr2->pow;
start5 = add_node(start5,c,p);
ptr2=ptr2->next;
}
ptr2=start2;
ptr1=ptr1->next;
}
//to remove duplicate power
ptr3=start5;
while((ptr3 != NULL)&& (ptr3->next != NULL))
{
ptr4=ptr3;
while(ptr4->next != NULL)
{
if (ptr3->pow == ptr4->next->pow)
{
ptr3->coeff = ptr3->coeff + ptr4->next->coeff;
temp=ptr4->next;
ptr4->next = ptr4->next->next;
free(temp);
}
else
ptr4 = ptr4->next;
}
ptr3 = ptr3->next;
}
return start5;
}
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
ARRAY IMPLEMENTATION OF STACK ADT
AIM: To write a C program to implement Stack ADT using arrays
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 30 // Altering this value changes size of stack created
int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);
int main() {
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2:
val = pop(st);
if(val != -1)
printf("\n The value deleted from stack is: %d", val);
break;
case 3:
val = peek(st);
if(val != -1)
printf("\n The value stored at top of stack is: %d", val);
break;
case 4:
display(st);
break;
}
}while(option != 5);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n"); // Added for formatting purposes
}
}
int peek(int st[])
{
if(top == -1)
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}
OUTPUT
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 1
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 2
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 3
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 4
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 2
The value deleted from stack is: 4
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 4
3
2
1
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option:5
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
LINKED LIST IMPLEMENTATION OF STACK ADT
AIM: To write a C program to implement Stack ADT using linked list implementation
Algorithm :
• Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
• Step 2 - Define a 'Node' structure with two members data and next.
• Step 3 - Define a Node pointer 'top' and set it to NULL.
• Step 4 - Implement the main method by displaying Menu with list of operations and make
suitable function calls in the main method.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
struct stack
{
int data;
struct stack *next;
};
struct stack *top = NULL;
struct stack *push(struct stack *, int);
struct stack *display(struct stack *);
struct stack *pop(struct stack *);
int peek(struct stack *);
int main() {
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
top = push(top, val);
break;
case 2:
top = pop(top);
break;
case 3:
val = peek(top);
if (val != -1)
printf("\n The value at the top of stack is: %d", val);
else
printf("\n STACK IS EMPTY");
break;
case 4:
top = display(top);
break;
}
}while(option != 5);
return 0;
}
struct stack *push(struct stack *top, int val)
{
struct stack *ptr;
ptr = (struct stack*)malloc(sizeof(struct stack));
ptr -> data = val;
if(top == NULL)
{
ptr -> next = NULL;
top = ptr;
}
else
{
ptr -> next = top;
top = ptr;
}
return top;
}
struct stack *display(struct stack *top)
{
struct stack *ptr;
ptr = top;
if(top == NULL)
printf("\n STACK IS EMPTY");
else
{
while(ptr != NULL)
{
printf("\n %d", ptr -> data);
ptr = ptr -> next;
}
}
return top;
}
struct stack *pop(struct stack *top)
{
struct stack *ptr;
ptr = top;
if(top == NULL)
printf("\n STACK UNDERFLOW");
else
{
top = top -> next;
printf("\n The value being deleted is: %d", ptr -> data);
free(ptr);
}
return top;
}
int peek(struct stack *top)
{
if(top==NULL)
return -1;
else
return top ->data;
}
OUTPUT:
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 100
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 200
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 300
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 2
The value being deleted is: 300
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 3
The value at the top of stack is: 200
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 4
200
100
*****MAIN MENU*****
1. PUSH
2. POP
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 5
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
ARRAY IMPLEMENTATION OF QUEUE ADT
AIM: To write a C program to implement Queue ADT using arrays.
ALGORITHM:
Queue Operations using Array
• Step 1 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
• Step 2 - Declare all the user defined functions which are used in queue implementation.
• Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE])
• Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -
1, rear = -1)
• Step 5 - Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on queue.
enQueue(value) - Inserting value into the queue
• Step 1 - Check whether queue is FULL. (rear == SIZE-1)
• Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
• Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and
set queue[rear] = value.
deQueue() - Deleting a value from the Queue
• Step 1 - Check whether queue is EMPTY. (front == rear)
• Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
• Step 3 - If it is NOT EMPTY, then increment the front value by one (front ++). Then
display queue[front] as deleted element. Then check whether both front and rear are equal
(front == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
display() - Displays the elements of a Queue
• Step 1 - Check whether queue is EMPTY. (front == rear)
• Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
• Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
• Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i'
value reaches to rear (i <= rear)
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define MAX 10 // Changing this value will change length of array
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n ***** MAIN MENU *****");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Peek");
printf("\n 4. Display the queue");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf("\n The number deleted is : %d", val);
break;
case 3:
val = peek();
if (val != -1)
printf("\n The first value in queue is : %d", val);
break;
case 4:
display();
break;
}
}while(option != 5);
getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue : ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
int peek()
{
if(front==-1 || front>rear)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front;i<= rear;i++)
printf("\t %d", queue[i]);
}
}
OUTPUT:
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 1
Enter the number to be inserted in the queue : 1
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 1
Enter the number to be inserted in the queue : 2
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 1
Enter the number to be inserted in the queue : 3
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 1
Enter the number to be inserted in the queue : 4
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 2
The number deleted is : 1
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 1
Enter the number to be inserted in the queue : 5
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 2
The number deleted is : 2
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 3
The first value in queue is : 3
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 4
3 4 5
***** MAIN MENU *****
1. Insert an element
2. Delete an element
3. Peek
4. Display the queue
5. EXIT
Enter your option : 5
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
LINKED LIST IMPLEMENTATION OF QUEUE ADT
AIM: To write a C program to implement Queue ADT using linked list implementation
ALGORITHM:
Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4 - Implement the main method by displaying Menu of list of operations and make suitable
function calls in the main method to perform user selected operation.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the
first node is always pointed by 'front'.
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}
OUTPUT:
:: Queue Implementation using Linked List ::
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 100
Insertion is Success!!!
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 200
Insertion is Success!!!
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 300
Insertion is Success!!!
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
100--->200--->300--->NULL
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted element: 100
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
200--->300--->NULL
****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 4
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
ARRAY IMPLEMENTATION OF CIRCULAR QUEUE ADT
ALGORITHM:
1. Queue Initialization:
o Define the maximum size SIZE for the queue.
o Declare the queue array and initialize front and rear pointers to -1.
2. enQueue (Insert):
o Step 1: Check if the queue is full. A circular queue is full if (rear + 1) % SIZE == front.
o Step 2: If full, display "Queue is FULL".
o Step 3: If not full, increment rear circularly using (rear + 1) % SIZE and insert the new
element into queue[rear].
o Step 4: If the queue was empty (i.e., front == -1), set front = 0.
3. deQueue (Delete):
o Step 1: Check if the queue is empty, i.e., if front == -1.
o Step 2: If empty, display "Queue is EMPTY".
o Step 3: If not empty, store the value of queue[front], increment front circularly using
(front + 1) % SIZE, and return the value.
o Step 4: If the queue becomes empty after deletion (i.e., front == rear), reset front and rear
to -1.
4. displayQueue (Display):
o Step 1: Check if the queue is empty, i.e., front == -1.
o Step 2: If empty, display "Queue is EMPTY".
o Step 3: If not empty, print all elements from front to rear circularly.
PROGRAM:
#include <stdio.h>
#define SIZE 5 // Define the maximum size of the queue
int queue[SIZE];
int front = -1, rear = -1;
// Main function
int main() {
int option, value;
do {
printf("\n***** MENU *****");
printf("\n1. Enqueue");
printf("\n2. Dequeue");
printf("\n3. Display Queue");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &option);
switch (option) {
case 1:
printf("\nEnter value to insert: ");
scanf("%d", &value);
enQueue(value);
break;
case 2:
value = deQueue();
if (value != -1) {
printf("\nDequeued value is: %d\n", value);
}
break;
case 3:
displayQueue();
break;
case 4:
printf("\nExiting...\n");
break;
default:
printf("\nInvalid choice! Try again.\n");
}
} while (option != 4);
return 0;
}
OUTPUT:
***** MENU *****
1. Enqueue
2. Dequeue
3. Display Queue
4. Exit
Enter your choice: 1
Enter value to insert: 10
Inserted 10 into the queue.
Exiting...
RESULT:
The C program to implement a circular queue using arrays was successfully executed.
EX. NO.
Date:
ALGORITHM:
1. Node Structure:
o Create a structure for the queue node containing data and a pointer to the next node.
2. Queue Structure:
o Create a structure for the circular queue containing pointers to the front and rear nodes.
3. enQueue (Insert):
o Step 1: Create a new node with the given value.
o Step 2: If the queue is empty (front is NULL), set both front and rear to the new node and
point it to itself (circular).
o Step 3: If the queue is not empty, link the new node to the rear's next and update the rear
to the new node, linking the new node to the front.
4. deQueue (Delete):
o Step 1: Check if the queue is empty (front is NULL).
o Step 2: If empty, display an error message.
o Step 3: If not empty, retrieve the front node's value. If the front and rear nodes are the
same (only one element), set both to NULL. Otherwise, update front to the next node and
link rear to the new front.
5. displayQueue (Display):
o Step 1: Check if the queue is empty.
o Step 2: If empty, display a message.
o Step 3: If not empty, traverse from front to rear, printing each node's data.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct CircularQueue* queue = createQueue();
int option, value;
do {
switch (option) {
case 1:
printf("\nEnter value to insert: ");
scanf("%d", &value);
enQueue(queue, value);
break;
case 2:
value = deQueue(queue);
if (value != -1) {
printf("\nDequeued value is: %d\n", value);
}
break;
case 3:
displayQueue(queue);
break;
case 4:
printf("\nExiting...\n");
break;
default:
printf("\nInvalid choice! Try again.\n");
}
} while (option != 4);
return 0;
}
OUTPUT:
4. Exit
Enter your choice: 1
Exiting...
RESULT:
The C program to implement a circular queue using linked lists was successfully executed.
EX. NO.
Date:
INFIX TO POSTFIX USING STACK
AIM:To write a C program to convert given infix to postfix expression
ALGORITHM:
1. Read all the symbols one by one from left to right in the given Infix Expression.
2. If the reading symbol is operand, then directly print it to the result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective
left parenthesis is popped and print each popped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first
pop the operators which are already on the stack that have higher or equal precedence than
current operator and print them to the result.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
char st[MAX];
int top=-1;
void push(char st[], char);
char pop(char st[]);
void InfixtoPostfix(char source[], char target[]);
int getPriority(char);
int main()
{
char infix[100], postfix[100];
clrscr();
printf("\n Enter any infix expression : ");
gets(infix);
strcpy(postfix, "");
InfixtoPostfix(infix, postfix);
printf("\n The corresponding postfix expression is : ");
puts(postfix);
getch();
return 0;
}
void InfixtoPostfix(char source[], char target[])
{
int i=0, j=0;
char temp;
//strcpy(target, "");
while(source[i]!='\0')
{
if(source[i]=='(')
{
push(st, source[i]);
i++;
}
else if(source[i] == ')')
{
while((top!=-1) && (st[top]!='('))
{
target[j] = pop(st);
j++;
}
if(top==-1)
{
printf("\n INCORRECT EXPRESSION");
exit(1);
}
temp = pop(st);//remove left parenthesis
i++;
}
else if(isdigit(source[i]) || isalpha(source[i]))
{
target[j] = source[i];
j++;
i++;
}
else if (source[i] == '+' || source[i] == '–' || source[i] == '*' ||
source[i] == '/' || source[i] == '%')
{
while( (top!=-1) && (st[top]!= '(') && (getPriority(st[top])
>= getPriority(source[i])))
{
target[j] = pop(st);
j++;
}
push(st, source[i]);
i++;
}
else
{
printf("\n INCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=-1))
{
target[j] = pop(st);
j++;
}
target[j]='\0';
}
int getPriority(char op)
{
if(op == '/' || op == '*' || op=='%')
return 2;
else if(op == '+' || op == '-')
return 1;
else
return -1;
}
void push(char st[], char val)
{
if(top==MAX-1)
printf("\n STACK OVERFLOW");
else
{
top++;
st[top]=val;
}
}
char pop(char st[])
{
char val=' ';
if(top==-1)
printf("\n STACK UNDERFLOW");
else
{
val=st[top];
top=top-1;
}
return val;
}
OUTPUT:
Enter any infix expression : a*b*c
RESULT:
Thus, the C program is executed successfully.
EX.NO.
Date:
IMPLEMENTATION OF BINARY SEARCH TREE
AIM:To write a C program to implement a binary search tree.
ALGORITHM:
Insertion of a key
Step 1: Create a newNode with given value and set its left and right to NULL.
Step 2: Check whether tree is Empty.
Step 3: If the tree is Empty, then set set root to newNode.
Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than
the node (here it is root node).
Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode is
larger than the node, then move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (ie., reach to NULL).
Step 7: After reaching a leaf node, then insert the newNode as left child if newNode is smaller
else insert it as right child.
Searching a key
Step 1: Read the search element from the user
Step 2: Compare, the search element with the value of root node in the tree.
Step 3: If both are matching, then display "Given node found!!!" and terminate the function
Step 4: If both are not matching, then check whether search element is smaller or larger than that
node value.
Step 5: If search element is smaller, then continue the search process in left subtree.
Step 6: If search element is larger, then continue the search process in right subtree.
Step 7: Repeat the same until we found exact element or we completed with a leaf node
Step 8: If we reach to the node with search value, then display "Element is found" and terminate
the function.
Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found"
and terminate the function
Minimum and maximum
An element in a binary search tree whose key is a minimum can always be found by following left
child pointers from the root until a NIL is encountered
Delete
When we delete a node, three possibilities arise.
1) Node to be deleted is leaf: Simply remove from the tree.
2) Node to be deleted has only one child: Copy the child to the node and delete the child
3) Node to be deleted has two children: Find inorder successor of the node. Copy contents of the
inorder successor to the node and delete the inorder successor. Note that inorder predecessor can
also be used.
Deleting a leaf
Step 1: Find the node to be deleted using search operation
Step 2: Delete the node using free function (If it is a leaf) and terminate the function
PROGRAM:
#include<stdio.h>
#include<conio.h>
typedef struct node *tree;
tree findmin(tree);
tree findmax(tree);
tree insert(int,tree);
tree del(int,tree);
void disp(tree);
struct node
{
int data;
tree left,right;
}*t=NULL;
void main()
{
int ch,n;
tree a;
clrscr();
printf("1.insert\n 2.delete\n 3.“indmax\n 4.findmin\n 5.disp\n 6.exit\n");
do
{
printf("ente” choice ");
scanf“"%d",&ch);
switch(ch)
{
case 1:printf("enter no to insert");
scanf("%d",&n);
t=insert(n,“);
break;
case 2:
printf("enter no to delete");
scanf("%d",&n);
t=del(n,t);
break;
case 3:a=findmax(t);
printf("%d",a->data);
break;
case 4:a=findmin(t);
printf("%d",a->data);
break;
case 5:disp(t);
break;
case 6:exit (0);
break;
default:printf("enter correct choice");
}
}while(ch!=6);
getch();
}
tree insert(int n,tree t)
{
if(t==NULL)
{
t=(tree)malloc(sizeof(struct node));
t->data=n;
t->left=t->right=NULL;
}
else if(n<t->data)
t->left=insert(n,t->left);
else if(n>t->data)
t->right=insert(n,t->right);
else
printf("already exist");
returnt;
}
tree del(int n,tree t)
{
tree p;
if(t==NULL)
printf("element not found");
else if(n<t->data)
t->left=del(n,t->left);
else if(n>t->data)
t->right=del(n,t->right);
else if(t->left&&t->right)
{
p=findmin(t->right);
t->data=p->data;
t- >right=del(t->data,t->right);
}
else
{
p=t;
if(t->left==NULL)
t=t->right;
else
t=t->left;
free(p);
}
return t;
}
void disp(tree t)
{
if(t!=NULL)
{
disp(t->left);
printf("%d\n",t->data);
disp(t“>rig”t);
}
}
tree findmin(tree t)
{
if(t==NULL)
return NULL;
else if(t->left==NULL)
return t;
else return findmin(t->left);
}
tree findmax(tree t)
{
if(t==NULL)
return NULL;
else if(t->right==NULL)
return t;
else
return findmax(t->right);
}
OUTPUT:
1. insert
2.delete
3.findmax
4.findmin
5.disp
6.exit
enter choice 1
enter no to insert 10
enter choice 1
enter no to insert 20
enter choice 1
enter no to insert 30
enter choice 1
enter no to insert 40
enter choice 1
enter no to insert 50
enter choice 5 : 10 20 30 40 50
enter choice 2
enter no to delete 30
enter choice 5 : 10 20 40 50
enter choice 3
50
enter choice 4
10
enter choice 6
RESULT:
Thus, the C program is executed successfully.
EX.NO : IMPLEMENTATION OF AVL TREE
Date:
ALGORITHM:
LL Rotation
The new node is inserted to the left sub-tree of left child of critical node.
RR Rotation
The new node is inserted to the right sub-tree of the right child of the critical node.
LR Rotation
The new node is inserted to the right sub-tree of the left child of the critical node.
RL Rotation
The new node is inserted to the left sub-tree of the right child of the critical node.
Source code:
#include<stdio.h>
#include<conio.h>
typedef struct node *pos;
struct node
{
int data,h;
pos left,right;
}*t=NULL;
int height(pos p)
{
if(p==NULL)
return -1;
else
return p->h;
}
int max(int x,int y)
{
return (x>y)?x:y;
}
pos insert(int x,pos t)
{
if(t==NULL)
{
t=(pos)malloc(sizeof(struct node));
t->data=x;
t->h=0;
t->left=t->right=NULL;
}
else if(x<t->data)
{
t->left=insert(x,t->left);
if(height(t->left)-height(t->right)==2)
{
if(x<t->left->data)
t=SRWL(t);
else
t=DRWL(t);
}}
else if(x>t->data)
{t->right=insert(x,t->right);
if(height(t->right)-height(t->left)==2)
{if(x>t->right->data)
t=SRWR(t);
else
t=DRWR(t);
}}
else
printf("Element is in the tree already");
t->h=1+max(height(t->left),height(t->right));
return t;
}
pos SRWL(pos k2)
{pos k1;
printf("\nSingle rotation with left %d\n",k2->data);
k1=k2->left;
k2->left=k1->right;
k1->right=k2;
k2->h=1+max(height(k2->left),height(k2->right));
k1->h=1+max(height(k1->left),k2->h);
return k1;
}
pos SRWR(pos k1)
{pos k2;
printf("\nSingle rotation with right %d\n",k1->data);
k2=k1->right;
k1->right=k2->left;
k2->left=k1;
k1->h=1+max(height(k1->left),height(k1->right));
k2->h=1+max(k1->h,height(k2->right));
return k2;
}
pos DRWL(pos k3){
printf("\nLeft right double rotation of %d\n",k3->data);
k3->left=SRWR(k3->left);
return SRWL(k3);}
pos DRWR(pos k1)
{printf("\nRight left double rotation of %d\n",k1->data);
k1->right=SRWL(k1->right);
return SRWR(k1);
}
void display(pos t){
if(t!=NULL)
{
display(t->left);
printf("\n %d\t%d\n",t->data,t->h);
display(t->right);
}}
pos deleteavl(int x,pos t)
{struct node *d;
if(t==NULL)
printf("\n Element not found");
else if(x<t->data)
t->left=deleteavl(x,t->left);
else if(x>t->data)
t->right=deleteavl(x,t->right);
else if((t->left==NULL)&&(t->right==NULL))
{
d=t;
t=NULL;
free(d);
printf("\nElement deleted!");
}
else if(t->left==NULL)
{
d=t;
t=t->right;
free(d);
printf("\n Element deleted!");
}
else if(t->right==NULL)
{
d=t;
t=t->left;
free(d);
printf("\nElement deleted!");
}
return t;
}
pos findmin(pos t){
if(t==NULL)
return NULL;
else if(t->left==NULL)
return t;
else
return findmin(t->left);}
pos findmax(pos t)
{if(t!=NULL)
while(t->right!=NULL)
t=t->right;
return t;}
void main()
{int ch,n;
struct node *x,*y;
clrscr();
do
{printf("\n\n1.Insert 2.Display 3.Delete 4.FindMin");
printf("\n\n5.FindMax \t6.Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the data to insert: ");
scanf("%d",&n);
t=insert(n,t);
break;
case 2:
printf("\nElement Height");
display(t);
break;
case 3:
printf("\nEnter the data to be deleted: ");
scanf("%d",&n);
t=deleteavl(n,t);
break;
case 4:
x=findmin(t);
printf("\nThe minimum element is %d",x->data);
break;
case 5:
y=findmax(t);
printf("\nThe maximum element is %d",y->data);
break;
case 6:
exit(0);
break;
default:
printf("\nEnter correct choice");
}
}while(ch!=0);
getch();
}
OUTPUT: AVL TREE
1. Insert 2. Display 3. Delete 4. FindMin 5. FindMax 6. Exit
Enter your choice: 1
Enter the data to insert: 33
1. Insert 2. Display 3. Delete 4. FindMin 5. FindMax 6. Exit
Enter your choice: 1
Enter the data to insert: 67
1. Insert 2. Display 3. Delete 4. FindMin 5. FindMax 6. Exit
Enter your choice: 1
Enter the data to insert: 78
Single rotation with right: 33
1. Insert 2. Display 3. Delete 4. FindMin 5. FindMax 6. Exit
Enter your choice: 2
Element Height
33 0
67 1
78 0
1. Insert 2. Display 3. Delete 4. FindMin 5. FindMax 6. Exit
Enter your choice: 4
The minimum element is 33.
RESULT:
Thus, the C program is executed successfully.
EX. NO.
Date:
IMPLEMENTATION OF HEAPS USING PRIORITY QUEUES
ALGORITHM:
Insertion
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
DeleteMin
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 20
int del();
void insert(int);
void disp();
int a[max],size=0;
void main()
{
int ch,n;
clrscr();
printf("1.insert 2.deletemin 3.findmin 4.disp 5.exit\n");
do
{
printf("enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter no to insert");
scanf("%d",&n);
insert(n);
break;
case 2:
n=del();
printf("deleted element is %d",n);
break;
case 3:
n=findmin();
printf("minimum element is %d",n);
break;
case 4:
disp();
break;
case 5:
exit(0);
}
}while(ch!=5);
getch();
}
void insert(int n)
{
int i;
a[0]=-1;
if(size==max)
printf("queue is full");
else
{
for(i=++size;a[i/2]>n;i=i/2)
a[i]=a[i/2];
a[i]=n;
}
}
int del()
{
int i,c,min,last;
min=a[1];
last=a[size--];
for(i=1;i*2<=size;i=c)
{
c=i*2;
if(c!=size&&a[c+1]<a[c])
c++;
if(last>a[c])
a[i]=a[c];
else
break;
}
a[i]=last;
return min;
}
void disp()
{
int i;
for(i=1;i<=size;i++)
{
printf("%d\n",a[i]);
}
}
int findmin()
{
if(size!=0)
return a[1];
else
{
printf("priority queue is empty");
exit(0);
return 0;
}
}
OUTPUT:
1.insert
2.deletemin
3.findmin
4.disp
5.exit
enter choice 1
enter no to insert 10
enter choice1
enter no to insert 20
enter choice 1
enter no to insert 30
enter choice 1
enter no to insert 40
enter choice 1
enter no to insert 50
enter choice4
10
20
30
40
50
enter choice 2
deleted element is 10
enter choice 3
minimum element is 20
enter choice 4
20
40
30
50
enter choice 5
RESULT:
Thus, the C program is executed successfully.
EX.NO.
Date:
1. i) Select the start or source vertex and mark its distance value(dv) and path vertex(pv)
as 0.
ii) Mark it as known vertex.
2. i) Mark the distance value(dv) and path vertex(pv) of other vertices as infinity α and
0 respectively.
ii) Find the adjacent of Known vertices.
3. i) If any of adjacent vertices is already known, then no need to calculate the dv and pv.
ii) Otherwise calculate the distance value for unknown adjacent vertices by using the
formula
dw=dv+c(v,w) (where c(v,w) is the cost on edge)
4. Among the unknown vertices select vertex which has shortest distance value(dv) and
mark it as the known vertex.
5. Repeat from the step 2 to step 4 until all the vertices are marked as the known vertices.
PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#define max 4
#define INFINITE 1000
int allknown( int *known)
{
int i;
for(i=0;i<max;i++)
if(known[i]==0)
return 0;
return 1;
}
void shortpath(int cost[][max],int *pv,int *dv)
{
int known[max]={0};
int current=0,i,k,dc,smalldist,dw;
for(i=0;i<max;i++)
dv[i]=INFINITE;
known[current]=1;
dv[0]=0;
current=0;
while(allknown(known)!=1)
{
smalldist=INFINITE;
dc=dv[current];
for(i=0;i<max;i++)
{
if(known[i]==0)
{
dw=dc+cost[current][i];
if(dw<dv[i])
{
dv[i]=dw;
pv[i]=current;
}
if(dv[i]<smalldist)
{
smalldist=dv[i];
k=i;
}}}
current=k;
known[current]=1;
}}
void main(){
int cost[max][max]={{INFINITE,2,4,INFINITE},{2,INFINITE,1,5},{4,1,INFINITE,2},
{INFINITE,5,2,INFINITE}};
int pv[max]={0},i,dv[max];
clrscr();
shortpath(cost,pv,dv);
printf("Vertex\t dv \tpv\n");
for(i=0;i<max;i++)
{
printf(" %d \t %d \t%d\n",i,dv[i],pv[i]);
}
getch();
}
SAMPLE OUTPUT:
Vertex dv pv
0 0 0
1 2 0
2 3 1
3 5 2
RESULT:
Thus the Dijksrta’s algorithm was sucessfully implemented and the shortest path from source
vertex was found and verified.
EX.NO.
Date:
IMPLEMENTATION OF PRIMS ALGORITHM
AIM:
ALGORITHM:
1. Start with the empty spanning tree.
2. Maintain a set mst[] to keep track to vertices included in minimum spanning tree.
3. Assign a key value to all the vertices, (say key []) and initialize all the keys with +∞ (Infinity)
except the first vertex. (We will start with this vertex, for which key will be 0).
4. Key value in step 3 will be used in making decision that which next vertex and edge will be
included in the mst[]. We will pick the vertex which is not included in mst[] and has the
minimum key. So at the beginning the first vertex will be picked first.
5. Repeat the following steps until all vertices are processed
1. Pick the vertex u which is not in mst[] and has minimum key.
2. Add vertex u to mst[].
3. Loop over all the adjacent vertices of u
▪ For adjacent vertex v, if v is not in mst[] and edge u-v weight is less than the
key of vertex u, key[u] then update the key[u]= edge u-v weight.
4. Return mst[].
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#define infinity 9999
#define MAX 20
int G[MAX][MAX],spanning[MAX][MAX],n;
int prims();
int main()
{
int i,j,total_cost;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
total_cost=prims();
printf("\nspanning tree matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",spanning[i][j]);
}
printf("\n\nTotal cost of spanning tree=%d",total_cost);
return 0;
}
int prims()
{
int cost[MAX][MAX];
int u,v,min_dist,distance[MAX],from[MAX];
int visited[MAX],no_of_edges,i,min_cost,j;
//create cost[ ][ ] matrix,spanning[ ][ ]
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(G[i][j]==0)
cost[i][j]=infinity;
else
cost[i][j]=G[i][j];
spanning[i][j]=0;
}
//initialise visited[ ],distance[ ] and from[ ]
distance[0]=0;
visited[0]=1;
for(i=1;i<n;i++)
{
distance[i]=cost[0][i];
from[i]=0;
visited[i]=0;
}
min_cost=0; //cost of spanning tree
no_of_edges=n-1; //no. of edges to be added
while(no_of_edges>0)
{
//find the vertex at minimum distance from the tree
min_dist=infinity;
for(i=1;i<n;i++)
if(visited[i]==0&&distance[i]<min_dist)
{
v=i;
min_dist=distance[i];
}
u=from[v];
//insert the edge in spanning tree
spanning[u][v]=distance[v];
spanning[v][u]=distance[v];
no_of_edges--;
visited[v]=1;
//updated the distance[ ] array
for(i=1;i<n;i++)
if(visited[i]==0&&cost[i][v]<distance[i])
{
distance[i]=cost[i][v];
from[i]=v;
}
min_cost=min_cost+cost[u][v];
}
return(min_cost);
}
SAMPLE OUTPUT:
3 0 5 0 3 0
1 5 0 5 6 4
6 0 5 0 0 2
0 3 6 0 0 6
0 0 4 2 6 0
spanning tree matrix:
0 3 1 0 0 0
3 0 0 0 3 0
1 0 0 0 0 4
0 0 0 0 0 2
0 3 0 0 0 0
0 0 4 2 0 0
Tot al cost of sp anning tree=13
RESULT:-
Thus the implementation of Prim’s algorithm is successfully implemented.
EX.NO.
Date: IMPLEMENTATION OF BUBBLE SORT
AIM:
Algorithm:
#include <stdio.h>
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++) {
// Flag to track if any swap happened during this pass
int swapped = 0;
// Last i elements are already sorted, so inner loop goes till n-i-1
for (j = 0; j < n - i - 1; j++) {
// If the current element is greater than the next element, swap them
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1; // Set flag indicating that a swap occurred
}
}
// If no two elements were swapped in the inner loop, then the list is sorted
if (!swapped)
break;
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
bubbleSort(arr, n);
return 0;
}
OUTPUT:
Sorted array:
11
12
22
25
34
64
90
RESULT:
Thus, the program is successfully executed and verified.
EX.NO.
Date: IMPLEMENTATION OF SHELL SORT
AIM:
ALGORITHM:
1. Initialize the Array. Start with an unsorted array of size n.
2. Choose an Initial Gap. Set the initial gap to be n/2 (i.e., divide the array size by 2).
3. Loop Until Gap Becomes 0. Repeat the following steps, reducing the gap at each step (typically by
dividing the gap by 2 at each iteration), until the gap becomes 0.
4. For Each Gap Size. Perform Gapped Insertion Sort:
o For each element array[i] (starting from i = gap), compare it with the element that is gap
positions before it (array[i - gap]).
o If array[i] < array[i - gap], swap these two elements.
o Continue comparing and swapping elements that are gap positions apart, until the correct
position is found for the current element array[i].
5. Update the Gap. After processing all elements with the current gap, reduce the gap by half (i.e., gap =
gap / 2).
6. Repeat Steps 4 and 5 Until Gap Becomes 0. Continue the process of comparing and swapping
elements for progressively smaller gap sizes until the gap is 1 (i.e., standard insertion sort), and finally
stop when the gap becomes 0.
7. End the Algorithm. Once the gap is reduced to 0, the array is fully sorted.
PROGRAM:
#include <stdio.h>
// Shell sort
void shellSort(int array[], int n) {
// Rearrange elements at each n/2, n/4, n/8, ... intervals
for (int interval = n / 2; interval > 0; interval /= 2) {
for (int i = interval; i < n; i += 1) {
int temp = array[i];
int j;
for (j = i; j >= interval && array[j - interval] > temp; j -= interval) {
array[j] = array[j - interval];
}
array[j] = temp;
}
}
}
// Print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int data[] = {9, 8, 3, 7, 5, 6, 4, 1};
int size = sizeof(data) / sizeof(data[0]);
shellSort(data, size);
printf("Sorted array: \n");
printArray(data, size);
}
OUTPUT:
Sorted array:
13456789
RESULT:
Thus, the program is successfully executed and verified.
EX.NO.
Date: IMPLEMENTATION OF HEAP SORT
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child
// If left child is larger than root
if (left < n && a[left] > a[largest])
largest = left;
// If right child is larger than root
if (right < n && a[right] > a[largest])
largest = right;
// If root is not largest
if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
/*Function to implement the heap sort*/
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, i, 0);
}
}
/* function to print the array elements */
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
OUTPUT:
RESULT:
Thus, the program is successfully executed and verified.
Ex.No: HASHING-SEPARATE CHAINING
DATE:
AIM:
To write a c program to use the concept of separate chaining.
ALGORITHM:
1. Define the node (listnode)with a data element and pointer to the node
2. Define a structure (hash tbl) with tablesize and pointer to the pointer of the listnode
3. Allocate space for the hash table
4. Create a header node for the lists in the hash table
5. Insert new element into the hash table by applying hash function to the data and locate the
corresponding list.
a. If collision occurs the data goes to the same list at the end
6. To find an data apply the hash function and locate the list in the hash table
a. Locate the element in the identified list
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef int elementtype;
typedef struct listnode *position;
typedef struct hashtbl *hashtable;
typedef position list;
struct hashtbl
{
int tablesize;
list *thelist;
}*h=NULL;
struct listnode
{
elementtype element;
position next;
};
hashtable initializetable(int tablesize)
{
hashtable h;
int i;
h=malloc(sizeof(struct hashtbl));
if(h==NULL)
printf("out of space");
h->tablesize=tablesize;
h->thelist=malloc(sizeof(list)*h->tablesize);
if(h->thelist==NULL)
printf("out of space");
for(i=0;i<h->tablesize;i++)
{
h->thelist[i]=malloc(sizeof(struct listnode));
if(h->thelist[i]==NULL)
printf("out of space");
else
{
h->thelist[i]->next=NULL;
h->thelist[i]->element=(char)'a';
}}
return h;
}
position find(elementtype key,hashtable h)
{
position p;
list l;
l=h->thelist[hash(key,h->tablesize)];
p=l->next;
while(p!=NULL && p->element!=key)
p=p->next;
return p;
}
void insert(elementtype key,hashtable h)
{
int i;
position pos,newcell;
list l;
pos=find(key,h);
if(pos==NULL)
{
newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)
printf("out of space");
else
{
newcell->element=key;
newcell->next=NULL;
l=h->thelist[hash(key,h->tablesize)];
link:
if(l->next==NULL)
l->next=newcell;
else
{
l=l->next;
goto link;
}}}}
int hash(int k,int s)
{
int a;
a=k%s;
return a;
}
void display()
{
int i;
position p1;
for(i=0;i<h->tablesize;i++)
{
p1=h->thelist[i];
if(p1->next==NULL)
{
if(p1->element!='a')
printf("\n%d\n",p1->element);
}
else
{
while(p1!=NULL)
{
if(p1->element!='a')
printf("\n%d\n",p1->element);
p1=p1->next;
}}}}
void main()
{
int ch,q,n,tsize,i,key1,key2;
position m;
clrscr();
printf("1.insert\n2.position find\n3.display");
do
{
printf("\nenter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the tablesize:");
scanf("%d",&tsize);
h=initializetable(tsize);
printf("\ninsert........");
printf("\nenter the no of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nenter the key element:");
scanf("%d",&key1);
insert(key1,h);
}
break;
case 2:
printf("\nposition find.......");
printf("\nenter the element to be searched:");
scanf("%d",&key2);
m=find(key2,h);
if(m==NULL)
printf("\nelement not present");
else
printf("\nelement is present");
break;
case 3:
printf("\ndisplaying the elements.....");
display();
break;
default:
printf("\ninvalid choice");
}
printf("\nwant to continue?");
scanf("%d",&q);
}while(q!=0);
getch();
}
Result:
Ex.No: HASHING-OPEN ADDRESSING
DATE:
AIM:
To write a c program to use the concept of open addressing
ALGORITHM:
7. Define the node (listnode)with a data element and pointer to the node
8. Define a structure (hash table) with tablesize and pointer to the pointer of the listnode
9. Allocate space for the hash table
10. Create a header node for the lists in the hash table
11. Insert new element into the hash table by applying hash function to the data and locate the
corresponding list.
a. If collision occurs the data goes to the same list at the end
12. To find an data apply the hash function and locate the list in the hash table
a. Locate the element in the identified list
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef int elementtype;
typedef unsigned int index;
typedef index position;
typedef struct hashtbl *HashTable;
enum kindofentry {legitimate,Empty};
int ch;
struct Hashentry
{
elementtype element;
enum kindofentry info;
};
typedef struct Hashentry cell;
struct hashtbl
{
int Tablesize;
cell *Thecells;
};
int Hash(int key,int tab)
{
return key%tab;
}
int nextprime(int size)
{
int i;
if(size%2==0)
size++;
for(;;size+=2)
{
for(i=3;i*i<size;i+=2)
{
if(size%i==0)
goto c;
return size;
c:;
}}}
HashTable H,a;
HashTable initializetable(int t)
{
int i;
/*allocate table*/
H=malloc(sizeof(struct hashtbl));
if(H==NULL)
printf("outofspace");
H->Tablesize=nextprime(t);
/*allocate array of cells*/
H->Thecells=malloc(sizeof(struct Hashentry)*H->Tablesize);
if(H->Thecells==NULL)
printf("out ofspace");
for(i=0;i<H->Tablesize;i++)
{
H->Thecells[i].info=Empty;
H->Thecells[i].element=0;
}
return H;
}
position find1(elementtype key,HashTable H)
{
position currentpos;
int i;
i=1;
currentpos=Hash(key,H->Tablesize);
while((H->Thecells[currentpos].info!=Empty)&&(H->Thecells[currentpos].element!=key))
{
currentpos=((Hash(key,H->Tablesize)+i)%H->Tablesize);
i=i+1;
}
return currentpos;
}
position find2(elementtype key,HashTable H)
{
position currentpos;
int i;
i=1;
currentpos=Hash(key,H->Tablesize);
while((H->Thecells[currentpos].info!=Empty)&&(H->Thecells[currentpos].element!=key))
{
currentpos=((Hash(key,H->Tablesize)+(i*i))%H->Tablesize);
i=i+1;
}
return currentpos;
}
position find3(elementtype key,HashTable H)
{
position currentpos;
int i,h2;
i=1;
currentpos=Hash(key,H->Tablesize);
h2=(7-(key%7));
while((H->Thecells[currentpos].info!=Empty)&&(H->Thecells[currentpos].element!=key))
{
currentpos=((Hash(key,H->Tablesize)+(i*h2))%H->Tablesize);
i=i+1;
}
return currentpos;
}
void insert(elementtype key,HashTable H)
{
position pos;
switch(ch)
{
case 1:
pos=find1(key,H);
break;
case 2:
pos=find2(key,H);
break;
case 3:
pos=find3(key,H);
break;
default:
pos=find1(key,H);
}
if(H->Thecells[pos].info!=legitimate)
{
H->Thecells[pos].info=legitimate;
H->Thecells[pos].element=key;
}}
void display()
{
int i;
printf("__________THE HASH TABLE__________\n");
for(i=0;i<H->Tablesize;i++)
printf("data[%d]=%d\n",i,H->Thecells[i].element);
}
void main()
{
int s,a,q,c=0,k;
HashTable r;
clrscr();
printf("\nEnter the size : ");
scanf("%d",&s) ;
printf("Enter the number of elements to be entered :");
scanf("%d",&k);
if(s<k)
{
printf("\nSize is less than number of elements to be inserted ");
exit(0);
}
r=initializetable(s);
printf("Choose the collision control technique\n");
printf("1.Linear probing\n2.Quadratic probing\n3.Double hashing\n");
scanf("%d",&ch);
if(ch!=1&&ch!=2&&ch!=3)
printf("Wrong choice, default collision technique Linear probing executes\n");
printf("Enter the elements\n");
do
{
scanf("%d",&a);
insert(a,r);
c++;
}
while(c!=k);
display();
getch();
}
RESULT: