0% found this document useful (0 votes)
23 views57 pages

DS Lab Mannual (1)

Uploaded by

arya132005
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)
23 views57 pages

DS Lab Mannual (1)

Uploaded by

arya132005
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/ 57

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.
Solution
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n = 7;
typedef struct {
char *Dname;
int dt;
char *act;
} calendar;
calendar *cal;
void create() {
cal = (calendar*)malloc(n * sizeof(calendar));
}
void read() {
int i;
for (i = 0; i < n; i++) {
cal[i].Dname = (char*)malloc(n * sizeof(char)); // Assuming a maximum length of 100
characters
printf("Enter the day=");
scanf("%s", cal[i].Dname);
printf("Enter the date=");
scanf("%d", &cal[i].dt);
fflush(stdin);
cal[i].act = (char*)malloc(n * sizeof(char)); // Assuming a maximum length of 100
characters
printf("Enter the activity of the day=");
scanf("%s", cal[i].act);
printf("\n");
}
}

void display() {
int i;
for (i = 0; i < n; i++) {
printf("Day=%s\n Date=%d\n activity=%s\n\n", cal[i].Dname, cal[i].dt, cal[i].act);
}
}
int main() {
create();
read();
display();
return 0;
}

Output
Enter the day=monday
Enter the date=1
Enter the activity of the day=reading
Enter the day=tuesday
Enter the date=2
Enter the activity of the day=writing
Enter the day=wednesday
Enter the date=3
Enter the activity of the day=singing
Enter the day=thursday
Enter the date=4
Enter the activity of the day=party
Enter the day=friday
Enter the date=5
Enter the activity of the day=revision
Enter the day=saturday
Enter the date=6
Enter the activity of the day=movie
Enter the day=sunday
Enter the date=7
Enter the activity of the day=outing
Day=monday
Date=1
activity=reading

Day=tuesday
Date=2
activity=writing

Day=wednesday
Date=3
activity=singing

Day=thursday
Date=4
activity=party

Day=friday
Date=5
activity=revision

Day=saturday
Date=6
activity=movie

Day=sunday
Date=7
activity=outing
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.
Solution
#include <stdio.h>
char str[100], pat[100], rep[100], 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])
{
i++;
m++;
if (pat[i] == '\0')
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++)
{
ans[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
ans[j] = '\0';
}
}
int main(){
printf("Enter the main string=");
gets(str);
printf("Enter the Pattern string=");
gets(pat);
printf("Enter the replace string=");
gets(rep);
StringMatch();
if (flag == 0)
{
printf("Pattern not found");
}
else
{
printf("The resultant string is=%s", ans);
}
return 0;
}

Output
Enter the main string=abcdef
Enter the Pattern string=ef
Enter the replace string=EF
The resultant string is=abcdEF
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
Solution
#include<stdio.h>
#define MAX 5
int top=-1, Stack[MAX];
void push(){
int item;
if(top==(MAX-1)){
printf("Stack Overflow\n");
}
else{
printf("Enter the elemnt to be pushed= ");
scanf("%d",&item);
Stack[++top]=item;
}
}
void pop(){
if(top==-1){
printf("Stack underflow\n");
}
else{
printf("Poped elemnt is=%d", Stack[top--]);
}
}
void display(){
int i;
if(top==-1){
printf("Stack Underflow\n");
}
else{
printf("The element of the stack are=\n");
for(i=top;i>=0;i--){
printf("Stack[%d]=%d \n",i,Stack[i]);
}
}
}
void palindrome(){
int i,count=0;
for(i=0;i<=(top/2);i++){
if(Stack[i]==Stack[top-i]){
count=count+1;
}
}
if((top/2+1)==count){
printf("Stack Content are palindrome\n");
}
else{
printf("Stack content are not palindrome\n");
}
}
int main(){
int ch;
do{
printf("Stack Menu");
printf("\n 1.push \n 2.pop\n 3.display\n 4.palindrome\n 5.exit\n");
printf("Enter your choice(1-5)=");
scanf("%d", &ch);
switch(ch){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
palindrome();
break;
case 5:
return 0;
default:printf("Wrong choice");
}
}while(ch!=4);
return 0;
}

Output
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=1
Enter the elemnt to be pushed= 1
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=1
Enter the elemnt to be pushed= 2
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=1
Enter the elemnt to be pushed= 3
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=1
Enter the elemnt to be pushed= 2
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=1
Enter the elemnt to be pushed= 1
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=3
The element of the stack are=
Stack[4]=1
Stack[3]=2
Stack[2]=3
Stack[1]=2
Stack[0]=1
Stack Menu
1.push
2.pop
3.display
4.palindrome
5.exit
Enter your choice(1-5)=4
Stack Content are palindrome
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
Solution
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX_SIZE 30
char s[MAX_SIZE];
int top=-1;
void push(char c){
s[++top]=c;
}
char pop(){
return s[top--];
}
int prc(char c){
switch (c)
{
case '+':
case '-':return 1;
case '*':
case '/':
case '%':return 2;
case '^':return 3;
case '(':return 0;
case '#':return -1;
}
}
int main(){
char infix[30],c,postfix[50];
int i=0,k=0;
push('#');
printf("Enter the expression=");
scanf("%s", infix);
while(infix[i]!='\0'){
c=infix[i];
if(c=='('){
push(c);
}
else if(isalnum(c)){
postfix[k++]=c;
}
else if(c==')'){
while(s[top]!='(')
postfix[k++] = pop();
pop();
}
else{
if(c=='^' && s[top]=='^'){
push(c);
}
else{
while(prc(c)<=prc(s[top])){
postfix[k++]=pop();
}
push(c);
}
}
i++;
}
while(s[top]!='#'){
postfix[k++]=pop();
postfix[k]='\0';
printf("postfix expression=%s", postfix);
return 0;
}
}

Output
Enter the expression=(a+b+c)*(a^b)*(a/b)
postfix expression=ab+c+ab^*ab/*
5. Develop a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
Solution
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define MAX_SIZE 30
float s[MAX_SIZE];
int top=-1;
void push(float c){
s[++top]=c;
}
float pop(){
return s[top--];
}

int main(){
char c,postfix[50];
int i=0;
float op1,op2;
printf("Enter the expression=");
scanf("%s", postfix);
while(postfix[i]!='\0'){
c=postfix[i];
if(isdigit(c)){
push(c-48);
}
else {
op2=pop();
op1=pop();
switch (c)
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
if(op2==0){
printf("error");
break;
}
push(op1/op2);
break;
case '^':
push(pow(op1,op2));
break;
default:
break;
}
}
i++;
}
printf("postfix expression=%s = evaluation=%f \n", postfix,pop());
return 0;
}

Output
Enter the expression=23+6*7/34-5
postfix expression=23+6*7/34-5 = evaluation=5.000000

b. Solving Tower of Hanoi problem with n disks


Solution
#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("Enter the number of disc:\n");
scanf("%d",&n);
tower(n,'A','B','C');
}

Output
Enter the number of disc:
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
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
Solution:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
# define MAX_SIZE 4
int cqueue[MAX_SIZE], front=0,rear=-1,count=0;
int cqfull(){
if(count==MAX_SIZE)
return 1;
else
return 0;
}
int cqempty(){
if(count==0)
return 1;
else
return 0;
}

void cinsert(int item){


if(cqfull()){
printf("Overflow\n");
}
else{
rear=(rear+1)%MAX_SIZE;
cqueue[rear]=item;
count++;
}
}

void cdelete()
{
if(cqempty()){
printf("Queue is empty\n");
}
else{
printf("deleted element is=%d\n", cqueue[front]);
front=(front+1)%MAX_SIZE;
count--;
}
}

void display() {
int i=front,k;
printf("The content are:");
if(cqempty()){
printf("queue is empty");
}
else{
for(k=0;k<count;k++){
printf("\n-------\n %d |", cqueue[i]);
printf("\n");
i=(i+1)%MAX_SIZE;
}
}
}
void main(){
int item,ch;
do{
printf("\n\n--------MAIN MENU------\n");
printf("1.Insert the element\n");
printf("2.Delete the element\n");
printf("3.Overflow \n");
printf("4.qempty \n");
printf("5.display\n");
printf("6.exit\n");
printf("Enter your choice =");
scanf("%d", &ch);
switch(ch){
case 1:{
printf("enter the element to inserted=");
scanf("%d",&item);
cinsert(item);
break;
}
case 2:
cdelete();
break;
case 3:
if(cqfull())
printf("Cqueue is full");
break;
case 4:
if(cqempty())
printf("Cqueue is empty");
break;
case 5:
display();
break;
case 6:
printf("exiting the program.\n");
break;
default:
printf("invalid choice please enter a valid option.\n");
}
}while(ch!=6);
return 0;
}

Output
--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =1
enter the element to inserted=1

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =1
enter the element to inserted=2

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =1
enter the element to inserted=3

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =1
enter the element to inserted=4

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =1
enter the element to inserted=5
Overflow

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =5
The content are:
-------
1|

-------
2|

-------
3|

-------
4|

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =2
deleted element is=1

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =2
deleted element is=2

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =2
deleted element is=3

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =2
deleted element is=4

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =2
Queue is empty

--------MAIN MENU------
1.Insert the element
2.Delete the element
3.Overflow
4.qempty
5.display
6.exit
Enter your choice =6
exiting the program.
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
Solution
#include <stdio.h>
#include<stdlib.h>
struct node{
char usn[10];
char name[30];
char program[15];
int sem;
int phoneno;
struct node*next;
};

typedef struct node NODE;


NODE *head=NULL;

void main(){
int choice;
while(1){
printf("\n--------\n");
printf("0.Create\n");
printf("1.display\n");
printf("2.To insert node at begging\n");
printf("3.To insert node at End\n");
printf("4.To delete the beginning node\n");
printf("5.To delete the end node\n");
printf("6.Exit\n");
printf("Enter the choice=");
scanf("%d", &choice);
switch(choice){
case 0:
create();
break;
case 1:
display();
break;
case 2:
insert_begin();
break;
case 3:
insert_end();
break;
case 4:
delete_begin();
break;
case 5:
delete_end();
break;
case 6:
exit(0);
default:
printf("Invalid");
break;
}
}
}

void create(){
int n;
NODE *temp;
printf("Enter how many nodes to be created=");
scanf("%d",&n);
for(int i=0;i<n;i++){
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the Usn, Name,Program,Sem,Phone number=");
scanf("%s", temp->usn);
scanf("%s", temp->name);
scanf("%s", temp->program);
scanf("%d", &(temp->sem));
scanf("%d", &(temp->phoneno));
temp->next=head;
head=temp;
}
}

void display(){
int count=0;
if(head==NULL){
printf("Linked List is empty\n");
return;
}
printf("LinkedList=");
NODE *temp=head;
while(temp!=NULL){
printf("Usn=%s \n Name=%s \n Program=%s \n Sem=%d \n Phone Number=%d\n\n", temp-
>usn,temp->name,temp->program,temp->sem,temp->phoneno);
count++;
temp=temp->next;
}
printf("No nodes in linked list=%d", count);
printf("\n");

void insert_begin(){
NODE *temp;
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the Usn, Name,Program,Sem,Phone number=");
scanf("%s", temp->usn);
scanf("%s", temp->name);
scanf("%s", temp->program);
scanf("%d", &(temp->sem));
scanf("%d", &(temp->phoneno));
temp->next=head;
head=temp;

void insert_end(){
NODE *temp;
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the Usn, Name,Program,Sem,Phone number=");
scanf("%s", temp->usn);
scanf("%s", temp->name);
scanf("%s", temp->program);
scanf("%d", &(temp->sem));
scanf("%d", &(temp->phoneno));
temp->next=NULL;
if(head==NULL){
head=temp;
return;
}
else{
NODE *ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}

void delete_begin(){
if(head==NULL){
printf("List does not exit");
return;
}
printf("\n %s", head->usn);
if(head->next==NULL){
free(head);
head==NULL;
}
else{
NODE *temp;
temp=head;
head=head->next;
free(temp);
}
}
void delete_end(){
if(head==NULL){
printf("List does not exit");
return;
}
printf("\n %s", head->usn);
if(head->next==NULL){
free(head);
head==NULL;
}
else{
NODE *temp, *prev;
while(temp->next!=NULL){
prev=temp;
temp=temp->next;
}
free(temp);
prev->next==NULL;
}
}

Output
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=0
Enter how many nodes to be created=2
Enter the Usn, Name,Program,Sem,Phone number=
1
XYZ
CSE
3
9876
Enter the Usn, Name,Program,Sem,Phone number=
2
ABC
ECE
3
8797

--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=2
Enter the Usn, Name,Program,Sem,Phone number=
3
PQR
IS
3
9988

--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=1
LinkedList=Usn=3
Name=PQR
Program=IS
Sem=3
Phone Number=9988

Usn=2
Name=ABC
Program=ECE
Sem=3
Phone Number=8797

Usn=1
Name=XYZ
Program=CSE
Sem=3
Phone Number=9876

No nodes in linked list=3

--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=3
Enter the Usn, Name,Program,Sem,Phone number=4
STU
AIML
3
6655
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=1
LinkedList=Usn=3
Name=PQR
Program=IS
Sem=3
Phone Number=9988

Usn=2
Name=ABC
Program=ECE
Sem=3
Phone Number=8797

Usn=1
Name=XYZ
Program=CSE
Sem=3
Phone Number=9876

Usn=4
Name=STU
Program=AIML
Sem=3
Phone Number=6655

No nodes in linked list=4

--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=4

3
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=6
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
Solution
#include <stdio.h>
#include<stdlib.h>
#include<stdlib.h>
struct node{
int ssn;
char name[20],department[20],designation[20],phone[20];
float salary;
struct node*llink,*rlink;
};

typedef struct node NODE;


NODE *head=NULL;

void create(){
int n;
printf("Enter how many employee to be added=");
scanf("%d",&n);
for(int i=0;i<n;i++)
insert_end();
}
void insert_begin(){
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
printf("ssn,name,department,designation,phone number,salary=");
scanf("%d",&(temp->ssn));
scanf("%s",(temp->name));
scanf("%s",(temp->department));
scanf("%s",(temp->designation));
scanf("%s",(temp->phone));
scanf("%f",&(temp->salary));
if(head==NULL){
temp->rlink=temp->llink=NULL;
head=temp;
}
else{
temp->rlink=head;
head->llink=temp;
temp->llink=NULL;
}
head=temp;
}
void insert_end(){
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
printf("ssn,name,department,designation,phone number,salary=");
scanf("%d",&(temp->ssn));
scanf("%s",(temp->name));
scanf("%s",(temp->department));
scanf("%s",(temp->designation));
scanf("%s",(temp->phone));
scanf("%f",&(temp->salary));
if(head==NULL){
temp->rlink=temp->llink=NULL;
head=temp;
}
else{
NODE *temp1=head;
while(temp1->rlink!=NULL)
temp1=temp1->rlink;
temp1->rlink=temp;
temp->llink=temp1;
temp->rlink=NULL;
}
}
void delete_begin(){
NODE *temp;
if(head==NULL){
printf("employee list is empty\n");
return;
}
if(head->rlink==NULL){
printf("Employee details deleted:ssn%d",head->ssn);
free(head);
head=NULL;
}
else{
printf("Employee details deleted:ssn%d",head->ssn);
head=head->rlink;
free(head->llink);
head->llink=NULL;
}
}
void display(){
NODE *temp=head;
int count=0;
if(head==NULL){
printf("List not there");
return;
}
while(temp!=NULL){
printf("%d\n%s\n%s\n%s\n%s\n%f\n\n",temp->ssn,temp->name,temp->department,temp-
>designation,temp->phone,temp->salary);
temp=temp->rlink;
count++;
}
printf("No of nodes=%d", count);
}
void delete_end(){
NODE *temp;
if(head==NULL){
printf("list is empty");
return;
}
if(head->rlink==NULL){
printf("Employee details deleted:ssn%d",head->ssn);
free(head);
}
else{
temp=head;
while(temp->rlink!=NULL)
temp=temp->rlink;
printf("Employee details deleted:ssn%d",temp->ssn);
temp->llink->rlink=NULL;
free(temp);
}
}
void main(){
int choice;
while(1){
printf("\n--------\n");
printf("0.Create\n");
printf("1.display\n");
printf("2.To insert node at begging\n");
printf("3.To insert node at End\n");
printf("4.To delete the beginning node\n");
printf("5.To delete the end node\n");
printf("6.Exit\n");
printf("Enter the choice=");
scanf("%d", &choice);
switch(choice){
case 0:
create();
break;
case 1:
display();
break;
case 2:
insert_begin();
break;
case 3:
insert_end();
break;
case 4:
delete_begin();
break;
case 5:
delete_end();
break;
case 6:
exit(0);
default:
printf("Invalid");
break;
}
}
}
Output
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=0
Enter how many employee to be added=2
ssn,name,department,designation,phone number,salary=
1
ABC
PORGRAMMER
manager
986
50000
ssn,name,department,designation,phone number,salary=
2
XYZ
Coder
ceo
8158
10000

--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=2
ssn,name,department,designation,phone number,salary=
3
Hii
Worker
head
6677
25000
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=3
ssn,name,department,designation,phone number,salary=4
STU
Owner
Owner
569
900000

--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=1
3
Hii
Worker
head
6677
25000.000000

1
ABC
PORGRAMMER
manager
986
50000.000000

2
XYZ
Coder
ceo
8158
10000.000000

4
STU
Owner
Owner
569
900000.000000

No of nodes=4
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=4
Employee details deleted:ssn3
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=5.
Employee details deleted:ssn4
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=1
1
ABC
PORGRAMMER
manager
986
50000.000000

2
XYZ
Coder
ceo
8158
10000.000000

No of nodes=2
--------
0.Create
1.display
2.To insert node at begging
3.To insert node at End
4.To delete the beginning node
5.To delete the end node
6.Exit
Enter the choice=6
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) = 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)
Support the program with appropriate functions for each of the above operations
Solution:
#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;
}//end switch
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)
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 1 5

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^1z^5 + -2x^1y^1z^3
Enter the value of x,y and z: 1 2 3

Result of polynomial evaluation is : -990

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

Enter the POLY1(x,y,z):

Enter the no of terms in the polynomial: 2

Enter the 1 term:


Coef = 5

Enter Pow(x) Pow(y) and Pow(z): 1 2 6

Enter the 2 term:


Coef = -6

Enter Pow(x) Pow(y) and Pow(z): 3 4 0

Polynomial 1 is:
5x^1y^2z^6 + -6x^3y^4z^0
Enter the POLY2(x,y,z):

Enter the no of terms in the polynomial: 2

Enter the 1 term:


Coef = 10

Enter Pow(x) Pow(y) and Pow(z): 1 2 6


Enter the 2 term:
Coef = 8

Enter Pow(x) Pow(y) and Pow(z): 1 4 5

Polynomial 2 is:
10x^1y^2z^6 + 8x^1y^4z^5
Polynomial addition result:
15x^1y^2z^6 + -6x^3y^4z^0 + 8x^1y^4z^5
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
Solution:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *left, *right;
};
typedef struct node NODE;
NODE *root = NULL;
NODE *create(int data){
NODE *temp;
temp = (NODE *)malloc(sizeof(NODE));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
NODE *insert(NODE *root, int data){
if (root == NULL)
return create(data);
else if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
else
printf("Duplicate output\n");
return root;
}

void Inorder(NODE *root){


if (root != NULL){
Inorder(root->left);
printf("%d ", root->data);
Inorder(root->right);
}
}
void Preorder(NODE *root){
if (root != NULL){
printf("%d ", root->data);
Preorder(root->left);
Preorder(root->right);
}
}

void Postorder(NODE *root){


if (root != NULL){
Postorder(root->left);
Postorder(root->right);
printf("%d ", root->data);
}
}

NODE *search(NODE *root, int key){


if (root == NULL || root->data == key)
return root;
else if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
}

int main(){
int data, choice, key;
NODE *temp;
while (1){
printf("1. Create Tree\n");
printf("2. Inorder\n");
printf("3. Preorder\n");
printf("4. Postorder\n");
printf("5. Search\n");
printf("6. Exit\n");
printf("Enter the choice=");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter the element=");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
Inorder(root);
printf("\n");
break;
case 3:
Preorder(root);
printf("\n");
break;
case 4:
Postorder(root);
printf("\n");
break;
case 5:
printf("Enter the search element=");
scanf("%d", &key);
temp = search(root, key);
if (temp == NULL)
printf("No data found\n");
else
printf("Data found\n");
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
break;
}
}
printf("\n");
return 0;
}
Output:
1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=6
1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=9

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=5

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=2

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=8

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=15

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=24

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=14

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=7

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=8
Duplicate output

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=5
Duplicate output

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=1
Enter the element=2
Duplicate output

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=2
2 5 6 7 8 9 14 15 24

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=3
6 5 2 9 8 7 15 14 24

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=4
2 5 7 8 14 24 15 9 6

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=5
Enter the search element=15
Data found

1. Create Tree
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter the choice=6
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
Solution:
#include <stdio.h>
#include <stdlib.h>
int a[10][10],q[10],f=0,r=-1,n,v[10],i,j;
void bfs(int s){
v[s]=1;
q[++r]=s;
printf("Visited=%d\n",s);
while(f<=r){
s=q[f++];
for(i=1;i<=n;i++){
if(a[s][i]==1&&v[i]==0){
v[i]=1;
printf("Visited=%d\n",i);
q[++r]=i;
}
}
}
}
void dfs(int s){
v[s]=1;
printf("Visited=%d\n",s);
for(i=1;i<=n;i++){
if(a[s][i]==1 && v[i]==0){
dfs(i);
}
}
}
void main() {
int choice,s;
while(1){
printf("\n1.Read adjacent matrix");
printf("\n2.DFS");
printf("\n3.BFS");
printf("\n4.Exit");
printf("\nEnter the choice=");
scanf("%d",&choice);
switch(choice){
case 1:printf("Enter the vertices=");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
break;
case 2:
printf("Enter the source=");
scanf("%d",&s);
dfs(s);
for(i=1;i<=n;i++){
v[i]=0;
}
break;
case 3:
printf("Enter the source=");
scanf("%d",&s);
bfs(s);
for(i=1;i<=n;i++){
v[i]=0;
}
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice. Please enter a valid option.");
break;
}
}
}

Output:
1.Read adjacent matrix
2.DFS
3.BFS
4.Exit
Enter the choice=1
Enter the vertices=4
0110010110010010
1.Read adjacent matrix
2.DFS
3.BFS
4.Exit
Enter the choice=2
Enter the source=3
Visited=3
Visited=1
Visited=2
Visited=4

1.Read adjacent matrix


2.DFS
3.BFS
4.Exit
Enter the choice=3
Enter the source=1
Visited=1
Visited=2
Visited=3
Visited=4

1.Read adjacent matrix


2.DFS
3.BFS
4.Exit
Enter the choice=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
Solution:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 110
struct employee {
int id;
char name[20];
char des[20];
};
struct employee emp[MAX];
void insert() {
int id, i, hash, loc;
char name[20];
char des[20];
printf("Enter Employee id, name, designation=");
scanf("%d%s%s", &id, name, des);
hash = id % MAX;
for (i = 0; i < MAX; i++) {
loc = (hash + i) % MAX;
if (emp[loc].id == -1) {
emp[loc].id = id;
strcpy(emp[loc].name, name);
strcpy(emp[loc].des, des);
return;
}
}
printf("\nTable full");
}

void display() {
int i;
for (i = 0; i < MAX; i++) {
if (emp[i].id != -1) {
printf("\n id=%d,\n Name=%s \n Des=%s\n", emp[i].id, emp[i].name, emp[i].des);
}
}
}
void search(int key) {
int hash, loc, i;
hash = key % MAX;
for (i = 0; i < MAX; i++) {
loc = (hash + i) % MAX;
if (emp[loc].id == key) {
printf("Employee found\n");
return;
}
}
printf("\nEmployee not found");
}

int main() {
int ch, key;
for (int i = 0; i < MAX; i++) {
emp[i].id = -1;
}
while (1) {
printf("1.Insert\n");
printf("2.Search\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("Enter the choice\n");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
printf("Enter employee key to be searched: ");
scanf("%d", &key);
search(key);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please enter a valid option.\n");
break;
}
}
return 0;
}
Output:
1.Insert
2.Search
3.Display
4.Exit
Enter the choice
1
Enter Employee id, name, designation=16
XYZ
CEO
1.Insert
2.Search
3.Display
4.Exit
Enter the choice
2
Enter employee key to be searched: 16
Employee found
1.Insert
2.Search
3.Display
4.Exit
Enter the choice
3

id=16,
Name=XYZ
Des=CEO
1.Insert
2.Search
3.Display
4.Exit
Enter the choice
4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy