The document describes a C program that implements various operations on arrays, strings, and stacks. It includes functions to create, display, insert, and delete elements from an array. For strings, it includes functions to read main, pattern, and replace strings, find occurrences of a pattern, and replace the pattern with the given string. For stacks, it includes functions to push and pop elements, check if a number is a palindrome, and display stack elements. The program uses menus to select these different operations and test cases are provided to demonstrate the output.
Immutable Image-Based Operating Systems - EW2024.pdf
VTU DSA Lab Manual
1. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 1
PROGRAM - 1 (ARRAY OPERATIONS)
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
PROGRAM CODE
/* C program to implement create, display, insert and delete operations on an Array.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
int a[30], n; /*Global variables are initialized to 0 automatically.*/
/* Function definition to create an array of n elements. */
void create()
{
int i;
printf("nEnter the number of elements: ");
scanf("%d", &n);
printf("nEnter the elements:n");
for( i = 0; i < n ; i++ )
{
scanf("%d", &a[i]);
}
}
/* Function definition to display array elements. */
void display()
{
int i;
if(n==0)
{
printf("nNo elements to display!!!");
}
else
2. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 2
{
printf("nThe elements are:n");
for( i = 0; i < n ; i++ )
{
printf("%dt", a[i]);
}
}
}
/* Function definition to insert an element at a given valid position. */
void insert()
{
int ele, pos, i;
printf("nEnter the element to be inserted: ");
scanf("%d", &ele);
printf("nEnter the position at which you want to insert: ");
scanf("%d", &pos);
if( pos<1 || pos>n )
{
printf("Invalid Position!!!");
}
else
{
for( i = n-1 ; i >= pos-1 ; i-- )
{
a[i+1] = a[i]; /* Copy the element in ith location to i+1th location. */
}
a[pos-1] = ele; /* Insert the element. */
n++; /* Increment the count of elements in the array. */
}
}
/* Function definition to delete an element at a given valid position. */
void delete()
{
int pos, i;
printf("nEnter the position from which you want to delete: ");
scanf("%d", &pos);
if( pos<1 || pos>n )
{
printf("Invalid Position!!!");
}
3. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 3
else
{
printf("nThe deleted element is: %d", a[pos-1]);
for( i = pos-1 ; i < n-1 ; i++ )
{
a[i] = a[i+1]; /* Copy the element in i+1th location to ith location. */
}
n--; /* Decrement the count of elements in the array. */
}
}
int main()
{
int ch;
while(1) /*Infinite Loop.*/
{
printf("nARRAY OPERATIONS");
printf("n--------------------------------------");
printf("n1: CREATE");
printf("n2: DISPLAY");
printf("n3: INSERT AN ELEMENT AT GIVEN POS");
printf("n4: DELETE AN ELEMENT FROM GIVEN POS");
printf("n5: EXIT");
printf("n--------------------------------------");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: delete();
break;
case 5: return ( 0 );
}
4. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 4
}
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc array.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 1
Enter the number of elements: 5
Enter the elements:
28
11
14
1
9
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 2
The elements are:
28 11 14 1 9
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
5. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 5
--------------------------------------
Enter your choice: 3
Enter the element to be inserted: 5
Enter the position at which you want to insert: 3
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 2
The elements are:
28 11 5 14 1 9
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 3
Enter the element to be inserted: 12
Enter the position at which you want to insert:
9
Invalid Position!!!
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 4
Enter the position from which you want to delete: 2
The deleted element is: 11
ARRAY OPERATIONS
6. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 6
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 2
The elements are:
28 5 14 1 9
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 4
Enter the position from which you want to delete: 8
Invalid Position!!!
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 2
The elements are:
28 5 14 1 9
ARRAY OPERATIONS
--------------------------------------
1: CREATE
2: DISPLAY
3: INSERT AN ELEMENT AT GIVEN POS
4: DELETE AN ELEMENT FROM GIVEN POS
5: EXIT
--------------------------------------
Enter your choice: 5
7. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 7
PROGRAM - 2 (PATTERN MATCHING)
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.
PROGRAM CODE
/* C program to replace all the occurrences of a pattern with another string.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
char str [ 100 ], pat [ 100 ], rep [ 100 ];
int pat_len; /*Global variables are initialized to 0 automatically.*/
/*Function definition to read main, pattern and replace strings.*/
void read_strings()
{
int i;
printf ( "nEnter the main string: " );
gets(str);
printf ( "nEnter the pattern string: " );
gets(pat);
printf ( "nEnter the replace string: " );
gets(rep);
/*Find the length of pattern string.*/
for ( i = 0 ; pat [ i ] != '0' ; i++ )
pat_len++;
}
/*Function to find the occurrences of pattern and replace with a string.*/
void pattern_match()
{
int i; /*Index to access main string.*/
int j; /*Index to access pattern string.*/
int k; /*Index to access replace string.*/
int z = 0; /*Index to access final string.*/
8. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 8
int count = 0; /*Keeps track of number of occurences of pattern.*/
char final_str [ 100 ];
for ( i = 0 ; str [ i ] != '0' ; i++ )
{
j = 0; /*Initilaize index of pattern to 0.*/
while ( ( str [ i + j ] == pat [ j ] ) && ( j < pat_len ) ) /*Check if pattern exists.*/
j++;
if ( pat [ j ] == '0' ) /*Check if it is matching.*/
{
count++; /*Counts the number of occurrences of pattern in main string.*/
for ( k = 0 ; rep [ k ] != '0' ; k++, z++ )
{
final_str [ z ] = rep [ k ];
}
i = i + pat_len - 1; /*i now points where the pattern is ending in the main string.*/
}
else /*Pattern not matching.*/
{
final_str [ z ] = str [ i ]; /*Copy the ith character to the final string.*/
z++;
}
}
if ( count == 0 )
printf ( "nPattern doesnt exist in main string!!!" );
else
{
final_str [ z ] = '0';
printf ( "nThe number of occurences of the pattern: %d", count );
printf ( "nThe main string after replacement is: %sn", final_str );
}
}
int main()
{
read_strings();
pattern_match();
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc pattern_matching.c
pattern_matching.c: In function ‘read_strings’:
9. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 9
pattern_matching.c:22:9: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-
Wdeprecated-declarations]
gets(str);
^
pattern_matching.c:25:9: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-
Wdeprecated-declarations]
gets(pat);
^
pattern_matching.c:28:9: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-
Wdeprecated-declarations]
gets(rep);
^
/tmp/cc1hSg6e.o: In function `read_strings':
pattern_matching.c:(.text+0x1d): warning: the `gets' function is dangerous and should not be used.
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the main string: A bird in the hand is worth two in the bush
Enter the pattern string: in
Enter the replace string: $$$$
The number of occurences of the pattern: 2
The main string after replacement is: A bird $$$$ the hand is worth two $$$$ the bush
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the main string: Hi. Welcome to Data Structures Lab!!
Enter the pattern string: az
Enter the replace string: junk
Pattern doesnt exist in main string!!!
10. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 10
PROGRAM - 3 (STACK OPERATIONS)
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 the Stack
b. Pop an Element from the 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
PROGRAM CODE
/* C program to implement push, pop, check palindrome and display operations on Stack.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#define MAX 5
/*Initially top of the stack will be -1.*/
int top = -1; /*Points to top of the stack.*/
int s [ MAX - 1 ];
/*Function definition to push elements onto stack.*/
void push ( int elem )
{
if ( top == MAX - 1 )
{
printf ( "nStack Overflow!!!" );
}
else
{
top = top + 1; /*Increment top of the stack.*/
s [ top ] = elem; /*Insert element on top of the stack.*/
}
}
/*Function definition to delete elements from stack.*/
int pop ( )
{
int del;
11. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 11
if ( top == -1 )
{
printf ( "nStack Underflow!!!" );
return ( -1 );
}
else
{
del = s [ top ]; /*Delete the topmost element from the stack.*/
top = top - 1; /*Decrement top of the stack.*/
return ( del );
}
}
/*Function definition to check whether a given number is palindrome or not.*/
int palindrome ( )
{
int n, i, p [ 10 ], flag = 0;
printf ( "nEnter the number of digits: " );
scanf ( "%d", &n );
printf ( "nEnter the digits: " );
for ( i = 0; i < n ; i++ )
scanf ( "%d", &p [ i ] );
/*Push all the digits onto the stack.*/
for ( i = 0 ; i < n ; i++ )
push ( p [ i ] );
for ( i = 0 ; i < n ; i++ )
{
/*check if each digit in the array p and the popped element from the stack are equal.*/
if ( p [ i ] != pop ( ) )
{
flag = 1; /*Not equal break.*/
break;
}
}
if ( flag == 0 )
printf ( "nPALINDROME!!!" );
else
printf ( "nNOT PALINDROME!!!" );
}
/*Function definition for displaying stack elements.*/
void display ( )
{
12. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 12
int i;
if ( top == -1 )
{
printf ( "nStack is Empty!!!" );
}
else
{
printf ( "nThe elements of stack are: " );
for ( i = top ; i >= 0 ; i-- )
printf ( "t%d", s [ i ] );
}
}
int main()
{
int ch, elem, del_elem, flag;
/*Infinite loop.*/
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nSTACK OPERATIONS" );
printf ( "n1: Push n2: Pop n3: Check Palindrome n4: Display n5: Exit" );
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: printf ( "nEnter the element to push onto the stack: " );
scanf ( "%d", &elem );
push ( elem );
break;
case 2: del_elem = pop ( );
if ( del_elem == -1 )
printf ("nNo element to delete!!!n");
else
printf ( "nThe deleted element is: %d", del_elem );
break;
case 3: palindrome ( );
break;
case 4: display ( );
break;
13. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 13
case 5: return ( 0 );
default: printf ( "nInvalid Choice!!!n" );
}
}
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc stack.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to push onto the stack: 9
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to push onto the stack: 1
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 1
14. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 14
Enter the element to push onto the stack: 11
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to push onto the stack: 14
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to push onto the stack: 28
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 4
The elements of stack are: 28 14 11 1 9
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 2
15. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 15
The deleted element is: 28
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: 14
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: 11
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: 1
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: 9
---------------------------------------------
STACK OPERATIONS
1: Push
16. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 16
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 2
Stack Underflow!!!
No element to delete!!!
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 4
Stack is Empty!!!
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 3
Enter the number of digits: 5
Enter the digits: 1
1
2
1
1
PALINDROME!!!
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
17. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 17
Enter your choice: 3
Enter the number of digits: 5
Enter the digits: 5
1
2
4
3
NOT PALINDROME!!!
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 5
18. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 18
PROGRAM - 4 (INFIX TO POSTFIX)
Design, Develop and Implement 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.
PROGRAM CODE
/* C program to convert an Infix Expression to Postfix Expression.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<ctype.h>
#define MAX 20
/*Initially top of the stack will be -1.*/
int top = -1; /*Points to top of the stack.*/
char stack [ MAX ];
/*Function definition to push elements onto stack.*/
void push ( char ele )
{
if ( top == MAX - 1 )
{
printf ( "nStack Overflow!!!" );
}
else
{
top = top + 1; /*Increment top of the stack.*/
stack [ top ] = ele; /*Insert element on top of the stack.*/
}
}
/*Function definition to delete elements from stack.*/
char pop()
{
char del;
if ( top == -1 )
{
printf ( "nStack Underflow!!!" );
}
else
{
del = stack [ top ]; /*Delete the topmost element from the stack.*/
19. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 19
top = top - 1; /*Decrement top of the stack.*/
return ( del );
}
}
/*Function definition that defines priority of operators.*/
int priority ( char c )
{
if ( c == '(' )
return ( 1 );
else if ( c == '+' || c == '-' )
return ( 2 );
else if ( c == '*' || c == '/' || c == '%' )
return ( 3 );
else if ( c == '^' )
return ( 4 );
else
return ( 0 );
}
int main()
{
char infix [ MAX ], postfix [ MAX ];
int i; /*Index to access infix expression.*/
int j = 0; /*Index to access postfix expression.*/
printf ( "nEnter the Infix Expression: " );
scanf ( "%s", infix );
for ( i = 0 ; infix [ i ] != '0' ; i++ )
{
if ( isalnum ( infix [ i ] ) ) /*Check if the character in the infix expression is alphanumeric.*/
postfix [ j++ ] = infix [ i ]; /*Place the character in postfix expression.*/
else if ( infix [ i ] == '(' ) /*Check if the character in the infix expression is '('.*/
push ( infix [ i ] ); /*Push the character on to the stack.*/
else if ( infix [ i ] == ')' ) /*Check if the character in the infix expression is ')'.*/
{
while ( stack [ top ] != '(' ) /*Until top o stack is not '('.*/
{
postfix [ j++ ] = pop(); /*Pop the characters from the stack and place it in the postfix
expression.*/
}
top--; /*Removes '('.*/
}
else
{
20. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 20
while ( priority ( stack [ top ] ) >= priority ( infix [ i ] ) ) /*Check the priority of character on
the stack and input character. If its higher then pop the character
from the stack and place it in the postfix expression.*/
{
postfix [ j++ ] = pop();
}
push ( infix [ i ] ); /*Push the infix character onto the stack. */
}
}
while ( top != -1 ) /*Pop the remaining characters from the stack and place it in the postfix
expression until '#' is encountered.*/
{
postfix [ j++ ] = pop();
}
postfix [ j ] = '0'; /*Append NULL character at the end of postix expression.*/
printf ( "nThe Postfix Expression is %s", postfix );
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc infix_to_postfix.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: a+b
The Postfix Expression is ab+
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: (a+b)
The Postfix Expression is ab+
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: ((A+(B-C)*D)^E+F)
The Postfix Expression is ABC-D*+E^F+
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: a+b-c*d^e+f
21. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 21
The Postfix Expression is ab+cde^*-f+
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: x^y^z-m+n+p/q
The Postfix Expression is xy^z^m-n+pq/+
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: (1+2(3*4)*5)%(6^7)
The Postfix Expression is 1234*5*+67^%
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Infix Expression: a/b^c-d
The Postfix Expression is abc^/d-
22. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 22
PROGRAM – 5a (SUFFIX EXPRESSION)
Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
PROGRAM CODE
/* C program to evaluate a Suffix Expression.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<math.h>
#define MAX 100
/*Initially top of the stack will be -1.*/
int top = -1; /*Points to top of the stack.*/
double s [ MAX ];
/*Function definition to push elements onto stack.*/
void push ( double elem )
{
if ( top == MAX - 1 )
{
printf ( "nStack Overflow!!!" );
}
else
{
top = top + 1; /*Increment top of the stack.*/
s [ top ] = elem; /*Insert element on top of the stack.*/
}
}
/*Function definition to delete elements from stack.*/
double pop ( )
{
double del;
if ( top == -1 )
{
printf ( "nStack Underflow!!!" );
}
else
{
del = s [ top ]; /*Delete the topmost element from the stack.*/
23. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 23
top = top - 1; /*Decrement top of the stack.*/
return ( del );
}
}
/*Function definition to evaluate.*/
double evaluate ( char op, double op1, double op2 )
{
switch ( op )
{
case '+': return ( op1 + op2 );
case '-': return ( op1 - op2 );
case '*': return ( op1 * op2 );
case '/': return ( op1 / op2 );
case '%': return ( fmod ( op1 , op2 ) );
case '^': return ( pow ( op1, op2 ) );
default: printf ( "nInvalid Operator!!!" );
}
}
int main()
{
char suffix [ 50 ];
int i; /*Index to access Suffix Expression.*/
double val, op1, op2, res;
printf ( "nEnter the Suffix Expression: " );
scanf ( "%s", suffix );
for ( i = 0 ; suffix [ i ] != '0' ; i++ )
{
if ( isdigit ( suffix [ i ] ) ) /*Check if the current character is digit.*/
push ( suffix [ i ] - '0' ); /*Push it onto stack. '0'- ASCII value of 0 is 48.*/
else if ( isalpha ( suffix [ i ] ) ) /*Check if the current character is alphabet.*/
{
printf ( "nEnter the value of %c: ", suffix [ i ] );
scanf ( "%lf", &val );
push ( val );
}
else
{
op2 = pop(); /*Get the second operand from the stack.*/
24. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 24
op1 = pop(); /*Get the first operand from the stack.*/
res = evaluate ( suffix [ i ], op1, op2 );
push ( res ); /*Push the partial result onto the stack.*/
}
}
res = pop(); /*Pop the final result.*/
printf ( "nThe result is %lf", res );
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc suffix_eval.c -lm
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Suffix Expression: abc-d*+
Enter the value of a: 5
Enter the value of b: 1
Enter the value of c: 9
Enter the value of d: 3
The result is -19.000000
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Suffix Expression: 1234*5*+67^%
The result is 62.000000
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Suffix Expression: xy^z^m-n+pq/+
Enter the value of x: -1
Enter the value of y: 2
Enter the value of z: -3
25. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 25
Enter the value of m: 4
Enter the value of n: -5
Enter the value of p: 6
Enter the value of q: -7
The result is -8.857143
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the Suffix Expression: 632-5*+1^7+
The result is 18.000000
26. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 26
PROGRAM – 5b (TOWER OF HANOI)
a. Solving Tower of Hanoi problem with n disks
PROGRAM CODE
/* C program to solve Tower of Hanoi problem.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
/*Function definition of tower of Hanoi to move disks.*/
void tower_hanoi ( int n, char src, char dest, char temp )
{
if ( n == 1 )
{
printf ( "nMove disk %d from peg %c to peg %c", n, src, dest );
return;
}
/*Move n - 1 disk from source to temp.*/
tower_hanoi ( n - 1, src, temp, dest );
/*Move nth disk from source to destination.*/
printf ( "nMove disk %d from peg %c to peg %c", n, src, dest );
/*Move n - 1 disk from temp to destination.*/
tower_hanoi ( n - 1, temp, dest, src );
}
int main()
{
int n;
printf ( "nEnter the number of disks: " );
scanf ( "%d", &n );
tower_hanoi ( n, 'A', 'B', 'C' );
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc tower_of_hanoi.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
27. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 27
Enter the number of disks: 1
Move disk 1 from peg A to peg Bakhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the number of disks: 2
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg Bakhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the number of disks: 3
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 3 from peg A to peg B
Move disk 1 from peg C to peg A
Move disk 2 from peg C to peg B
Move disk 1 from peg A to peg Bakhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the number of disks: 4
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
Move disk 4 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 2 from peg C to peg A
Move disk 1 from peg B to peg A
Move disk 3 from peg C to peg B
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
28. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 28
PROGRAM - 6 (CIRCULAR QUEUE OPERATIONS)
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
PROGRAM CODE
/* C program to implement insert, delete and display operations on Circular Queue.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#define MAX 5
/*Global Declaration.*/
int f = -1, r = -1;
/*Function definition for inserting an element into circular queue.*/
void insert ( char cq [ MAX ] )
{
char elem;
printf ( "nEnter the element to insert into the queue: " );
scanf ( "n%c", &elem ); /*Use n in scanf() to remove n from the buffer.*/
if ( ( f == 0 && r == MAX - 1 ) || ( f == r + 1 ) )
{
printf ( "nQueue Overflow!!!" );
return;
}
if ( f == -1 ) /*Circular queue is empty.*/
f = 0;
r = ( r + 1 ) % MAX; /*Points to the index of rear where element is to be inserted.*/
cq [ r ] = elem; /*Insert element at rear end.*/
}
29. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 29
/*Function definition for deleting an element from circular queue.*/
void delete ( char cq [ MAX ] )
{
if ( f == -1 )
{
printf ( "nQueue Underflow!!!" );
return ;
}
printf ( "nThe deleted element is: %c", cq [ f ] );
if ( f == r ) /*Circular queue has one element.*/
{
f = -1;
r = -1;
}
else
f = ( f + 1 ) % MAX; /*Points to the index of front after the element is deleted from front.*/
}
/*Function definition for displaying elements in a circular queue.*/
void display ( char cq [ MAX ] )
{
int i;
if ( f == -1 )
{
printf ( "nQueue Underflow!!!" );
}
else
{
printf ( "nThe elements of queue are: " );
for ( i = f ; i != r ; i = ( ( i + 1 ) % MAX ) )
printf ( "t%c", cq [ i ] );
printf ( "t%c", cq [ i ] );
}
}
int main()
{
int ch;
char cq [ MAX ];
/*Infinite loop.*/
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nCIRCULAR QUEUE OPERATIONS" );
30. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 30
printf ( "n1: Insert n2: Delete n3: Display n4: Exit" );
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: insert ( cq );
break;
case 2: delete ( cq );
break;
case 3: display ( cq );
break;
case 4: return ( 0 );
default: printf ( "nInvalid Choice!!!n" );
}
}
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc circular_queue.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to insert into the queue: a
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
31. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 31
---------------------------------------------
Enter your choice: 1
Enter the element to insert into the queue: s
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to insert into the queue: c
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to insert into the queue: n
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to insert into the queue: p
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 1
Enter the element to insert into the queue: r
32. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 32
Queue Overflow!!!
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 3
The elements of queue are: a s c n p
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: a
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 3
The elements of queue are: s c n p
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: s
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
33. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 33
---------------------------------------------
Enter your choice: 3
The elements of queue are: c n p
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: c
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: n
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 2
The deleted element is: p
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 2
Queue Underflow!!!
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
34. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 34
3: Display
4: Exit
---------------------------------------------
Enter your choice: 3
Queue Underflow!!!
---------------------------------------------
CIRCULAR QUEUE OPERATIONS
1: Insert
2: Delete
3: Display
4: Exit
---------------------------------------------
Enter your choice: 4
35. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 35
PROGRAM - 7 (SINGLY LINKED LIST OPERATIONS)
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. Creating 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
PROGRAM CODE
/* C program to implement operations on Singly Linked List.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
char usn [ 20 ];
char name [ 20 ];
char branch [ 20 ];
int sem;
long int phno;
struct student *link;
};
typedef struct student* STUDENT;
STUDENT start = NULL; /*Initially start contains nothing.*/
/*Function definition to allocate memory.*/
STUDENT create()
{
STUDENT getnode;
getnode = ( STUDENT ) malloc ( sizeof ( struct student ) );
if ( getnode == NULL )
{
printf ( "nMemory could not be allocated!!!" );
return;
}
36. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 36
printf ( "nEnter the details of Student" );
printf ( "nEnter the usn: " );
scanf ( "%s", getnode->usn );
printf ( "nEnter the name: " );
scanf ( "%s", getnode->name );
printf ( "nEnter the branch: " );
scanf ( "%s", getnode->branch );
printf ( "nEnter the sem: " );
scanf ( "%d", &getnode->sem );
printf ( "nEnter the phno: " );
scanf ( "%ld", &getnode->phno );
getnode->link = NULL;
return ( getnode );
}
/*Function definition to insert element at front of the list.*/
void insert_front()
{
STUDENT node;
node = create();
if ( start == NULL ) /*If the list is empty.*/
{
start = node;
}
else
{
node->link = start; /*The link of node is assigned the address of the next node which present in
start.*/
start = node; /*Now start contains the address of created node.*/
}
}
/*Function definition to delete element at front of the list.*/
void delete_front()
{
STUDENT temp;
if ( start == NULL )
{
37. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 37
printf ( "nList is Empty" );
}
else
{
temp = start;
start = temp->link; /*Now start will contain the address of the next node.*/
printf ( "nThe deleted student usn is %s", temp->usn );
free ( temp );
}
}
/*Function definition to create a list using front insertion.*/
void create_list()
{
int n, i;
printf ( "nEnter the number of students: " );
scanf ( "%d", &n );
for ( i = 0 ; i < n ; i++ )
{
insert_front(); /*Call the insert front function.*/
}
}
/*Function definition to display the status of the list.*/
void status()
{
STUDENT temp;
int count = 0;
if ( start == NULL )
{
printf ( "nList is Empty" );
return;
}
temp = start;
printf ( "nThe Student details are: " );
while ( temp != NULL )
{
printf ( "n%sn%sn%sn%dn%ldn", temp->usn, temp->name, temp->branch, temp->sem,
temp->phno );
temp = temp->link;
count++;
}
38. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 38
printf ( "nThe number of nodes are: %d", count );
}
/*Function definition for inserting a node at end of the list.*/
void insert_end ( )
{
STUDENT node, temp;
node = create();
if ( start == NULL ) /*If the list is empty.*/
{
start = node;
}
else
{
temp = start;
while ( temp->link != NULL )/*Traverse till the end of the list.*/
{
temp = temp->link;
}
temp->link = node; /*Insert the node at the end.*/
}
}
/*Function definition to delete element at end of the list.*/
void delete_end()
{
STUDENT temp, prev;
temp = start;
if ( temp == NULL )
{
printf ( "nList is Empty" );
}
else if ( temp->link == NULL ) /*One node in the list.*/
{
printf ( "nThe deleted student usn is %s", temp->usn );
free ( temp );
start = NULL;
}
else
{
while ( temp->link != NULL )
39. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 39
{
prev = temp;
temp = temp->link;
}
printf ( "nThe deleted student usn is %s", temp->usn );
free ( temp );
prev->link = NULL;
}
}
/*Function to demonstrate stack operations.*/
void stack_demo()
{
int ch;
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nSTACK OPERATIONS" );
printf ( "n1: Insert End n2: Delete End n3: Status of Stack n4: Exit" );
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: insert_end();
break;
case 2: delete_end();
break;
case 3: status ();
break;
case 4: return;
default: printf ( "nInvalid Choice!!!" );
}
}
}
int main()
{
int ch;
/*Infinite loop.*/
40. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 40
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nSINGLY LINKED LIST OPERATIONS" );
printf ( "n1: Create List n2: Status of List n3: Insert End n4: Delete End n5: Insert Front n6:
Delete Front n7: Stack Demo n8: Exit" );
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: create_list ( );
break;
case 2: status ();
break;
case 3: insert_end();
break;
case 4: delete_end();
break;
case 5: insert_front();
break;
case 6: delete_front();
break;
case 7: stack_demo();
break;
case 8: return ( 0 );
default: printf ( "nInvalid Choice!!!" );
}
}
return ( 0 );
}
41. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 41
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc singly_linked_list.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 1
Enter the number of students: 2
Enter the details of Student
Enter the usn: 1TJ08IS002
Enter the name: Akhilaa
Enter the branch: ISE
Enter the sem: 2
Enter the phno: 9842048329
Enter the details of Student
Enter the usn: 1BI06EC023
Enter the name: Nikkhil
Enter the branch: ECE
Enter the sem: 3
Enter the phno: 8478559201
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
42. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 42
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Student details are:
1BI06EC023
Nikkhil
ECE
3
8478559201
1TJ08IS002
Akhilaa
ISE
2
9842048329
The number of nodes are: 2
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 3
Enter the details of Student
Enter the usn: 1VI08ME091
Enter the name: Shanky
Enter the branch: MECH
Enter the sem: 2
Enter the phno: 9484848093
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
43. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 43
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Student details are:
1BI06EC023
Nikkhil
ECE
3
8478559201
1TJ08IS002
Akhilaa
ISE
2
9842048329
1VI08ME091
Shanky
MECH
2
9484848093
The number of nodes are: 3
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 6
The deleted student usn is 1BI06EC023
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
44. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 44
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Student details are:
1TJ08IS002
Akhilaa
ISE
2
9842048329
1VI08ME091
Shanky
MECH
2
9484848093
The number of nodes are: 2
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 5
Enter the details of Student
Enter the usn: 1PE12CV102
Enter the name: Vihana
Enter the branch: CIVIL
Enter the sem: 5
Enter the phno: 7385024831
---------------------------------------------
45. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 45
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Student details are:
1PE12CV102
Vihana
CIVIL
5
7385024831
1TJ08IS002
Akhilaa
ISE
2
9842048329
1VI08ME091
Shanky
MECH
2
9484848093
The number of nodes are: 3
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 4
The deleted student usn is 1VI08ME091
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
46. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 46
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Student details are:
1PE12CV102
Vihana
CIVIL
5
7385024831
1TJ08IS002
Akhilaa
ISE
2
9842048329
The number of nodes are: 2
---------------------------------------------
SINGLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Stack Demo
8: Exit
---------------------------------------------
Enter your choice: 8
47. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 47
PROGRAM - 8 (DOUBLY LINKED LIST OPERATIONS)
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. Creating 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 / Deletion at End of DLL
d. Perform Insertion / Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
PROGRAM CODE
/* C program to implement operations on Doubly Linked List.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<stdlib.h>
struct employee
{
char ssn [ 20 ];
char name [ 20 ];
char dept [ 20 ];
char designation [ 20 ];
int salary;
long int phno;
struct employee *llink, *rlink;
};
typedef struct employee* EMPLOYEE;
EMPLOYEE start = NULL; /*Initially start contains nothing.*/
/*Function definition to allocate memory.*/
EMPLOYEE create()
{
EMPLOYEE getnode;
getnode = ( EMPLOYEE ) malloc ( sizeof ( struct employee ) );
if ( getnode == NULL )
{
printf ( "nMemory couldnt be allocated!!!" );
48. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 48
return;
}
printf ( "nEnter the details of Employee" );
printf ( "nEnter the ssn: " );
scanf ( "%s", getnode->ssn );
printf ( "nEnter the name: " );
scanf ( "%s", getnode->name );
printf ( "nEnter the department: " );
scanf ( "%s", getnode->dept );
printf ( "nEnter the designation: " );
scanf ( "%s", getnode->designation );
printf ( "nEnter the salary: " );
scanf ( "%d", &getnode->salary );
printf ( "nEnter the phno: " );
scanf ( "%ld", &getnode->phno );
getnode->llink = NULL;
getnode->rlink = NULL;
return ( getnode );
}
/*Function definition to insert element at end of the list.*/
void insert_end()
{
EMPLOYEE node, temp;
node = create();
if ( start == NULL ) /*If the list is empty.*/
{
start = node;
}
else
{
temp = start;
while ( temp->rlink != NULL ) /*Traverse till the end of the list.*/
{
temp = temp->rlink;
}
49. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 49
temp->rlink = node; /*Temp's right link is assigned the address of node.*/
node->llink = temp; /*Node's left link is assigned the address of temp.*/
}
}
/*Function definition to delete element at end of the list.*/
void delete_end()
{
EMPLOYEE temp, prev;
temp = start;
if ( temp == NULL ) /*If the list is empty.*/
{
printf ( "nList is Empty" );
}
else if ( temp->rlink == NULL ) /*If there is one node in the list.*/
{
printf ( "nThe deleted employee ssn is %s", temp->ssn );
free ( temp );
start = NULL; /*List is Empty.*/
}
else /*If there are many nodes.*/
{
while ( temp->rlink != NULL ) /*Traverse till the end of the list.*/
{
prev = temp; /*Prev keeps track of previous node.*/
temp = temp->rlink;
}
prev->rlink = NULL; /*Pre's right link is assigned NULL.*/
printf ( "nThe deleted employee ssn is %s", temp->ssn );
free ( temp );
}
}
/*Function definition to create a list using end insertion.*/
void create_list()
{
int n, i;
printf ( "nEnter the number of employees: " );
scanf ( "%d", &n );
for ( i = 0 ; i < n ; i++ )
{
insert_end();
50. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 50
}
}
/*Function definition to display the status of the list.*/
void status()
{
EMPLOYEE temp;
int count = 0;
if ( start == NULL ) /*If the list is empty.*/
{
printf ( "nList is Empty" );
return;
}
temp = start;
printf ( "nThe Employee details are: " );
while ( temp != NULL )
{
printf ( "n%sn%sn%sn%sn%dn%ldn", temp->ssn, temp->name, temp->dept, temp-
>designation, temp->salary, temp->phno );
temp = temp->rlink;
count++;
}
printf ( "nThe number of nodes are: %d", count );
}
/*Function definition to insert element at front of the list.*/
void insert_front()
{
EMPLOYEE node;
node = create();
if ( start == NULL ) /*If the list is empty.*/
{
start = node;
}
else /*If there are many nodes.*/
{
node->rlink = start; /*Node's right link is assigned the address of start.*/
start->llink = node; /*Node's right link is assigned the address of start.*/
start = node; /*Now node is the start node.*/
}
}
51. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 51
/*Function definition to delete element at front of the list.*/
void delete_front()
{
EMPLOYEE temp;
temp = start;
if ( temp == NULL ) /*If the list is empty.*/
{
printf ( "nList is Empty" );
}
else if ( temp->rlink == NULL ) /*If there is one node in the list.*/
{
printf ( "nThe deleted employee ssn is %s", temp->ssn );
free ( temp );
start = NULL;
}
else /*If there are many nodes.*/
{
start = temp->rlink; /*Assign the address of next node which is present in start's right link to
start.*/
start->llink = NULL;
printf ( "nThe deleted employee ssn is %s", temp->ssn );
free ( temp );
}
}
/*Function definition to demonstrate operations on double ended queue using DLL.*/
void double_ended_queue()
{
int ch;
/*Infinite loop.*/
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nDOUBLE ENDED QUEUE OPERATIONS" );
printf ( "n1: Insert Rear n2: Delete Rear n3: Insert Front n4: Delete Front n5: Display n6: Exit"
);
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: insert_end();
break;
52. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 52
case 2: delete_end();
break;
case 3: insert_front();
break;
case 4: delete_front();
break;
case 5: status ();
break;
case 6: return;
default: printf ( "nInvalid Choice!!!" );
}
}
}
int main()
{
int ch;
/*Infinite loop.*/
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nDOUBLY LINKED LIST OPERATIONS" );
printf ( "n1: Create List n2: Status of List n3: Insert End n4: Delete End n5: Insert Front n6:
Delete Front n7: Double Ended Queue Demon8: Exit" );
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: create_list ( );
break;
case 2: status ();
break;
case 3: insert_end();
break;
case 4: delete_end();
53. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 53
break;
case 5: insert_front();
break;
case 6: delete_front();
break;
case 7: double_ended_queue();
break;
case 8: return ( 0 );
default: printf ( "nInvalid Choice!!!" );
}
}
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc doubly_linked_list.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 1
Enter the number of employees: 2
Enter the details of Employee
Enter the ssn: 1982
Enter the name: Akhilaa
Enter the department: Education
Enter the designation: AP
54. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 54
Enter the salary: 50000
Enter the phno: 9847289837
Enter the details of Employee
Enter the ssn: 3674
Enter the name: Shanky
Enter the department: Software
Enter the designation: SA
Enter the salary: 110000
Enter the phno: 8274929459
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Employee details are:
1982
Akhilaa
Education
AP
50000
9847289837
3674
Shanky
Software
SA
110000
8274929459
The number of nodes are: 2
---------------------------------------------
55. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 55
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 6
The deleted employee ssn is 1982
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Employee details are:
3674
Shanky
Software
SA
110000
8274929459
The number of nodes are: 1
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 3
56. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 56
Enter the details of Employee
Enter the ssn: 7483
Enter the name: Nikkhil
Enter the department: Software
Enter the designation: SSE
Enter the salary: 150000
Enter the phno: 7462892748
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Employee details are:
3674
Shanky
Software
SA
110000
8274929459
7483
Nikkhil
Software
SSE
150000
7462892748
The number of nodes are: 2
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
57. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 57
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 5
Enter the details of Employee
Enter the ssn: 3801
Enter the name: Vihana
Enter the department: Hardware
Enter the designation: LE
Enter the salary: 270000
Enter the phno: 9405589030
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 2
The Employee details are:
3801
Vihana
Hardware
LE
270000
9405589030
3674
Shanky
Software
SA
110000
8274929459
58. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 58
7483
Nikkhil
Software
SSE
150000
7462892748
The number of nodes are: 3
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 4
The deleted employee ssn is 7483
---------------------------------------------
DOUBLY LINKED LIST OPERATIONS
1: Create List
2: Status of List
3: Insert End
4: Delete End
5: Insert Front
6: Delete Front
7: Double Ended Queue Demo
8: Exit
---------------------------------------------
Enter your choice: 8
59. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 59
PROGRAM – 9a (SINGLY CIRCULAR LINKED LIST)
Design, Develop and Implement a Program in C for the following operations on Singly Circular Linked List
(SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z – 4yz5 + 3x3yz + 2xy5z – 2xyz3
PROGRAM CODE
/* C program to evaluate a polynomial using singly circular linked list with header nodes.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct polynomial
{
int coeff, x, y, z;
struct polynomial *link;
};
typedef struct polynomial * POLYNOMIAL;
/*Function definition to allocate memory.*/
POLYNOMIAL create()
{
POLYNOMIAL getnode;
getnode = ( POLYNOMIAL ) malloc ( sizeof ( struct polynomial ) );
if ( getnode == NULL )
{
printf ( "nMemory couldnt be allocated!!!" );
return;
}
return ( getnode );
}
/*Function definition to insert a polynomial in a singly circular linked list with header node.*/
POLYNOMIAL insert ( POLYNOMIAL head, int c, int px, int py, int pz )
{
POLYNOMIAL node, temp;
node = create();
60. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 60
node->coeff = c;
node->x = px;
node->y = py;
node->z = pz;
node->link = NULL;
temp = head->link;
while ( temp->link != head ) /*Traverse till the end of the list.*/
{
temp = temp->link;
}
temp->link = node; /*Attach the node to the end of the list.*/
node->link = head; /*Assign the address of the head to node's link.*/
return ( head );
}
/*Function definition to read the polynomial.*/
POLYNOMIAL input_polynomial ( POLYNOMIAL head )
{
int i, c, px, py, pz;
printf ( "nEnter 999 to end the polynomial!!!" );
for ( i = 1 ; ; i++ )
{
printf ( "nEnter the coefficient %d: ", i );
scanf ( "%d", &c );
if ( c == 999 ) /*Breaks the loop when 999 is entered indicating end of input.*/
break;
printf ( "nEnter the power of x: " );
scanf ( "%d", &px );
printf ( "nEnter the power of y: " );
scanf ( "%d", &py );
printf ( "nEnter the power of z: " );
scanf ( "%d", &pz );
head = insert ( head, c, px, py, pz );
}
return ( head );
}
61. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 61
/*Function definition to display the list.*/
void display ( POLYNOMIAL head )
{
POLYNOMIAL temp;
if ( head->link == head )
{
printf ( "nPolynomial doesnt exist!!!" );
}
else
{
temp = head->link;
while ( temp != head )
{
printf ( "%dx^%dy^%dz^%d + ", temp->coeff, temp->x, temp->y, temp->z );
temp = temp->link;
}
printf ( "999" );
}
}
/*Function definition to evalulate the polynomial.*/
int evaluate_polynomial ( POLYNOMIAL head )
{
int vx, vy, vz, sum = 0;
POLYNOMIAL temp;
printf ( "nnEnter the value of x, y and z: " );
scanf ( "%d%d%d", &vx, &vy, &vz );
temp = head->link;
while ( temp != head )
{
sum = sum + ( temp->coeff * pow ( vx, temp->x ) * pow ( vy, temp->y ) * pow ( vz, temp->z ) );
temp = temp->link;
}
return ( sum );
}
int main()
{
POLYNOMIAL head;
int res;
62. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 62
/*Create a header node whose link field points to the address of itself initially.*/
head = create();
head->link = head;
printf ( "nEnter the polynomial to be evaluated: " );
head = input_polynomial ( head );
printf ( "nThe given polynomial is: " );
display ( head );
res = evaluate_polynomial ( head );
printf ( "nThe result after evaluation is: %d", res );
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc poly_eval.c -lm
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the polynomial to be evaluated:
Enter 999 to end the polynomial!!!
Enter the coefficient 1: 6
Enter the power of x: 2
Enter the power of y: 2
Enter the power of z: 1
Enter the coefficient 2: -4
Enter the power of x: 0
Enter the power of y: 1
Enter the power of z: 5
Enter the coefficient 3: 3
Enter the power of x: 3
Enter the power of y: 1
Enter the power of z: 1
Enter the coefficient 4: 2
63. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 63
Enter the power of x: 1
Enter the power of y: 5
Enter the power of z: 1
Enter the coefficient 5: -2
Enter the power of x: 1
Enter the power of y: 1
Enter the power of z: 3
Enter the coefficient 6: 999
The given polynomial is: 6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 + -2x^1y^1z^3 + 999
Enter the value of x, y and z: 1
2
3
The result after evaluation is: -1770akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the polynomial to be evaluated:
Enter 999 to end the polynomial!!!
Enter the coefficient 1: 5
Enter the power of x: 1
Enter the power of y: 5
Enter the power of z: 1
Enter the coefficient 2: -1
Enter the power of x: 1
Enter the power of y: 0
Enter the power of z: 2
Enter the coefficient 3: -2
Enter the power of x: 0
Enter the power of y: 0
64. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 64
Enter the power of z: 0
Enter the coefficient 4: 999
The given polynomial is: 5x^1y^5z^1 + -1x^1y^0z^2 + -2x^0y^0z^0 + 999
Enter the value of x, y and z: -5
1
-2
The result after evaluation is: 68
65. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 65
PROGRAM – 9b (SUM OF TWO POLYNOMIAL)
a. Find the sum of two polynomial 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
PROGRAM CODE
/* C program to find sum of two polynomial using singly circular linked list with header nodes.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct polynomial
{
int coeff, x, y, z;
struct polynomial *link;
};
typedef struct polynomial * POLYNOMIAL;
/*Function definition to allocate memory.*/
POLYNOMIAL create()
{
POLYNOMIAL getnode;
getnode = ( POLYNOMIAL ) malloc ( sizeof ( struct polynomial ) );
if ( getnode == NULL )
{
printf ( "nMemory couldnt be allocated!!!" );
return;
}
return ( getnode );
}
/*Function definition to insert a polynomial in a singly circular linked list with header node.*/
POLYNOMIAL insert_end ( POLYNOMIAL head, int c, int px, int py, int pz )
{
POLYNOMIAL node, temp;
node = create();
66. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 66
node->coeff = c;
node->x = px;
node->y = py;
node->z = pz;
node->link = NULL;
temp = head->link;
while ( temp->link != head ) /*Traverse till the end of the list.*/
{
temp = temp->link;
}
temp->link = node; /*Attach the node to the end of the list.*/
node->link = head; /*Assign the address of the head to node's link.*/
return ( head );
}
/*Function definition to read the polynomial.*/
POLYNOMIAL input_polynomial ( POLYNOMIAL head )
{
int i, c, px, py, pz;
printf ( "nEnter 999 to end the polynomial!!!" );
for ( i = 1 ; ; i++ )
{
printf ( "nEnter the coefficient %d: ", i );
scanf ( "%d", &c );
if ( c == 999 ) /*Breaks the loop when 999 is entered indicating end of input.*/
break;
printf ( "nEnter the power of x: " );
scanf ( "%d", &px );
printf ( "nEnter the power of y: " );
scanf ( "%d", &py );
printf ( "nEnter the power of z: " );
scanf ( "%d", &pz );
head = insert_end ( head, c, px, py, pz );
}
return ( head );
}
67. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 67
/*Function definition to display the list.*/
void display ( POLYNOMIAL head )
{
POLYNOMIAL temp;
if ( head->link == head )
{
printf ( "nPolynomial doesnt exist!!!" );
return;
}
temp = head->link;
while ( temp != head )
{
printf ( "%dx^%dy^%dz^%d + ", temp->coeff, temp->x, temp->y, temp->z );
temp = temp->link;
}
printf ( "999" );
}
/*Function definition to sum the two polynomials.*/
POLYNOMIAL sum_polynomial ( POLYNOMIAL head1, POLYNOMIAL head2, POLYNOMIAL head3 )
{
POLYNOMIAL p1, p2;
int c, c1, c2, x1, y1, z1, x2, y2, z2, flag;
p1 = head1->link;
while ( p1 != head1 )
{
c1 = p1->coeff;
x1 = p1->x;
y1 = p1->y;
z1 = p1->z;
p2 = head2->link;
flag = 0; /*No matching polynomial.*/
while ( p2 != head2 )
{
c2 = p2->coeff;
x2 = p2->x;
y2 = p2->y;
z2 = p2->z;
if ( ( x1 == x2 ) && ( y1 == y2 ) && ( z1 == z2 ) ) /*Check if the power of x, y and z of both of
the polynomials are equal or not.*/
68. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 68
{
head3 = insert_end ( head3, c1+c2, x1, y1, z1 ); /*Sum the coefficients and inert into
the final polynomial.*/
p2->coeff = 0; /*Assign 0 to the coeff of polynomial to indicate that we have finished
evaluating it.*/
flag = 1; /*Matching polynomial.*/
break;
}
else
p2 = p2->link;
}
if ( flag == 0 ) /*No matching polynomial.*/
head3 = insert_end ( head3, c1, x1, y1, z1 ); /*Insert polynomial1 into final polynomial.*/
p1 = p1->link;
}
p2 = head2->link;
while ( p2 != head2 )
{
if ( p2->coeff != 0 ) /*Check for left out plolynomial's in polynomial2.*/
head3 = insert_end ( head3, p2->coeff, p2->x, p2->y, p2->z ); /*Insert polynomial2 into final
polynomial.*/
p2 = p2->link;
}
return head3;
}
int main()
{
POLYNOMIAL head1, head2, head3;
/*Create a header node for polynomial1 whose link field points to the address of itself initially.*/
head1 = create();
head1->link = head1;
/*Create a header node for polynomial2 whose link field points to the address of itself initially.*/
head2 = create();
head2->link = head2;
/*Create a header node for sum of polynomial whose link field points to the address of itself
initially.*/
head3 = create();
head3->link = head3;
69. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 69
printf ( "nEnter the first polynomial: " );
head1 = input_polynomial ( head1 );
display ( head1 );
printf ( "nnEnter the second polynomial: " );
head2 = input_polynomial ( head2 );
display ( head2 );
head3 = sum_polynomial ( head1, head2, head3 );
printf ( "nnThe sum of two polynomials is: " );
display ( head3 );
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc poly_sum.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the first polynomial:
Enter 999 to end the polynomial!!!
Enter the coefficient 1: 6
Enter the power of x: 2
Enter the power of y: 1
Enter the power of z: 2
Enter the coefficient 2: -2
Enter the power of x: 1
Enter the power of y: 2
Enter the power of z: 1
Enter the coefficient 3: 5
Enter the power of x: 0
Enter the power of y: 1
Enter the power of z: 0
Enter the coefficient 4: 7
70. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 70
Enter the power of x: 1
Enter the power of y: 2
Enter the power of z: 2
Enter the coefficient 5: 999
6x^2y^1z^2 + -2x^1y^2z^1 + 5x^0y^1z^0 + 7x^1y^2z^2 + 999
Enter the second polynomial:
Enter 999 to end the polynomial!!!
Enter the coefficient 1: 3
Enter the power of x: 00
Enter the power of y: 0
Enter the power of z: 1
Enter the coefficient 2: 6
Enter the power of x: 2
Enter the power of y: 1
Enter the power of z: 2
Enter the coefficient 3: -2
Enter the power of x: 0
Enter the power of y: 1
Enter the power of z: 0
Enter the coefficient 4: 3
Enter the power of x: 1
Enter the power of y: 2
Enter the power of z: 2
Enter the coefficient 5: 0
Enter the power of x: 2
71. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 71
Enter the power of y: 5
Enter the power of z: 7
Enter the coefficient 6: 999
3x^0y^0z^1 + 6x^2y^1z^2 + -2x^0y^1z^0 + 3x^1y^2z^2 + 0x^2y^5z^7 + 999
The sum of two polynomials is: 12x^2y^1z^2 + -2x^1y^2z^1 + 3x^0y^1z^0 + 10x^1y^2z^2 + 3x^0y^0z^1 +
999akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the first polynomial:
Enter 999 to end the polynomial!!!
Enter the coefficient 1: 4
Enter the power of x: 1
Enter the power of y: 1
Enter the power of z: 1
Enter the coefficient 2: 2
Enter the power of x: 1
Enter the power of y: 2
Enter the power of z: 1
Enter the coefficient 3: 0
Enter the power of x: 1
Enter the power of y: 1
Enter the power of z: 2
Enter the coefficient 4: 999
4x^1y^1z^1 + 2x^1y^2z^1 + 0x^1y^1z^2 + 999
Enter the second polynomial:
Enter 999 to end the polynomial!!!
Enter the coefficient 1: -2
Enter the power of x: 1
Enter the power of y: 2
Enter the power of z: 1
72. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 72
Enter the coefficient 2: 3
Enter the power of x: 1
Enter the power of y: 5
Enter the power of z: 1
Enter the coefficient 3: 999
-2x^1y^2z^1 + 3x^1y^5z^1 + 999
The sum of two polynomials is: 4x^1y^1z^1 + 0x^1y^2z^1 + 0x^1y^1z^2 + 3x^1y^5z^1 + 999
73. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 73
PROGRAM – 10 (BINARY SEARCH TREE)
Design, Develop and Implement 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
PROGRAM CODE
/* C program to perform operations on Binary Search Tree of Integers.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left, *right;
};
typedef struct tree * TREE;
/*Function definition to allocate memory.*/
TREE create()
{
TREE node;
node = ( TREE ) malloc ( sizeof ( struct tree ) );
if ( node == NULL )
{
printf ( "nMemory could not be allocated!!!" );
return;
}
return ( node );
}
/*Function definition to create a BST of N integers.*/
TREE create_bst ( TREE root )
{
74. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 74
TREE node, prev, cur;
node = create();
printf ( "nEnter the data to be inserted: " );
scanf ( "%d", &node->data );
node->left = NULL;
node->right = NULL;
if ( root == NULL )
{
root = node;
return ( root );
}
prev = NULL;
cur = root;
while ( cur != NULL )
{
prev = cur;
if ( node->data < cur-> data )
cur = cur->left; /*Traverse towards left sub-tree.*/
else
cur = cur->right; /*Traverse towards right sub-tree.*/
}
if ( node->data < prev->data )
prev->left = node; /*Attach node onto the left of previous node.*/
else
prev->right = node; /*Attach node onto the right of previous node.*/
return ( root );
}
/*Function definition for Inorder Traversal of BST.*/
void inorder_traversal ( TREE root )
{
if ( root == NULL )
return;
inorder_traversal ( root->left );
printf ( "%3d", root->data );
inorder_traversal ( root->right );
}
75. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 75
/*Function definition for Preorder Traversal of BST.*/
void preorder_traversal ( TREE root )
{
if ( root == NULL )
return;
printf ( "%3d", root->data );
preorder_traversal ( root->left );
preorder_traversal ( root->right );
}
/*Function definition for Post order Traversal of BST.*/
void postorder_traversal ( TREE root )
{
if ( root == NULL )
return;
postorder_traversal ( root->left );
postorder_traversal ( root->right );
printf ( "%3d", root->data );
}
/*Function definition to search for a key in BST.*/
void search_bst ( TREE root )
{
int key, flag = 0;
TREE temp;
printf ( "nEnter the key to be searched: " );
scanf ( "%d", &key );
if ( root == NULL )
return;
temp = root;
while ( temp != NULL )
{
if ( key == temp->data )
{
flag = 1; /*Key found.*/
break;
}
else if ( key < temp->data )
temp = temp->left; /*Search towards left of the sub-tree.*/
else
temp = temp->right; /*Search towards right of the sub-tree.*/
76. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 76
}
if ( flag == 1 )
printf ( "nKey found!!!" );
else
printf ( "nKey not found!!!" );
}
int main()
{
int ch, n, i;
TREE root = NULL;
/*Infinite loop.*/
for ( ; ; )
{
printf ( "n---------------------------------------------" );
printf ( "nBINARY SEARCH TREE OPERATIONS" );
printf ( "n1: Create N Integers n2: Inorder Traversal n3: Pre-order Traversal n4: Post order
traversal n5: Search for a KEY n6: Exit" );
printf ( "n---------------------------------------------" );
printf ( "nEnter your choice: " );
scanf ( "%d", &ch );
switch ( ch )
{
case 1: printf ( "nEnter the number of integers: " );
scanf ( "%d", &n );
for ( i = 0 ; i < n ; i++ )
{
root = create_bst ( root );
}
break;
case 2: printf ( "nThe Inorder Traversal of the given BST is: " );
inorder_traversal ( root );
break;
case 3: printf ( "nThe Pre-order Traversal of the given BST is: " );
preorder_traversal ( root );
break;
case 4: printf ( "nThe Post order Traversal of the given BST is: " );
postorder_traversal ( root );
break;
77. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 77
case 5: search_bst ( root );
break;
case 6: return ( 0 );
default: printf ( "nInvalid Choice!!!" );
}
}
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc binary_search_tree.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 1
Enter the number of integers: 12
Enter the data to be inserted: 6
Enter the data to be inserted: 9
Enter the data to be inserted: 5
Enter the data to be inserted: 2
Enter the data to be inserted: 8
Enter the data to be inserted: 15
Enter the data to be inserted: 24
Enter the data to be inserted: 14
Enter the data to be inserted: 7
78. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 78
Enter the data to be inserted: 8
Enter the data to be inserted: 5
Enter the data to be inserted: 2
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 2
The Inorder Traversal of the given BST is: 2 2 5 5 6 7 8 8 9 14 15 24
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 3
The Pre-order Traversal of the given BST is: 6 5 2 2 5 9 8 7 8 15 14 24
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 4
The Post order Traversal of the given BST is: 2 2 5 5 7 8 8 14 24 15 9 6
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
79. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 79
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 5
Enter the key to be searched: 91
Key not found!!!
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 6
akhilaa:~/workspace/akhi_dslab $ ./a.out
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 1
Enter the number of integers: 12
Enter the data to be inserted: 50
Enter the data to be inserted: 30
Enter the data to be inserted: 25
Enter the data to be inserted: 75
Enter the data to be inserted: 82
Enter the data to be inserted: 28
Enter the data to be inserted: 63
80. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 80
Enter the data to be inserted: 70
Enter the data to be inserted: 4
Enter the data to be inserted: 43
Enter the data to be inserted: 74
Enter the data to be inserted: 33
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 2
The Inorder Traversal of the given BST is: 4 25 28 30 33 43 50 63 70 74 75 82
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 3
The Pre-order Traversal of the given BST is: 50 30 25 4 28 43 33 75 63 70 74 82
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 4
The Post order Traversal of the given BST is: 4 28 25 33 43 30 74 70 63 82 75 50
---------------------------------------------
81. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 81
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 5
Enter the key to be searched: 33
Key found!!!
---------------------------------------------
BINARY SEARCH TREE OPERATIONS
1: Create N Integers
2: Inorder Traversal
3: Pre-order Traversal
4: Post order traversal
5: Search for a KEY
6: Exit
---------------------------------------------
Enter your choice: 6
82. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 82
PROGRAM – 11 (GRAPHS)
Design, Develop and Implement 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
PROGRAM CODE
/* C program to print all the nodes reachable from a node using Depth First Search (DFS) method.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
int n, g [ 20 ] [ 20 ], visited [ 20 ];
/*Function definition to find Depth First Search ( DFS ).*/
void depth_first_search ( int c )
{
int i;
printf ( "t%d", c );
visited [ c ] = 1; /*Mark the city as visited.*/
for ( i = 0; i < n ; i++ )
{
/*If the city is not visited and if there is a path from c to i.*/
if ( !visited [ i ] && ( g [ c ] [ i ] == 1 ) )
depth_first_search ( i );
}
}
int main()
{
int i, j, c;
printf ( "nEnter the number of cities: " );
scanf ( "%d", &n );
printf ( "nEnter the adjacency matrix:n" );
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
83. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 83
scanf ( "%d", &g [ i ] [ j ] );
}
}
printf ( "nEnter the starting city: " );
scanf ( "%d", &c );
for ( i = 0 ; i < n ; i++ )
visited [ i ] = 0;
printf ( "nThe cities reachable from starting city are: " );
depth_first_search ( c );
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc depth_first_search.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the number of cities: 8
Enter the adjacency matrix:
0 1 1 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
Enter the starting city: 0
The cities reachable from starting city are: 0 1 4 7 2 5 3 6
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the number of cities: 6
Enter the adjacency matrix:
0 1 0 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 1 0 0 0 1
0 0 1 0 0 0
84. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 84
Enter the starting city: 0
The cities reachable from starting city are: 0 1 2 3 4 5
akhilaa:~/workspace/akhi_dslab $ ./a.out
Enter the number of cities: 6
Enter the adjacency matrix:
0 1 0 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 1 0 0 0 1
0 0 1 0 0 0
Enter the starting city: 3
The cities reachable from starting city are: 3 4 1 2 5
PROGRAM CODE
/* C program to print all the nodes reachable from a node using Breadth First Search (BFS) method.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
int n, g [ 20 ] [ 20 ], q [ 20 ], visited [ 20 ], f = 0, r = -1;
/*Function definition to find Breadth First Search ( BFS ).*/
void breadth_first_search ( int c )
{
int i;
for ( i = 0; i < n ; i++ )
{
/*If the city is not visited and if there is a path from i to j.*/
if ( !visited [ i ] && ( g [ c ] [ i ] == 1 ) )
q [ ++ r ] = i;
visited [ q [ r ] ] = 1; /*Mark the city as visited.*/
}
if ( f <= r )
{
85. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 85
breadth_first_search ( q [ ++f ] );
}
}
int main()
{
int i, j, c;
printf ( "nEnter the number of cities: " );
scanf ( "%d", &n );
printf ( "nEnter the adjacency matrix:n" );
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
scanf ( "%d", &g [ i ] [ j ] );
}
}
for ( i = 0 ; i < n ; i++ )
{
q [ i ] = 0;
visited [ i ] = 0;
}
printf ( "nEnter the starting city: " );
scanf ( "%d", &c );
q [ ++ r ] = c;
breadth_first_search ( c );
printf ( "nThe cities reachable from starting city are: " );
for ( i = 0 ; i < n ; i++ )
{
if ( visited [ i ] )
printf ( "t%d", q [ i ] );
}
return ( 0 );
}
OUTPUT
akhilaa:~/workspace/akhi_dslab $ cc breadth_first_search.c
akhilaa:~/workspace/akhi_dslab $ ./a.out
86. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 86
Enter the number of cities: 6
Enter the adjacency matrix:
0 1 1 0 0 0
0 0 1 1 0 0
0 0 0 1 0 0
0 0 0 0 1 0
1 1 0 0 0 1
0 0 0 0 0 0
Enter the starting city: 2
The cities reachable from starting city are: 2 3 4 0 1 5
87. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 87
PROGRAM – 12 (HASHING)
Given a File of N employee records with a set of 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. 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.
PROGRAM CODE
/* C program to implement hashing technique using linear probing.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int F [ MAX ], HT [ MAX], L;
/*Function definition for Linear Probing.*/
void linear_probe ( int k, int key )
{
L= k % MAX; /*Hash function.*/
if ( HT [ L ] == 0 ) /*If no entry is made at L location.*/
HT [ L ] = key; /*Assign the key to L location in hash table.*/
else
linear_probe ( k+1, key ); /*Call the function recursively until u find the next empty
location.*/
}
/*Function definition to display the contents of Hash Table.*/
void display()
{
int i;
printf ( "nHash Table:" );
for ( i=0 ; i < MAX ; i++)
{
printf ( "nHT [ %d ] ------> %d" , i, HT [ i ] );
}
}
88. Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 88
int main()
{
FILE *fp;
int i;
char buff[1000];
fp = fopen ( "data.txt", "r" );
i = 0;
while ( fscanf ( fp, "%d", &F [ i ] ) != EOF ) /*Scan the keys from the data.txt file and store it in
an array F.*/
{
fscanf ( fp, "%[^n]s ", buff ); /*Scan a complete line until u encounter a next line (n).*/
i++; /*Keeps track of number of records in data.txt.*/
}
printf ( "nThe number of records in the File are: %d", i );
for ( i = 0 ; i < MAX ; i++ )
{
L= F [ i ] % MAX; /*Hash function.*/
if ( HT [ L ] == 0 ) /*If no entry is made at L location.*/
HT [ L ] = F [ i ]; /*Assign the key (F[i]) to L location in hash table.*/
else
linear_probe ( F [ i ] + 1 , F [ i ] ); /*Collision occurs. Check linearly for next free
location in the hash table.*/
}
display();
return ( 0 );
}
In data.txt
1546 Arnav
1521 Nikkhil
1535 Vihan
1531 Nithin
1528 Shashank
1537 Anvitha
1518 Chandni
1543 Sneha
1533 Kishore
1533 Reethu
1534 Gagan