Ds Lab Manual
Ds Lab Manual
1. Design, Develop and Implement a menu driven Program in C for the following Array
operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
int n,a[10],ELEM,pos,j;
void create()
{
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the array elements are\n");
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
}
}
void display()
{
if(n==0)
{
printf("Empty array\n");
}
else
{
printf("The array elements are\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[j]);
}
}
}
void insertion()
{
if(n==0)
{
printf("Empty array\n");
}
else
{
void main()
{
int ch;
while(1)
{
printf("\n-----MENU-----\n");
printf("1.Create\n2.Display\n3.Insertion \n4.Deletion \n5.Exit\n");
printf("Enter your option\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:insertion();
break;
case 4:deletion();
break;
case 5:exit(0);
default:printf("Invalid choice\n");
}
}
}
OUTPUT:
-----MENU-----
1.Create
2.Display
3.Insertion
4.Deletion
5.Exit
Enter your option
1
Enter the number of elements
5
Enter the array elements are
10 20 30 40 50
-----MENU-----
1.Create
2.Display
3.Insertion
4.Deletion
5.Exit
Enter your option
2
The array elements are
10 20 30 40 50
-----MENU-----
1.Create
2.Display
3.Insertion
4.Deletion
5.Exit
Enter your option
3
Enter the position of element to be inserted
0 Enter your option
Enter the element to be inserted 4
60 Enter the position of element to be deleted
The array elements are 5
60 10 20 30 40 50 The array elements are
60 70 10 20 30 50
-----MENU-----
1.Create -----MENU-----
2.Display 1.Create
3.Insertion 2.Display
4.Deletion 3.Insertion
5.Exit 4.Deletion
5.Exit
Enter your option
Enter your option 5
3 sahyadri@sahyadri-Veriton-M275:~/ds$
Enter the position of element to be inserted
8
Enter the element to be inserted
90
8 is a invalid position
-----MENU-----
1.Create
2.Display
3.Insertion
4.Deletion
5.Exit
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. 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>
int c,m,i,j,k,flag;
char str[100],pat[50],rep[50],ans[150];
void patmat();
void main()
{
printf("Enter the main string\n");
gets(str);
printf("Enter the pattern string\n");
gets(pat);
printf("Enter the replacing string\n");
gets(rep);
patmat();
if(flag==1)
{
printf("The resultant string is\n");
puts(ans);
}
else
{
printf("The pattern is not found\n");
}
}
void patmat()
{
while(str[c]!='\0')
{
if(str[m]==pat[i])
{
m++;
i++;
if(pat[i]=='\0')
{
flag=1;
for(k=0;rep[k]!='\0';k++)
{
ans[j]=rep[k];
j++;
c=m;
i=0;
}
}
}
else
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
}
OUTPUT:
3. Design, Develop and Implement a menu driven Program in C for the following operations
on STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#define MS 5
int s[MS],flag,top= -1;
void push()
{
int n;
if(top==(MS-1))
{
printf("The stack is overflow\n");
}
else
{
printf("Enter the element to be pushed\n");
scanf("%d",&n);
s[++top]=n;
}
}
void pop()
{
int n;
if(top==-1)
{
printf("The stack is underflow\n");
}
else
{
n=s[top--];
printf("The poped element is %d\n",n);
}
}
void display()
{
int i;
if(top==-1)
{
printf("The stack is underflow\n");
}
else
{
printf("The stack elements are\n");
for(i=top;i>=0;i--)
{
printf("%d\n",s[i]);
}
}
}
void palindrome()
{
int i,j;
if(top==-1)
{
printf("The stack is underflow\n");
}
else
{
for(i=0,j=top;i<=((j/2)+1);i++,j--)
{
if(s[i]==s[j])
{
continue;
}
else
{
flag=1;
break;
}
}
if(flag==1)
{
printf("Not a palindrome\n");
flag=0;
}
else
{
printf("Palindrome\n");
}
}
}
void main()
{
int ch;
do
{
printf("\n-----MENU-----\n");
printf("1.Push\n2.Pop\n3.Display\n4.Palindrome\n5.Exit\n");
printf("Enter your option\n");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: palindrome();
break;
case 5: exit(0);
default:printf("Invalid option\n");
}
}while(ch>=1&&ch<=5);
}
OUTPUT:
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
3
The stack elements are
10
15
15
10
-----MENU-----
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
4
Palindrome
-----MENU-----
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
3
The stack elements are
10
15
15
10
-----MENU-----
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
3
The stack elements are
10
15
-----MENU-----
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
2
The poped element is 10
-----MENU-----
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
2
The stack is underflow
-----MENU-----
1.Push
2.Pop
3.Display
4.Palindrome
5.Exit
Enter your option
5
sahyadri@sahyadri-Veriton-M275:~/ds$
Postfix Expression. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, %(Remainder), ^(Power) and alphanumeric
operands.
#include<stdio.h>
#include<stdlib.h>
#define MS 5
char infix[50],postfix[50],item;
void convert();
struct stack
{
int top;
char item[MS];
}s;
void push(char value)
{
if(s.top==(MS-1))
{
printf("The stack is overflow\n");
exit(0);
}
else
{
s.item[++s.top]=value;
}
}
char pop()
{
if(s.top= =-1)
{
printf("stack underflow\n");
exit(0);
}
return(s.item[s.top--]);
}
int empty()
{
if(s.top= = -1)
{
return 1;
}
else
{
return 0;
}
}
int precedence(char c)
{
switch(c)
{
case '^':return 3;
case '*':
case '/':
case '%':return 2;
case '+':
case '-':return 1;
case '(':return 0;
}
}
void main()
{
s.top = -1;
printf("Enter the infix expression :\n");
gets(infix);
convert();
printf("The postfix expression is:\n");
puts(postfix);
}
void convert()
{
int i,pos=0;
char symb,t;
for(i=0;infix[i]!='\0';i++)
{
symb=infix[i];
switch(symb)
{
case '(':push(symb);
break;
case ')':while((t=pop())!='(')
{
postfix[pos++]=t;
}
break;
case '^':
case '*':
case '/':
case '%':
case '+':
case’-‘:
while((!empty())&&((precedence(s.item[s.top]))>=precedence(symb)))
{
postfix[pos++]=pop();
}
push(symb);
break;
default: postfix[pos++]=symb;
break;
}
}
while(!empty())
{
postfix[pos++]=pop();
}
postfix[pos]='\0';
}
OUTPUT 1:
OUTPUT 2:
sahyadri@sahyadri-Veriton-M275:~/ds$ ./a.out
Enter the infix expression :
(a+b)*c/d^e%f
The postfix expression is:
ab+c*de^/f%
OUTPUT 3:
sahyadri@sahyadri-Veriton-M275:~/ds$ ./a.out
Enter the infix expression :
a+)
stack underflow
sahyadri@sahyadri-Veriton-M275:~/ds$
5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *,
/, %, ^
b. Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MS 50
char postfix[50];
struct stack
{
int top;
int item[MS];
}s;
void push(int value)
{
if(s.top==(MS-1))
{
printf("overflow\n");
}
else
{
s.item[++s.top]=value;
}
}
int pop()
{
if(s.top==-1)
{
printf("\n stack underflow");
exit(0);
}
return(s.item[s.top--]);
}
int empty()
{
if(s.top==-1)
{
return 1;
}
else
{
return 0;
}
}
int operation(int a,int b,char c)
{
switch(c)
{
case '^':return(pow(a,b));
case '*':return(a*b);
case '%':return(a%b);
case '/':return(a/b);
case '+':return(a+b);
case '-':return(a-b);
}
}
int evaluate()
{
int i,a,b,ans,value;
char symb;
for(i=0;postfix[i]!='\0';i++)
{
symb=postfix[i];
if((symb>='0')&&(symb<='9'))
{
push((int)(symb-'0'));
}
else
{
a=pop();
b=pop();
value=operation(b,a,symb);
push(value);
}
}
ans=pop();
return ans;
}
void main()
{
s.top=-1;
int ans;
printf("Enter the postfix expression\n");
gets(postfix);
ans=evaluate();
printf("The resultant ans is %d\n",ans);
}
OUTPUT:
#include<stdio.h>
#include<math.h>
void toh(int n,char s,char t,char d);
void main()
{
int n;
char s,t,d;
printf("Enter the number of disks\n");
scanf("%d",&n);
toh(n,'s','t','d');
printf("Total Number of moves are:%d\n",(int)pow(2,n)-1);
}
void toh(int n,char s,char t,char d)
{
if(n==0)
{
return;
}
toh(n-1,s,d,t);
printf("%d move %c to %c\n",n,s,d);
toh(n-1,t,s,d);
}
OUTPUT:
6. Design, Develop and Implement 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>
#define MS 5
typedef struct
{
int front,rear;
float item[MS];
}cqueue;
cqueue cq;
void cqinsert(float val)
{
if(cq.front==(cq.rear+1)%MS)
{
printf("Circular queue is full\n");
}
else
{
cq.rear=(cq.rear+1)%MS;
cq.item[cq.rear]=val;
}
}
float cqdelete()
{
float value;
if(cq.front==cq.rear)
{
printf("Circular queue is empty\n");
}
else
{
cq.front=(cq.front+1)%MS;
value=cq.item[cq.front];
printf("The deleted element is\n");
printf("%f",value);
}
}
void cqdisplay()
{
int i;
if(cq.front==cq.rear)
{
printf("Circular queue is empty\n");
}
else
{
if(cq.front<cq.rear)
{
for(i=cq.front+1;i<=cq.rear;i++)
{
printf("%f\t",cq.item[i]);
}
}
else
{
if(cq.front!=(MS-1))
for(i=cq.front+1;i<=(MS-1);i++)
{
printf("%f\t",cq.item[i]);
}
for(i=0;i<=cq.rear;i++)
{
printf("%f\t",cq.item[i]);
}
}
}
}
void main()
{
cq.front=MS-1;
cq.rear=MS-1;
int ch;
float m;
while(1)
{
printf("\n\tMenu\n1.Cqinsert\n2.Cqdelete\n3.Cqdisplay\n4.Exit\n");
printf("Enter your option\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element to be inserted\n");
scanf("%f",&m);
cqinsert(m);
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
case 4:exit(0);
default:printf("Invalid choice\n");
}
}
}
OUTPUT:
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
1
Enter the element to be inserted
5
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
1
Enter the element to be inserted
15
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
1
Enter the element to be inserted
25
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
1
Enter the element to be inserted
35
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
1
Enter the element to be inserted
45
Circular queue is full
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
3
5.000000 15.000000 25.000000 35.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
2
The deleted element is
5.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
1
Enter the element to be inserted
45
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
3
15.000000 25.000000 35.000000 45.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
2
The deleted element is
15.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
2
The deleted element is
25.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
2
The deleted element is
35.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
2
The deleted element is
45.000000
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
2
Circular queue is empty
Menu
1.Cqinsert
2.Cqdelete
3.Cqdisplay
4.Exit
Enter your option
4
sahyadri@sahyadri-Veriton-M275:~/ds$
7. Design, Develop and Implement a menu driven Program in C for the following operations
on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 5
int c;
struct student
{
char name[25],usn[12],branch[10],phon[11];
int sem;
struct student *next;
};
typedef struct student node;
node *head = NULL;
int countnodes()
{
node *p;
p=head;
c=0;
while(p!=NULL)
{
p=p->next;
c++;
}
return c;
}
node *getnode()
{
node *nn;
nn=(node*)malloc(sizeof(node));
printf("Enter student details \n");
printf("Name:");
scanf("%s",nn->name);
printf("USN:");
scanf("%s",nn->usn);
printf("Branch:");
scanf("%s",nn->branch);
printf("Phone NO:");
scanf("%s",nn->phon);
printf("SEM:");
scanf("%d",&nn->sem);
nn->next=NULL;
return nn;
}
node *display()
{
node *p;
if(head==NULL)
printf("No student data\n");
else
{
p=head;
printf("Name\tUSN\t\tBranch\tPhoneNO\tSEM\n");
while(p!=NULL)
{
printf("%s\t%s\t%s\t%s\t%d\n",p->name,p->usn,p->branch,p->phon,p->sem);
p=p->next;
}
printf("The number of nodes in list is %d",countnodes(head));
}
return head;
}
node *create()
{
node *nn;
if(countnodes(head)==MAX)
{
printf("List is overflow");
}
else if(head==NULL)
{
nn=getnode(head);
head=nn;
}
else
{
nn=getnode(head);
nn->next=head;
head=nn;
}
return head;
}
node *insertfront()
{
create();
}
node *insertrear()
{
node *nn,*p;
if(countnodes(head)==MAX)
printf("List is overflow");
else
{
p=head;
if(head==NULL)
{
nn=getnode(head);
head=nn;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
nn=getnode(head);
p->next=nn;
}
}
return head;
}
node *deletefront()
{
node *p;
if(head==NULL)
{
printf("No data\n");
}
else
{
p=head;
head=head->next;
}
free(p);
return head;
}
node *deleterear()
{
node *p,*q;
if(head==NULL)
{
printf("No data\n");
}
else if(countnodes(head)==1)
{
p=head;
head=NULL;
free(p);
printf("Node is Deleted");
}
else
{
p=head;
while((p->next)->next!=NULL)
{
p=p->next;
}
q=p->next;
p->next=NULL;
free(q);
}
return head;
}
void main()
{
int ch,i,n;
node *head;
head=NULL;
do
{
printf("\n\t*...Student Data...*");
printf("\n1.Create\n2.Display\n3.Insertfront\n4.Insertrear\n5.Deletefront\n6.Deleterear\n7
.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
create();
break;
case 2:display();
break;
case 3:insertfront();
break;
case 4:insertrear();
break;
case 5:deletefront();
break;
case 6:deleterear();
break;
case 7:exit(0);
default:printf("Invalid choice\n");
}
}while(ch>=1&&ch<=7);
}
OUTPUT:
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
1
Enter number of students
2
Enter student details
Name:ashu
USN:4sf15is001
Branch:is
Phone NO:9945672381
SEM:3
Enter student details
Name:bindu
USN:4sf15is002
Branch:cs
Phone NO:9449688290
SEM:6
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
2
Name USN Branch PhoneNO SEM
bindu 4sf15is002 cs 9449688290 6
ashu 4sf15is001 is 9945672381 3
The number of nodes in list is 2
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
3
Enter student details
Name:chitra
USN:4sf15is009
Branch:me
Phone NO:9923456789
SEM:8
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
2
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
2
Name USN Branch PhoneNO SEM
bindu 4sf15is002 cs 9449688290 6
ashu 4sf15is001 is 9945672381 3
dhanu 4sf15is008 is 9876542387 4
The number of nodes in list is 3
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
6
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
2
Name USN Branch PhoneNO SEM
bindu 4sf15is002 cs 9449688290 6
ashu 4sf15is001 is 9945672381 3
The number of nodes in list is 2
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
5
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
6
Node is Deleted
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
2
No student data
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
7
sahyadri@sahyadri-Veriton-M275:~/ds$
8. Design, Develop and Implement a menu driven Program in C for the following operations
on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int c;
struct employee
{
int ssn;
char name[25],dept[12],desig[20],phon[11];
float salary;
struct employee *next;
struct employee *prev;
};
typedef struct employee node;
node *head = NULL;
node *getnode()
{
node *nn;
nn=(node*)malloc(sizeof(node));
printf("Enter employee details \n");
printf("Name:");
scanf("%s",nn->name);
printf("SSN:");
scanf("%d",&nn->ssn);
printf("Department:");
scanf("%s",nn->dept);
printf("Designation:");
scanf("%s",nn->desig);
printf("Phone NO:");
scanf("%s",nn->phon);
printf("Salary:");
scanf("%f",&nn->salary);
nn->next=nn->prev=NULL;
return nn;
}
node *p;
p=head;
c=0;
while(p!=NULL)
{
p=p->next;
c++;
}
return c;
}
void create()
{
node *nn,*p;
p=head;
if(head==NULL)
{
nn=getnode(head);
head=nn;
}
else
{
nn=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=nn;
nn->prev=p;
}
}
void insertfront()
{
node *nn;
nn=getnode();
if(head==NULL)
{
head=nn;
}
else
{
nn->next=head;
head->prev=nn;
head=nn;
}
void insertrear()
{
create();
}
void display()
{
node *p;
int count =0;
if(head==NULL)
{
printf("No data\n");
}
else
{
p=head;
printf("Name\tSSN\tDepartment\tDesig\tPhoneNO\tSalary\n");
while(p!=NULL)
{
count++;
printf("%s\t%d\t%s\t%s\t%s\t%f\n",p->name,p->ssn,p->dept,p->desig,p-
>phon,p->salary);
p=p->next;
}
}
printf("The number of nodes in list is %d\n",count);
}
void deletefront()
{
node *p;
if(head==NULL)
{
printf("No data\n");
}
else if((head->next)==NULL)
{
p=head;
head=NULL;
free(p);
}
else
{
p=head;
(head->next)->prev=NULL;
head=head->next;
p->next=NULL;
free(p);
}
}
void deleterear()
{
node *p,*q;
if(head==NULL)
{
printf("No data\n");
}
else if((head->next)==NULL)
{
p=head;
head=NULL;
free(p);
}
else
{
p=head;
while(p->next!=NULL)
{
p=p->next;
}
q=p->prev;
q->next=NULL;
p->prev=NULL;
free(p);
}
}
void main()
{
int ch,i,n;
do
{ printf("\n\t*....Employee Data......*");
printf("\n1.Create\n2.Display\n3.InsertFront\n4.InsertRear\n5.DeleteFront\n6.DeleteRear
\n7.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter number of employees\n");
scanf("%d",&n);
for(i=0;i<n;i++)
create();
break;
case 2:display();
break;
case 3:insertfront();
break;
case 4:insertrear();
break;
case 5:deletefront();
break;
case 6:deleterear();
break;
case 7:exit(0);
default:printf("Invalid choice\n");
}
}while(ch>=1&&ch<=7);
}
OUTPUT:
Phone NO:9876542345
Salary:25000
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
2
Name SSN Department Desig PhoneNO Salary
Ajay 101 cs manager 9876545678 50000.000000
Bhanu 102 cv instructor 9876542345 25000.000000
The number of nodes in list is 2
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
3
Enter employee details
Name:Chiru
SSN:103
Department:cs
Designation:professor
Phone NO:9876534562
Salary:75000
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
2
Name SSN Department Desig PhoneNO Salary
Chiru 103 cs professor 9876534562 75000.000000
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
2
Name SSN Department Desig PhoneNO Salary
Ajay 101 cs manager 9876545678 50000.000000
Bhanu 102 cv instructor 9876542345 25000.000000
Dhanvi 104 is manager 8967567890 30000.000000
The number of nodes in list is 3
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
6
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
2
Name SSN Department Desig PhoneNO Salary
Ajay 101 cs manager 9876545678 50000.000000
Bhanu 102 cv instructor 9876542345 25000.000000
The number of nodes in list is 2
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
5
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
6
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
5
No data
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
7
sahyadri@sahyadri-Veriton-M275:~/ds$
9. Design, Develop and Implement a Program in C for the following operationson Singly
Circular Linked List (SCLL) with header nodes
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
struct poly
{
int cf,px,py,pz;
int flag;
struct poly *next;
};
typedef struct poly node;
node* getnode()
{
node *nn;
nn=(node*)malloc(sizeof(node));
if(nn==NULL)
{
printf("Insufficient memory\n");
exit(0);
}
return nn;
}
void display(node *head)
{
node *p;
if(head->next==head)
{
printf("Polynomial does not exist\n");
return;
}
p=head->next;
while(p!=head)
{
printf("%dx^%dy^%dz^%d",p->cf,p->px,p->py,p->pz);
if(p->next!= head)
printf(" + ");
p=p->next;
}
}
node* insert_rear(int cf,int x,int y,int z,node *head)
{
node *p,*v;
p=getnode();
p->cf=cf;
p->px=x;
p->py=y;
p->pz=z;
v=head->next;
while(v->next!=head)
{
v=v->next;
}
v->next=p;
p->next=head;
return head;
}
node* read_poly(node *head)
{
int px, py, pz, cf, ch;
do
{
printf("Enter coeff: ");
scanf("%d",&cf);
printf("Enter powers of x,y,z\n ");
scanf("%d%d%d",&px,&py,&pz);
head=insert_rear(cf,px,py,pz,head);
printf("If your wish to continue press 1 otherwise 0\n");
scanf("%d", &ch);
}while(ch != 0);
return head;
}
node* add_poly(node *h1,node *h2,node *h3)
{
node *p1,*p2;
int x1,x2,y1,y2,z1,z2,cf1,cf2,cf;
p1=h1->next;
while(p1!=h1)
{
x1=p1->px;
y1=p1->py;
z1=p1->pz;
cf1=p1->cf;
p2=h2->next;
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->next;
}
if(p2!=h2)
{
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->next;
}
p2=h2->next;
while(p2!=h2)
{
if(p2->flag==0)
h3=insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
p2=p2->next;
}
return h3;
}
void evaluate(node *head)
{
node *p;
int x, y, z;
int result=0;
p=head->next;
printf("\nEnter x,y,z terms to evaluate:\n");
scanf("%d%d%d",&x,&y,&z);
while(p!= head)
{
result = result + (p->cf * pow(x,p->px) * pow(y,p->py) * pow(z,p->pz));
p=p->next;
}
printf("Polynomial result is: %d", result);
}
void main()
{
node *h1,*h2,*h3;
int ch;
h1=getnode();
h2=getnode();
h3=getnode();
h1->next=h1;
h2->next=h2;
h3->next=h3;
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: h1->next=h1;
printf("\nEnter polynomial to evaluate:\n");
h1=read_poly(h1);
printf("The polynomial is :");
display(h1);
evaluate(h1);
break;
case 2: h1->next=h1;
printf("\nEnter the first polynomial:\n");
h1=read_poly(h1);
printf("\nEnter the second polynomial:\n");
h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: \n");
display(h3);
case 3: exit(0);
default:printf("\nInvalid entry");
break;
}
}
}
OUTPUT:
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 1
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 2
Enter coeff: 4
Enter powers of x,y,z
222
If your wish to continue press 1 otherwise 0
1
Enter coeff: 3
Enter powers of x,y,z
112
If your wish to continue press 1 otherwise 0
0
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 3
sahyadri@sahyadri-Veriton-M275:~/ds$
10. Design, Develop and Implement a menu driven Program in C for the following operations
on Binary Search Tree (BST) of Integers
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct BST
{
int data;
struct BST *lc,*rc;
};
typedef struct BST node;
node *create(node *root,int a)
{
node *temp;
if(root==NULL)
{
temp=(node*)malloc(sizeof(node));
root=temp;
temp->data=a;
temp->rc=temp->lc=NULL;
return root;
}
else if(a<root->data)
{
root->lc=create(root->lc,a);
}
else if(a>root->data)
{
root->rc=create(root->rc,a);
}
else
return root;
}
void search(node *root,int k)
{
if(root==NULL)
{
printf("The key is not found\n");
}
else if(k<root->data)
{
search(root->lc,k);
}
else if(k>root->data)
{
search(root->rc,k);
}
else
printf("The key is found\n");
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->lc);
preorder(root->rc);
}
}
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->lc);
postorder(root->rc);
printf("%d\t",root->data);
}
}
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->lc);
printf("%d\t",root->data);
inorder(root->rc);
}
}
void traversal(node *root)
{
int ch;
do
{
printf("\n\tMenu\n1.Inorder traversal\n2.Preorder traversal\n3.Postorder
traversal\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
Menu
1.Create
2.Search
3.Traversal
4.Exit
Enter your choice
1
Enter the number of elements
12
Enter the elements to be inserted
6 9 5 2 8 15 24 14 7 8 5 2
Menu
1.Create
2.Search
3.Traversal
4.Exit
Enter your choice
2
Enter the element to be searched
8
The key is found
Menu
1.Create
2.Search
3.Traversal
4.Exit
Enter your choice
2
Enter the element to be searched
25
The key is not found
Menu
1.Create
2.Search
3.Traversal
4.Exit
Enter your choice
3
Menu
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Exit
Enter your choice
1
Inorder traversal:2 5 6 7 8 9 14 15 24
Menu
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Exit
Enter your choice
2
Preorder traversal:6 5 2 9 8 7 15 14 24
Menu
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Exit
Enter your choice
3
Postorder traversal: 2 5 7 8 14 24 15 9 6
Menu
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Exit
Enter your choice
4
Menu
1.Create
2.Search
3.Traversal
4.Exit
Enter your choice
4
sahyadri@sahyadri-Veriton-M275:~/ds$
#include<stdio.h>
#include<stdlib.h>
int st[10],top=-1,v[10],a[10][10],u[10];
int n,q[10],front=0;
int rear=-1;
void dfs(int s)
{
int i;
v[s]=1;
st[++top]=s;
for(i=1;i<=n;i++)
{
if(a[s][i]==1&&v[i]==0)
{
printf("%d->%d\n",s,i);
dfs(i);
}
}
}
void bfs(int s)
{
int m,i;
u[s]=1;
q[++rear]=s;
printf("Nodes reachable from %d are\n",s);
while(front<=rear)
{
m=q[front++];
for(i=1;i<=n;i++)
{
if(a[m][i]==1&&u[i]==0)
{
q[++rear]=i;
printf("%d\n",i);
u[i]=1;
}
}
}
}
void main()
{
int s,i,j,ch;
while(1)
{
printf("1.create\n2.DFS\n3.BFS\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number of vertices\n");
scanf("%d",&n);
printf("Enter the adjacency matrix representation\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
break;
case 2:printf("print reachable nodes using BFS method\n");
printf("enter source\n");
scanf("%d",&s);
printf("Nodes reachable from %d\n",s);
dfs(s);
for(i=1;i<=n;i++)
{
if(v[i]==0)
{
OUTPUT:
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. Design and 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<malloc.h>
int *ht,c,n,m,flag;
void create()
{
int i;
ht=(int*)malloc(m*sizeof(int));
if(ht==NULL||m==0)
{
printf("Hash table is not present\n");
}
for(i=0;i<m;i++)
ht[i]=-1;
}
void display()
{
int i;
printf("The hash table is\n");
for(i=0;i<m;i++)
{
printf("%d %d\n",i,ht[i]);
}
}
void insert(int key)
{
int j;
j=key%m;
while(ht[j]!=-1)
{
j=(j+1)%m;
flag=1;
}
if(flag)
{
printf("Collision is detected and it is solved using linear probing\n");
flag=0;
}
ht[j]=key;
display();
c++;
}
void main()
{
int i,key;
printf("Enter the number of employees\n");
scanf("%d",&n);
printf("Enter the memory size\n");
scanf("%d",&m);
create();
for(i=0;i<n;i++)
{
if(c!=m)
{
printf("Enter the key\n");
scanf("%d",&key);
insert(key);
}
else
printf("The hash table is full\n");
}
}
OUTPUT: