You are on page 1of 67

SAPTHAGIRI COLLEGE OF ENGINEERING

#14/5, Chikkasandra, Hesaraghatta Main Road, Bangalore-57


(Affiliated to VTU, Belgavi & Approved by AICTE-New Delhi)(ISO 9001:2015 & ISO 14001:2015
certified Institution, Accredited by NBA and NACC with “A” Grade)

DEPARTMENT OF INFORMATION SCIENCE & ENGINEERING

DATA STRUCTURES LABORATORY


LAB MANUAL
BCSL305

Prepared By:
Prof. AMBIKA S
Assistant Professor
Dept. of ISE
DATA STRUCTURES LABORATORY
SEMESTER – III

Course Code BCSL305 CIE Marks 50


Number of Contact Hours/Week 0:0:2 SEE Marks 50
Total Number of Lab Contact Hours 28 Exam Hours 03
Credits – 1
Course Learning Objectives:
This laboratory course enables students to get practical experience in design, develop, implement, analyzeand
evaluation/testing of
● Dynamic memory management
● Linear data structures and their applications such as stacks, queues and lists
● Non-Linear data structures and their applications such as trees and graphs

Descriptions (if any):


● Implement all the programs in “C ” Programming Language and Linux OS.
Programs List:
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.

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.

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

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.

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

9. 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
10. 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

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

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.

Laboratory Outcomes: The student should be able to:


● Analyze various linear and non-linear data structures
● Demonstrate the working nature of different types of data structures and their applications
● Use appropriate searching and sorting algorithms for the give scenario.
● Apply the appropriate data structure for solving real world problems
PROGRAM – 1
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>
#define MAX_DAYS 7
int i;
// Structure to represent a day of the week
struct Day {
char *name; int date;
char *activity;
};
// Function to create the calendar
struct Day* create()
{
struct Day *calendar;
calendar = (struct Day*)malloc(MAX_DAYS * sizeof(struct Day));
if(calendar==NULL)
{
printf("Insufficient Memory\n");
exit(0);
}
return calendar;
}
void read(struct Day *calendar)
{
for (i = 0; i < MAX_DAYS; i++)
{
calendar[i].name = (char *)malloc(20 * sizeof(char));
// Assuming max day name length of 20 characters
printf("Enter the name of day %d: ", i + 1);
scanf("%s", calendar[i].name);
printf("Enter the date of %s: ", calendar[i].name);
scanf("%d", &calendar[i].date);
calendar[i].activity = (char *)malloc(100 * sizeof(char));
// Assuming max activity description length of 100 characters
printf("Enter the activity for %s: ", calendar[i].name);
scanf(" %s", calendar[i].activity);
}
}

// Function to display the calendar


void display(struct Day *calendar)
{
printf("\nDay\t\tDate\t\tActivity\n");
for (i = 0; i < MAX_DAYS; i++) {
printf("%s\t\t\t%d\t\t\t%s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
}
int main() {
struct Day *calendar=create();
// Read the calendar
read(calendar);
// Display the calendar
display(calendar);
// Clean up dynamically allocated memory
for (int i = 0; i < MAX_DAYS; i++)
{
free(calendar[i].name);
free(calendar[i].activity);
}
return 0;
}

Sample Output:
Enter the name of day 1: Monday
Enter the date of Monday: 1
Enter the activity for Monday: Meeting
Enter the name of day 2: Tuesday
Enter the date of Tuesday: 2
Enter the activity for Tuesday: Survey
Enter the name of day 3: Wednesday
Enter the date of Wednesday: 3
Enter the activity for Wednesday: Budget
Enter the name of day 4: Thursday
Enter the date of Thursday: 4
Enter the activity for Thursday: Planning
Enter the name of day 5: Friday
Enter the date of Friday: 5
Enter the activity for Friday: Lunch
Enter the name of day 6: Saturday
Enter the date of Saturday: 6
Enter the activity for Saturday: Outing
Enter the name of day 7: Sunday
Enter the date of Sunday: 7
Enter the activity for Sunday: Family
Day Date Activity
Monday 1 Meeting
Tuesday 2 Survey
Wednesday 3 Budget
Thursday 4 Planning
Friday 5 Lunch
Saturday 6 Outing
Sunday 7 Family
PROGRAM – 2
2. Design, Develop and Implement 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. Repost 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>
//Declarations
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag=0;
void readinput(char str[],char pat[],char rep[])
{
printf("\nEnter a main string \n");
gets(str);
printf("\nEnter a pattern string \n");;
gets(pat);
printf("\nEnter a replace string \n");
gets(rep);
}
void stringmatch(char str[],char pat[],char rep[],char ans[])
{
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()
{
char str[100],pat[100],rep[100],ans[100];
readinput(str,pat,rep);
stringmatch(str,pat,rep,ans);
if(flag==1)
printf("\nThe resultant string is\n %s" , ans);
else
printf("\nPattern string NOT found\n");
} // end of main
OUTPUT

Enter a main string


Hello everyone
Enter a pattern string
everyone
Enter a replace string
everybody
The resultant string is
Hello everybody
PROGRAM – 3

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<stdlib.h>
#include<stdio.h>
#define max_size 5
int stack[max_size],top=-1;
/*Function Prototype*/
void push();
void pop();
void display();
void pali();
int main()
{
int choice;
while(1)
{
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); //to insert an item into stack
break;
case 2: pop(); //to delete an item from stack
break;
case 3: pali(); //to check palindrome or not
break;
case 4: display(); //to display items in stack
break;
case 5: exit(0); //exit the program
break;
default: printf("\n Invalid choice:\n");
break;
}
}
return 0;
}
void push() //Inserting element into the stack
{
int item,n;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void pali() //checking whether palindrome or not
{
int num[10],rev[10],i=0,k,flag=1;
k=top;
while(k!=-1)
{
num[i++]=stack[k--];
}
for(i=0;i<=top;i++)
{
if(num[i]==stack[i])
continue;
else
flag=0;
}
if(top!=-1)
{
if(flag)
printf("It is palindrome number\n");
else
printf("It is not a palindrome number\n");
}
else
printf("Stack is Empty:");
}
void display() //Displaying the elements of stack
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("\n ------\n| %d |", stack[i]); }
}
}
OUTPUT

--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 1
Enter the element to be inserted: 10
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 1
Enter the element to be inserted: 20
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 1
Enter the element to be inserted: 10
--------STACK OPERATIONS-----------
1.Push
2.Pop3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 4
The stack elements are:

------
| 10 |
------
| 20 |
------
| 10 |
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 3
It is palindrome number

--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 2
The poped element: 10

--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 4
The stack elements are:

------
| 20 |
------
| 10 |

--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice:5
PROGRAM – 4

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 <ctype.h>
#include <stdio.h>
#define SIZE 50 /* Size of Stack */
char s[SIZE]; /* Global declarations */
int top = -1;
push(char elem) /* Function for PUSH operation */
{
s[++top] = elem;
}
char pop() /* Function for POP operation */
{
return (s[top--]);
}
int pr(char elem) /* Function for precedence */
{
switch (elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/':
case '%': return 3;
case '^': return 4;
}
}
void main() /* Main Program */
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\n Enter the Infix Expression : ");
gets(infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
}
else /* Operator */
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\n Given Infix Expn is: %s\n The Postfix Expn is: %s\n", infx, pofx);
}
OUTPUT

Enter the Infix Expression: (a+b)*c/d^5%1


Given Infix Expn is: (a+b)*c/d^5%1
The Postfix Expn is: ab+c*d5^/1%
PROGRAM – 5
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.

Source Code for program 5a:


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
double compute(char symbol, double op1, double op2)
{
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);
}
OUTPUT
Enter the postfix expression:
623+-382/+*2$3+
The result is : 52.000000

5b. Solving Tower of Hanoi problem with n disks.

#include<stdio.h>
int count=0,n;
int tower(int n,char s,char t,char d)
{
if(n==1)
{
printf("\n Move disc 1 from %c to %c",s,d);
count++;
return 1;
}
tower(n-1,s,d,t);
printf("\n Move disc %d from %c to %c",n,s,d);
count++;
tower(n-1,t,s,d);
}
int main( )
{
printf("\n Enter the no. of discs:");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n The no. of disc moves is:%d",count);
}

OUTPUT

Enter the no. 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
The no. of disc moves is: 7
PROGRAM – 6

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>
#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. ExitEnter Your Choice: 1
Enter the element to be inserted: A
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. ExitEnter Your Choice: 1
Enter the element to be inserted: B
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. ExitEnter Your Choice: 1
Enter the element to be inserted: C
~~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: D
~~Circular Queue Overflow
~~~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. ExitEnter Your Choice: 3
Circular Queue contents are:
Front[0]-> A B C <-[2]Rear
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. ExitEnter Your Choice: 2
Deleted element from the queue is: A
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. ExitEnter Your Choice: 3
Circular Queue contents are:
Front[1]-> B C <-[2]Rear
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 4
PROGRAM – 7
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");}
}
}
OUTPUT
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:1
Enter the no of students: 3
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1ME21CS017
Braham
CSE
5
8768586443
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1SG21CS015
Bikash
CSE
5
8734687996
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1SG21AI015
Shoaib
AI&ML
5
6748353877
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:2
The contents of SLL:
||1|| USN:1SG21AI015| Name:Shoaib| Branch:AI&ML| Sem:5| Ph:6748353877|
||2|| USN:1SG21CS015| Name:Bikash| Branch:CSE| Sem:5| Ph:8734687996|
||3|| USN:1SG21CS017| Name:Braham| Branch:CSE| Sem:5| Ph:8768586443|
No of student nodes is 3
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:3
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1SG21CS068
Rajan
CSE
5
3426527765
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:2
The contents of SLL:
||1|| USN:1SG21AI015| Name:Shoaib| Branch:AI&ML| Sem:5| Ph:6748353877|
||2|| USN:1SG21CS015| Name:Bikash| Branch:CSE| Sem:5| Ph:8734687996|
||3|| USN:1SG21CS017| Name:Braham| Branch:CSE| Sem:5| Ph:8768586443|
||4|| USN:1SG21CS068| Name:Rajan| Branch:CSE| Sem:5| Ph:3426527765|
No of student nodes is 4
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:4
The student node with the usn:1SG21CS068 is deleted
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:2
The contents of SLL:
||1|| USN:1SG21AI015| Name:Shoaib| Branch:AI&ML| Sem:5| Ph:6748353877|
||2|| USN:1SG21CS015| Name:Bikash| Branch:CSE| Sem:5| Ph:8734687996|
||3|| USN:1SG21CS017| Name:Braham| Branch:CSE| Sem:5| Ph:8768586443|
No of student nodes is 3
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:4
The student node with the usn:1SG21CS017 is deleted
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:5
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:1
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1SG21CS005
Aman
CSE
5
6587594335
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:3
The contents of SLL:
||1|| USN:1SG21CS005| Name:Aman| Branch:CSE| Sem:5| Ph:6587594335|
||2|| USN:1SG21AI015| Name:Shoaib| Branch:AI&ML| Sem:5| Ph:6748353877|
||3|| USN:1SG21CS015| Name:Bikash| Branch:CSE| Sem:5| Ph:8734687996|
No of student nodes is 3
~~~Stack Demo using SLL~~~
1: Push operation
2: Pop operation
3: Display
4: Exit
Enter your choice for stack demo:1
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1SG21CS092
Shubham
CSE
5
9869754354
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:3
The contents of SLL:
||1|| USN:1SG21CS092| Name:Shubham| Branch:CSE| Sem:5| Ph:9869754354|
||2|| USN:1SG21CS005| Name:Aman| Branch:CSE| Sem:5| Ph:6587594335|
||3|| USN:1SG21AI015| Name:Shoaib| Branch:AI&ML| Sem:5| Ph:6748353877|
||4|| USN:1SG21CS015| Name:Bikash| Branch:CSE| Sem:5| Ph:8734687996|
No of student nodes is 4
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:2
The Student node with usn:1SG21CS092 is deleted
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:3
The contents of SLL:
||1|| USN:1SG21CS005| Name:Aman| Branch:CSE| Sem:5| Ph:6587594335|
||2|| USN:1SG21AI015| Name:Shoaib| Branch:AI&ML| Sem:5| Ph:6748353877|
||3|| USN:1SG21CS015| Name:Bikash| Branch:CSE| Sem:5| Ph:8734687996|
No of student nodes is 3
~~~Stack Demo using SLL~~~
1: Push operation
2: Pop operation
3: Display
4: Exit
Enter your choice for stack demo:4
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:6
PROGRAM – 8
8. 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>
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:Ndeqdemo();
break;
case 8: exit(0);
default: printf("\nPlease Enter the valid choice");
}
}
}
OUTPUT
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 1
Enter the no of Employees: 2
Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
1EPL
Braham
Developer
Senior
13627
8476283712
Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
2EPL
Aman
Trader
Manager
20000
2763578156
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice:
2ENode:1||SSN:1EPL|Name:Braham|Department:Developer|Designation:Senior|Salary:13
627|Phone no:8476283712
ENode:2||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|
Phone no:2763578156
No of employee nodes is 2
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 3
Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
3EPL
Bikash
Meeting
Manager
30000
8237462936
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice:
2ENode:1||SSN:1EPL|Name:Braham|Department:Developer|Designation:Senior|Salary:13
627|Phone no:8476283712
ENode:2||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|
Phone no:2763578156
ENode:3||SSN:3EPL|Name:Bikash|Department:Meeting|Designation:Manager|Salary:300
00|Phone no:8237462936
No of employee nodes is 3
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 5
Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
4EPL
Shoaib
Digital Marketing
Manager
40000
2835826437
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice:
2ENode:1||SSN:4EPL|Name:Shoaib|Department:Digital
Marketing|Designation:Manager|Salary:40000|Phone no:2835826437
ENode:2||SSN:1EPL|Name:Braham|Department:Developer|Designation:Senior|Salary:136
27|Phone no:8476283712
ENode:3||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|
Phone no:2763578156
ENode:4||SSN:3EPL|Name:Bikash|Department:Meeting|Designation:Manager|Salary:300
00|Phone no:8237462936
No of employee nodes is 4
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 4
The employee node with the ssn:
3EPL is deleted
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 6The employee node with the ssn:4EPL is deleted~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice:
2ENode:1||SSN:1EPL|Name:Braham|Department:Developer|Designation:Senior|Salary:13
627|Phone no:8476283712
ENode:2||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|
Phone no:2763578156
No of employee nodes is 2
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 7
Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 2
The employee node with the ssn:
1EPL is deletedDemo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 4
The employee node with the ssn:
2EPL is deletedDemo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 2
Doubly Linked List is empty
Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 6
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 8
PROGRAM – 9
9. 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) = 6x2 y 2 z-4yz5 +3x3 yz+2xy5 z-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 cf, px, py, pz;
int flag;
struct node *link;
};
typedef struct node NODE;
NODE* getnode()
{
NODE *x;
x=(NODE*)malloc(sizeof(NODE));
if(x==NULL)
{
printf("Insufficient memory\n");exit(0);
}
return x;
}
void display(NODE *head)
{
NODE *temp;
if(head->link==head)
{
printf("Polynomial does not exist\n");return;
}
temp=head->link;
printf("\n");
while(temp!=head)
{
printf("%d x^%d y^%d z^%d",temp->cf,temp->px,temp->py,temp->pz);
if(temp->link != head)
printf("+");
temp=temp->link;
}
printf("\n");
}
NODE* insert_rear(int cf,int x,int y,int z,NODE *head)
{
NODE *temp,*cur;
temp=getnode();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->pz=z;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE* read_poly(NODE *head)
{
int px, py, pz, cf, ch;
printf("\nEnter coeff: ");
scanf("%d",&cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head=insert_rear(cf,px,py,pz,head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
while(ch != 0)
{
printf("\nEnter coeff: ");
scanf("%d",&cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head=insert_rear(cf,px,py,pz,head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
}
return head;
}
NODE* add_poly(NODE *h4,NODE *h2,NODE *h3)
{
NODE *p1,*p2;
int x1,x2,y1,y2,z1,z2,cf1,cf2,cf;
p1=h4->link;
while(p1!=h4)
{
x1=p1->px;
y1=p1->py;
z1=p1->pz;
cf1=p1->cf;
p2=h2->link;
while(p2!=h2)
{
x2=p2->px;
y2=p2->py;
z2=p2->pz;
cf2=p2->cf;
if(x1==x2 && y1==y2 && z1==z2)
break;
p2=p2->link;
}
if(p2->flag!=1)
{
cf=cf1+cf2;
p2->flag=1;
if(cf!=0)
h3=insert_rear(cf,x1,y1,z1,h3);
}
else
{
h3=insert_rear(cf1,x1,y1,z1,h3);
}
p1=p1->link;
}
p2=h2->link;
while(p2!=h2)
{
if(p2->flag==0)
h3=insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
p2=p2->link;
}
return h3;
}
void evaluate(NODE *h1)
{
NODE *head;
int x, y, z;
float result=0.0;
head=h1;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d",&x, &y, &z);
while(h1->link != head)
{
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) *pow(z,h1->pz));
h1=h1->link;
}
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) *pow(z,h1->pz));
printf("\nPolynomial result is: %f", result);
}
int main()
{
NODE *h1,*h2,*h3,*h4;
int ch;
h1=getnode();
h2=getnode();
h3=getnode();
h4=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;
h4->link=h4;
while(1)
{
printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\n3.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter polynomial to evaluate:\n");h1=read_poly(h1);
display(h1);
evaluate(h1);
break;
case 2:
printf("\nEnter the first polynomial:");
h1=read_poly(h4);
printf("\nEnter the second polynomial:");
h2=read_poly(h2);
h3=add_poly(h4,h2,h3);
printf("\nFirst polynomial is: ");
display(h4);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: ");
display(h3);
break;
case 3:
exit(0);
break;
default:
printf("\nInvalid entry");break;
}
return 0;
}
}
OUTPUT
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 1
Enter polynomial to evaluate:

Enter coeff: 6
Enter x, y, z powers(0-indiacate NO term): 2 2 1
If you wish to continue press 1 otherwise 0: 1
Enter coeff: -4
Enter x, y, z powers(0-indiacate NO term): 0 1 5
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 3
Enter x, y, z powers(0-indiacate NO term): 3 1 1
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 2
Enter x, y, z powers(0-indiacate NO term): 1 5 1
If you wish to continue press 1 otherwise 0: 1
Enter coeff: -2
Enter x, y, z powers(0-indiacate NO term): 1 1 3
If you wish to continue press 1 otherwise 0: 0
6 x^2 y^2 z^1+-4 x^0 y^1 z^5+3 x^3 y^1 z^1+2 x^1 y^5 z^1+-2 x^1 y^1 z^3
Enter x, y, z, terms to evaluate:
1
1
1
Polynomial result is: 5.000000
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 2
Enter the first polynomial:
Enter coeff: 3
Enter x, y, z powers(0-indiacate NO term): 3 3 3
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 2
Enter x, y, z powers(0-indiacate NO term): 3 2 2
If you wish to continue press 1 otherwise 0: 0
Enter the second polynomial:
Enter coeff: 2
Enter x, y, z powers(0-indiacate NO term): 3 3 3
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 3
Enter x, y, z powers(0-indiacate NO term): 3 2 2
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 10
Enter x, y, z powers(0-indiacate NO term): 3 2 2
If you wish to continue press 1 otherwise 0: 0
First polynomial is:
3 x^3 y^3 z^3+2 x^3 y^2 z^2

Second polynomial is:


2 x^3 y^3 z^3+3 x^3 y^2 z^2+10 x^3 y^2 z^2

The sum of 2 polynomials is:


5 x^3 y^3 z^3+5 x^3 y^2 z^2+10 x^3 y^2 z^2
PROGRAM - 10
10. 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 * lchild;
struct BST * rchild;
};
typedef struct BST * NODE;
NODE create() {
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", & temp -> data);
temp -> lchild = NULL;
temp -> rchild = NULL;
return temp;
}
void insert(NODE root, NODE newnode);
void inorder(NODE root);
void preorder(NODE root);
void postorder(NODE root);
void search(NODE root);
void insert(NODE root, NODE newnode) {
if (newnode -> data < root -> data) {
if (root -> lchild == NULL)
root -> lchild = newnode;
else
insert(root -> lchild, newnode);
}
if (newnode -> data >=root -> data) {
if (root -> rchild == NULL)
root -> rchild = newnode;
else
insert(root -> rchild, newnode);
}
}
void search(NODE root) {
int key;
NODE cur;
if (root == NULL)
{
printf("\nBST is empty.");
return;
}
printf("\nEnter Element to be searched: ");
scanf("%d", & key);
cur = root;
while (cur != NULL) {
if (cur -> data == key) {
printf("\nKey element is present in BST");
return;
}
if (key < cur -> data)
cur = cur -> lchild;
else
cur = cur -> rchild;
}
printf("\nKey element is not found in the BST");
}
void inorder(NODE root) {
if (root != NULL) {
inorder(root -> lchild);
printf("%d ", root -> data);
inorder(root -> rchild);
}
}
void preorder(NODE root) {
if (root != NULL) {
printf("%d ", root -> data);
preorder(root -> lchild);
preorder(root -> rchild);
}
}
void postorder(NODE root) {
if (root != NULL) {
postorder(root -> lchild);
postorder(root -> rchild);
printf("%d ", root -> data);
}
}
void main() {
int ch, key, val, i, n;
NODE root = NULL, newnode;
while (1) {
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.BST Traversals: ");
printf("\n3.Search");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", & ch);
switch (ch) {
case 1:
printf("\nEnter the number of elements: ");
scanf("%d", & n);
for (i = 1; i <= n; i++) {
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2:
if (root == NULL)
printf("\nTree Is Not Created");
else {
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 3:
search(root);
break;
case 4:
exit(0);
}
}
}

OUTPUT
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 1
Enter the number of elements: 12
Enter The value: 6
Enter The value: 9
Enter The value: 5
Enter The value: 2
Enter The value: 8
Enter The value: 15
Enter The value: 24
Enter The value: 14
Enter The value: 7
Enter The value: 8
Enter The value: 5
Enter The value: 2
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 2
The Preorder display : 6 5 2 2 5 9 8 7 8 15 14 24
The Inorder display : 2 2 5 5 6 7 8 8 9 14 15 24
The Postorder display : 2 2 5 5 7 8 8 14 24 15 9 6
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 3
Enter Element to be searched: 2
Key element is present in BST
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 4
PROGRAM – 11
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:");
}
}

OUTPUT
*************************case-1*************************
Enter the number of vertices in graph:4
Enter the adjacency matrix:
0101
0010
0001
0000
Enter the starting vertex: 1
==>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
Nodes reachable from starting vertex 1 are: 2 4 3
*************************case-2*************************
Enter the number of vertices in graph:4
Enter the adjacency matrix:
0101
0010
0001
0000
Enter the starting vertex: 2
==>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
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:
0101
0010
0001
0000
Enter the starting vertex: 1
==>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
Nodes reachable from starting vertex 1 are: 2 3 4
*************************case-4*************************
Enter the number of vertices in graph:4
Enter the adjacency matrix:
0101
0010
0001
0000
Enter the starting vertex: 2
==>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
Nodes reachable from starting vertex 2 are: 3 4
PROGRAM – 12

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) :10
Enter the two digit memory locations (m) for hash table:15
Enter the four digit key values (K) for N Employee Records:
4020
4560
9908
6785
0423
7890
6547
3342
9043
6754
Hash Table contents are:
T[0] --> 4020
T[1] --> 4560
T[2] --> 7890
T[3] --> 423
T[4] --> 6754
T[5] --> 6785
T[6] --> -1
T[7] --> 6547
T[8] --> 9908
T[9] --> -1
T[10] --> -1
T[11] --> -1
T[12] --> 3342
T[13] --> 9043
T[14] --> -1

You might also like