DSA Lab Manual
DSA Lab Manual
Lab Program - 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
a)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Day
{
char* name;
int date;
char* description;
};
int main()
{
struct Day calendar[7];
for (int i = 0; i < 7; i++)
{
calendar[i].name = (char*)malloc(sizeof(char) * 20);
calendar[i].description = (char*)malloc(sizeof(char) * 100);
sprintf(calendar[i].name, "Day %d", i + 1);
calendar[i].date = i + 1; // Date 1, 2, ...
sprintf(calendar[i].description, "Activity for Day %d", i + 1);
}
for (int i = 0; i < 7; i++)
{
printf("Day: %s\n", calendar[i].name);
printf("Date: %d\n", calendar[i].date);
printf("Description: %s\n", calendar[i].description);
free(calendar[i].name);
free(calendar[i].description);
}
return 0;
}
Output:
Day: Day 1
Date: 1
Description: Activity for Day 1
Day: Day 2
Date: 2
Description: Activity for Day 2
Day: Day 3
Date: 3
Description: Activity for Day 3
Day: Day 4
Date: 4
Description: Activity for Day 4
Day: Day 5
Date: 5
Description: Activity for Day 5
Day: Day 6
Date: 6
Description: Activity for Day 6
Day: Day 7
Date: 7
Description: Activity for Day 7
b)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NoDays 7
// Structure to represent a day
typedef struct
{
char *DayName; // Dynamically allocated string for the day name
int Dateofday; // Date of the day
char *Activity; // Dynamically allocated string for the activity description
}DAYTYPE;
DAYTYPE *Create();
void Read(DAYTYPE *);
void Display(DAYTYPE *);
void FreeCal(DAYTYPE *);
int main()
{
DAYTYPE *weeklyCalendar = Create(); // Create the calendar
Read(weeklyCalendar); // Read data from the keyboard
Display(weeklyCalendar); // Display the week's activity details
FreeCal(weeklyCalendar); // Free allocated memory
return 0;
}
DAYTYPE *Create()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NoDays * sizeof(DAYTYPE));
for(int i = 0; i < NoDays; i++)
{
calendar[i].DayName = NULL;
calendar[i].Dateofday = 0;
calendar[i].Activity = NULL;
}
return calendar;
}
Output:
Do you want to enter details for day 1 [Y/N]: Y
Day Name: Monday
Date: 1
Activity: Meeting at 10 AM
Day 2:
Day Name: Tuesday
Date: 2
Activity: Project Work
Day 3:
No Activity
Day 4:
Day Name: Thursday
Date: 4
Activity: Client Call at 3 PM
Day 5:
Day Name: Friday
Date: 5
Activity: Code Review
Day 6:
No Activity
Day 7:
Day Name: Sunday
Date: 7
Activity: Rest day
Lab Program - 2
Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with
REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in
functions.
#include <stdio.h>
// Function to read a string from the user
void readString(char *str, const char *message) {
printf("Enter %s: ", message);
scanf(" %[^\n]", str);
}
int main() {
char mainStr[100], pattern[50], replaceStr[50];
return 0;
}
Output:
Enter main string: Hello world, welcome to the world of C programming.
Enter pattern: world
Enter replace string: universe
Modified String: Hello universe, welcome to the universe of C programming.
Lab Program - 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
#include<stdio.h>
#define MAX 4
int stack[MAX],item;
int choice,top=-1;
/PUSH Opeation/
void push(int stack[])
{
if(top==(MAX-1))
printf("\nStack Overflow");
else
{ printf("\nEnter a element to be Pushed:");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
/*POP Operation */
void pop(int stack[])
{
int itemdel;
if(top==-1)
printf("\nStack Underflow");
else
{
itemdel=stack[top];
top=top-1;
printf("\nPoped (Deleted) element is:%d",itemdel);
}
}
/*Function to Check Stack is Palindrome or Not */
void palindrome(int stack[])
{
int i,temp,count=0;
temp = top;
if(top == -1)
printf("\nStack is empty");
else
{
for(i=0;i<=top;i++)
{
if(stack[i]==stack[temp])
count=count+1;
temp--;
}
if((top+1)==count)
printf("\nStack contents are Palindrome");
else
printf("\nStack contents are not Palindrome");
}
}
void display(int stack[])
{
int i;
if(top==-1)
printf("\nStack is Empty");
else
printf("\nStack Contents are:");
for(i=top;i>=0;i--)
printf("\n%d\n",stack[i]);
}
void main()
{
do{
printf("\n....MAIN MENU.....");
printf("\n1. PUSH(Insert) to the Stack");
printf("\n2. POP(Delete) from the Stack");
printf("\n3. Palindrome check using Stack");
printf("\n4. Display");
printf("\n5. Exit");
printf("\nEnter your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:push(stack);
display(stack);
break;
case 2:pop(stack);
display(stack);
break;
case 3:palindrome(stack);
break;
case 4:display(stack);
break;
case 5: exit(0);
default:printf("\nInvalid Choice");
break;
}
}while(choice!=5);
Output:
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 1
Enter a element to be Pushed: 1
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 1
Enter a element to be Pushed: 2
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 1
Enter a element to be Pushed: 1
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 3
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 2
Popped (Deleted) element is:1
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 4
....MAIN MENU.....
1. PUSH(Insert) to the Stack
2. POP(Delete) from the Stack
3. Palindrome check using Stack
4. Display
5. Exit
Enter your Choice: 5
Lab Program - 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<conio.h>
#include<string.h>
int stkpre(char symbol)
{
switch(symbol)
{
case '+' :
case '-' : return 2;
case '*' :
case '/' : return 4;
case '^' :
case '$' : return 5;
case '(' : return 0;
case '#' : return -1;
default : return 8;
}
}
int inpre(char symbol)
{
switch(symbol)
{
case '+' :
case '-' : return 1;
case '*' :
case '/' : return 3;
case '^' :
case '$' : return 6;
case '(' : return 9;
case ')' : return 0;
default : return 7;
}
}
void infix_postfix(char infix[],char postfix[])
{
int top=-1,j=0,i;
char s[30],symbol;
s[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(stkpre(s[top])>inpre(symbol))
{
postfix[j]=s[top--];
j++;
}
if(stkpre(s[top])!=inpre(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 Postfix Expression is:");
puts(postfix);
getch();
}
Output:
Enter a valid infix Expression:
A+B*C-D
The Postfix Expression is: ABC*+D-
Lab Program- 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
a)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.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=-1, i;
char postfix[20],symbol;
clrscr();
printf("\nEnter a valid postfix expression:");
gets(postfix);
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
top=top+1;
s[top]=symbol - '0';
}
else
{
op2=s[top];
top=top-1;
op1=s[top];
top=top-1;
res=compute(symbol,op1,op2);
top=top+1;
s[top]=res;
}
}
res=s[top];
top=top-1;
printf("\nThe result is:%f\n",res);
getch();
}
Output:
Enter a valid postfix expression: 23*45*+
The result is: 365.000000
b)
#include<stdio.h>
#include<conio.h>
int count=0;
void transfer(int n,char s,char t,char d)
{
if(n==0)
return;
else
transfer(n-1,s,d,t);
printf("\nMove disc %d from Peg %c to Peg %c",n,s,d);
count++;
transfer(n-1,t,s,d);
}
void main()
{
int n;
clrscr();
printf("\nEnter the number of discs:");
scanf("%d",&n);
transfer(n,'A','B','C');
printf("\n\nTotal number of Moves=%d",count);
getch();
}
Output:
Enter the number of discs: 3
#include <stdio.h>
# define max 6
char queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(char element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}
switch(choice)
{
case 1:
}}
return 0;
}
Output:
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice1
Enter the element which is to be inserted: A
Queue is underflow..
#include<stdio.h>
int MAX=4,count;
struct student
{
char usn[20];
char name[30];
char branch[5];
int sem;
long int phno;
struct student *next; //Self Referential Pointer.
};
typedef struct student NODE;
int countnodes(NODE *head)
{
NODE *p;
count=0;
p=head;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
NODE *getnode(NODE *head)
{
NODE *newnode;
newnode=(NODE *)malloc(sizeof(NODE)); //Create First Node
printf("Enter USN\n");
scanf("%s",&newnode->usn);
printf("Enter Name\n");
scanf("%s",&newnode->name);
printf("Enter Branch\n");
scanf("%s",&newnode->branch);
printf("Enter Sem \n");
scanf("%d",&(newnode->sem));
printf("Enter Phno\n");
scanf("%ld",&(newnode->phno));
newnode->next=NULL; //Set next to NULL
head=newnode;
return head;
}
NODE *display(NODE *head)
{
NODE *p;
if(head==NULL)
printf("\nNo Students Data");
else
{
p=head;
printf("\n-----STUDENT DATA-----");
printf("\nUSN\t\NAME\tBRANCH\tSEM\tPh.No.");
while(p!=NULL)
{
printf("\n%s \t %s \t %s \t %d \t %ld",p->usn,p->name,p->branch,p->sem,p->phno);
p=p->next; //Go to Next Node;
}
printf("\nThe No.of nodes in the list is:%d",countnodes(head));
}
return head;
}
NODE *create(NODE *head)
{
NODE *newnode;
if(head==NULL)
{
newnode=getnode(head);
head=newnode;
}
else
{
newnode=getnode(head);
newnode->next=head;
head=newnode;
}
return head;
}
NODE *insert_front(NODE *head)
{
if(countnodes(head)==MAX)
printf("\nList is full / Overflow!!");
else
head=create(head); //Create() insert nodes at front
return head;
}
NODE *insert_rear(NODE *head)
{
NODE *p,*newnode;
p=head;
if((countnodes(head)==MAX))
printf("\nList is full");
else
{
if(head==NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
newnode=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
}
return head;
}
NODE *insert(NODE *head)
{
int ch;
do{
printf("\n------SLL MENU-----");
printf("\n1.Insert at Front End(First)\n2.Insert at Rear End(Last)");
printf("\n3.Exit");
printf("\n3.Enter your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:head=insert_front(head);
break;
case 2:head=insert_rear(head);
break;
case 3:break;
}
head=display(head);
}while(ch!=3);
return head;
}
NODE *delete_front(NODE *head)
{
NODE *p;
if(head==NULL)
printf("\nList is Empty/Undeflow of Stack");
else
{
p=head;
p=p->next; //set n2d NODE as Head free
printf("\nFront End(first) node is deleted");
}
return p;
}
NODE *delete_rear(NODE *head)
{
NODE *prev=0,*cur=head;
if(head==NULL)
printf("\nList is Empty/Underflow of Stack");
else
{
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
free(cur);
printf("\nRear End(Last) node is deleted");
if(head->next==NULL)
head=0;
else
prev->next=NULL;
}
return head;
}
NODE* delet(NODE *head)
{
int ch;
do
{
printf("\n1.Delete from Front End(First)");
printf("\n2.Delete from End(Last)\n3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:head=delete_front(head);
break;
case 2:head=delete_rear(head);
break;
case 3:
break;
}
head=display(head);
}
while(ch!=3);
return head;
}
NODE *stack(NODE *head)
{
int ch; do
{
printf("\n...SSL used as Stack...");
printf("\n1.Insert at Front(PUSH)");
printf("\n2.Delete from Front(POP))\n3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: head=insert_front(head);
break;
case 2: head=delete_front(head);
break;
case 3:
break;
}
head=display(head);
}while(ch!=3);
return head;
}
void main()
{
int ch,i,n;
NODE *head;
head=NULL;
//clrscr();
printf("\n*----------Studednt Database-----------*");
do
{
printf("\n1.Create\n 2.Display\n3.Insert\n 4.Delete\n 5.Stack \n6.Exit");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:printf("How many student data you want to create:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
head=create(head); //Call to Create NODE...
break;
case 2:head=display(head); //Call to Display...
break;
case 3:head=insert(head); //Call to Insert...
break;
case 4:head=delet(head); //Call to delete
break;
case 5:head=stack(head);
break;
case 6:exit(0); //Exit...
}
}while(ch!=6);
// getch();
}
Output:
*----------Student Database-----------*
1.Create
2.Display
3.Insert
4.Delete
5.Stack
6.Exit
Enter your choice:1
How many student data you want to create:
2
Enter USN
1AA01
Enter Name
Alice
Enter Branch
CSE
Enter Sem
5
Enter Phno
9876543210
Enter USN
2BB02
Enter Name
Bob
Enter Branch
ECE
Enter Sem
3
Enter Phno
9876543222
-----STUDENT DATA-----
USN NAME BRANCH SEM Ph.No.
1AA01 Alice CSE 5 9876543210
2BB02 Bob ECE 3 9876543222
The No.of nodes in the list is:2
1.Create
2.Display
3.Insert
4.Delete
5.Stack
6.Exit
Enter your choice:3
------SLL MENU-----
1.Insert at Front End(First)
2.Insert at Rear End(Last)
3.Exit
Enter your Choice:1
Enter the element which is to be inserted: Charlie
Enter USN
3CC03
Enter Name
Charlie
Enter Branch
ME
Enter Sem
4
Enter Phno
9876543233
-----STUDENT DATA-----
USN NAME BRANCH SEM Ph.No.
3CC03 Charlie ME 4 9876543233
1AA01 Alice CSE 5 9876543210
2BB02 Bob ECE 3 9876543222
The No.of nodes in the list is:3
1.Create
2.Display
3.Insert
4.Delete
5.Stack
6.Exit
Enter your choice:4
-----STUDENT DATA-----
USN NAME BRANCH SEM Ph.No.
1AA01 Alice CSE 5 9876543210
2BB02 Bob ECE 3 9876543222
The No.of nodes in the list is:2
1.Create
2.Display
3.Insert
4.Delete
5.Stack
6.Exit
Enter your choice:5
-----STUDENT DATA-----
USN NAME BRANCH SEM Ph.No.
4DD04 David IT 6 9876543244
1AA01 Alice CSE 5 9876543210
2BB02 Bob ECE 3 9876543222
The No.of nodes in the list is:3
1.Create
2.Display
3.Insert
4.Delete
5.Stack
6.Exit
Enter your choice:2
-----STUDENT DATA-----
USN NAME BRANCH SEM Ph.No.
1AA01 Alice CSE 5 9876543210
2BB02 Bob ECE 3 9876543222
The No.of nodes in the list is:2
1.Create
2.Display
3.Insert
4.Delete
5.Stack
6.Exit
Enter your choice:6
Lab Program - 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<conio.h>
#include<stdlib.h>
int MAX=4,count;
struct emp
{
int ssn;
char name[20];
char dept[10];
char desig[15];
int sal;
char phno[10];
struct emp *left;
struct emp *right;
};
typedef struct emp NODE;
int countnodes(NODE *head)
{
NODE *p;
count=0;
p=head;
while(p!=NULL)
{
p=p->right;
count++;
}
return count;
}
NODE* getnode(NODE *head)
{
NODE *newnode;
newnode=(NODE*)malloc(sizeof(NODE));
newnode->right=newnode->left=NULL;
printf("\nEnter SSN, Name, Dept, Designation,Sal,Ph.No\n");
scanf("%d",&newnode->ssn);
scanf("%s",&newnode->name);
scanf("%s",&newnode->dept);
scanf("%s",&newnode->desig);
scanf("%d",&newnode->sal);
scanf("%s",&newnode->phno);
head=newnode;
return head;
}
NODE* display(NODE *head)
{
NODE *p;
if(head==NULL)
{
printf("\nNo Employee data\n");
}
else
{
p=head;
printf("\n----EMPLOYEE DATA----\n");
printf("\nSSN\tNAME\tDEPT\tDESINGATION\tSAL\t\tPh.NO.");
while(p!=NULL)
{
printf("\n%d\t%s\t%s\t%s\t\t%d\t\t%s",p->ssn,p->name,p->dept,p->desig,p->sal,p->phno);
p = p->right;
}
printf("\nThe no. of nodes in list is: %d",countnodes(head));
}
return head;
}
NODE* create(NODE *head)// creating & inserting at end.
{
NODE *p, *newnode;
p=head;
if(head==NULL)
{
newnode=getnode(head);
head=newnode;
}
else
{
newnode=getnode(head);
while(p->right!=NULL)
{
p=p->right;
}
p->right=newnode;
newnode->left=p;
}
return head;
}
NODE* insert_end(NODE *head)
{
if(countnodes(head)==MAX)
{
printf("\nList is Full!!");
}
else
head=create(head);
return head;
}
NODE* insert_front(NODE *head)
{
NODE *p,*newnode;
if(countnodes(head)==MAX)
printf("\nList is Full!!");
else
{
if(head==NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
newnode->right=head;
head->left=newnode;
head=newnode;
}
}
return head;
}
NODE* insert(NODE *head)
{
int ch;
do
{
printf("\n1.Insert at Front(First)\t2.Insert at End(Rear/Last)\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: head=insert_front(head);
break;
case 2: head=insert_end(head);
break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
NODE* delete_front(NODE *head)
{
NODE *p;
if(head==NULL)
{
printf("\nList is Empty (QUEUE)");
return head;
}
if(head->right == NULL)
{
head=NULL;
printf("\nLast(end) entry is deleted");
return head;
}
else
{
p=head;
head=head->right;
head->left=NULL;
free(p);
printf("\nFront(first)node is deleted");
}
return head;
}
NODE* delete_end(NODE *head)
{
NODE *p,*q;
p=head;
if(head==NULL)
{
printf("\nList is Empty (QUEUE)\n");
return head;
}
if(p->right==NULL)
{
head=NULL;
printf("\nLast(end) entry is deleted");
return head;
}
else{
while(p->right!=NULL)
{
q=p;
p=p->right; //Go upto -1 node which you want to delete
}
q=p->left;
q->right=NULL;
p->left=NULL;
//free(p);//Delete last node...
printf("\nLast(end) entry is deleted");
return head;
}
}
NODE *del(NODE *head)
{
int ch;
do
{
printf("\n1.Delete from Front(First)\t2.Delete from End(Rear/Last))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=delete_front(head);
break;
case 2: head=delete_end(head);
break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
NODE* queue(NODE *head)
{
int ch, ch1, ch2;
do
{
printf("\nDLL used as Double Ended Queue");
printf("\n1.QUEUE-Insert at Rear & Delete from Front");
printf("\n2.QUEUE-Insert at Front & Delete from Rear");
printf("\n3.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: do
{
printf("\n1.Insert at Rear\t2.Delete from From Front\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch1);
switch(ch1)
{
case 1: head=insert_end(head);
break;
case 2: head=delete_front(head);
break;
case 3: break;
}
head=display(head);
}while(ch1!=3);
break;
case 2:
do
{
printf("\n1.Insert at Front\t2.Delete from Rear\t3.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1: head=insert_front(head);
break;
case 2: head=delete_end(head);
break;
case 3: break;
}
head=display(head);
}while(ch2!=3);
break;
case 3: break;
}
}while(ch!=3);
head=display(head);
return head;
}
void main()
{
int ch, i, n; NODE *head; head=NULL;
printf("\n----------Employee Database-----------");
do
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Queue\n6.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many employees data you want to create:");
scanf("%d",&n);
for(i=0;i<n;i++)
head=create(head);//Call to Create node...
break;
case 2: head=display(head); //Call to Display...
break;
case 3: head=insert(head); //Call to Insert...
break;
case 4: head=del(head); //Call to delete
break;
case 5: head=queue(head);
break;
case 6: exit(0); //Exit...
break;
}
}while(ch!=6);
}
Output:
----------Employee Database-----------
1.Create
2.Display
3.Insert
4.Delete
5.Queue
6.Exit
Enter your choice: 1
How many employees data you want to create: 2
----EMPLOYEE DATA----
SSN NAME DEPT DESINGATION SAL Ph.NO.
12345 John IT Manager 50000 9876543210
67890 Jane HR Executive 40000 9876543220
The no. of nodes in list is: 2
1.Create
2.Display
3.Insert
4.Delete
5.Queue
6.Exit
Enter your choice: 3
1.Create
2.Display
3.Insert
4.Delete
5.Queue
6.Exit
Enter your choice: 4
----EMPLOYEE DATA----
SSN NAME DEPT DESINGATION SAL Ph.NO.
12345 John IT Manager 50000 9876543210
67890 Jane HR Executive 40000 9876543220
The no. of nodes in list is: 2
1.Create
2.Display
3.Insert
4.Delete
5.Queue
6.Exit
Enter your choice: 5
----EMPLOYEE DATA----
SSN NAME DEPT DESINGATION SAL Ph.NO.
12345 John IT Manager 50000 9876543210
67890 Jane HR Executive 40000 9876543220
33445 Luke Finance Analyst 45000 9876543240
The no. of nodes in list is: 3
1.Create
2.Display
3.Insert
4.Delete
5.Queue
6.Exit
Enter your choice: 6
Lab Program - 9
Develop 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) = 6x 2 y 2 z-4yz 5 +3x 3 yz+2xy 5 z-2xyz 3
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)
#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->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("\n3.Exit");
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;
Output:
~~Menu~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
3.Exit
Enter your choice: 1
~~Menu~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
3.Exit
Enter your choice: 2
Polynomial 1 is:
6x^2y^2z^1 + -4x^0y^1z^5
Enter the POLY2(x,y,z):
Polynomial 2 is:
3x^3y^1z^1 + 2x^1y^1z^5
Polynomial addition result:
6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^1z^5
~~Menu~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
3.Exit
Enter your choice: 3
Lab Program - 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<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;
NODE *insert(int item,NODE *root)
{
NODE *temp,*cur,*prev;
temp=(NODE *)malloc(sizeof(NODE));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(item<cur->info)?cur->llink:cur->rlink;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}NODE *construct_BST(NODE *root)
{
int a,n,i;
printf("\nEnter the number of elements:\n");
scanf("%d",&n);
printf("\nEnter the elements to be inserted in the tree\n");
for(i=0;i<n;i++)
{
scanf("%d",&a);
root=insert(a,root);
}
printf("\nTree Constructed Sucessfully!!!\n");
return root;
}
void preorder(NODE *root)
{
if(root!=NULL)
{
printf("%d\n",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
void inorder(NODE *root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\n",root->info);
inorder(root->rlink);
}
}
void postorder(NODE *root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\n",root->info);
}
}
int search_element(NODE *root,int key)
{
NODE *cur;
int n=0;
cur=root;
if(cur!=NULL)
{
if(key==cur->info)
n=1;
else if(key<cur->info)
return search_element(cur->llink,key);
else
return search_element(cur->rlink,key);
}
return n;
}
void main()
{
int item,ch,key,n;
NODE *root;
root=NULL;
while(1)
{
printf("\n...BST MENU...");
printf("\n1.Construct BST");
printf("\n2.Preorder(DLR)\n3.Inorder(LDR)\n4.Postorder(LRD)");
printf("\n5.Search an Element");
printf("\n6.Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:root=construct_BST(root);
break;
case 2:preorder(root);
break;
case 3:inorder(root);
break;
case 4:postorder(root);
break;
case 5:if(root==NULL)
printf("\nList is Empty");
else
{
printf("\nEnter the element:");
scanf("%d",&key);
n=search_element(root,key);
if(n)
printf("\nKey is found\n");
else
printf("\nKey Not found");
}
break;
case 6:exit(0);
break;
default: printf("\nWrong Choice");
}
}
}
Output:
...BST MENU...
1.Construct BST
2.Preorder(DLR)
3.Inorder(LDR)
4.Postorder(LRD)
5.Search an Element
6.Exit
Enter the choice: 1
...BST MENU...
1.Construct BST
2.Preorder(DLR)
3.Inorder(LDR)
4.Postorder(LRD)
5.Search an Element
6.Exit
Enter the choice: 2
6
5
2
2
5
9
8
7
15
14
24
...BST MENU...
1.Construct BST
2.Preorder(DLR)
3.InOrder(LDR)
4.Postorder(LRD)
5.Search an Element
6.Exit
Enter the choice: 3
2
2
5
5
6
7
8
8
9
14
15
24
...BST MENU...
1.Construct BST
2.Preorder(DLR)
3.Inorder(LDR)
4.Postorder(LRD)
5.Search an Element
6.Exit
Enter the choice: 4
2
2
5
5
7
8
8
14
24
15
9
6
...BST MENU...
1.Construct BST
2.Preorder(DLR)
3.Inorder(LDR)
4.Postorder(LRD)
5.Search an Element
6.Exit
Enter the choice: 5
Enter the element: 8
Key is found
...BST MENU...
1.Construct BST
2.Preorder(DLR)
3.Inorder(LDR)
4.Postorder(LRD)
5.Search an Element
6.Exit
Enter the choice: 5
Enter the element: 10
Key Not found
Lab Program - 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<conio.h>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d", &a[i][j]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
printf("\n%d", source);
while(front<=rear)
{
u = q[front++];
while(1)
{
printf("\n1.Create Graph\n2.BFS\n3.DFS\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create(); break;
case 2: bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf("\nThe vertex that is not rechable %d" ,i);
break;
case 3: printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d",&source);
m=1;
printf (" reachable nodes\n") ;
printf("\n %d", source);
dfs(source);
break;
default: exit(0);
}
}
}
Output:
...BST MENU...
1.Create Graph
2.BFS
3.DFS
4.Exit
Enter your choice: 1
1. Create Graph
Enter the number of vertices of the digraph: 5
...BST MENU...
1.Create Graph
2.BFS
3.DFS
4.Exit
Enter your choice: 2
2. BFS
Enter the source vertex to find other nodes reachable or not: 1
The reachable vertices are:
1
2
3
4
5
...BST MENU...
1.Create Graph
2.BFS
3.DFS
4.Exit
Enter your choice: 2
2. BFS
Enter the source vertex to find other nodes reachable or not: 2
The reachable vertices are:
2
3
4
5
The vertex that is not reachable 1
...BST MENU...
1.Create Graph
2.BFS
3.DFS
4.Exit
Enter your choice: 3
3. DFS
Enter the source vertex to find the connectivity: 1
reachable nodes
12345
...BST MENU...
1.Create Graph
2.BFS
3.DFS
4.Exit
Enter your choice: 4
4. Exit
Lab Program - 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>
#include<string.h>
#define HASH_SIZE 10
typedef struct employee
{
int id;
char name[20];
}EMPLOYEE;
/Create initial hash table/
void initialize_hash_table(EMPLOYEE a[])
{
int i;
for(i=0;i<HASH_SIZE;i++)
{
a[i].id=0;
}
}
/Compute hash value using the function: H(K)=k%m/
int H(int k)
{
return k%HASH_SIZE;
}
/Insert an item into the empty slot using linear probing/
void insert_hash_table(int id, char name[], EMPLOYEE a[])
{
int i,index,h_value;
h_value= H(id);
for(i=0;i<HASH_SIZE;i++)
{
index=(h_value+i)%HASH_SIZE;
if(a[index].id==0) //empty slot found
{
a[index].id=id; //insert employee id
strcpy(a[index].name,name); //insert employee name
break;
}
}
if(i==HASH_SIZE)
printf("Hash table full\n"); // NO empty slot
}
/Display the hash table/
void display_hash_table(EMPLOYEE a[],int n)
{
int k,i;
printf("H_Value\t Emp_id\t Emp_name\n");
for(i=0;i<n;i++)
{
if(a[i].id!=0)
printf("%d\t %d\t %s\n",i,a[i].id,a[i].name);
}
}
void main()
{
EMPLOYEE a[HASH_SIZE];
char name[20];
int key,id,choice,flag;
Output:
....HASH FUNCTION MENU....
1.Insert
2.Display
3.Exit
Enter the choice: 1
Enter Employee Id: 1045
Enter Employee Name: John