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

DS Lab Programs 1-12

The document describes a C program to implement a calendar using a dynamically allocated array of structures. It includes functions to create, read and display the calendar. It also describes a C program to perform string operations like pattern matching and replacement without built-in functions. Finally, it outlines a menu driven program to perform stack operations on integers using an array implementation including push, pop, palindrome checking and overflow/underflow handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

DS Lab Programs 1-12

The document describes a C program to implement a calendar using a dynamically allocated array of structures. It includes functions to create, read and display the calendar. It also describes a C program to perform string operations like pattern matching and replacement without built-in functions. Finally, it outlines a menu driven program to perform stack operations on integers using an array implementation including push, pop, palindrome checking and overflow/underflow handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

1.

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>

// Structure to represent a day in the calendar


struct Day
{
char *name;
int date;
char *activity;
};

// Function to create a day in the calendar


struct Day create()
{
struct Day day;
day.name = (char *)malloc(20 * sizeof(char)); // Allocating memory for the name
day.activity = (char *)malloc(100 * sizeof(char)); // Allocating memory for the activity
printf("Enter the day name: ");
scanf("%s", day.name);
printf("Enter the date: ");
scanf("%d", &day.date);
printf("Enter the activity for the day: ");
scanf(" %[^\n]", day.activity);
return day;
}
// Function to read data for the calendar
void read(struct Day calendar[], int size)
{
for (int i = 0; i < size; i++)
{
calendar[i] = create();
}
}

// Function to display the weekly activity details report


void display(struct Day calendar[], int size)
{
printf("\nWeekly Activity Details:\n");
for (int i = 0; i < size; i++)
{
printf("Day %d: %s\n", i + 1, calendar[i].name);
printf("Date: %d\n", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
printf("\n");
}
}

int main()
{
int weekSize = 7;
struct Day calendar[weekSize];
// Create the calendar
read(calendar, weekSize);
// Display the weekly activity details
display(calendar, weekSize);
// Free dynamically allocated memory
for (int i = 0; i < weekSize; i++)
{
free(calendar[i].name);
free(calendar[i].activity);
}
return 0;
}
Sample Output
Enter the day name: Monday
Enter the date: 161023
Enter the activity for the day: Reading
Enter the day name: Tuesday
Enter the date: 171023
Enter the activity for the day: Coding
Enter the day name: Wednesday
Enter the date: 181023
Enter the activity for the day: Painting
Enter the day name: Thursday
Enter the date: 191023
Enter the activity for the day: playing
Enter the day name: Friday
Enter the date: 201023
Enter the activity for the day: shopping
Enter the day name: Saturday
Enter the date: 211023
Enter the activity for the day: Watching Movie
Enter the day name: Sunday
Enter the date: 221023
Enter the activity for the day: Completing Assignments

Weekly Activity Details:


Day 1: Monday
Date: 161023Activity: Reading

Day 2: Tuesday
Date: 171023
Activity: Coding

Day 3: Wednesday
Date: 181023
Activity: Painting

Day 4: Thursday
Date: 191023
Activity: playing

Day 5: Friday
Date: 201023
Activity: shopping

Day 6: Saturday
Date: 211023
Activity: Watching Movie

Day 7: Sunday
Date: 221023
Activity: Completing Assignments
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[100],pat[50],rep[50], ans[100];
int i, j, c, m, k, flag=0;
void stringmatch()
{
i=m=c=j=0;
while(str[c] != '\0')
{
if(str[m]==pat[i]) // matching
{
i++; m++;
if(pat[i] =='\0') //found occurrences.
{
flag =1;
...... //copy replace string in ans string.
for(k = 0; rep[k] != '\0'; k++, j++)
ans[j] = rep[k];
i=0;
c=m;
}
} //if ends.
else //...mismatch
{
ans[j] = str[c];
j++;c++;
m = c; i = 0;
} //else ends
} //end of while ans[j] = '\0';
} //end stringmatch()

void main()
{
printf("\nEnter a main string \n");
gets(str);
printf("\nEnter a pattern string \n");
gets(pat);
printf("\nEnter a replace string \n");
gets(rep);
stringmatch();
if(flag = = 1)
printf("\nThe resultant string is\n %s" , ans);
else
printf("\n Pattern string NOT found\n");
} // end of main

SAMPLEOUTPUT:

RUN 1:
Enter a main string
Test
Enter a pattern string
Te
Enter a replace string
Re
The resultant string is
Rest

RUN 2:
Enter a main string
This is Data Structure lab
Enter a pattern string
Data Structure
Enter a replace string
Data structure with C
The resultant string is
This is Data Structure with C lab
3. Develop 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 how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. 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("\n Stack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
{
int flag=1,i;
printf("\n Stack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);

printf("\n Reverse 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: 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: 1
Enter an element to be pushed: 14

~~~~~~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: 15

~~~~~~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: 16
~~~~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:


| 15 |
| 14 |
| 13 |
| 12 |
| 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: 2
Element popped is: 15

~~~~~~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:
| 14 |
| 13 |
| 12 |
| 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: 2
Element popped is: 14

~~~~~~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: 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: 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: 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: 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: 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: 22

~~~~~~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: 3
Stack content are:
| 11 |
| 22 |
| 11 |

Reverse of stack content are:


| 11 |
| 22 |
| 11 |

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: 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: 2
Element popped is: 22

~~~~~~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: 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: 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: 22

~~~~~~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: 33

~~~~~~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:
| 33 |
| 22 |
| 11 |

Reverse of stack content are:


| 11 |
| 22 |
| 33 |

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
4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized expressions with the operators:
+, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands.

#include<stdio.h>
#include<string.h>
int F(char symbol)
{
switch(symbol)
{
case'+':
case'-': return2;

case'*':
case'/': return4;

case'^':
case'$': return5;

case'(': return 0;
case'#': return -1;
default: return 8;
}
int G(char symbol)
{
switch(symbol)
{
case'+':
case'-': return 1;

case'*':
case'/': return 3;

case'^':
case'$': return 6;

case'(': return 9;

case')': return 0;

default: return7;
}
}
void infix_postfix(char infix[], char postfix[])
{
int top ,j, i;
chars[30],symbol;
top=-1;
s[++top]='#';
j=0;
for(i=0; i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j] = s[top--];j++;
}
if(F(s[top]) != G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}

void main()
{
char infix[20],postfix[20];clrscr();
printf("\nEnter a valid infix expression\n");
gets(infix);
infix_postfix(infix,postfix);
printf("\nThe infix expression is:\n");
printf("%s",infix);
printf("\nThe postfix expression is:\n");printf("%s",postfix);
}

SAMPLEOUTPUT:
Enter a valid infix expression (a+(b-c)*d)
The infix expression is :(a+(b-c)*d)
The postfix expression is: abc-d*+
5. Develop 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<math.h>
#include<string.h>
double compute(charsymbol,doubleop1,doubleop2)
{
switch(symbol)
{
case'+': return op1+op2;
case'-': return op1-op2;
case'*': return op1*op2;
case'/': return op1 / op2;
case'$':
case'^': return pow (op1,op2);
default: return 0;
}
}
void main()
{
double s[20],res,op1,op2;
int top, i;
char postfix[20],symbol;
printf("\nEnter the postfix expression:\n");
gets(postfix);
top= -1;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
s[++top]=symbol-'0';
else
{
op2=s[top--];
op1=s[top--];
res = compute(symbol, op1, op2);
s[++top]=res;
}
}
res= s[top--];
printf("\nThe result is : %f\n", res);
}
SAMPLEOUTPUT:
RUN1:
Enter the postfix expression: 23+
The result is:5.000000

RUN2:
Enter the postfix expression: 23+7*
The result is:35.000000

PROGRAM 5B:

#include<stdio.h>
void tower(int n, int source, int temp ,int destination)
{
if(n = = 0)
return;
tower(n-1, source, destination, temp);
printf("\n Move 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");
scanf("%d", &n);
tower(n, 'A','B', 'C');
printf("\n\n Total Number of moves are:%d",(int)pow(2,n)-1);
}

SAMPLE OUTPUT:

Enter the number of discs: 3


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


6. 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>
#define MAX 4
int ch, front = 0, rear = -1, count=0;
char q[MAX];
int item;
void insert(int item)
{
if(count ==MAX)
{
printf("\n Queue is Full");
return;
}
else
{
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
{
printf("\n Queue is Empty");
return;
}
else
{
item=q[front];
printf("\n Deleted item is: %d",item);
front = (front + 1) % MAX;
count--;
}
}
void display()
{
int i, f=front, r=rear;
if(count == 0)
printf("\n Queue is Empty");
else
{
printf("\n Contents of Queue is:\n");
for(i=0; i<=count; i++)
{
printf("%d\t", q[f]);
f = (f + 1) % MAX;
}
}
}
void main()
{
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the character / item to be inserted: ");
scanf("%d", &item);
insert(item);
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
}
}while(ch!=4);
}
SAMPLE POUTPUT:
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1
Enter the character / item to be inserted: 1

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1


Enter the character / item to be inserted: 2

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1


Enter the character / item to be inserted: 3

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1


Enter the character / item to be inserted: 4
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3
Contents of Queue is:
1 2 3 4
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1
Enter the character / item to be inserted: 5
Queue is Full
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 2
Deleted item is: 1
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 2
Deleted item is: 2
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3
Contents of Queue is:
3 4
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1
Enter the character / item to be inserted: 5
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3
Contents of Queue is:
3 4 5
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 4
7. 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>

struct node
{
char usn[25],name[25],branch[25];
int sem;
long int phone;
struct node *link;
};
typedef struct node * NODE;

NODE start = NULL;


int count=0;

NODE create()
{
NODE snode;
snode = (NODE)malloc(sizeof(struct node));

if(snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch, &snode->sem, &snode-
>phone);
snode->link=NULL;
count++;
return snode;
}

NODE insertfront()
{
NODE temp;
temp = create();
if(start == NULL)
{
return temp;
}

temp->link = start;
return temp;
}
NODE deletefront()
{
NODE temp;
if(start == NULL)
{
printf("\nLinked list is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe Student node with usn:%s is deleted ",start->usn);
count--;
free(start);
return NULL;
}
temp = start;
start = start->link;
printf("\nThe Student node with usn:%s is deleted",temp->usn);
count--;
free(temp);
return start;
}
NODE insertend()
{
NODE cur,temp;
temp = create();

if(start == NULL)
{
return temp;
}
cur = start;
while(cur->link !=NULL)
{
cur = cur->link;
}
cur->link = temp;
return start;
}
NODE deleteend()
{
NODE cur,prev;
if(start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe student node with the usn:%s is deleted",start->usn);
free(start);
count--;
return NULL;
}
prev = NULL;
cur = start;
while(cur->link!=NULL)
{
prev = cur;
cur = cur->link;
}
printf("\nThe student node with the usn:%s is deleted",cur->usn);
free(cur);
prev->link = NULL;
count--;
return start;
}
void display()
{
NODE cur;
int num=1;
if(start == NULL)
{
printf("\nNo Contents to display in SLL \n");
return;
}
printf("\nThe contents of SLL: \n");
cur = start;
while(cur!=NULL)
{
printf("\n||%d|| USN:%s| Name:%s| Branch:%s| Sem:%d| Ph:%ld|",num,cur->usn, cur->name,cur-
>branch, cur->sem,cur->phone);
cur = cur->link;
num++;
}
printf("\n No of student nodes is %d \n",count);
}
void stackdemo()
{
int ch;
while(1)
{
printf("\n~~~Stack Demo using SLL~~~\n");
printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");
printf("\nEnter your choice for stack demo");
scanf("%d",&ch);
switch(ch)
{
case 1: start = insertfront();
break;
case 2: start = deletefront();
break;
case 3: display();
break;
default : return;
}
}
return;
}

int main()
{
int ch, i, n;
while(1)
{
printf("\n~~~Menu~~~");
printf("\nEnter your choice for SLL operation \n");
printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
printf("\nEnter your choice:");
scanf("%d", &ch);

switch(ch)
{
case 1 : printf("\nEnter the no of students: ");
scanf("%d", &n);
for(i=1;i<=n; i++)
start = insertfront();
break;

case 2: display();
break;

case 3: start = insertend();


break;

case 4: start = deleteend();


break;

case 5: stackdemo();
break;

case 6: exit(0);

default: printf("\nPlease enter the valid choice");

}
}
}
8. Design, Develop and Implement 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>
struct node
{
char ssn[25],name[25],dept[10],designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
NODE first = NULL;
int count=0;
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)
{
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept, enode->designation, &enode-
>sal, &enode->phone);
enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}
void display()
{
NODE cur;
int nodeno=1;
cur = first;
if(cur == NULL)
printf("\nNo Contents to display in DLL");
while(cur!=NULL)
{
printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone no:%ld",
nodeno, cur->ssn, cur->name,cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d",count);
}
NODE deletefront()
{
NODE temp;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink== NULL)
{
printf("\nThe employee node with the ssn:%s is deleted", first->ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;
temp->rlink = NULL;
first->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",temp->ssn);
free(temp);
count--;
return first;
}
NODE insertend()
{
NODE cur, temp;
temp = create();

if(first == NULL)
{
return temp;
}
cur= first;
while(cur->rlink!=NULL)
{
cur = cur->rlink;
}

cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE deleteend()
{
NODE prev,cur;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}

if(first->rlink == NULL)
{
printf("\nThe employee node with the ssn:%s is deleted",first->ssn);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;

while(cur->rlink!=NULL)
{
prev=cur;
cur = cur->rlink;
}
cur->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",cur->ssn);
free(cur);
prev->rlink = NULL;
count--;
return first;
}
void deqdemo()
{
int ch;
while(1)
{
printf("\nDemo Double Ended Queue Operation");
printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n 3:InsertQueueRear\n 4:DeleteQueueRear\n
5:DisplayStatus\n 6: Exit \n");
scanf("%d", &ch);
switch(ch)
{
case 1: first=insertfront();
break;
case 2: first=deletefront();
break;
case 3: first=insertend();
break;
case 4: first=deleteend();
break;
case 5: display();
break;
default : return;
}
}
}
void main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n7:Double Ended Queue Demo using DLL");
printf("\n8:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of Employees: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;
case 2: display();
break;
case 3: first = insertend();
break;
case 4: first = deleteend();
break;
case 5: first = insertfront();
break;
case 6: first = deletefront();
break;
case 7: deqdemo();
break;
case 8 : exit(0);
default: printf("\n Please Enter the valid choice");
}
}
}
9. Design, Develop and Implement a Program in C for the following operations on 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>
#define COMPARE(x, y) ( (x == y) ? 0 : (x > y) ? 1 : -1)
struct node
{
int coef;
int xexp, yexp, zexp;
struct node *link;
};
typedef struct node *NODE;

NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if(x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}

NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
cur = head->link;
while(cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}

NODE read_poly(NODE head)


{
int i, j, coef, xexp, yexp, zexp, n;
printf("\nEnter the no of terms in the polynomial: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
printf("\n\tEnter the %d term: ",i);
printf("\n\t\tCoef = ");
scanf("%d", &coef);
printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): ");
scanf("%d", &xexp);
scanf("%d", &yexp);
scanf("%d", &zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}

void display(NODE head)


{
NODE temp;
if(head->link == head)
{
printf("\nPolynomial does not exist.");
return;
}
temp = head->link;

while(temp != head)
{
printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp, temp->yexp, temp->zexp);
temp = temp->link;
if(temp != head)
printf(" + ");
}
}

int poly_evaluate(NODE head)


{
int x, y, z, sum = 0;
NODE poly;

printf("\nEnter the value of x,y and z: ");


scanf("%d %d %d", &x, &y, &z);
poly = head->link;
while(poly != head)
{
sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp) * pow(z,poly->zexp);
poly = poly->link;
}
return sum;
}

NODE poly_sum(NODE head1, NODE head2, NODE head3)


{
NODE a, b;
int coef;
a = head1->link;
b = head2->link;

while(a!=head1 && b!=head2)


{
while(1)
{
if(a->xexp == b->xexp && a->yexp == b->yexp && a->zexp == b->zexp)
{
coef = a->coef + b->coef;
head3 = attach(coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
b = b->link;
break;
} //if ends here
if(a->xexp!=0 || b->xexp!=0)
{
switch(COMPARE(a->xexp, b->xexp))
{
case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;

case 0 : if(a->yexp > b->yexp)


{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->yexp < b->yexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
else if(a->zexp > b->zexp)
{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->zexp < b->zexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
case 1 : head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
break;
} //switch ends here
break;
} //if ends here
if(a->yexp!=0 || b->yexp!=0)
{
switch(COMPARE(a->yexp, b->yexp))
{
case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
case 0 : if(a->zexp > b->zexp)
{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->zexp < b->zexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
break;
}
if(a->zexp!=0 || b->zexp!=0)
{
switch(COMPARE(a->zexp,b->zexp))
{
case -1 : head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
break;
case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
break;
}
}
}
while(a!= head1)
{
head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
}
while(b!= head2)
{
head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
}
return head3;
}
void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode(); /* For polynomial evalaution */
head1 = getnode(); /* To hold POLY1 */
head2 = getnode(); /* To hold POLY2 */
head3 = getnode(); /* To hold POLYSUM */

head->link=head;
head1->link=head1;
head2->link=head2;
head3->link= head3;

while(1)
{
printf("\n~~~Menu~~~");
printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n~~~~Polynomial evaluation P(x,y,z)~~~\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;

case 2: printf("\nEnter the POLY1(x,y,z): \n");


head1 = read_poly(head1);
printf("\nPolynomial 1 is: \n");
display(head1);

printf("\nEnter the POLY2(x,y,z): \n");


head2 = read_poly(head2);
printf("\nPolynomial 2 is: \n");
display(head2);

printf("\nPolynomial addition result: \n");


head3 = poly_sum(head1,head2,head3);
display(head3);
break;
case 3: exit(0);
}
}
}

Output:
~~~Menu~~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z)
Enter your choice: 1

~~~~Polynomial evaluation P(x,y,z)~~~


Enter the no of terms in the polynomial: 5
Enter the 1 term:
Coef = 6
Enter Pow(x) Pow(y) and Pow(z): 2 2 1
Enter the 2 term:
Coef = -4
Enter Pow(x) Pow(y) and Pow(z): 0 1 5
Enter the 3 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 3 1 1
Enter the 4 term:
Coef = 2
Enter Pow(x) Pow(y) and Pow(z): 1 5 1
Enter the 5 term:
Coef = -2
Enter Pow(x) Pow(y) and Pow(z): 1 1 3
Representation of Polynomial for evaluation:
6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 + -2x^1y^1z^3
Enter the value of x,y and z: 1 1 1
Result of polynomial evaluation is : 5

~~~Menu~~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z)
Enter your choice: 2

Enter the POLY1(x,y,z):


Enter the no of terms in the polynomial: 5
Enter the 1 term:
Coef = 6
Enter Pow(x) Pow(y) and Pow(z): 4 4 4
Enter the 2 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 4 3 1
Enter the 3 term:
Coef = 5
Enter Pow(x) Pow(y) and Pow(z): 0 1 1
Enter the 4 term:
Coef = 10
Enter Pow(x) Pow(y) and Pow(z): 0 1 0
Enter the 5 term:
Coef = 5
Enter Pow(x) Pow(y) and Pow(z): 0 0 0
Polynomial 1 is:
6x^4y^4z^4 + 3x^4y^3z^1 + 5x^0y^1z^1 + 10x^0y^1z^0 + 5x^0y^0z^0

Enter the POLY2(x,y,z):


Enter the no of terms in the polynomial: 5
Enter the 1 term:
Coef = 8
Enter Pow(x) Pow(y) and Pow(z): 4 4 4
Enter the 2 term:
Coef = 4
Enter Pow(x) Pow(y) and Pow(z): 4 2 1
Enter the 3 term:
Coef = 30
Enter Pow(x) Pow(y) and Pow(z): 0 1 0
Enter the 4 term:
Coef = 20
Enter Pow(x) Pow(y) and Pow(z): 0 0 1
Enter the 5 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 0 0 0
Polynomial 2 is:
8x^4y^4z^4 + 4x^4y^2z^1 + 30x^0y^1z^0 + 20x^0y^0z^1 + 3x^0y^0z^0

Polynomial addition result:


14x^4y^4z^4 + 3x^4y^3z^1 + 4x^4y^2z^1 + 5x^0y^1z^1 + 40x^0y^1z^0 + 20x^0y^0z^1 +
8x^0y^0z^0

~~~Menu~~~
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z)
Enter your choice: 3
10. Design, Develop and Implement 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 messaged.
d. Exit
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *lchild;
int data;
struct node *rchild;
};
typedef struct node* NODE;

NODE root = NULL;


NODE create_node()
{
NODE ptr;
ptr = (NODE)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("Insufficient memory\n");
exit(0);
}
else
{
printf("Enter data:: ");
scanf("%d", &ptr->data);
ptr->lchild = ptr->rchild = NULL;
}
return ptr;
}
NODE create_bst(NODE root)
{
NODE ptr, temp, p;
ptr = create_node();
if (root == NULL)
{
root = ptr;
return root;
}
p = NULL;
temp = root;
while (temp != NULL)
{
p = temp;
if (ptr->data == temp->data)
{
printf("Duplicate items are not allowed\n\n");
free(ptr);
return root;
}
if (ptr->data < temp->data)
temp = temp->lchild;
else
temp = temp->rchild;
}
if (ptr->data < p->data)
p->lchild = ptr;
else
p->rchild = ptr;
return root;
}
int search(NODE root, int key)
{
if (root == NULL)
return 0; // Element not found
if (root->data == key)
return 1; // Element found
else if (key < root->data)
return search(root->lchild, key);
else
return search(root->rchild, key);
}
void inorder(NODE t)
{
if (t != NULL)
{
inorder(t->lchild);
printf("%d\t", t->data);
inorder(t->rchild);
}
}
void preorder(NODE t)
{
if(t!=NULL)
{
printf("%d\t",t->data);
preorder(t->lchild);
preorder(t->rchild);
}/* end of if*/
}/* end of preorder*/
void postorder(NODE t)
{
if(t!=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%d\t",t->data);
}/* end of if*/
}/* end of postorder*/
void traverse(NODE root)
{
if(root==NULL)
printf("Empty tree\n\n");
else
{
printf("\n\nPreorder traversal:\t");
preorder(root);
printf("\n\nInorder traversal:\t");
inorder(root);
printf("\n\nPostorder traversal:\t");
postorder(root);
}/* end of else*/
}/* end of traversal function*/
void main()
{
int ch, n, i, key, result;
for (;;)
{
printf("\n\nOPERATIONS ON BST\n\n");
printf("\n\n1:CREATE BST \n2:TRAVERSE BST\n3:SEARCH\n4:EXIT\n\n");
printf("Enter your choice::");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter number of elements in BST::");
scanf("%d", &n);
for (i = 0; i < n; i++)
root = create_bst(root);
break;
case 2:
traverse(root);
break;
case 3:
printf("Enter the element to search: ");
scanf("%d", &key);
result = search(root, key);
if (result)
printf("Element %d found in the BST.\n", key);
else
printf("Element %d not found in the BST.\n", key);
break;
default:
exit(0);
}
}
}
11. 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[50][50], n, visited[50];
int q[20], front = -1,rear = -1;
int s[20], top = -1, count=0;
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}
void dfs(int v)
{
int i;
visited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("%d ", i);
dfs(i);
}
}
}
int main()
{

int ch, start, i,j;


printf("\nEnter the number of vertices in graph: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}

for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);

printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\nThe vertex that is not reachable is %d" ,i);
}
break;
case 2: printf("\nNodes reachable from starting vertex %d are:\n",start);
dfs(start);
break;
case 3: exit(0);
default: printf("\nPlease enter valid choice:");
}
}
Case 1:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0

~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Enter the starting vertex: 1
Nodes reachable from starting vertex 1 are: 2 4 3

Case 2:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Enter the starting vertex: 2
Nodes reachable from starting vertex 2 are: 3 4
The vertex that is not reachable is 1

Case 3:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 2
Enter the starting vertex: 1
Nodes reachable from starting vertex 1 are: 2 3 4

Case 4:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 2
Enter the starting vertex: 2
Nodes reachable from starting vertex 2 are: 3 4

12. 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>

int key[20],n,m;
int *ht,index;
int count = 0;

void insert(int key)


{
index = key % m;
while(ht[index] != -1)
{
index = (index+1)%m;
}
ht[index] = key;
count++;
}

void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is empty");
return;
}
printf("\nHash Table contents are:\n ");
for(i=0; i<m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}
void main()
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);

printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);

ht = (int *)malloc(m*sizeof(int));
for(i=0; i<m; i++)
ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for(i=0; i<n; i++)
scanf("%d", &key[i]);
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);
break;
}
insert(key[i]);
}
//Displaying Keys inserted into hash table
display();
}

Output:
Enter the number of employee records (N): 12
Enter the two digit memory locations (m) for hash table: 15
Enter the four digit key values (K) of 'N' Employee Records:
1234
5678
3456
2345
6799
1235
7890
3214
3456
1235
5679
2346

Hash Table contents are:

T[0] --> 7890


T[1] --> -1
T[2] --> -1
T[3] --> -1
T[4] --> 1234
T[5] --> 2345
T[6] --> 3456
T[7] --> 6799
T[8] --> 5678
T[9] --> 1235
T[10] --> 3214
T[11] --> 3456
T[12] --> 1235
T[13] --> 5679
T[14] --> 2346

You might also like