0% found this document useful (0 votes)
27 views

Data Structure Lab Manual

Uploaded by

banushri914
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Data Structure Lab Manual

Uploaded by

banushri914
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

IMPACT COLLEGE OF ENGINEERING & APPLIED SCIENCES

Kodigehalli Post, Bangalore – 560 092

“LABORATORY MANUAL ON DATA STRUCTURES “

(BCSL305)

As Per VTU Revised Syllabus

Compiled by:

Banushri S

Asst. Professor,

Dept. of CS & E

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

2023 -2024
Program1: Develop a Program in C for the following: a) Declare a calendar as an array of 7 elements (A
dynamically Created array) to represent 7 days of a week. Each Element of the array is a structure
having three fields. The first field is the name of the Day (A dynamically allocated String), The second
field is the date of the Day (A integer), the third field is the description of the activity for a particular day
(A dynamically allocated String).

b) Write functions create(), read() and display(); to create the calendar, to read the data from the
keyboard and to print weeks activity details report on screen.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct day

char *name;

int date;

char *activity;

};struct day week[7];

void create()

for(int i=0;i<=7;i++)

week[i].name= (char*)malloc(20*sizeof(char));

week[i]. activity= (char*)malloc(20*sizeof(char));

printf("enter the name of day %d",i+1);

scanf("%s",week[i].name);

printf("enter the date of day%d",i+1);

scanf("%s",&week[i].date);

printf("enter the activity day%d",i+1);

getchar();

fgets(week[i].activity,100,stdin);

void read()

create();

void display(){
printf("\n day\t date\t activity\n");

for(int i=0;i<7;i++)

printf("%s\t%d\t%s",week[i].name,week[i].date,week[i].activity);

int main()

printf("creating calender\n");

create();

printf("displaying the calender\n");

display();

return 0;

Output:
Program 2: Develop a Program in C for the following operations on Strings.

a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)

b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if
PAT exists in STR. Report suitable messages in case PAT does not exist in STR Support the program
with functions for each of the above operations. Don't use Built-in functions.

#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
while(str[c] !='\0')
{
if(str[m] == pat[i])
{
i++;
m++;
if(pat[i] == '\0')
{
flag = 1;
for(k=0; rep[k]!='\0'; k++, j++)
{
ans[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
ans[j]= str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
}
void main()
{
printf("\nEnter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
stringmatch();
if(flag == 1)
printf("\nResultant string is %s", ans);
else
printf("\nPattern string is not found");
}

Output

Enter the main string: PatternMatching


Enter the pat string: Pat

Enter the replace string: Mat

Resultant string is MatternMatching

Enter the main string: PatternMatching

Enter the pat string: Tap

Enter the replace string: Mat

Pattern string is not found


Program 03 : Stack of Integers

Design, Develop and Implement a menu driven Program in C for the following operations on STACK of
Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. Exit
Support the program with appropriate functions for each of the above operations

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

int s[MAX];
int top = -1;

void push(int item);


int pop();
void palindrome();
void display();

void main()
{
int choice, item;
while(1)
{
printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
printf("\n=>1.Push an Element to Stack and Overflow demo ");
printf("\n=>2.Pop an Element from Stack and Underflow demo");
printf("\n=>3.Palindrome demo ");
printf("\n=>4.Display ");
printf("\n=>5.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter an element to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2: item = pop();
if(item != -1)
printf("\nElement popped is: %d", item);
break;
case 3: palindrome();
break;
case 4: display();
break;
case 5: exit(1);
default: printf("\nPlease enter valid choice ") ;
break;
}
}
}

void push(int item)


{
if(top == MAX-1)
{
printf("\n~~~~Stack overflow~~~~");
return;
}

top = top + 1 ;
s[top] = item;
}

int pop()
{
int item;
if(top == -1)
{
printf("\n~~~~Stack underflow~~~~");
return -1;
}
item = s[top];
top = top - 1;
return item;
}

void display()
{
int i;
if(top == -1)
{
printf("\n~~~~Stack is empty~~~~");
return;
}
printf("\nStack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}

void palindrome()
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);

printf("\nReverse of stack content are:\n");


for(i=0; i<=top; i++)
printf("| %d |\n", s[i]);

for(i=0; i<=top/2; i++)


{
if( s[i] != s[top-i] )
{
flag = 0;
break;
}
}
if(flag == 1)
{
printf("\nIt is palindrome number");
}
else
{
printf("\nIt is not a palindrome number");
}
}

Output
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 23

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 34

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 45

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1

Enter an element to be pushed: 60

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 24


~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 36

~~~~Stack overflow~~~~
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4

Stack elements are:


| 24 |
| 60 |
| 45 |
| 34 |
| 23 |

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

Element popped is: 24

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

Element popped is: 60

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

Element popped is: 45

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 2

Element popped is: 24

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

~~~~Stack underflow~~~~

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4

~~~~Stack is empty~~~~

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 12

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 23


~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 12

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 3

Stack content are:


| 12 |
| 23 |
| 12 |

Reverse of stack content are:


| 12 |
| 23 |
| 12 |

It is palindrome number

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

Element popped is: 12

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

Element popped is: 23

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2

Element popped is: 12

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 11

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 12

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1

Enter an element to be pushed: 13

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 3

Stack content are:


| 13 |
| 12 |
| 11 |

Reverse of stack content are:


| 11 |
| 12 |
| 13 |

It is not a palindrome number

~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 5
Program 04: Infix to Postfix Conversion

Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesised and free parenthesised expressions with the operators: +, -, *, /, %
(Remainder), ^ (Power) and alphanumeric operands.

#include<stdio.h>
#include<stdlib.h>

void evaluate();
void push(char);
char pop();
int prec(char);

char infix[30], postfix[30], stack[30];


int top = -1;

void main()
{
printf("\nEnter the valid infix expression:\t");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n", infix);
printf("\nThe corresponding postfix expression is :\n %s \n", postfix);
}

void evaluate()
{
int i = 0, j = 0;
char symb, temp;

push('#');

for(i=0; infix[i] != '\0'; i++)


{
symb = infix[i];
switch(symb)
{
case '(' : push(symb);
break;

case ')' : temp = pop();


while(temp != '(' )
{
postfix[j] = temp;
j++;
temp = pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '%' :
case '^' :
case '$' : while( prec(stack[top]) >= prec(symb) )
{
temp = pop();
postfix[j] = temp;
j++;
}
push(symb);
break;
default: postfix[j] = symb;
j++;
}
}
while(top > 0)
{
temp = pop();
postfix[j] = temp;
j++;
}
postfix[j] = '\0';
}

void push(char item)


{
top = top+1;
stack[top] = item;
}

char pop()
{
char item;
item = stack[top];
top = top-1;
return item;
}

int prec(char symb)


{
int p;
switch(symb)
{
case '#' : p = -1;
break;

case '(' :
case ')' : p = 0;
break;

case '+' :
case '-' : p = 1;
break;

case '*' :
case '/' :
case '%' : p = 2;
break;

case '^' :
case '$' : p = 3;
break;
}
return p;
}
Output

Enter the valid infix expression: (2+3)/6+7*9

The entered infix expression is :


(2+3)/6+7*9

The corresponding postfix expression is :


23+6/79*+
Program 05 : Stack Applications

Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int i, top = -1;

int op1, op2, res, s[20];

char postfix[90], symb;

void push(int item)

top = top+1;

s[top] = item;

int pop()

int item;

item = s[top];

top = top-1;

return item;

void main()

printf("\nEnter a valid postfix expression:\n");

scanf("%s", postfix);

for(i=0; postfix[i]!='\0'; i++)

symb = postfix[i];

if(isdigit(symb))

push(symb - '0');

else
{

op2 = pop();

op1 = pop();

switch(symb)

case '+': push(op1+op2);

break;

case '-': push(op1-op2);

break;

case '*': push(op1*op2);

break;

case '/': push(op1/op2);

break;

case '%': push(op1%op2);

break;

case '$':

case '^': push(pow(op1, op2));

break;

default : push(0);

res = pop();

printf("\n Result = %d", res);

Output

Enter a valid postfix expression:

657-+/82+*3$5-

Result = -5

Enter a valid postfix expression:

623+-382/+2$3+

Result = 52
#include<stdio.h>

#include<math.h>

void tower(int n, int source, int temp, int destination);

void tower(int n, int source, int temp, int destination)

if(n == 0)

return;

tower(n-1, source, destination, temp);

printf("\nMove disc %d from %c to %c", n, source, destination);

tower(n-1, temp, source, destination);

void main ()

int n;

printf("\nEnter the number of discs: \n\n");

scanf("%d", &n);

printf("\nThe sequence of moves involved in the Tower of Hanoi are\n");

tower(n, 'A', 'B', 'C');

printf("\n\nTotal Number of moves are: %d\n", (int)pow(2,n)-1);

Output

Enter the number of discs:

The sequence of moves involved in the Tower of Hanoi are

Move disc 1 from A to C

Move disc 2 from A to B

Move disc 1 from C to B

Move disc 3 from A to C

Move disc 1 from B to A

Move disc 2 from B to C

Move disc 1 from A to C

Total Number of moves are: 7


Program 06 : Circular Queue

Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters
(Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

#include <stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
#define MAX 3

char cq[MAX];
int front = -1, rear = -1;

void insert(char);
void delete();
void display();
void main()
{
int ch;
char item;
while(1)
{
printf("\n\n~~Main Menu~~");
printf("\n==> 1. Insertion and Overflow Demo");
printf("\n==> 2. Deletion and Underflow Demo");
printf("\n==> 3. Display");
printf("\n==> 4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
__fpurge(stdin);
switch(ch)
{
case 1: printf("\n\nEnter the element to be inserted: ");
scanf("%c", &item);
insert(item);
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n\nPlease enter a valid choice");
}
}
}

void insert(char item)


{
if(front == (rear+1)%MAX)
{
printf("\n\n~~Circular Queue Overflow~~");
}
else
{
if(front == -1)
front = rear = 0;
else
rear = (rear+1)%MAX;
cq[rear] = item;
}
}

void delete()
{
char item;
if(front == -1)
{
printf("\n\n~~Circular Queue Underflow~~");
}
else
{
item = cq[front];
printf("\n\nDeleted element from the queue is: %c ",item );

if(front == rear) //only one element


front = rear = -1;
else
front = (front+1)%MAX;
}
}

void display ()
{
int i ;
if(front == -1)
{
printf("\n\nCircular Queue Empty");
}
else
{
printf("\nCircular Queue contents are:\n");
printf("Front[%d]-> ", front);
for(i = front; i != rear ; i = (i+1)%MAX)
{
printf(" %c", cq[i]);
}
printf(" %c", cq[i]);
printf(" <-[%d]Rear", rear);
}
}

output

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 2

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1

Enter the element to be inserted: 4

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1

Enter the element to be inserted: 5

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1

Enter the element to be inserted: 3

~~Circular Queue Overflow~~

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit

Enter Your Choice: 3

Circular Queue contents are:


Front[0]-> 2 4 5 <-[2]Rear

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit

Enter Your Choice: 2

Deleted element from the queue is: 2

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2

Deleted element from the queue is: 4

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2

Deleted element from the queue is: 5

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2
~~Circular Queue Underflow~~

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit

Enter Your Choice: 2

~~Circular Queue Underflow~~

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3

Circular Queue Empty

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2

~~Circular Queue Underflow~~


~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 4
Program 07 : Singly Linked List of Student Data

Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student
Data with the fields: USN, Name, Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct stud
{
long long int ph;
int sem;
char name[15],usn[15],brnch[8];
struct stud *next;
}
*head=NULL,*tail=NULL,*temp=NULL,*temp1;
void create(long long int n,int s,char na[20],char u[15],char b[5])
{
if(head==NULL)
{
head=(struct stud*)malloc(1*sizeof(struct stud));
head->ph=n;
head->sem=s;
strcpy(head->name,na);
strcpy(head->usn,u);
strcpy(head->brnch,b);
head->next=NULL;
tail=head;
count++;
}
else
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=NULL;
tail->next=temp;
tail=temp;
count++;
}
}
void display()
{
temp1=head;
if(temp1==NULL)
{
printf("\nlist is empty\n");
}
else
{
printf("student details are as follows:\n");
while(temp1!=NULL)
{
printf(" \n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1->name,temp1-
>usn,temp1->brnch,temp1->sem,temp1->ph);
printf(" \n");
temp1=temp1->next;
}
printf("no. of nodes=%d\n",count);
}
}
void insert_head(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=head;
head=temp;
count++;
}
void insert_tail(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
tail->next=temp;
temp->next=NULL;
tail=temp;
count++;
}
void delete_head()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
else
{
head=head->next;
printf("deleted node is:\n");
printf(" \n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1->name,temp1-
>usn,temp1->brnch,temp1->sem,temp1->ph);
printf(" \n");
free(temp1);
count--;
}
}
void delete_tail()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
while(temp1->next!=tail)
{
temp1=temp1->next;
}
printf("deleted node is:\n"); printf(" \n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",tail->name,tail->usn,tail-
>brnch,tail->sem,tail->ph); printf(" \n");
free(tail);
tail=temp1;
tail->next=NULL;
count--;
}
void main()
{
int choice;
long long int ph; int sem;
char name[20],usn[15],brnch[5]; printf("--------MENU \n");
printf("1.create\n2.Insert from head\n3.Insert from tail\n4.Delete from head\n5.Delete fromtail\n6.display\
n7.exit\n");
printf(" \n");
while(1)
{
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
create(ph,sem,name,usn,brnch);
break;
case 2:
printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_head(ph,sem,name,usn,brnch);
break;
case 3:
printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_tail(ph,sem,name,usn,brnch);
break;
case 4:
delete_head();
break;
case 5:
delete_tail();
break;
case 6:
display();
break;
case 7:
exit(0);
default:printf("invalid option\n");
}
}
}

Output:
impact@impact-H110M-H:~$ gcc pga7.c
impact@impact-H110M-H:~$ ./a.out
--------MENU
1.create
2.Insert from head
3.Insert from tail
4.Delete from head
5.Delete fromtail
6.display
7.exit

enter your choice


1
enter the name usn branch sem phno. of the student respectively
an 1 cs 3 123
enter your choice
2
enter the name usn branch sem phno. of the student respectively
am 2 ise 3 234
enter your choice
3
enter the name usn branch sem phno. of the student respectively
as 3 ece 3 345
enter your choice
6
student details are as follows:

NAME:am
USN:2
BRANCH:ise
SEM:3
PHONE NO.:234

NAME:an
USN:1
BRANCH:cs
SEM:3
PHONE NO.:123

NAME:as
USN:3
BRANCH:ece
SEM:3
PHONE NO.:345

no. of nodes=3
enter your choice
4
deleted node is:

NAME:am
USN:2
BRANCH:ise
SEM:3
PHONE NO.:234
enter your choice
5
deleted node is:

NAME:as
USN:3
BRANCH:ece
SEM:3
PHONE NO.:345

enter your choice


6
student details are as follows:

NAME:an
USN:1
BRANCH:cs
SEM:3
PHONE NO.:123

no. of nodes=1
Program 08 : Doubly Linked List of Employee Data

Develop a menu driven Program in C for the following operations on Doubly Linked List(DLL) of
Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int ssn;
char name[20],department[20],designation[20],phone[20];
float salary;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
struct node *item;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void read()
{
item=getnode();
printf("ssn:");
scanf("%d",&item->ssn);
printf("name:");
scanf("%s",item->name);
printf("department:");
scanf("%s",item->department);
printf("designation:");
scanf("%s",item->designation);
printf("salary:");
scanf("%f",&item->salary);
printf("phone:");
scanf("%s",item->phone);
item->llink=item->rlink=NULL;
}
NODE insert_front(NODE first)
{
read();
if(first==NULL) return item;
item->rlink=first;
first->llink=item;
return item;
}
NODE insert_rear(NODE first)
{
NODE cur;
read();
if(first==NULL) return item;
cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
cur->rlink=item;
item->llink=cur;
return first;
}
NODE delete_front(NODE first)
{
NODE second;
if(first==NULL)
{
printf("employee list is empty\n");
return NULL;
}
if(first->rlink==NULL)
{
printf("employee details deleted:ssn:=%d\n",first->ssn);
free(first);
return NULL;
}
second=first->rlink;
second->llink=NULL;
printf("employee details deleted:ssn:=%d\n",first->ssn);
free(first);
return second;
}
NODE delete_rear(NODE first)
{
NODE cur,prev;
if(first==NULL)
{
printf("list is empty cannot delete\n");
return first;
}
if(first->rlink==NULL)
{
printf("employee details deleted:ssn:=%d\n",first->ssn);
free(first);
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur=cur->rlink;
}
printf("employee details deleted:ssn:=%d\n",cur->ssn);
free(cur);
prev->rlink=NULL;
return first;
}
void display(NODE first)
{
NODE temp,cur;
int count=0;
if(first==NULL)
{
printf("employee list is empty\n");
return;
}
cur=first;
while(cur!=NULL)
{
printf("%d %f %s %s %s %s\n",cur->ssn,cur->salary,cur->name,cur->department,cur->designation,cur-
>phone);
cur=cur->rlink;
count++;
}
printf("number of employees=%d\n",count);
}
void main()
{
NODE first;
int choice;
first=NULL;
for(;;)
{
printf("1:insert_front\n2:insert_rear\n");
printf("3:delete_front\n4:delete_rear\n");
printf("5:display\n6:exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:first=insert_front(first);
break;
case 2:first=insert_rear(first);
break;
case 3:first=delete_front(first);
break;
case 4:first=delete_rear(first);
break;
case 5:display(first);
break;
default:exit(0);
}
}
}

Output:
impact@impact-H110M-H:~$ gcc pg8.c
impact@impact-H110M-H:~$ ./a.out
1:insert_front
2:insert_rear
3:delete_front
4:delete_rear
5:display
6:exit
enter the choice
1
ssn:10
name:an
department:cse
designation:prof
salary:50
phone:123

1:insert_front
2:insert_rear
3:delete_front
4:delete_rear
5:display
6:exit
enter the choice
1
ssn:11
name:am
department:ise
designation:prof
salary:50
phone:234
1:insert_front
2:insert_rear
3:delete_front
4:delete_rear
5:display
6:exit

enter the choice


2
ssn:12
name:as
department:ece
designation:prof
salary:50
phone:345

enter the choice


5
11 50.000000 am ise prof 234
10 50.000000 an cse prof 123
12 50.000000 as ece prof 345
enter the choice
3
employee details deleted:ssn:=11
enter the choice
4
employee details deleted:ssn:=12
enter the choice
5
10 50.000000 an cse prof 123
number of employees=1
Program 09: Polynomial Evaluation and Addition

Develop a Program in C for the following operationson Singly Circular Linked List (SCLL)
with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

struct node

{ int coeff;

int expo;

struct node *ptr;

};

struct node *head1,*head2,*head3, *temp,*temp1,*temp2,*temp3,*list1,*list2,*list3;

struct node *dummy1,*dummy2;

void create_poly1(int , int);

void create_poly2(int , int);

void display();

void add_poly();

void eval_poly(int );

int n,ch;

int c,e,i;

void main()

{ int x; list1=list2=NULL;

printf("1.Create first polynomial\n2.Create Second Polynomial\n3.Display both the polynomials\n");

printf("4.Add Polynomials\n5.Evaluate a Polynomial\n6.Exit\n");

while(1)

{ printf("Enter choice\n");

scanf("%d",&ch);

switch(ch)
{ case 1: printf("Enter the number of terms\n");

scanf("%d",&n);

printf("Enter coefficient & power of each term\n");

for(i=0;i<n;i++)

scanf("%d%d",&c,&e);

create_poly1(c,e);

break;

case 2: printf("Enter the number of terms\n");

scanf("%d",&n);

printf("Enter coefficient & power of each term\n");

for(i=0;i<n;i++)

scanf("%d%d",&c,&e);

create_poly2(c,e);

break;

case 3:display();

break;

case 4:add_poly();

break;

case 5:printf("Enter the value for x\n");

scanf("%d",&x);

eval_poly(x);

break;

case 6:exit(0);

void create_poly1(int c, int e)

{ dummy1=(struct node*)malloc(1*sizeof(struct node));

dummy1->coeff=0;

dummy1->expo=0;
dummy1->ptr=list1;

if(list1==NULL)

{ list1=(struct node*)malloc(1*sizeof(struct node));

list1->coeff=c;

list1->expo=e;

list1->ptr=list1;

head1=list1;

head1->ptr=dummy1;

else

temp=(struct node*)malloc(1*sizeof(struct node));

temp->coeff=c;

temp->expo=e;

head1->ptr=temp;

temp->ptr=dummy1;

head1=temp;

void create_poly2(int c, int e)

{ dummy2=(struct node*)malloc(1*sizeof(struct node));

dummy2->coeff=0;

dummy2->expo=0;

dummy2->ptr=list2;

if(list2==NULL)

{ list2=(struct node*)malloc(1*sizeof(struct node));

list2->coeff=c;

list2->expo=e;

list2->ptr=list2;

head2=list2;

head2->ptr=dummy2;

else

{ temp=(struct node*)malloc(1*sizeof(struct node));


temp->coeff=c;

temp->expo=e;

head2->ptr=temp;

temp->ptr=dummy2;

head2=temp;

void add_poly()

{ temp1=list1;

temp2=list2;

while((temp1!=dummy1)&&(temp2!=dummy2))

{ temp=(struct node*)malloc(1*sizeof(struct node));

if(list3==NULL)

{ list3=temp;

head3=list3;

if(temp1->expo==temp2->expo)

{ temp->coeff=temp1->coeff+temp2->coeff;

temp->expo=temp1->expo;

temp->ptr=list3;

head3->ptr=temp;

head3=temp;

temp1=temp1->ptr;

temp2=temp2->ptr;

else if(temp1->expo>temp2->expo)

{ temp->coeff=temp1->coeff;

temp->expo=temp1->expo;

temp->ptr=list3;

head3->ptr=temp;

head3=temp;

temp1=temp1->ptr;

else
{ temp->coeff=temp2->coeff;

temp->expo=temp2->expo;

temp->ptr=list3;

head3->ptr=temp;

head3=temp;

temp2=temp2->ptr;

if(temp1==dummy1)

{ while(temp2!=dummy2)

{ temp=(struct node*)malloc(1*sizeof(struct node));

temp->coeff=temp2->coeff;

temp->expo=temp2->expo;

temp->ptr=list3;

head3->ptr=temp;

head3=temp;

temp2=temp2->ptr;

if(temp2==dummy2)

{ while(temp1!=dummy1)

{ temp=(struct node*)malloc(1*sizeof(struct node));

temp->coeff=temp1->coeff;

temp->expo=temp1->expo;

temp->ptr=list3;

head3->ptr=temp;

head3=temp;

temp1=temp1->ptr;

void display()

{ temp1=list1;

temp2=list2;
temp3=list3;

printf("\nPOLYNOMIAL 1:");

while(temp1!=dummy1)

{ printf("%dX^%d+",temp1->coeff,temp1->expo);

temp1=temp1->ptr;

printf("\b ");

printf("\nPOLYNOMIAL 2:");

while(temp2!=dummy2)

{ printf("%dX^%d+",temp2->coeff,temp2->expo);

temp2=temp2->ptr;

printf("\b ");

printf("\n\nSUM OF POLYNOMIALS:\n");

while(temp3->ptr!=list3)

{ printf("%dX^%d+",temp3->coeff,temp3->expo);

temp3=temp3->ptr;

printf("%dX^%d\n",temp3->coeff,temp3->expo);

void eval_poly(int x)

{ int result=0;

temp1=list1;

temp2=list2;

while(temp1!=dummy1)

{ result+=(temp1->coeff)*pow(x,temp1->expo);

temp1=temp1->ptr;

printf("Polynomial 1 Evaluation:%d\n",result);

result=0;

while(temp2!=dummy2)

{ result+=(temp2->coeff)*pow(x,temp2->expo);

temp2=temp2->ptr;

}
printf("Polynomial 2 Evaluation:%d\n",result);

Output:

impact@impact-H110M-H:~$ gcc pg9.c -lm

impact@impact-H110M-H:~$ ./a.out

1.Create first polynomial

2.Create Second Polynomial

3.Display both the polynomials

4.Add Polynomials

5.Evaluate a Polynomial

6.Exit

Enter choice

Enter the number of terms

Enter coefficient & power of each term

63

10 2

01

Enter choice

Enter the number of terms

Enter coefficient & power of each term

63

10 2

01

Enter choice

Enter the number of terms

Enter coefficient & power of each term


63

42

21

60

Enter choice

Enter choice

Enter the value for x

Polynomial 1 Evaluation:16

Polynomial 2 Evaluation:18

Enter choice

POLYNOMIAL 1:6X^3+10X^2+0X^1

POLYNOMIAL 2:6X^3+4X^2+2X^1+6X^0

SUM OF POLYNOMIALS:

12X^3+14X^2+2X^1+6X^0
Program 10 : Binary Search Tree

Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of
Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit

#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE *node;
NODE* createtree(NODE *node, int data)
{
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (node->data))
{
node->left = createtree(node->left, data);
}
else if (data > node->data)
{
node -> right = createtree(node->right, data);
}
return node;
}
NODE* search(NODE *node, int data)
{
if(node == NULL)
printf("\nElement not found");
else if(data < node->data)
{
node->left=search(node->left, data);
}
else if(data > node->data)
{
node->right=search(node->right, data);
}
else
printf("\nElement found is: %d", node->data);
return node;
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
}
}
void preorder(NODE *node)
{
if(node != NULL)
{
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(NODE *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}
NODE* findMin(NODE *node)
{
if(node==NULL)
{
return NULL;
}
if(node->left)
return findMin(node->left);
else
return node;
}
NODE* del(NODE *node, int data)
{
NODE *temp;
if(node == NULL)
{
printf("\nElement not found");
}
else if(data < node->data)
{
node->left = del(node->left, data);
}
else if(data > node->data)
{
node->right = del(node->right, data);
}
else
{
if(node->right && node->left)
{
temp = findMin(node->right);
node -> data = temp->data;
node -> right = del(node->right,temp->data);
}
else
{
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp);
}
}
return node;
}
void main()
{
int data, ch, i, n;
NODE *root=NULL;

while (1)
{
printf("\n1.Insertion in Binary Search Tree"); printf("\n2.Search Element in Binary Search Tree");
printf("\n3.Delete Element in Binary Search Tree");
printf("\n4.Inorder\n5.Preorder\n6.Postorder\n7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root=createtree(root, data);
}
break;
case 2:printf("\nEnter the element to search: ");
scanf("%d", &data);
root=search(root, data);
break;
case 3:printf("\nEnter the element to delete: ");
scanf("%d", &data);
root=del(root, data);
break;
case 4:printf("\nInorder Traversal: \n");
inorder(root);
break;
case 5:printf("\nPreorder Traversal: \n");
preorder(root);
break;
case 6:printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 7:exit(0);
default : printf("\nWrong option");
break;
}
}
Output

impact@impact-H110M-H:~$ gcc pg10.c


impact@impact-H110M-H:~$ ./a.out
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 1
Enter N value: 12
Enter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)
6 9 5 2 8 15 24 14 7 8 5 2

1.Insertion in Binary Search Tree


2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 2

Enter the element to search: 8

Element found is: 8


1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 4

Inorder Traversal:
2 5 6 7 8 9 14 15 24
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 5
Preorder Traversal:
6 5 2 9 8 7 15 14 24
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 6

Postorder Traversal:
2 5 7 8 14 24 15 9 6
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 3

Enter the element to delete: 6

1.Insertion in Binary Search Tree


2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 4

Inorder Traversal:
2 5 7 8 9 14 15 24
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 5

Preorder Traversal:
7 5 2 9 8 15 14 24
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 6

Postorder Traversal:
2 5 8 14 24 15 9 7
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Program 11 : Graph Reachability using DFS/BFS

Develop a Program in C for the following operations on Graph(G) of Cities


a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method

#include <stdio.h>

#include <stdlib.h>

int a[20][20],q[20],visited[20],reach[10],n,i,j,f=0,r=-1,count=0;

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 dfs(int v)

int i; reach[v]=1;

for(i=1;i<=n;i++)

if(a[v][i] && !reach[i])


{

printf("\n %d->%d",v,i);

count++;

dfs(i);

void main()

int v, choice;

printf("\n Enter the number of vertices:");

scanf("%d",&n);

for(i=1;i<=n;i++)

q[i]=0;

visited[i]=0;

for(i=1;i<=n-1;i++)

reach[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("1.BFS\n 2.DFS\n 3.Exit\n");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\n Enter the starting vertex:");

scanf("%d",&v);

bfs(v);

if((v<1)||(v>n))

printf("\n Bfs is not possible");


}

else

printf("\n The nodes which are reachable from %d:\n",v);

for(i=1;i<=n;i++)

if(visited[i])

printf("%d\t",i);

break;

case 2:

dfs(1);

if(count==n-1)

printf("\n Graph is connected");

else

printf("\n Graph is not connected");

break;

case 3:

exit(0);

Output:

impact@impact-H110M-H:~$ gcc pg11.c

impact@impact-H110M-H:~$ ./a.out

Enter the number of vertices:5

Enter graph data in matrix form:

01010

10101

01010

10100

01000

1.BFS

2.DFS
3.Exit

Enter the starting vertex:1

The nodes which are reachable from 1:

1 2 3 4 5

impact@impact-H110M-H:~$ gcc pg11.c

impact@impact-H110M-H:~$ ./a.out

Enter the number of vertices:5

Enter graph data in matrix form:

01010

10101

01010

10100

01000

1.BFS

2.DFS

3.Exit

1->2

2->3

3->4

2->5

Graph is connected
Program 12: Hashing & Linear Probing

Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in
file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as
the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are
Integers. Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder
method), and implement hashing technique to map a given key K to the address space L. Resolve the
collision (if any) using linear probing.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int create(int);
void display (int[]);
void main()
{ int a[MAX],num,key,i;
int ans=1;
printf(" collision handling by linear probing : \n");
for (i=0;i<MAX;i++)
{ a[i] = -1;
}
do
{ printf("\n Enter the data");
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) ");
CSE DEPT,MSEC Page 48
scanf("%d",&ans);
}while(ans);
display(a);
}
int create(int num)
{ int key;
key=num%100;
return key;
}
void linear_prob(int a[MAX], int key, int num)
{ int flag, i, count=0;
flag=0;
if(a[key]== -1)
{ a[key] = num;
}
else
{ printf("\nCollision Detected...!!!\n");
i=0;
while(i<MAX)
{ if (a[i]!=-1)
count++;
i++;
}
printf("Collision avoided successfully using LINEAR PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
display(a);
exit(1);
}
for(i=key+1; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag =1;break;
}
i=0;
while((i<key) && (flag==0))
{ if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
}
i++;
}
}
}
void display(int a[MAX])
{ int i,choice;
printf("1.Display ALL\n 2.Filtered Display\n");
scanf("%d",&choice);
if(choice==1)
{ printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
printf("\n %d %d ", i, a[i]);
}
else
{ printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
if(a[i]!=-1)
{ printf("\n %d %d ", i, a[i]);
continue;
}
}
}

Output:A
impact@impact-H110M-H:~$ gcc pg12.c
impact@impact-H110M-H:~$ ./a.out
collision handling by linear probing :

Enter the data1234

Do you wish to continue ? (1/0) 1

Enter the data2548

Do you wish to continue ? (1/0) 1

Enter the data3256

Do you wish to continue ? (1/0) 1

Enter the data1299

Do you wish to continue ? (1/0) 1


Enter the data1298

Do you wish to continue ? (1/0) 1

Enter the data1398

Collision Detected...!!!
Collision avoided successfully using LINEAR PROBING

Do you wish to continue ? (1/0) 0


1.Display ALL
2.Filtered Display
2

the hash table is

0 1398
34 1234
48 2548
56 3256
98 1298
99 1299

You might also like