Uploaded by Ashiq

Ashiq DS

advertisement
ASHIQ M
1804021
EX.NO-01
DATE
IMPLEMENTATION OF LIST USING ARRAY
AIM
ALGORITHM
1
ASHIQ M
1804021
2
ASHIQ M
1804021
CODE
#include<stdio.h>
#define size 10
int a[size],i,j,n;
void create();
void display();
void insert();
void delete();
void update();
void main()
{
intch;
printf("---*********Linear List using Arrays***********---");
do
{
printf("\nEnter your Choice");
printf("\n 1. Create\t 2.Insert\t 3.Delete\t 4.Update\t 5.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
insert();
display();
break;
case 3:
delete();
break;
case 4:
update();
display();
break;
default:
printf("\nEnter the proper option");
exit();
}
}
while(ch !=0); } void create()
3
ASHIQ M
1804021
{
int i;
printf("Enter the number of elements in the array");
scanf("%d",&n);
printf("\nEnter the numbers");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display()
{
int i;
printf("\nThe elements in the array");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
}
void insert()
{
int x,pos;
printf("\nEnter The element to be inserted");
scanf("%d",&x);
printf("\nEnter the position in which the element to be inserted. Maximum size:10");
scanf("%d",&pos);
a[pos]=x;
}
void delete()
{
int pos,x;
printf("\nEnter the position in which the element to be deleted");
scanf("%d",&pos);
a[pos]=-1;
printf("\nThe elements after deleteion");
display();
4
ASHIQ M
1804021
}
Void update() //Function to modify the element
{
int x,pos;
printf("\nEnter the element to be modified in the array");
scanf("%d",&x);
printf(“\nEnter the position in which the element to be
modified”);
scanf(“%d”,&pos);
printf(“\n the array after modification is: “);
a[pos]=x ;
}
Output:
---*********Linear List using Arrays***********--Enter your Choice
1 .create 2 .Insert 3.Delete 4.Update 5.Exit 1
Enter the number of elements in the array 5
Enter the numbers 1
2
3
4
5
The elements in the array 1 2
3
4
5
0
0
0
Enter your Choice
1.Create 2.Insert 3.Delete 4.Update 5.Exit 2
Enter the element to be inserted 7
Enter the position in which the element to be inserted. Maximum size:10 5
1
2
3
4
5
7
0
0
0
0
Enter your Choice
1. Create 2.Insert 3.Delete 4.Update 5.Exit 3
Enter the position in which the element to be deleted 5
The elements after deletion 1 2
3
4
5
-1
0
0
0
Enter your Choice
1.Create 2.Insert 3.Delete 4.Update 5.Exit 4
Enter the element to be modified in the array 6
Enter the position in which the element to be modified 5
5
0
ASHIQ M
1804021
The array after modification is 1
2
3
0
Enter your Choice
1.Create 2.Insert 3.Delete 4.Update 5.Exit 5
/* gets out from output screen*/
4
5
6
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
0
0
0
MAX
MARKS
MARKS OBTAINED
5
6
7
2
20
RESULT
The C program to implement linear list using arrays has been written and executed successfully.
6
ASHIQ M
1804021
EX NO-02
DATE
LINEAR AND BINARY SEARCH USING ARRAY
AIM
ALGORITHM
7
ASHIQ M
1804021
LINEAR SEARCH
CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
OUTPUT
How many elements ? 4
Enter array elements:
6891
Enter the element to search:9
Element found at index:2
How many elements? 4
Enter array elements:
6891
Enter the elements to search 5
Element not found
8
ASHIQ M
1804021
BINARY SEARCH
CODE
#include<stdio.h>
int main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");
for(i=0;i<n;++i)
{
scanf("%d",&arr[i]);
printf("\nEnter the element to search:");
scanf("%d",&x);
}
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(x==arr[mid])
{
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
printf("\nElement not found");
return 0;
}
9
ASHIQ M
1804021
OUTPUT
Enter size or array: 6
Enter array elements (ascending order)
20 27 40 50 58 99
Enter the element to search:27
Element found at position 2
Enter size or array: 6
Enter array elements (ascending order)
20 27 40 50 58 99
Enter the element to search:24
Element notfound
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAINED
5
6
7
2
20
RESULT
The C program to search elements list using arrays by linear and binary method has been
verified and executed successfully.
10
ASHIQ M
1804021
EX.NO-03
DATE
STACK USING ARRAY
AIM
ALGORITHM
11
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<conio.h>
#define size 10
void push(int);
void pop();
void display();
int top=-1;x,s[size];
void main()
{
int ch;
clrscr();
while(1)
{
printf(“\n 1.push\t2.pop\t3.display”);
printf(“\n enter your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n enter the data to insert”);
scanf(“%d”,&x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void push(int x)
{
int i;
if(top==size-1)
{
printf(“\n stack is full”);
}
else
12
ASHIQ M
1804021
{
top=top+1;
s[top]=x;
}
for(i=0;i<=top;i++)
{
printf(“\n stack”,s[i]);
}
}
void pop()
{
int r;
if(top==-1);
{
printf(“\n empty”);
}
else
r=s[top];
top=top-1;
printf(“\n removed=%d”,r);
}
void display()
{
int I;
for(i=0;i<=top;i++)
{
printf(“\n stack=%d”,s[i]);
}
}
OUTPUT:
Enter your choice1
Enter the data to insert11
Stack11
1.push 2.pop 3.display
Enter your choice1
Enter the data to insert 2
Stack 11 2
1.push 2.pop 3.display
Enter your choice1
Enter the data to insert 3
Stack 11 2 3
1.push 2.pop 3.display
Enter your choice2
Remove =3
13
ASHIQ M
1804021
Stack 11 2
1.push 2.pop 3.display
Enter your choice3
Stack
11
2
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
The C program to enter, remove ,display the elements in the stack has been verified and
executed successfully.
14
ASHIQ M
1804021
EX.NO-04
DATE
QUEUE USING ARRAY
AIM
ALGORITHM
15
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<conio.h>
#define size 5
void enqueue(int);
void dequeue();
void display();
int q[size],x;
int front=-1,rear=-1;
void main()
{
int ch;
clrscr();
while(1)
{
printf(“enter the choice “);
printf(“1.enqueue\t 2.dequeue\t 3.display “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“enter the data”);
scanf(“%d”,&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void enqueue(int x)
{
if(rear==size-1)
{
16
ASHIQ M
1804021
printf(“queue is full”);
}
else
{
if(front= = -1)
{
fornt = 0;
}
rear=rear+1;
q[rear]=x;
printf(“enter the element to insert”);
}
}
void dequeue()
{
int y;
if(front= =-1)
{
printf(“queue is empty”);
}
else
{
y=q[front];
printf(“%d”,y);
}
if (front=rear)
{
front= -1;
rear= -1;
}
else
{
front= fornt-1;
}
}
void display()
{
int I;
if(front== -1)
17
ASHIQ M
1804021
{
printf(“queue is empty”);
}
printf(“queue elememts are “);
for(i=front;i<=rear;i++)
{
printf(“%d”q[i]);
}
}
OUTPUT
1.enqueue 2.dequeue 3.display
enter the choice 1
enter the element to insert 67
1.enqueue 2.dequeue 3.display
enter the choice 1
enter the element to insert 45
1.enqueue 2.dequeue 3.display
enter the choice 2
1.enqueue 2.dequeue 3.display
enter the choice 3
NOMENCLATURE
queue elements are 45
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the c program to perform queue operations are executed and output was verified.
18
ASHIQ M
1804021
EX.NO-05 a
DATE
INSERTION SORT
AIM
ALGORTHIM
19
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<conio.h>
void main()
{
int n,array[10],c,d,I;
clrscr();
printf(“emter the number of elements”);
scanf(“%d”,&n);
printf(“enter the %d integers \n”,n);
for(c=0;c<n;c++)
{
scanf(“%d”&array[c]);
}
for(c=1;c<=n;c++)
{
d=c;
while(d>0 && array[d-1]>array[d])
{
t=array[d];
array[d]=array[d-1];
array[d-1]=t;
d--;
}
}
printf(“sorted list is ascending order”);
for(c=0;c<n;c++)
{
printf(“%d\n”array[c]);
}
getch();
}
OUTPUT
Enter the number of elements 5
Enter 5 integers
45
98
12
35
13
Sorted list in ascending order
12
13
35
45
98
20
ASHIQ M
1804021
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the sorting by insertion sort was done and the output was verified successfully.
21
ASHIQ M
1804021
EX.NO-05 b
DATE
MERGE SORT
AIM
ALGORITHM
22
ASHIQ M
1804021
CODE
#include <stdio.h>
void merge(int [], int, int [], int, int []);
int main()
{
int a[10], b[10], m, n, c, sorted[20];
printf("Input number of elements in first array\n");
scanf("%d", &m);
printf("Input %d integers\n", m);
for (c = 0; c < m; c++)
{
scanf("%d", &a[c]);
}
printf("Input number of elements in second array\n");
scanf("%d", &n);
printf("Input %d integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &b[c]);
}
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
for (c = 0; c < m + n; c++)
{
printf("%d\n", sorted[c]);
}
return 0;
}
void merge(int a[], int m, int b[], int n, int sorted[])
{
int i, j, k;
j = k = 0;
for (i = 0; i < m + n;)
{
if (j < m && k < n)
23
ASHIQ M
1804021
{
if (a[j] < b[k])
{
sorted[i] = a[j];
j++;
}
else
{
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m)
{
for (; i < m + n;)
{
sorted[i] = b[k];
k++;
i++;
}
}
else
{
for (; i < m + n;)
{
sorted[i] = a[j];
j++;
i++;
}
}
}
}
24
ASHIQ M
1804021
OUTPUT
Enter no of elements 5
Enter array elements 34
6
12
Sorted array is 0 6
22
34
12
0
22
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the c programming to sort elements of the array using merge sort was verified
successfully.
25
ASHIQ M
1804021
EX.NO-05 c
DATE
QUICK SORT
AIM
ALGORITHM
26
ASHIQ M
1804021
CODE
#include <stdio.h>
int main()
{
int array[10], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
OUTPUT
Enter number of elements 5
Enter 5 integers
45
78
01
89
32
Sorted list in ascending order
01
32
27
ASHIQ M
1804021
45
78
89
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to perform sorting using quick sort was performed and output was verified.
28
ASHIQ M
1804021
EX.NO-06
DATE
SINGLY LINKED LIST
AIM
ALGORITHM
29
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;
int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();
while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n---- Insert Menu ----");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
30
ASHIQ M
1804021
}
break;
case 2: display();
break;
case 3: printf("\n---- Delete Menu ----");
printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified
position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}
void insert_beg()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
if(start==NULL)
{
t->next=NULL;
start=t;
}
else
{
//If list is empty
31
ASHIQ M
1804021
t->next=start;
start=t;
}
}
void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}
int insert_pos()
{
int pos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;
q=start;
for(i=1;i<pos-1;i++)
{
32
ASHIQ M
1804021
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t->next=q->next;
q->next=t;
return 0;
}
void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}
void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}
33
ASHIQ M
1804021
void delete_end()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
while(q->next->next!=NULL)
q=q->next;
t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}
int delete_pos()
{
int pos,i;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
printf("Enter position to delete:");
scanf("%d",&pos);
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
34
ASHIQ M
1804021
printf("Deleted element is %d",t->data);
free(t);
return 0;
}
OUTPUT
—- Singly Linked List(SLL) Menu —1.Insert
2.Display
3.Delete
4.ExitEnter your choice(1-4):1—- Insert Menu —1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.ExitEnter your choice(1-4):1
Enter data:4—- Singly Linked List(SLL) Menu —1.Insert
2.Display
3.Delete
4.ExitEnter your choice(1-4):2
The linked list is:
4->
—- Singly Linked List(SLL) Menu —1.Insert
2.Display
3.Delete
4.Exit
Enter your choice(1-4):4
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to insertion deletion and display are performed and output was verified.
35
ASHIQ M
1804021
EX.NO-07
DATE
STACK USING LINKED LIST
AIM
ALGORITHM
36
ASHIQ M
1804021
CODE
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
37
ASHIQ M
1804021
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
38
ASHIQ M
1804021
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
OUTPUT
*********Stack operations using linked list*********
Chose one from the below options
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 1
Enter the value 45
Item pushed
39
ASHIQ M
1804021
Chose one from the below options
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 1
Enter the value 50
Item pushed
Chose one from the below options
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 2
Item popped
Chose one from the below options
1.Push
2.Pop
3.Show
4.Exit
Enter your choice 3
Printing Stack elements
45
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to perform push pop and display was done using linked list was verified.
40
ASHIQ M
1804021
EX.NO-08
DATE
QUEUE USING LINKED LIST
AIM
ALGORITHM
41
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
42
ASHIQ M
1804021
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
43
ASHIQ M
1804021
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
OUTPUT
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter value?
123
***********Main Menu**********
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter value?
90
***********Main Menu**********
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
123
90
***********Main Menu**********
1.insert an element
2.Delete an element
44
ASHIQ M
1804021
3.Display the queue
4.Exit
Enter your choice ?2
***********Main Menu**********
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
printing values .....
90
***********Main Menu**********
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?4
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the insertion deletion and displaying the queue was performed and output was verified.
45
ASHIQ M
1804021
EX.NO-09
DATE
DOUBLY LINKED LIST
AIM
ALGORITHM
46
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete fro
m Beginning\n5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show
\n9.Exi\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
47
ASHIQ M
1804021
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
48
ASHIQ M
1804021
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
49
ASHIQ M
1804021
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
50
ASHIQ M
1804021
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
51
ASHIQ M
1804021
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
52
ASHIQ M
1804021
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
OUTPUT
*********Main Menu*********
Choose one option from the following list ..
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
8
printing values...
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
53
ASHIQ M
1804021
1
Enter Item value12
Node inserted
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
*********Main Menu*********
Choose one option from the following list…
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
54
ASHIQ M
1804021
9.Exit
Enter your choice?
8
printing values...
12
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
2
Enter value89
node inserted
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
3
Enter the location1
55
ASHIQ M
1804021
Enter value12345
node inserted
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
8
printing values...
12345
12
89
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
4
node deleted
*********Main Menu*********
56
ASHIQ M
1804021
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
5
node deleted
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
8
printing values...
123
12345
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
57
ASHIQ M
1804021
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
6
Enter the data after which the node is to be deleted : 123
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
8
printing values...
12
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
58
ASHIQ M
1804021
Enter your choice?
7
Enter item which you want to search?
123
item found at location 1
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
6
Enter the data after which the node is to be deleted : 123
Can't delete
*********Main Menu*********
Choose one option from the following list ...
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
9
59
ASHIQ M
1804021
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to perform insertion deletion and display using double linked list was
executed successfully and output was verified.
60
ASHIQ M
1804021
EX.NO-10
DATE
CIRCULAR LINKED LIST
AIM
ALGORITHM
61
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int info;
struct Node *next;
}node;
node *front=NULL,*rear=NULL,*temp;
void create();
void del();
void display();
int main()
{
int chc;
do
{
printf("\nMenu\n\t 1 to create the element : ");
printf("\n\t 2 to delete the element : ");
printf("\n\t 3 to display the queue : ");
printf("\n\t 4 to exit from main : ");
printf("\nEnter your choice : ");
scanf("%d",&chc);
switch(chc)
{
case 1:
create();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
return 1;
62
ASHIQ M
1804021
default:
printf("\nInvalid choice :");
}
}while(1);
return 0;
}
void create()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the node value : ");
scanf("%d",&newnode->info);
newnode->next=NULL;
if(rear==NULL)
front=rear=newnode;
else
{
rear->next=newnode;
rear=newnode;
}
rear->next=front;
}
void del()
{
temp=front;
if(front==NULL)
printf("\nUnderflow :");
else
{
if(front==rear)
{
printf("\n%d",front->info);
front=rear=NULL;
}
else
{
printf("\n%d",front->info);
front=front->next;
rear->next=front;
}
temp->next=NULL;
63
ASHIQ M
1804021
free(temp);
}
}
void display()
{
temp=front;
if(front==NULL)
printf("\nEmpty");
else
{
printf("\n");
for(;temp!=rear;temp=temp->next)
printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);
printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);
}
}
OUTPUT
Menu
1 to create the element :
2 to delete the element :
3 to display the queue :
4 to exit from main :
Enter your choice : 1
Enter the node value: 30
Menu
1 to create the element :
2 to delete the element :
3 to display the queue :
4 to exit from main :
Enter your choice : 1
Enter the node value: 50
Menu
1 to create the element :
2 to delete the element :
3 to display the queue :
4 to exit from main :
Enter your choice : 3
30 50
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to insert delete and display using circular linked list was executed successfully
and output was verified.
64
ASHIQ M
1804021
EX.NO-11
DATE
BINARY SEARCH TREE
AIM
ALGORITHM
65
ASHIQ M
1804021
CODE :
include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rightc;
struct node *leftc;
};
struct node* search(struct node *root, int x)
{
if(root==NULL||root #
->data==x)
return root;
else if(x>root->data)
return search(root->rightc, x);
else
return search(root->leftc,x);
}
struct node* findmin(struct node *root)
{
if(root == NULL)
return NULL;
else if(root->leftc !=NULL)
return findmin(root->leftc);
return root;
}
struct node* newnode(int x)
{
struct node *p;
p=malloc(sizeof(struct node));
p->data=x;
p->leftc=NULL;
p->rightc=NULL;
return p;
}
struct node* insert(struct node *root, int x)
{
66
ASHIQ M
1804021
if(root==NULL)
return newnode(x);
else if(x>root->data)
root->rightc=insert(root->rightc, x);
else
root->leftc=insert(root->leftc,x);
return root;
}
struct node* delete(struct node *root,int x)
{
if(root==NULL)
return NULL;
if(x>root->data)
root->rightc=delete(root->rightc, x);
else if(x<root->data)
root->leftc=delete(root->leftc,x);
else
{
if(root->leftc==NULL && root->rightc==NULL)
{
free(root);
return NULL;
}
else if(root->leftc==NULL || root->rightc==NULL)
{
struct node *temp;
if(root->leftc==NULL)
temp=root->rightc;
else
temp=root->leftc;
free(root);
return temp;
}
else
{
struct node *temp=findmin(root->rightc);
root->data=temp->data;
root->rightc=delete(root->rightc,temp->data);
}
67
ASHIQ M
1804021
}
return root;
}
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->leftc);
printf("%d ", root->data);
inorder(root->rightc);
}
}
void preorder(struct node *root)
{
if(root!=NULL)
{
printf("%d ", root->data);
preorder(root->leftc);
preorder(root->rightc);
}
}
void postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->leftc);
postorder(root->rightc);
printf("%d ", root->data);
}
}
void main()
{
int i,n,x;
struct node *root;
root=newnode(50);
clrscr();
printf("\n the binary search tree:");
printf("enter the num of nodes in tree:");
68
ASHIQ M
1804021
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the %dth node",i+1);
scanf("%d",&x);
insert(root,x);
}
printf("INORDER TRAVERSAL\n");
printf("------------------\n");
inorder(root);
printf("\nPREORDER TRAVERSAL\n");
printf("------------------\n");
preorder(root);
printf("\nPOSTORDER TRAVERSAL\n");
printf("------------------\n");
postorder(root);
printf("\ndeletion operation\n");
printf("enter the data to be deleted\n");
scanf("%d",&x);
delete(root,x);
printf("After deletion\n");
inorder(root);
getch();
}
OUTPUT :
The binary search tree:
enter the num of nodes in tree: 4
enter the 1th node: 4
enter the 2th node: 8
enter the 3th node: 3
enter the 4th node: 7
INORDER TRAVERSAL
-----------------69
ASHIQ M
1804021
4837
PREORDER TRAVERSAL
-----------------4387
POSTORDER TRAVERSAL
-----------------3784
deletion operation
enter the data to be deleted
8
After deletion
347
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT:
Thus the program to implement Binary Search Tree has been successfully implemented
and the output is verified.
70
ASHIQ M
1804021
EX.NO-12 a
DATE
GRAPH CREATION AND TRAVERSAL
(Depth First Search)
AIM
ALGORITHM
71
ASHIQ M
1804021
CODE :
#include<stdio.h>
void DFS(int);
int G[10][10],visited,n;
void main()
{
int i, j;
printf(“Enter the number of vertices :”);
scanf(“%d”,&n);
printf(“\n Enter adjacency matrix of the graph :”);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(“%d”,&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;
printf(“\n %d”,i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
OUTPUT :
Enter the number of vertices :8
Enter adjacency matrix of the graph:
01111000
10000100
10000100
10000010
10000010
01100001
72
ASHIQ M
1804021
00011001
00000110
0
1
5
2
7
6
3
4
Process returned 8
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT :
The program to perform Depth -wise search has been successfully implemented and
the output is verified .
73
ASHIQ M
1804021
EX.NO-12 b
DATE
GRAPH CREATION AND TRAVERSAL
(Breadth First Search)
AIM
ALGORITHM
74
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<conio.h>
int a[10][10],q[10],visited[10],n,i,j,f=0,r=-1;
void bfs(int v)
{
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}
75
ASHIQ M
1804021
OUTPUT :
Enter the number of vertices: 2
Enter graph data in matrix form: 1 2
23
Enter the starting vertex: 1
The node which are reachable are: 1 2
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT :
The program to perform Breadth-wise search has been successfully implemented and
the output is verified .
76
ASHIQ M
1804021
EX.NO-13 a
DATE
HASHING SEPARATE CHAINING
AIM
ALGORITHM
77
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<stdlib.h>
#define size 7
struct node
{
int data;
struct node *next;
};
struct node *chain[size];
void init()
{
int i;
for(i = 0; i < size; i++)
chain[i] = NULL;
}
void insert(int value)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
int key = value % size;
if(chain[key] == NULL)
chain[key] = newNode;
else
{
struct node *temp = chain[key];
while(temp->next)
{
temp = temp->next;
}
temp->next = newNode;
}
}
void print()
{
int i;
for(i = 0; i < size; i++)
{
struct node *temp = chain[i];
printf("chain[%d]-->",i);
while(temp)
{
printf("%d -->",temp->data);
78
ASHIQ M
1804021
temp = temp->next;
}
printf("NULL\n");
}
}
int main()
{
init();
insert(7);
insert(0);
insert(3);
insert(10);
insert(4);
insert(5);
print();
return 0;
}
OUTPUT
chain[0]-->7 -->0 -->NULL
chain[1]-->NULL
chain[2]-->NULL
chain[3]-->3 -->10 -->NULL
chain[4]-->4 -->NULL
NOMENCLATURE
chain[5]-->5 -->NULL
chain[6]-->NULL
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to perform separate hashing was executed and output was verified.
79
ASHIQ M
1804021
EX.NO-13 b
DATE
HASHING OPEN ADDRESSING
AIM
ALGORITHM
80
ASHIQ M
1804021
CODE
#include<stdio.h>
#include<limits.h>
void insert(int ary[],int hFn, int size){
int element,pos,n=0;
printf("Enter key element to insert\n");
scanf("%d",&element);
pos = element%hFn;
while(ary[pos]!= INT_MIN)
{
if(ary[pos]== INT_MAX)
break;
pos = (pos+1)%hFn;
n++;
if(n==size)
break;
}
if(n==size)
printf("Hash table was full of elements\nNo Place to insert this element\n\n");
else
ary[pos] = element;
}
void delete(int ary[],int hFn,int size)
{
int element,n=0,pos;
printf("Enter element to delete\n");
scanf("%d",&element);
pos = element%hFn;
while(n++ != size){
if(ary[pos]==INT_MIN){
printf("Element not found in hash table\n");
break;
}
else if(ary[pos]==element)
{
ary[pos]=INT_MAX;
printf("Element deleted\n\n");
break;
}
else
{
pos = (pos+1) % hFn;
}
81
ASHIQ M
1804021
}
if(--n==size)
printf("Element not found in hash table\n");
}
void search(int ary[],int hFn,int size)
{
int element,pos,n=0;
printf("Enter element you want to search\n");
scanf("%d",&element);
pos = element%hFn;
while(n++ != size){
if(ary[pos]==element){
printf("Element found at index %d\n",pos);
break;
}
else
if(ary[pos]==INT_MAX ||ary[pos]!=INT_MIN)
pos = (pos+1) %hFn;
}
if(--n==size) printf("Element not found in hash table\n");
}
void display(int ary[],int size)
{
int i;
printf("Index\tValue\n");
for(i=0;i<size;i++)
printf("%d\t%d\n",i,ary[i]);
}
int main()
{
int size,hFn,i,choice;
printf("Enter size of hash table\n");
scanf("%d",&size);
int ary[size];
printf("Enter hash function [if mod 10 enter 10]\n");
scanf("%d",&hFn);
for(i=0;i<size;i++)
ary[i]=INT_MIN;
{
printf("Enter your choice\n");
printf(" 1-> Insert\n 2-> Delete\n 3-> Display\n 4-> Searching\n 0-> Exit\n");
scanf("%d",&choice);
switch(choice)
82
ASHIQ M
1804021
{
case 1:
insert(ary,hFn,size);
break;
case 2:
delete(ary,hFn,size);
break;
case 3:
display(ary,size);
break;
case 4:
search(ary,hFn,size);
break;
default:
printf("Enter correct choice\n");
break;
}
}
while(choice);
return 0;
}
OUTPUT
Enter size of hash table
10
Enter hash function [if mod 10 enter 10]
10
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
0->Exit
1
Enter key element to insert
12
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
0->Exit
1
Enter key element to insert
83
ASHIQ M
1804021
22
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
0->Exit
1
Enter key element to insert
32
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
0->Exit
3
Index Value
0
-2147483648
1
-2147483648
2
12
3
22
4
32
5
-2147483648
6
-2147483648
7
-2147483648
8
-2147483648
9
-2147483648
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
0->Exit
2
Enter element to delete
12
Element deleted
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
84
ASHIQ M
1804021
0->Exit
4
Enter element you want to search
32
Element found at index 4
Enter your choice
1-> Insert
2-> Delete
3->Display
4->Searching
0->Exit
0
NOMENCLATURE
PRE VIVA
PRE LAB
PREPARATION
IN LAB
PREPARATION
POST LAB
TOTAL
MAX
MARKS
MARKS OBTAIN
ED
5
6
7
2
20
RESULT
Thus the program to execute hashing using open addressing was executed successfully.
85
ASHIQ M
1804021
EX.NO-14
DATE
EXTENSIBLE HASHING
AIM
ALGORITHM
86
ASHIQ M
1804021
CODE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <ext/hash_map>
#define MAX_BUCKETS 512
#define SIZE 10
int makesub(char *in, int depth) {
char key[21];
int i;
int hash, sub = 0;
int remainder;
strncpy(key, in, 20);
key[20] = ' ';
hash = hasher(key);
for(i=0; i < depth; i++) {
sub = sub << 1;
remainder = hash % 2;
sub = sub + remainder;
hash = hash >> 1;
}
return sub;
}
struct rec_tag {
char key_field[21];
};
typedef struct buck_tag {
long number_of_records;
int bucket_bits;
struct rec_tag record[SIZE];
} BUCKET;
struct head_tag {
int bits_used_in_index;
long buckets_used;
};
int doublidx(int bits, long *addresses)
{
int i;
int size;
long old_table[MAX_BUCKETS];
size = pow(2, bits);
if((size * 2) == MAX_BUCKETS) puts("WARNING: You are about to run out of index
space!");
87
ASHIQ M
1804021
memcpy(old_table, addresses, (size * sizeof(long)));
for(i=0; i < size; i++)
{
addresses[i*2] = old_table[i];
addresses[i*2 +1] = old_table[i];
}
return (bits + 1);
}
int moverecs(BUCKET * old, BUCKET * neu)
{
char spaces[21] = " ";
int i, j, sub, n_sub;
int odd, flag, space, check;
old->bucket_bits += 1;
n_sub = 0;
for(i=0; i < SIZE; i++)
{
sub = makesub(old->record[i].key_field, old->bucket_bits);
odd = sub%2;
if(odd)
{
memcpy(&neu->record[n_sub], &old->record[i], sizeof(struct rec_tag));
strcpy(old->record[i].key_field, spaces);
n_sub++;
}
}
neu->number_of_records = n_sub;
neu->bucket_bits = old->bucket_bits;
for(i=0, flag=0; i < SIZE && flag==0; i++)
{
space = strcmp(old->record[i].key_field, spaces);
if(space == 0)
{
for(j=i+1, check=1; j < SIZE && check != 0; j++)
check = strcmp(old->record[j].key_field, spaces);
if(j < SIZE)
{
memcpy(&old->record[i], &old->record[j-1], sizeof(struct rec_tag));
strcpy(old->record[j-1].key_field, spaces);
}
else flag = 1;
}
}
old->number_of_records -= n_sub;
return 0;
}
88
ASHIQ M
1804021
OUTPUT
89
Download