Data Structures MANUAL Writing
Data Structures MANUAL Writing
Data Structures MANUAL Writing
Ex. No. 1
STACK USING ARRAY
Date:
AIM
To implement stack operations using array.
ALGORITHM
1. Start
2. Define a array stack of size max = 5
3. Initialize top = -1
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
If top < max -1
Increment top
Store element at current position of top
Else
Print Stack overflow
Else If choice = 2 then
If top < 0 then
Print Stack underflow
Else
Display current top element Decrement top
Else If choice = 3 then
Display stack elements starting from top
7. Stop
PROGRAM
/* Stack Operation using Arrays */
#include <stdio.h>
#include <conio.h>
#define max 5
static int stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
2
int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n");
else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}
main()
{
int ch=0, val;
clrscr();
while(ch != 4)
{
printf("\n STACK OPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
3
else
printf("\n Stack Overflow \n");
break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n");
else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
break;
default:
printf("\n Invalid Choice \n");
}
}
}
OUTPUT
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12
4
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 34
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 45 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 34 23 12
STACK OPERATION
1. PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4
RESULT
Thus push and pop operations of a stack was demonstrated using arrays.
5
Ex. No. 2
QUEUE USING ARRAY
Date:
AIM
To implement queue operations using array.
ALGORITHM
1. Start
2. Define a array queue of size max = 5
3. Initialize front = rear = –1
4. Display a menu listing queue operations
5. Accept choice
6. If choice = 1 then
If rear < max -1
Increment rear
Store element at current position of rear
Else
Print Queue Full
Else If choice = 2 then
If front = –1 then
Print Queue empty
Else
Display current front element Increment front
Else If choice = 3 then
Display queue elements starting from front to rear.
7. Stop
PROGRAM
/* Queue Operation using Arrays */
#include <stdio.h>
#include <conio.h>
#define max 5
static int queue[max];
int front = -1;
int rear = -1;
void insert(int x)
{
queue[++rear] = x;
if (front == -1)
front = 0;
}
6
int remove()
{
int val;
val = queue[front];
if (front==rear && rear==max-1)
front = rear = -1;
else
front ++;
return (val);
}
void view()
{
int i;
if (front == -1)
printf("\n Queue Empty \n");
else
{
printf("\n Front-->");
for(i=front; i<=rear; i++)
printf("%4d", queue[i]);
printf(" <--Rear\n");
}
}
main()
{
int ch= 0,val;
clrscr();
while(ch != 4)
{
printf("\n QUEUE OPERATION \n");
printf("1.INSERT ");
printf("2.DELETE ");
printf("3.VIEW ");
printf("4.QUIT\n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
7
case 1:
if(rear < max-1)
{
printf("\n Enter element to be inserted : "); scanf("%d", &val);
insert(val);
}
else
printf("\n Queue Full \n");
break;
case 2:
if(front == -1)
printf("\n Queue Empty \n");
else
{
val = remove();
printf("\n Element deleted : %d \n", val);
}
break;
case 3:
view();
break;
case 4: exit(0);
break;
default: printf("\n Invalid Choice \n");
}
}
}
OUTPUT
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 12
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 23
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
8
Enter Choice : 1
Enter element to be inserted : 34
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 45
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 56
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Queue Full
QUEUE OPERATION
1. INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 3
Front--> 12 23 34 45 56 <--Rear
RESULT
Thus insert and delete operations of a queue was demonstrated using arrays.
9
Ex. No. 3
LIST USING ARRAY
Date:
AIM
To perform various operations on List ADT using array implementation.
ALGORITHM
1. Start
2. Create a list of n elements
3. Display list operations as a menu
4. Accept user choice
5. If choice = 1 then
Get position of element to be deleted
Move elements one position upwards thereon. Decrement length of the list
Else if choice = 2
Get position of element to be inserted. Increment length of the list
Move elements one position downwards thereon Store the new element in
corresponding position
Else if choice = 3
Traverse the list and inspect each element Report position if it exists.
6. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void create();
void insert();
void search();
void deletion();
void display();
int i, e, n, pos;
static int b[50];
main()
{
int ch;
char g = 'y';
create();
do
{
printf("\n List Operations");
10
void create()
{
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter list elements: ");
for(i=0; i<n; i++)
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n enter the position you want to delete: "); scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
11
void insert()
{
printf("\n Enter the position you need to insert: ");
scanf("%d", &pos);
if(pos >= n)
printf("\n Invalid location");
else
{
++n;
for(i=n; i>pos; i--)
b[i] = b[i-1];
printf("\n Enter the element to insert: ");
scanf("%d", &e);
b[pos] = e;
}
12
void display()
{
for(i=0; i<n; i++)
printf("\n %d", b[i]);
}
OUTPUT
Enter the number of elements:5
Enter list elements: 12 23 34 45 56
List Operations
1.Deletion 2.Insert 3.Search 4.Exit
Enter your choice:2
Enter the position you need to insert: 1
Enter the element to insert: 99
List after insertion:
12
99
23
34
45
56
Do you want to continue: y
RESULT
Thus various operations were successfully executed on list using array
implementation.
13
Ex. No. 4
SINGLY LINKED LIST
Date:
AIM
To write a C program to implement Singly Linked List.
ALGORITHM
1. Start the program
2. Create a structure for the Node with a Data part and a Address part containing the
address of the next element.
3. Initialize the variables and functions.
4. Using the Switch case, call the insert or delete or display functions.
5. The insertion operation can be done either in the first or middle or Last.
6. The new node to be inserted is first created at runtime using the malloc function.
7. The new node is inserted into the list by changing the head pointers
8. To insert the node in the middle of the list,
a) The node after which the new node has to insert is obtained.
b) The list is traversed until the noe3 is 3ndoun5343e.
c) The new node is inserted by changing the links.
9. To insert the node at the last,
a) The list is traversed until the node is encountered.
b) The new node is inserted by changing the links.
10. The deletion operation is done at three places.
11. The node at the front is deleted from the list by changing the head pointers.
12. To delete an element from the middle of the list,
a) Traverse the list until the node which has to be deleted is encountered.
b) Remove the node by changing the links.
13. To delete an element at the last,
a) Traverse the list until the last node is encountered.
b) Remove the node by changing the links.
14. In display operation all the elements are displayed by traversing the list using a
while loop until the last element.
15. Stop the execution.
14
PROGRAM
#include<stdio.h>
#include<conio.h>
#define null 0
typedef struct n
{
int data;
struct n*next;
}
node;
node*head=null,*newn,*temp,*t;
void insert();
void delete();
void view();
void main()
{
int choice;
clrscr();
printf("\n\t\t SINGLY LINKED LIST");
while (1)
{
printf("\n 1.Insert\t 2.Delete\t 3.View");
printf("\n Enter the choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:view();
break;
case 4:exit(0);
break;
default: printf("\n Enter the correct option");
}
}
}
15
void insert()
{
int option,no;
newn=(node*)malloc(sizeof(node));
printf("Enter the data to be inserted : ");
scanf("%d",&newn->data);
newn->next=null;
printf("\n 1.Insert first\t 2.Insert middle\t 3.Insert last");
printf("\n Enter the option:");
scanf("%d", &option);
switch (option)
{
case 1:if(head==null)
head=newn;
else
{
newn-> next=head;
head=newn;
}
break;
case 2:if(head-> next==null)
printf("single entry list cannot perform insertion in the middle");
else
{
printf("\n entry the data which the node has to insert");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp-> next;
}
newn->next=temp->next;
temp->next = newn;
}
break;
case 3:temp=head;
if(head == null)
head=newn;
else
{
temp=head;
while(temp->next!=null)
16
{
temp=temp->next;
}
temp->next = newn;
}
break;
}
}
void delete()
{
int option,no;
if(head==null)
{
printf("empty list cannot perform deletion");
}
printf("1.Deletion at first\t 2.Deletion in middle\t 3.Deletion at last\n");
printf(“Enter the Option : “);
scanf("%d",&option);
switch (option)
{
case 1: temp=head;
head =head->next;
printf("The data deleted is %d",temp ->data);
free(temp);
break;
case 2:if(head->next == null)
printf("single entry list cannot perform deletion at middle");
else
{
printf("Enter the data which has to be deleted : ");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("the data deleted is %d",temp->data);
t->next=temp->next;
free(temp);
}
break;
17
case 3:temp=head;
if(head->next == null)
{
printf("the data deleted is%d",head->data);
head=null;
}
else
{
while(temp->next!=null)
{
t=temp;
temp=temp->next;
printf("the data deleted is%d",temp->data);
free (temp);
}
}
break;
}
}
void view()
{
temp=head;
printf("Head-->");
while(temp!=null)
{
printf("%d->\t",temp->data);
temp=temp->next;
}
printf("NULL");
}
18
OUTPUT
RESULT
Thus the program to implement Singly Linked List has been executed and verified
successfully.
19
Ex. No. 5
DOUBLY LINKED LIST
Date:
AIM
To write a C program to implement Doubly Linked List.
ALGORITHM
1. Start the program.
2. Create a Structure for the Node with a Data part and two Address part
one pointing to the previous node and other pointing to the Next node.
3. Initialize the variables and functions.
4. Using the Switch case, call the insert or delete or display function
5. The insertion operation can be done either in the first or middle or last.
6. The new node to be inserted is first created at runtime using the malloc function.
7. The new node to be inserted into the list by changing the head pointers.
8. To insert the node in the middle of the list,
a. The node after which the new node has to insert is obtained.
b. The list is traversed until the node is encounter.
c. The new node is inserted by changing the previous and next links.
9. To insert the node at the last,
a. The list is traversed until the last node is encounter.
b. the new node is inserted by changing the previous and next links.
10. The deletion operation is done at three places.
11. The node at the front is deleted from the list by changing the Head points.
12. To delete an element from the middle of the list,
a. Traverse the list until the node which has to be deleted is encountered
b. Remove the node by changing the previous and next list
13. To delete an element at the last,
a. Traverse the list until the last node is encountered.
b. Remove the node by changing the previous and next links.
14. In display operation all the elements are displayed by traversing the list
using a while loop until the last element.
16. Stop the execution.
20
PROGRAM
#include<stdio.h>
#include<conio.h>
#define null 0
typedef struct n
{
int data;
struct n*next,*prev;
}node;
node *head,*newn,*temp,*t;
void insert();
void delet();
void view();
void main()
{
int c;
clrscr();
printf("\n\t\t DOUBLY LINKED LIST \n");
printf("\n1.Insertion 2.Deletion 3.View 4.Exit\n");
while(1)
{
printf("\nEnter the option : ");
scanf("%d",&c);
switch(c)
{
case 1:insert();
break;
case 2:delet();
break;
case 3:view();
break;
case 4:exit();
break;
default:printf("\nEnter the Correct Option\n");
}
}
}
void insert()
{
int option,no;
newn=(node*)malloc(sizeof (node));
21
temp=temp->next;
temp->next=newn;
newn->prev=temp;
}
break;
}
}
void delet()
{
int option, no;
if(head==null)
printf("empty list cannot perform");
printf("\n1.Deletion at the First 2.Deletion at the Middle 3.Deletion at the Last\n");
printf("\nEnter the Choice : ");
scanf("%d",&option);
switch(option)
{
case 1: temp=head;
head=head->next;
printf("\nThe data deleted is %d\n",temp->data);
free(temp);
break;
case 2: if(head->next==null)
printf("\nSingle entry list cannot perform deletion operation\n");
else
{
printf("\nEnter the data to be deleted : ");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
temp->prev=t;
}
printf("\nThe data deleted is %d\n",temp->data);
t->next=temp->next;
temp->next->prev=t;
free(temp);
}
break;
23
case 3: temp=head;
if(head->next==null)
{
printf("\nThe data deleted is %d\n",head->data);
head=null;
}
else
{
while(temp->next!=null)
{
t=temp;
temp=temp->next;
}
t->next=null;
printf("\nThe data deleted is %d\n",temp->data);
free(temp);
}
break;
}
}
void view()
{
printf("\nHEAD --> ");
for(temp=head;temp!=null;t=temp,temp=temp->next)
printf("%d --> ",temp->data);
printf("NULL\n");
printf("\nHEAD <-- ");
for(temp=head;temp!=null;t=temp,temp=temp->next)
printf("%d <-- ",temp->data);
printf("NULL\n");
}
24
OUTPUT
RESULT
Thus the program to implement Doubly Linked List has been executed and verified
successfully.
25
Ex. No. 6
STACK USING LINKED LIST
Date:
AIM
To implement stack operations using linked list.
ALGORITHM
1. Start
2. Define a singly linked list node for stack
3. Create Head node
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
Create a new node with data Make new node point to first node
Make head node point to new node Else If choice = 2 then
Make temp node point to first node
Make head node point to next of temp node Release memory
Else If choice = 3 then
Display stack elements starting from head node till null
7. Stop
PROGRAM
/* Stack using Single Linked List */
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>
struct node
{
int label;
struct node *next;
};
main()
{
int ch = 0; int k;
struct node *h, *temp, *head;
while(1)
{
printf("\n Stack using Linked List \n");
printf("1->Push ");
printf("2->Pop ");
printf("3->View");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
h = head;
temp->next = h->next;
h->next = temp;
break;
case 2:
/* Delink the first node */
h = head->next;
head->next = h->next;
printf("Node %s deleted\n", h->label);
free(h);
break;
case 3:
printf("\n HEAD -> ");
h = head;
/* Loop till last node */
while(h->next != NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
27
printf("NULL \n");
break;
case 4: exit(0);
}
}
}
OUTPUT
RESULT
Thus push and pop operations of a stack was demonstrated using linked list.
28
Ex. No. 7
QUEUE USING LINKED LIST
Date:
AIM
To implement queue operations using linked list.
ALGORITHM
1. Start
2. Define a singly linked list node for stack
3. Create Head node
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
Create a new node with data Make new node point to first node
Make head node point to new node Else If choice = 2 then
Make temp node point to first node
Make head node point to next of temp node Release memory
Else If choice = 3 then
Display stack elements starting from head node till null
7. Stop
PROGRAM
/* Queue using Single Linked List */
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>
struct node
{
int label;
struct node *next;
};
main()
{
int ch=0, k;
struct node *h, *temp, *head;
29
while(1)
{
printf("\n Queue using Linked List \n");
printf("1->Insert ");
printf("2->Delete ");
printf("3->View ");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);
case 2:
/* Delink the first node */
h = head->next;
head->next = h->next;
printf("Node deleted \n");
free(h);
break;
case 3:
printf("\n\nHEAD -> ");
h=head;
30
while (h->next!=NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \n");
break;
case 4:
exit(0);
}
}
}
OUTPUT
RESULT
Thus insert and delete operations of a queue was demonstrated using linked list.
31
Ex. No. 8
POLYNOMIAL ADDITION USING LINKED LIST
Date:
AIM
To write a ‘C’ Program to represent a polynomial as a linked list and to write functions
for polynomial addition.
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
do
{
printf("\n Enter Co-efficient : ");
scanf("%d",&node -> coeff);
printf("\n Enter Power : ");
scanf("%d",&node -> pow);
node -> next = (struct link*)malloc(sizeof(struct link));
node = node -> next;
node -> next = NULL;
printf("\n Continue (y/n) : ");
ch = getch();
}
while(ch = = 'y' || ch = = 'Y');
}
OUTPUT
Enter 1st Polynomial
Enter Co-efficient : 2
Enter Power : 3
Continue (y/n) :
Enter Co-efficient :3
Enter Power : 2
Continue (y/n) :
Enter Co-efficient : 4
Enter Power : 1
Continue (y/n) :
RESULT
Thus the ‘C’ Program to represent a polynomial as a linked list and to write functions for
polynomial addition has been executed and verified successfully.
36
Ex. No. 9
CONVERSION OF INFIX TO POSTFIX EXPRESSION
Date:
AIM
To write a C program to convert the infix expression to postfix expression using stack
concept.
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push();
char pop();
void main()
{
clrscr();
printf("\n\t\tCONVERSION OF INFIX TO POSTFIX EXPRESSION \n\n");
printf("\n\tEnter the infix expression : ");
scanf("%s",inf);
postfix();
37
getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':while(st[top]>=1)
post[i++]=pop();
push(1);
break;
case '-':while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':while(st[top]>=2)
post[j++]=pop();
push(3);
break;
case '/':while(st[top]>=2)
post[j++]=pop();
push(4);
break;
case '^':while(st[top]>=3)
post[j++]=pop();
push(5);
break;
case '(':push(0);
break;
case ')':while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\n\tPostfix exp is =>\n\n\t %s",post);
}
38
OUTPUT
RESULT
Thus C program to convert the infix expression to postfix expression using stack
concept was verified successfully.
39
Ex. No. 10
POSTFIX EXPRESSION EVALUATION
Date:
AIM
To evaluate the given postfix expression using stack operations.
ALGORITHM
1. Start
2. Define a array stack of size max = 20
3. Initialize top = -1
4. Read the postfix expression character-by-character
If character is an operand, push it onto the stack
If character is an operator, Pop topmost two elements from stack.
Apply operator on the elements and push the RESULT onto the stack,
5. Eventually only RESULT will be in the stack at end of the expression.
6. Pop the RESULT and print it.
7. Stop
40
PROGRAM
#include <stdio.h>
#include <conio.h>
struct stack
{
int top;
float a[50];
}s;
main()
{
char pf[50];
float d1,d2,d3;
int i;
clrscr();
s.top = -1;
printf("\n\n Enter the postfix expression: ");
gets(pf);
for(i=0; pf[i]!='\0'; i++)
{
switch(pf[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
s.a[++s.top] = pf[i]-'0';
break;
case '+':
d1 = s.a[s.top--];
d2 = s.a[s.top--];
s.a[++s.top] = d1 + d2;
break;
41
case '-':
d2 = s.a[s.top--];
d1 = s.a[s.top--];
s.a[++s.top] = d1 - d2;
break;
case '*':
d2 = s.a[s.top--];
d1 = s.a[s.top--];
s.a[++s.top] = d1*d2;
break;
case '/':
d2 = s.a[s.top--];
d1 = s.a[s.top--];
s.a[++s.top] = d1 / d2;
break;
}
}
printf("\n Expression value is %5.2f", s.a[s.top]);
getch();
}
OUTPUT
RESULT
Thus the given postfix expression was evaluated using stack.
42
Ex. No. 11
FCFS SCHEDULING
Date:
AIM
To schedule snapshot of processes queued according to FCFS scheduling.
Process Scheduling
CPU scheduling is used in multiprogrammed operating systems.
By switching CPU among processes, efficiency of the system can be improved.
Some scheduling ALGORITHMs are FCFS, SJF, Priority, Round-Robin, etc.
Gantt chart provides a way of visualizing CPU scheduling and enables to understand
better.
ALGORITHM
1. Define an array of structure process with members pid, btime, wtime & ttime.
2. Get length of the ready queue, i.e., number of process (say n)
3. Obtain btime for each process.
4. The wtime for first process is 0.
5. Compute wtime and ttime for each process as:
a. wtimei+1 = wtimei + btimei
b. ttimei = wtimei + btimei
6. Compute average waiting time awat and average turnaround time atur
7. Display the btime, ttime and wtime for each process.
8. Display GANTT chart for the above scheduling
9. Display awat time and atur
10. Stop
43
PROGRAM
#include <stdio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10];
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}
OUTPUT
$ gcc fcfs.c
$ ./a.out
Enter no. of process : 4
Burst time for process P1 (in ms) : 10
Burst time for process P2 (in ms) : 4
Burst time for process P3 (in ms) : 11
Burst time for process P4 (in ms) : 6
FCFS Scheduling
----------------------------
Process B-Time T-Time W-Time
----------------------------
P1 10 10 0
P2 4 14 10
P3 11 25 14
P4 6 31 25
----------------------------
GANTT Chart
----------------------------------------
|P1 |P2 | P3 |P4 |
----------------------------------------
0 10 14 25 31
RESULT
Thus waiting time & turnaround time for processes based on FCFS scheduling was
computed and the average waiting time was determined.
46
Ex. No. 12
BINARY TREE TRAVERSAL
Date:
AIM
To implement different types of traversal for the given binary tree.
ALGORITHM
1. Create a structure with key and 2 pointer variable left and right
2. Read the node to be inserted.
If (root==NULL)
root=node
else if (root->key < node->key)
root->right=NULL
else
Root->left=node
3. For Inorder Traversal
Traverse Left subtree
Visit root
Traverse Right subtree
4. For Preorder Traversal
Visit root
Traverse Left subtree
Traverse Right subtree
5. For Postorder Traversal
Traverse Left subtree
Traverse Right subtree
Visit root
6. Stop.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
int count=1;
main()
{
node *root = NULL; int digit;
puts("Enter integer:To quit enter 0");
scanf("%d", &digit);
while(digit != 0)
{
root=insert(root,digit); scanf("%d",&digit);
}
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
printf("\nThe inorder traversal of tree is:\n");
inorder(root);
printf("\nThe postorder traversal of tree is:\n");
postorder(root);
getch();
}
OUTPUT
Enter integer:To quit enter 0 12 4 6 9 14 17 3 19 0
RESULT
Thus three types of tree traversal were performed on the given binary tree.
49
Ex. No. 13
BINARY SEARCH TREE TRAVERSAL
Date:
AIM
To write a C program to implement binary search tree traversal and its inorder,
preorder, and postorder traversals.
ALGORITHM
1. Start the program.
2. Create the tree structure with a data part and two address parts to hold the
Address its right and left child.
3. Initialize the variables and functions.
4. Create a root node.
5. Examine each node and place in its right position as left or right child.
a. if the element is less than the parent node, then place it as left child.
b. if the element is greater than the parent node, then place it as right child.
6. In the inorder traversal, each node in the tree is visited in the corresponding order
left, root, right recursively.
7. In the preorder traversal, each node in the tree is visited in the corresponding order
root, left, right recursively.
8. In the postorder traversal, each node in the tree is visited in the corresponding order
left, right, root recursively.
9. Stop the execution.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define null 0
typedef struct btree
{
int data;
struct btree*left,*right;
}*NODEPTR,node;
NODEPTR maketree(int);
void setleft(NODEPTR,int);
void setright(NODEPTR,int);
void intra(NODEPTR);
void pretra(NODEPTR);
50
void posttra(NODEPTR);
void main()
{
NODEPTR tree,p,q;
int no;
clrscr();
printf("\n\t\tBINARY SEARCH TREE TRAVERSAL");
printf("\nEnter the root value : ");
scanf("%d",&no);
tree=maketree(no);
printf("\nEnter the other nodes. Enter 0 to exit\n");
while(1)
{
scanf("%d",&no);
if(no==0)
break;
else
{
p=q=tree;
while(no!=p->data&&q!=null)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no==p->data)
printf("%d is duplicate \n",no);
if(no<p->data)
setleft(p,no);
else
setright(p,no);
}
}
printf("\nInorder traversal \n");
intra(tree);
printf("\nPreorder traversal \n");
pretra(tree);
printf("\nPostorder traversal \n" );
posttra(tree);
getch();
51
if(p!=null)
{
printf(" %d --> ",p->data);
pretra(p->left);
pretra (p->right);
}
}
void posttra(NODEPTR p)
{
if(p!=null)
{
posttra(p->left);
posttra (p->right);
printf(" %d --> ",p->data);
}
}
OUTPUT
RESULT
Thus program to implement binary search tree traversal and its inorder, preorder, and
postorder traversals.
53
Ex. No. 14
AVL TREE
Date:
AIM
To write a C program to implement insertion operation in AVL trees.
ALGORITHM
1. Start the program.
2. Create a structure node with data, balfact, left child address and right child address.
3. Create a function to insert the data in an AVL tree.
a) If the node is created as first node, then store the data as the root node.
b) After creating root node, insert other elements in the next level.
i) Recursively, check whether the data is less than the parent data and
then insert the node as the left node. While inserting the node check
the height of the tree, if it is not balanced, then make rotation in the
tree.
ii) Recursively, check whether the data is greater than the parent data and
then insert the node as the right node. While inserting the node check
the height of the tree, if it is not balanced, then make rotation in the
tree.
4. Create a function to display the elements in the AVL tree as inorder traversal.
5. Stop the execution.
ROGRAM
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define FALSE 0
#define TRUE 1
struct node
{
int data, balfact;
struct node *left;
struct node *right;
};
struct node *insert(struct node*, int, int *);
void display(struct node*);
int h;
54
void main()
{
int cho,item;
struct node *avl=NULL;
clrscr();
printf("\n AVL TREE \n");
do
{
printf("\n 1.Insertion \t 2. Display \t 3. Exit");
printf ("\n Enter your Choice : ");
scanf("%d",&cho);
switch(cho)
{
case 1:
printf("Enter the item to be inserted in the AVL tree : ");
scanf("%d", &item);
avl=insert(avl,item,&h);
break;
case 2:
display(avl);
break;
case 3: break;
}
}
while(cho!=3);
getch();
}
struct node *insert(struct node *root, int data, int *h)
{
struct node *node1, *node2;
if(!root)
{
root=(struct node*) malloc(sizeof(struct node));
root -> data = data;
root -> left = NULL;
root -> right = NULL;
root -> balfact = 0;
*h = TRUE;
return(root);
}
55
case -1:
root -> balfact =0;
*h =FALSE;
}
}
}
if(data > root -> data)
{
root -> right = insert(root -> right, data, h);
if( *h)
{
switch(root -> balfact)
{
case 1:
root -> balfact =0;
*h =FALSE;
break;
case 0:
root -> balfact =-1;
break;
case -1:
node1 = root -> right;
if(node1 -> balfact == -1)
{
printf("\n Left Rotation along %d ", root -> data);
root -> right = node1 -> left;
node1 -> left = root;
root -> balfact = 0;
root = node1;
}
else
{
printf("\n Double Rotation, Right along %d", node1 -> data);
node2 = node1 -> left;
node1 -> left = node2 -> right;
node2 -> right = node1;
printf("Then left along %d \n", root -> data);
root -> right = node2 -> left;
node2 -> left = root;
if(node2 -> balfact ==-1)
root -> balfact =1;
57
else
root -> balfact = 0;
if(node2 -> balfact == 1)
node1 -> balfact = -1;
else
node1 -> balfact =0;
root=node2;
}
root -> balfact =0;
*h = FALSE;
}
}
}
return(root);
}
void display(struct node *root)
{
if(root!=NULL)
{
display(root -> left);
printf("%d \t ",root -> data);
display(root -> right);
}
}
OUTPUT:
AVL TREE
1.Insertion 2. Display 3. Exit
Enter your Choice : 1
Enter the item to be inserted in the AVL tree : 11
RESULT:
Thus C program to implement insertion operation in AVL trees was verified
successfully.
59
Ex. No. 15
BINARY HEAP
Date:
AIM
To build a binary heap from an array of input elements.
ALGORITHM
1. Start
2. In a heap, for every node x with parent p, the key in p is smaller than or equal to the key
in x.
3. For insertion operation
a. Add the element to the bottom level of the heap.
b. Compare the added element with its parent; if they are in the correct order, stop.
c. If not, swap the element with its parent and return to the previous step.
4. For deleteMin operation
a. Replace the root of the heap with the last element on the last level.
b. Compare the new root with its children; if they are in the correct order, stop.
c. If not, Swap with its smaller child in a min-heap
5. Stop
60
PROGRAM
#include <stdio.h>
#include <limits.h>
int heap[1000000], heapSize;
void Init()
{
heapSize = 0;
heap[0] = -INT_MAX;
}
int DeleteMin()
{
int minElement, lastElement, child, now;
minElement = heap[1];
lastElement = heap[heapSize--];
for (now = 1; now * 2 <= heapSize; now = child)
{
child = now * 2;
if (child != heapSize && heap[child + 1] < heap[child])
child++;
if (lastElement > heap[child])
heap[now] = heap[child];
else
break;
}
heap[now] = lastElement;
return minElement;
}
61
main()
{
int number_of_elements;
printf("PROGRAM to demonstrate Heap:\nEnter the number of elements: ");
scanf("%d", &number_of_elements);
int iter, element;
Init();
printf("Enter the elements: ");
for (iter = 0; iter < number_of_elements; iter++)
{
scanf("%d", &element);
Insert(element);
}
for (iter = 0; iter < number_of_elements; iter++)
printf("%d ", DeleteMin());
printf("\n");
}
OUTPUT
2 3 4 5 15 45
RESULT
Thus a binary heap is constructed for the given elements.
62
Ex. No. 16
BREADTH FIRST SEARCH
Date:
AIM
To create adjacency matrix of the given graph and to perform breadth first search
traversal.
ALGORITHM
1. Start
2. Obtain Adjacency matrix for the given graph
3. Define a Queue of size total number of vertices in the graph.
4. Select any vertex as starting point for traversal. Visit that vertex and insert it into the
Queue.
5. Visit all the adjacent vertices of the verex which is at front of the Queue which is not
visited and insert them into the Queue.
6. When there is no new vertex to be visit from the vertex at front of the Queue then
delete that vertex from the Queue.
7. Repeat step 5 and 6 until queue becomes empty.
8. When queue becomes Empty, then produce final spanning tree by removing unused
edges from the graph.
9. Stop
PROGRAM
/* Graph Traversal – BFS */
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
63
int main()
{
create_graph();
BF_Traversal();
return 0;
}
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS: ");
scanf("%d", &v);
BFS(v);
}
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
printf("BFS Traversal : ");
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ", v);
state[v] = visited;
for(i=0; i<n; i++)
{
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}
}
64
printf("\n");
}
void insert_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
printf("Enter number of vertices : ");
scanf("%d", &n);
max_edge = n * (n-1);
for(count=1; count<=max_edge; count++)
{
printf("Enter edge %d( -1 -1 to quit ) : ",count);
65
OUTPUT
RESULT
Thus Breadth First Traversal is executed on the given graph.
66
Ex. No. 17
DEPTH FIRST SEARCH
Date:
AIM
To create adjacency matrix of the given graph and to perform depth first search
traversal.
ALGORITHM
1. Start
2. Obtain Adjacency matrix for the given graph
3. Define a Stack of size total number of vertices in the graph.
4. Select any vertex as starting point for traversal. Visit that vertex and push it on to the
Stack.
5. Visit any one of the adjacent vertex of the verex which is at top of the stack which is
not visited and push it on to the stack.
6. Repeat step 5 until there are no new vertex to be visit from the vertex on top of the
stack.
7. When there is no new vertex to be visit then use back tracking and pop one vertex from
the stack.
8. Repeat steps 5, 6 and 7 until stack becomes Empty.
9. When stack becomes Empty, then produce final spanning tree by removing unused
edges from the graph.
10. Stop
PROGRAM
/* DFS on undirected graph */
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define MAX 5
struct Vertex
{
char label;
int visited;
};
int stack[MAX];
int top = -1;
67
int pop()
{
return stack[top--];
}
int peek()
{
return stack[top];
}
int isStackEmpty()
{
return top == -1;
}
void depthFirstSearch()
{
int i;
lstVertices[0]->visited = true;
displayVertex(0);
push(0);
while(!isStackEmpty())
{
int unvisitedVertex = getAdjUnvisitedVertex(peek());
if(unvisitedVertex == -1)
pop();
else
{
lstVertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
push(unvisitedVertex);
}
}
for(i = 0;i < vertexCount;i++)
lstVertices[i]->visited = false;
}
main()
{
int i, j, n, edges, orgn, destn; char ch;
printf("Enter no. of vertices : ");
scanf("%d", &n);
edges = n * (n - 1);
printf("Enter Vertex Labels : \n");
for (i=0; i<n; i++)
69
{
fflush(stdin);
scanf("%c", &ch);
addVertex(ch);
}
for(i=0; i<edges; i++)
{
printf("Enter edge ( -1 -1 to quit ) : ");
scanf("%d %d", &orgn, &destn);
if((orgn == -1) && (destn == -1)) break;
if(orgn>=n || destn>=n || orgn<0 || destn<0) printf("Invalid edge!\n");
else
addEdge(orgn, destn);
}
printf("\nDepth First Search: "); depthFirstSearch();
}
OUTPUT
Enter no. of vertices : 5
Enter Vertex Labels :
SABCD
Enter edge ( -1 -1 to quit ) : 0 1
Enter edge ( -1 -1 to quit ) : 0 3
Enter edge ( -1 -1 to quit ) : 0 2
Enter edge ( -1 -1 to quit ) : 1 4
Enter edge ( -1 -1 to quit ) : 2 4
Enter edge ( -1 -1 to quit ) : 3 4
Enter edge ( -1 -1 to quit ) : -1 -1
Depth First Search: S A D B C
RESULT
Thus depth first traversal is executed on the given undirected graph.
70
Ex. No. 18
DIJKSTRA’S SHORTEST PATH
Date:
AIM
To find the shortest path for the given graph from a specified source to all other
vertices using Dijkstra’s algorithm.
ALGORITHM
1. Start
2. Obtain no. of vertices and adjacency matrix for the given graph
3. Create cost matrix from adjacency matrix. C[i][j] is the cost of going from vertex i to
vertex j. If there is no edge between vertices i and j then C[i][j] is infinity
4. Initialize visited[] to zero
5. Read source vertex and mark it as visited
6. Create the distance matrix, by storing the cost of vertices from vertex no. 0 to n-1 from
the source vertex distance[i]=cost[0][i];
7. Choose a vertex w, such that distance[w] is minimum and visited[w] is 0. Mark
visited[w] as 1.
8. Recalculate the shortest distance of remaining vertices from the source.
9. Only, the vertices not marked as 1 in array visited[ ] should be considered for
recalculation of distance. i.e. for each vertex v
if(visited[v]==0)
distance[v]=min(distance[v] distance[w]+cost[w][v])
10. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX], int n, int startnode);
main()
{
int G[MAX][MAX], i, j, n, u;
printf("Enter no. of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d", &G[i][j]);
71
}
for(i=0; i<n; i++)
if(i != startnode)
{
printf("\nDistance to node%d = %d", i,distance[i]);
printf("\nPath = %d", i); j = i;
do
{
j = pred[j];
printf("<-%d", j);
} while(j != startnode);
}
}
OUTPUT
RESULT
Thus Dijkstra's algorithm is used to find shortest path from a given vertex.
73
Ex. No. 19
LINEAR SEARCH
Date:
AIM
To perform linear search of an element on the given array.
ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai, i = 0,1,2,…n–1
4. Read search value
5. Assign 0 to found
6. Check each array element against search
If Ai = search then found = 1
Print "Element found"
Print position i
Stop
7. If found = 0 then print "Element not found"
8. Stop
74
PROGRAM
/* Linear search on a sorted array */
#include <stdio.h>
#include <conio.h>
main()
{
int a[50],i, n, val, found;
clrscr();
printf("Enter number of elements : ");
scanf("%d", &n);
printf("Enter Array Elements : \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter element to locate : ");
scanf("%d", &val);
found = 0; for(i=0; i<n; i++)
{
if (a[i] == val)
{
printf("Element found at position %d", i);
found = 1;
break;
}
}
if (found == 0)
printf("\n Element not found");
getch();
}
OUTPUT
Enter number of elements : 7
Enter Array Elements :
23 6 12 5 0 32 10
Enter element to locate : 5
Element found at position 3
RESULT
Thus an array was linearly searched for an element's existence.
75
Ex. No. 20
BINARY SEARCH
Date:
AIM
To locate an element in a sorted array using Binary search method
ALGORITHM
1. Start
2. Read number of array elements, say n
3. Create an array arr consisting n sorted elements
4. Get element, say key to be located
5. Assign 0 to lower and n to upper
6. While (lower < upper)
Determine middle element mid = (upper+lower)/2
If key = arr[mid] then Print mid Stop
Else if key > arr[mid] then lower = mid + 1
Else upper = mid – 1
7. Print "Element not found"
8. Stop
PROGRAM
/* Binary Search on a sorted array */
#include <stdio.h>
#include <conio.h>
main()
{
int a[50],i, n, upper, lower, mid, val, found;
clrscr();
printf("Enter array size : ");
scanf("%d", &n);
for(i=0; i<n; i++)
a[i] = 2 * i;
printf("\n Elements in Sorted Order \n");
for(i=0; i<n; i++)
printf("%4d", a[i]);
printf("\n Enter element to locate : ");
scanf("%d", &val);
upper = n; lower = 0;
found = -1;
76
OUTPUT
Enter array size :9
Elements in Sorted Order
0 2 4 6 8 10 12 14 16
Enter element to locate : 12
Located at position 6
RESULT
Thus an element is located quickly using binary search method.
77
Ex. No. 21
BUBBLE SORT
Date:
AIM
To sort an array of N numbers using Bubble sort.
ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Index i varies from 0 to n-2
5. Index j varies from i+1 to n-1
6. Traverse the array and compare each pair of elements If Ai > Aj then Swap Ai and A
7. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
main()
{
int a[50],i, j, n, t; clrscr();
printf("Enter number of elements : ");
scanf("%d", &n);
printf("Enter Array Elements \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if (a[i] > a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
78
OUTPUT
RESULT
Thus an array was sorted using bubble sort.
79
Ex. No. 22
QUICK SORT
Date:
AIM
To sort an array of N numbers using Quick sort.
ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Select an pivot element x from Ai
5. Divide the array into 3 sequences: elements < x, x, elements > x
6. Recursively quick sort both sets (Ai < x and Ai > x)
7. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void qsort(int arr[20], int fst, int last);
main()
{
int arr[30], i, size;
printf("Enter total no. of the elements : ");
scanf("%d", &size);
printf("Enter total %d elements : \n", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
qsort(arr,0,size-1);
printf("\n Quick sorted elements \n");
for(i=0; i<size; i++)
printf("%d\t", arr[i]);
getch();
}
void qsort(int arr[20], int fst, int last)
{
int i, j, pivot, tmp;
if(fst < last)
{
pivot = fst;
i = fst;
80
j = last;
while(i < j)
{
while(arr[i] <=arr[pivot] && i<last)
i++;
while(arr[j] > arr[pivot])
j--;
if(i <j )
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;
qsort(arr, fst, j-1);
qsort(arr, j+1, last);
}
}
OUTPUT
Enter total no. of the elements : 8
Enter total 8 elements :
1
2
7
-1
0
4
-2
3
Quick sorted elements
-2 -1 0 1 2 3 4 7
RESULT
Thus an array was sorted using quick sort's divide and conquer method.
81
Ex. No. 23
MERGE SORT
Date:
AIM
To sort an array of N numbers using Merge sort.
ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Divide the array into sub-arrays with a set of elements
5. Recursively sort the sub-arrays
6. Merge the sorted sub-arrays onto a single sorted array.
7. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void merge(int [],int ,int ,int );
void part(int [],int ,int );
int size;
main()
{
int i, arr[30];
printf("Enter total no. of elements : ");
scanf("%d", &size);
printf("Enter array elements : ");
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
part(arr, 0, size-1);
printf("\n Merge sorted list : ");
for(i=0; i<size; i++)
printf("%d ",arr[i]);
getch();
}
82
else
{
for(k=j; k<=mid; k++)
{
tmp[i] = arr[k]; i++;
}
}
for(k=min; k<=max; k++)
arr[k] = tmp[k];
}
OUTPUT
RESULT
Thus array elements was sorted using merge sort's divide and conquer method.
84
Ex. No. 24
INSERTION SORT
Date:
AIM
To sort an array of N numbers using Insertion sort.
ALGORITHM
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Sort the elements using insertion sort
In pass p, move the element in position p left until its correct place is found among the
first p + 1 elements.
Element at position p is saved in temp, and all larger elements (prior to position p) are
moved one spot to the right. Then temp is placed in the correct spot.
5. Stop
PROGRAM
main()
{
int i, j, k, n, temp, a[20], p=0;
printf("Enter total elements: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=1; i<n; i++)
{
temp = a[i]; j = i - 1;
while((temp<a[j]) && (j>=0))
{
a[j+1] = a[j]; j = j - 1;
}
a[j+1] = temp;
p++;
printf("\n After Pass %d: ", p);
for(k=0; k<n; k++)
printf(" %d", a[k]);
}
85
OUTPUT
RESULT
Thus array elements was sorted using insertion sort.
86
Ex. No. 25
OPEN ADDRESSING HASHING TECHNIQUE
Date:
AIM
To implement hash table using a C program.
ALGORITHM
1. Create a structure, data (hash table item) with key and value as data.
2. Now create an array of structure, data of some certain size (10, in this case). But, the
size of array must be immediately updated to a prime number just greater than initial
array capacity (i.e 10, in this case).
3. A menu is displayed on the screen.
4. User must choose one option from four choices given in the menu
5. Perform all the operations
6. Stop
PROGRAM
/* Open hashing */
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
main()
{
int a[MAX], num, key, i; char ans;
int create(int);
void linearprobing(int[], int, int);
void display(int[]);
printf("\nCollision handling by linear probing\n\n");
for(i=0; i<MAX; i++)
a[i] = -1;
do
{
printf("\n Enter number:");
scanf("%d", &num);
key = create(num);
linearprobing(a, key, num);
printf("\nwish to continue?(y/n):");
ans = getch();
}
87
OUTPUT
Collision handling by linear probing
Enter number:1
wish to continue?(y/n):
Enter number:26
wish to continue?(y/n):
Enter number:62
wish to continue?(y/n):
Enter number:93
wish to continue?(y/n):
Enter number:84
wish to continue?(y/n):
Enter number:15
wish to continue?(y/n):
Enter number:76
wish to continue?(y/n):
Enter number:98
wish to continue?(y/n):
Enter number:26
wish to continue?(y/n):
Enter number:199
wish to continue?(y/n):
Enter number:1234 wish to continue?(y/n):
Enter number:5678 hash table is full
Hash table is:
0 1234
1 1
2 62
3 93
4 84
5 15
6 26
7 76
8 98
9 199
RESULT
Thus hashing has been performed successfully.