0% found this document useful (0 votes)
55 views61 pages

Ds Lab Manual

The document describes programs to perform operations on arrays and strings in C. It includes: 1. A menu-driven program to perform create, display, insert, and delete operations on an integer array using functions. 2. A program to perform pattern matching on strings by finding and replacing all occurrences of a pattern string with a replacement string in a main string. 3. A menu-driven program to perform push, pop, check for palindrome, and display operations on a stack implemented as an integer array, and handle overflow and underflow conditions.

Uploaded by

Pushpesh
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)
55 views61 pages

Ds Lab Manual

The document describes programs to perform operations on arrays and strings in C. It includes: 1. A menu-driven program to perform create, display, insert, and delete operations on an integer array using functions. 2. A program to perform pattern matching on strings by finding and replacing all occurrences of a pattern string with a replacement string in a main string. 3. A menu-driven program to perform push, pop, check for palindrome, and display operations on a stack implemented as an integer array, and handle overflow and underflow conditions.

Uploaded by

Pushpesh
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/ 61

DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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
{

SCEM, Dept of ISE, MANGALURU Page 1


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

printf("Enter the position of element to be inserted\n");


scanf("%d",&pos);
printf("Enter the element to be inserted\n");
scanf("%d",&ELEM);
if(pos>=0&&pos<=n)
{
for(j=n-1;j>=pos;j--)
{
a[j+1]=a[j];
}
n=n+1;
a[pos]=ELEM;
display();
}
else
{
printf("%d is a invalid position\n",pos);
}
}
}
void deletion()
{
if(n==0)
{
printf("Empty array\n");
}
else
{
printf("Enter the position of element to be deleted\n");
scanf("%d",&pos);
if(pos>=0&&pos<=n-1)
{
for(j=pos;j<n-1;j++)
{
a[j]=a[j+1];
}
ELEM=a[pos];
n=n-1;
display();
}
else
{
printf("%d is a invalid position\n",pos);
}
}
}

SCEM, Dept of ISE, MANGALURU Page 2


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab1.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab1.c -o lab1.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab1.out

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

SCEM, Dept of ISE, MANGALURU Page 3


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 4


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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++;

SCEM, Dept of ISE, MANGALURU Page 5


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

c=m;
i=0;
}
}
}
else
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
}

OUTPUT:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab2.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab2.c -o lab2.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab2.out
Enter the main string
good morning
Enter the pattern string
o
Enter the replacing string
*
The resultant string is
g**d m*rning
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab2.out
Enter the main string
sahyadri college
Enter the pattern string
management
Enter the replacing string
mangaluru
The pattern is not found
sahyadri@sahyadri-Veriton-M275:~/ds$

SCEM, Dept of ISE, MANGALURU Page 6


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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()

SCEM, Dept of ISE, MANGALURU Page 7


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

{
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");
}
}

SCEM, Dept of ISE, MANGALURU Page 8


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

}
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:

SCEM, Dept of ISE, MANGALURU Page 9


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab3.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab3.c -o lab3.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab3.out
-----MENU-----
-----MENU-----
1.Push
1.Push
2.Pop
2.Pop
3.Display
3.Display
4.Palindrome
4.Palindrome
5.Exit
5.Exit
Enter your option
Enter your option
1
1
Enter the element to be pushed
Enter the element to be pushed
25
10
-----MENU-----
1.Push
-----MENU-----
2.Pop
1.Push
3.Display
2.Pop
4.Palindrome
3.Display
5.Exit
4.Palindrome
Enter your option
5.Exit
3
Enter your option
The stack elements are
1
25
Enter the element to be pushed
10
15
15
-----MENU-----
15
1.Push
10
2.Pop
-----MENU-----
3.Display
1.Push
4.Palindrome
2.Pop
5.Exit
3.Display
Enter your option
4.Palindrome
1
5.Exit
Enter the element to be pushed
Enter your option
15
4
-----MENU-----
Not a palindrome
1.Push
-----MENU-----
2.Pop
1.Push
3.Display
2.Pop
4.Palindrome
3.Display
5.Exit
4.Palindrome
Enter your option
5.Exit
1
Enter your option
Enter the element to be pushed
2
10
The poped element is 25
-----MENU-----
15
10
SCEM, Dept of ISE, MANGALURU Page 10
-----MENU-----
1.Push
2.Pop
3.Display
DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 11


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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$

4. Design, Develop and Implement a Program in C for converting an Infix Expression to

SCEM, Dept of ISE, MANGALURU Page 12


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;
}

SCEM, Dept of ISE, MANGALURU Page 13


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

}
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 '+':

SCEM, Dept of ISE, MANGALURU Page 14


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab4.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab4.c -o lab4.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab.out
Enter the infix expression :
(a+b)
The postfix expression is:
ab+

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$

SCEM, Dept of ISE, MANGALURU Page 15


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;

SCEM, Dept of ISE, MANGALURU Page 16


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

}
}
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);
}

SCEM, Dept of ISE, MANGALURU Page 17


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

OUTPUT:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab5.c


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab5.c -o lab5.out -lm
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab5.out
Enter the postfix expression
123+*321-+*
The resultant ans is 20
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab5.out
Enter the postfix expression
21+3-
The resultant ans is 0
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab5.out
Enter the postfix expression
42/
The resultant ans is 2
sahyadri@sahyadri-Veriton-M275:~/ds$

5b. Solving Tower of Hanoi problem with n disks

#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);
}

SCEM, Dept of ISE, MANGALURU Page 18


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

OUTPUT:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab5b.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ cc lab5b.c -o lab5b.out -lm
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab5b.out
Enter the number of disks
3
1 move s to d
2 move s to t
1 move d to t
3 move s to d
1 move t to s
2 move t to d
1 move s to d

Total Number of moves are:7


sahyadri@sahyadri-Veriton-M275:~/ds$

SCEM, Dept of ISE, MANGALURU Page 19


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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);
}

SCEM, Dept of ISE, MANGALURU Page 20


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

}
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);

SCEM, Dept of ISE, MANGALURU Page 21


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

cqinsert(m);
break;
case 2:cqdelete();
break;
case 3:cqdisplay();
break;
case 4:exit(0);
default:printf("Invalid choice\n");
}
}
}

OUTPUT:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab6.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab6.c -o lab6.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab6.out

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

SCEM, Dept of ISE, MANGALURU Page 22


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 23


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 24


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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$

SCEM, Dept of ISE, MANGALURU Page 25


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:");

SCEM, Dept of ISE, MANGALURU Page 26


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;
}

SCEM, Dept of ISE, MANGALURU Page 27


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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()

SCEM, Dept of ISE, MANGALURU Page 28


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

{
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;

SCEM, Dept of ISE, MANGALURU Page 29


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab7.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab7.c -o lab7.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab7.out

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

SCEM, Dept of ISE, MANGALURU Page 30


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

Name USN Branch PhoneNO SEM

SCEM, Dept of ISE, MANGALURU Page 31


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

chitra 4sf15is009 me 9923456789 8


bindu 4sf15is002 cs 9449688290 6
ashu 4sf15is001 is 9945672381 3
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
4
Enter student details
Name:dhanu
USN:4sf15is008
Branch:is
Phone NO:9876542387
SEM:4
*...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
chitra 4sf15is009 me 9923456789 8
bindu 4sf15is002 cs 9449688290 6
ashu 4sf15is001 is 9945672381 3
dhanu 4sf15is008 is 9876542387 4
The number of nodes in list is 4
*...Student Data...*
1.Create
2.Display
3.Insertfront
4.Insertrear
5.Deletefront
6.Deleterear
7.Exit
Enter your choice
5

SCEM, Dept of ISE, MANGALURU Page 32


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 33


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 34


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;
}

int countnodes(node *head)


{

SCEM, Dept of ISE, MANGALURU Page 35


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;
}

SCEM, Dept of ISE, MANGALURU Page 36


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;

SCEM, Dept of ISE, MANGALURU Page 37


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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();

SCEM, Dept of ISE, MANGALURU Page 38


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab8.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab8.c -o lab8.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab8.out
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
1
Enter number of employees
2
Enter employee details
Name:Ajay
SSN:101
Department:cs
Designation:manager
Phone NO:9876545678
Salary:50000
Enter employee details
Name:Bhanu
SSN:102
Department:cv
Designation:instructor

SCEM, Dept of ISE, MANGALURU Page 39


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 40


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

Ajay 101 cs manager 9876545678 50000.000000


Bhanu 102 cv instructor 9876542345 25000.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
4
Enter employee details
Name:Dhanvi
SSN:104
Department:is
Designation:manager
Phone NO:8967567890
Salary:30000
*....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
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 4
*....Employee Data......*
1.Create
2.Display
3.InsertFront
4.InsertRear
5.DeleteFront
6.DeleteRear
7.Exit
Enter your choice
5

SCEM, Dept of ISE, MANGALURU Page 41


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 42


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

SCEM, Dept of ISE, MANGALURU Page 43


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

#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)
{

SCEM, Dept of ISE, MANGALURU Page 44


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;

SCEM, Dept of ISE, MANGALURU Page 45


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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()
{

SCEM, Dept of ISE, MANGALURU Page 46


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:

SCEM, Dept of ISE, MANGALURU Page 47


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab9.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab9.c -o lab9.out -lm
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab9.out

1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 1

Enter polynomial to evaluate:


Enter coeff: 6
Enter powers of x,y,z
221
If your wish to continue press 1 otherwise 0
1
Enter coeff: -4
Enter powers of x,y,z
015
If your wish to continue press 1 otherwise 0
1
Enter coeff: 3
Enter powers of x,y,z
311
If your wish to continue press 1 otherwise 0
1
Enter coeff: 2
Enter powers of x,y,z
151
If your wish to continue press 1 otherwise 0
1
Enter coeff: -2
Enter powers of x,y,z
111
If your wish to continue press 1 otherwise 0
0
The polynomial is :6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 + -2x^1y^1z^1
Enter x,y,z terms to evaluate:
111
Polynomial result is: 5

1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 2

Enter the first polynomial:

SCEM, Dept of ISE, MANGALURU Page 48


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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

Enter the second polynomial:


Enter coeff: 6
Enter powers of x,y,z
222
If your wish to continue press 1 otherwise 0
0

First polynomial is: 4x^2y^2z^2 + 3x^1y^1z^2


Second polynomial is: 6x^2y^2z^2
The sum of 2 polynomials is:
10x^2y^2z^2 + 3x^1y^1z^2

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

SCEM, Dept of ISE, MANGALURU Page 49


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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
e. Exit

#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);

SCEM, Dept of ISE, MANGALURU Page 50


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

}
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)
{

SCEM, Dept of ISE, MANGALURU Page 51


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

case 1:printf("Inorder traversal:");


inorder(root);
break;
case 2:printf("Preorder traversal:");
preorder(root);
break;
case 3:printf("Postorder traversal:");
postorder(root);
break;
default:break;
}
}while(ch>=1&&ch<=3);
}
void main()
{
node *root;
int i,n,ele,key,ch;
root=NULL;
while(1)
{
printf("\n\tMenu\n1.Create\n2.Search\n3.Traversal\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements to be inserted\n");
for(i=0;i<n;i++)
{
scanf("%d",&ele);
root=create(root,ele);
}
break;
case 2:printf("Enter the element to be searched\n");
scanf("%d",&key);
search(root,key);
break;
case 3:traversal(root);
break;
case 4:exit(0);
default:printf("Invalid choice\n");
}
}}
OUTPUT:

SCEM, Dept of ISE, MANGALURU Page 52


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab10.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab10.c -o lab10.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab10.out

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

SCEM, Dept of ISE, MANGALURU Page 53


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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$

11. Design, Develop and Implement a Program in C for the following


operations on Graph(G) of Cities

SCEM, Dept of ISE, MANGALURU Page 54


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

a. Create a Graph of N cities using Adjacency Matrix.


b. Print all the nodes reachable from a given starting node in a digraph using
DFS/BFS method

#include<stdio.h>
#include<stdlib.h>
int 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()
{

SCEM, Dept of ISE, MANGALURU Page 55


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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)
{

printf("%d is not visited and it is disconnected graph\n",i);


}
}
break;
case 3: printf("print reachable nodes using DFS method\n");
printf("enter source\n");
scanf("%d",&s);
bfs(s);
for(i=1;i<=n;i++)
{
if(u[i]==0)
{
printf("%d is not visited and it is disconnected graph\n",i);
}
}
break;
case 4:exit(0);

SCEM, Dept of ISE, MANGALURU Page 56


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

default: printf("Invalid choice\n");


}
}
}

OUTPUT:

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab11.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab11.c -o lab11.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab11.out
1.create
2.DFS
3.BFS
4.Exit 1
Enter your choice Nodes reachable from 1 are
1 3
Enter the number of vertices 4
4 2
Enter the adjacency matrix representation 1.create
0011 2.DFS
0000 3.BFS
0100 4.Exit
0100 Enter your choice
1.create 4
2.DFS sahyadri@sahyadri-Veriton-M275:~/ds$
3.BFS
4.Exit
Enter your choice
2
print reachable nodes using BFS method
enter source
1
Nodes reachable from 1
1->3
3->2
1->4
1.create
2.DFS
3.BFS
4.Exit
Enter your choice
3
print reachable nodes using DFS method
enter source
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

SCEM, Dept of ISE, MANGALURU Page 57


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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;

SCEM, Dept of ISE, MANGALURU Page 58


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

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:

SCEM, Dept of ISE, MANGALURU Page 59


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

sahyadri@sahyadri-Veriton-M275:~/ds$ gedit lab12.c &


sahyadri@sahyadri-Veriton-M275:~/ds$ gcc lab12.c -o lab12.out
sahyadri@sahyadri-Veriton-M275:~/ds$ ./lab12.out 8 -1
Enter the number of employees 9 6789
11 Enter the key
Enter the memory size 4567
10 The hash table is
Enter the key 0 -1
2345 1 -1
The hash table is 2 -1
0 -1 3 -1
1 -1 4 -1
2 -1 5 2345
3 -1 6 3456
4 -1 7 4567
5 2345 8 -1
6 -1 9 6789
7 -1 Enter the key
8 -1 5666
9 -1 Collision is detected and it is
Enter the key solved using linear probing
6789 The hash table is
The hash table is 0 -1
0 -1 1 -1
1 -1 2 -1
2 -1 3 -1
3 -1 4 -1
4 -1 5 2345
5 2345 6 3456
6 -1 7 4567
7 -1 8 5666
8 -1 9 6789
9 6789 Enter the key
Enter the key 4323
3456 The hash table is
The hash table is 0 -1
0 -1 1 -1
1 -1 2 -1
2 -1 3 4323
3 -1 4 -1
4 -1 5 2345
5 2345 6 3456
6 3456 7 4567
7 -1 8 5666
Enter the key 9 6789
4333

SCEM, Dept of ISE, MANGALURU Page 60


DATA STRUCTURES LABORATORY - 18CSL38 III SEMESTER / BE

Collision is detected and it is solved using linear probing


The hash table is
0 -1
1 -1 Enter the key
2 -1 1200
3 4323 The hash table is
4 4333 0 1200
5 2345 1 1221
6 3456 2 1222
7 4567 3 4323
8 5666 4 4333
9 6789 5 2345
Enter the key 6 3456
1222 7 4567
The hash table is 8 5666
0 -1 9 6789
1 -1 The hash table is full
2 1222 sahyadri@sahyadri-Veriton-M275:~/ds$
3 4323
4 4333
5 2345
6 3456
7 4567
8 5666
9 6789
Enter the key
1221
The hash table is
0 -1
1 1221
2 1222
3 4323
4 4333
5 2345
6 3456
7 4567
8 5666
9 6789

SCEM, Dept of ISE, MANGALURU Page 61

You might also like