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
Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 2
{
printf("\nThe elements are:\n");
for( i = 0; i < n ; i++ )
{
printf("%d\t", 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 i
th
location to i+1
th
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);
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+1
th
location to i
th
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);
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
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
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
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.*/
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: %s\n", 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’:
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!!!
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;
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!!!" );
}
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 ] );
}
}
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;
---------------------------------------------
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
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
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
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
NOT PALINDROME!!!
---------------------------------------------
STACK OPERATIONS
1: Push
2: Pop
3: Check Palindrome
4: Display
5: Exit
---------------------------------------------
Enter your choice: 5
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.*/
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.*/
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
{
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 );
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-
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.*/
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;
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.*/
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.*/
}
}
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 );
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
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.*/
}
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 ] );
}
}
---------------------------------------------
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
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
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
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
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;
};
/*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 )
{
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.*/
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---------------------------------------------" );
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
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
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
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
---------------------------------------------
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
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
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;
};
/*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;
}
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 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.*/
}
}
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;
---------------------------------------------
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
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
---------------------------------------------
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
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
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
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
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) = 6x
2
y
2
z – 4yz
5
+ 3x
3
yz + 2xy
5
z – 2xyz
3
PROGRAM CODE
/* C program to evaluate a polynomial using singly circular linked list with header nodes.
Author: Akhilaa
Designation: Assistant Professor
Dept: ISE, CMRIT
*/
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;
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
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
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
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
*/
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;
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.*/
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;
Data Structures Laboratory Manual (18CSL38)
Dept. of ISE, CMRIT Page 69
printf ( "\nEnter the first polynomial: " );
head1 = input_polynomial ( head1 );
display ( head1 );
printf ( "\n\nEnter the second polynomial: " );
head2 = input_polynomial ( head2 );
display ( head2 );
head3 = sum_polynomial ( head1, head2, head3 );
printf ( "\n\nThe sum of two polynomials is: " );
display ( head3 );
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
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
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
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
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 )
{
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 ( 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;
/*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.*/
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---------------------------------------------" );
---------------------------------------------
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
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
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
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
---------------------------------------------
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
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++ )
{
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 )
{
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;
}
The cities reachable from starting city are: 2 3 4 0 1 5
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 ] );
}
}
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.*/
}