0% found this document useful (0 votes)
12 views37 pages

DSA Lab Manual

Uploaded by

haarismaniyar09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views37 pages

DSA Lab Manual

Uploaded by

haarismaniyar09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Lab Manual

Data Structures Laboratory

Department of Computer Science and Engineering

Prepared by
Mrs. H.N.Ingaleshwar

BLDEA’s VP Dr.P.G.HAlakatti College of Engineering and Technology,


Vijayapura

November 18, 2025


Data Structures Lab Manual Dept. of CSE

Experiment No. 1:
Aim
Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent
7 days of a week. Each Element of the array is a structure having three fields. The first
field is the name of the Day (A dynamically allocated String), The second field is the date
of the Day (A integer), the third field is the description of the activity for a particular
day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.

Program Code

1 // Calendar Program using array ( A dynamically created array )


2 # include < stdio .h >
3 # include < stdlib .h >
4

5 struct Day {
6 char * dayName ;
7 int date ;
8 char * activity ;
9 };
10

11 void create ( struct Day * day ) {


12 day - > dayName = ( char *) malloc ( sizeof ( char ) * 20) ;
13 day - > activity = ( char *) malloc ( sizeof ( char ) * 100) ;
14

15 printf ( " Enter the day name : " ) ;


16 scanf ( " % s " , day - > dayName ) ;
17

18 printf ( " Enter the date : " ) ;


19 scanf ( " % d " , & day - > date ) ;
20

21 printf ( " Enter the activity for the day : " ) ;


22 scanf ( " %[^\ n ] s " , day - > activity ) ;
23 }
24

25 void read ( struct Day * calendar , int size ) {


26 for ( int i = 0; i < size ; i ++) {
27 printf ( " Enter details for Day % d :\ n " , i + 1) ;
28 create (& calendar [ i ]) ;
29 }
30 }
31

32 void display ( struct Day * calendar , int size ) {


33 printf ( " \ nWeek ’s Activity Details :\ n " ) ;
34 for ( int i = 0; i < size ; i ++) {
35 printf ( " Day % d :\ n " , i + 1) ;
36 printf ( " Day Name : % s \ n " , calendar [ i ]. dayName ) ;

BLDEACET, Vijayapura 1 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

37 printf ( " Date : % d \ n " , calendar [ i ]. date ) ;


38 printf ( " Activity : % s \ n " , calendar [ i ]. activity ) ;
39 printf ( " \ n " ) ;
40 }
41 }
42

43 void freeMemory ( struct Day * calendar , int size ) {


44 for ( int i = 0; i < size ; i ++) {
45 free ( calendar [ i ]. dayName ) ;
46 free ( calendar [ i ]. activity ) ;
47 }
48 }
49

50 int main () {
51 int size ;
52 printf ( " Enter the number of days in the week : " ) ;
53 scanf ( " % d " , & size ) ;
54

55 struct Day * calendar = ( struct Day *) malloc ( sizeof ( struct Day


) * size ) ;
56

57 if ( calendar == NULL ) {
58 printf ( " Memory allocation failed . Exiting program .\ n " ) ;
59 return 1;
60 }
61

62 read ( calendar , size ) ;


63 display ( calendar , size ) ;
64

65 freeMemory ( calendar , size ) ;


66 free ( calendar ) ;
67

68 return 0;
69 }

Sample Input/Output
Enter the number of days in the week: 7

Enter details for Day 1:


Enter the day name: Sunday
Enter the date: 1
Enter the activity for the day: Learning

Enter details for Day 2:


Enter the day name: Monday
Enter the date: 2
Enter the activity for the day: Coding

Enter details for Day 3:

BLDEACET, Vijayapura 2 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

Enter the day name: Tuesday


Enter the date: 3
Enter the activity for the day: Testing

Enter details for Day 4:


Enter the day name: Wednesday
Enter the date: 4
Enter the activity for the day: Debugging

Enter details for Day 5:


Enter the day name: Thrusday
Enter the date: 5
Enter the activity for the day: Publishing

Enter details for Day 6:


Enter the day name: Friday
Enter the date: 6
Enter the activity for the day: Marketing

Enter details for Day 7:


Enter the day name: Saturday
Enter the date: 7
Enter the activity for the day: Earning

Week’s Activity Details:


Day 1:
Day Name: Sunday
Date: 1
Activity: Learning

Day 2:
Day Name: Monday
Date: 2
Activity: Coding

Day 3:
Day Name: Tuesday
Date: 3
Activity: Testing

Day 4:
Day Name: Wednesday
Date: 4
Activity: Debugging

Day 5:
Day Name: Thrusday
Date: 5

BLDEACET, Vijayapura 3 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

Activity: Publishing

Day 6:
Day Name: Friday
Date: 6
Activity: Marketing

Day 7:
Day Name: Saturday
Date: 7
Activity: Earning

Result
The calendar was successfully implemented using arrays.

Experiment No. 2:
Aim
Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR
Support the program with functions for each of the above operations. Don’t use Built-in
functions.

Program Code

1 // Pattern Matching Program


2 # include < stdio .h >
3 char str [50] , pat [20] , rep [20] , res [50];
4 int c = 0 , m = 0 , i = 0 , j = 0 , k , flag = 0;
5 void stringmatch ()
6 {
7 while ( str [ c ] != ’ \0 ’)
8 {
9 if ( str [ m ] == pat [ i ])
10 {
11 i ++;
12 m ++;
13 if ( pat [ i ] == ’ \0 ’)
14 {
15 flag = 1;
16 for ( k = 0; rep [ k ] != ’ \0 ’; k ++ , j ++)
17 {
18 res [ j ] = rep [ k ];
19 }

BLDEACET, Vijayapura 4 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

20 i = 0;
21 c = m;
22 }
23 }
24 else
25 {
26 res [ j ] = str [ c ];
27 j ++;
28 c ++;
29 m = c;
30 i = 0;
31 }
32 }
33 res [ j ] = ’ \0 ’;
34 }
35 void main ()
36 {
37 printf ( " Enter the main string : " ) ;
38 gets ( str ) ;
39 printf ( " \ nEnter the pat string : " ) ;
40 gets ( pat ) ;
41 printf ( " \ nEnter the replace string : " ) ;
42 gets ( rep ) ;
43 printf ( " \ nThe string before pattern match is :\ n % s " , str ) ;
44 stringmatch () ;
45 if ( flag == 1)
46 printf ( " \ nThe string after pattern match and replace is :
\ n % s " , res ) ;
47 else
48 printf ( " \ nPattern string is not found " ) ;
49 }

Sample Input/Output
Enter the main string:Good Morning

Enter the pat string:Morning

Enter the replace string:Evening

The string before pattern match is:


Good Morning

The string after pattern match and replace is:


Good Evening

Result
The Pattern Matching was successfully implemented using arrays.

BLDEACET, Vijayapura 5 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

Experiment No. 3:
Aim
Develop a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations

Program Code

1 // Stack Program To Check Number is Palindrome or not Program


2

3 # include < stdio .h >


4

5 # include < stdlib .h >


6

7 # define MAX 3
8

9 int s [ MAX ];
10 int top = -1;
11

12 void push ( int item ) ;


13 int pop () ;
14 void palindrome () ;
15 void display () ;
16

17 void main ()
18 {
19 int choice , item ;
20 while (1)
21 {
22 printf ( " \ n \ n \ n \n - - - - - - - - - - - Menu - - - - - - - - - - - : " ) ;
23 printf ( " \ n = >1. Push an Element to Stack and Overflow demo
");
24 printf ( " \ n = >2. Pop an Element from Stack and Underflow
demo " ) ;
25 printf ( " \ n = >3. Palindrome demo " ) ;
26 printf ( " \ n = >4. Display " ) ;
27 printf ( " \ n = >5. Exit " ) ;
28 printf ( " \ nEnter your choice : " ) ;
29 scanf ( " % d " , & choice ) ;
30 switch ( choice )
31 {
32 case 1:
33 printf ( " \ nEnter an element to be pushed : " ) ;

BLDEACET, Vijayapura 6 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

34 scanf ( " % d " , & item ) ;


35 push ( item ) ;
36 break ;
37 case 2:
38 item = pop () ;
39 if ( item != -1)
40 printf ( " \ nElement popped is : % d " , item ) ;
41 break ;
42 case 3:
43 palindrome () ;
44 break ;
45 case 4:
46 display () ;
47 break ;
48 case 5:
49 exit (1) ;
50 default :
51 printf ( " \ nPlease enter valid choice " ) ;
52 break ;
53 }
54 }
55 }
56

57 void push ( int item )


58 {
59 if ( top == MAX - 1)
60 {
61 printf ( " \n - - - - - - - - - - - Stack overflow - - - - - - - - - - - " ) ;
62 return ;
63 }
64

65 top = top + 1;
66 s [ top ] = item ;
67 }
68

69 int pop ()
70 {
71 int item ;
72 if ( top == -1)
73 {
74 printf ( " \n - - - - - - - - - - - Stack underflow - - - - - - - - - - - " ) ;
75 return -1;
76 }
77 item = s [ top ];
78 top = top - 1;
79 return item ;
80 }
81

82 void display ()
83 {
84 int i ;

BLDEACET, Vijayapura 7 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

85 if ( top == -1)
86 {
87 printf ( " \n - - - - - - - - - - - Stack is empty - - - - - - - - - - - " ) ;
88 return ;
89 }
90 printf ( " \ nStack elements are :\ n " ) ;
91 for ( i = top ; i >= 0; i - -)
92 printf ( " | % d |\ n " , s [ i ]) ;
93 }
94

95 void palindrome ()
96 {
97 int flag = 1 , i ;
98 printf ( " \ nStack content are :\ n " ) ;
99 for ( i = top ; i >= 0; i - -)
100 printf ( " | % d |\ n " , s [ i ]) ;
101

102 printf ( " \ nReverse of stack content are :\ n " ) ;


103 for ( i = 0; i <= top ; i ++)
104 printf ( " | % d |\ n " , s [ i ]) ;
105

106 for ( i = 0; i <= top / 2; i ++)


107 {
108 if ( s [ i ] != s [ top - i ])
109 {
110 flag = 0;
111 break ;
112 }
113 }
114 if ( flag == 1)
115 {
116 printf ( " \ nIt is palindrome number " ) ;
117 }
118 else
119 {
120 printf ( " \ nIt is not a palindrome number " ) ;
121 }
122 }

Sample Input/Output
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 11

BLDEACET, Vijayapura 8 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 12

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 13

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 14
-----------Stack overflow-----------

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 4


Stack elements are:
| 13 |
| 12 |
| 11 |

BLDEACET, Vijayapura 9 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 2


Element popped is: 13

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 4


Stack elements are:
| 12 |
| 11 |

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 2


Element popped is: 12

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 2


Element popped is: 11

BLDEACET, Vijayapura 10 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 2


-----------Stack underflow-----------

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 4


-----------Stack is empty-----------

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 11

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 22

-----------Menu----------- :

BLDEACET, Vijayapura 11 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

=>1.Push an Element to Stack and Overflow demo


=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 11

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 3


Stack content are:
| 11 |
| 22 |
| 11 |

Reverse of stack content are:


| 11 |
| 22 |
| 11 |

It is palindrome number

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 2


Element popped is: 11

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display

BLDEACET, Vijayapura 12 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

=>5.Exit

Enter your choice: 2


Element popped is: 22

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 33

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 1


Enter an element to be pushed: 22

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 3


Stack content are:
| 22 |
| 33 |
| 11 |

Reverse of stack content are:


| 11 |
| 33 |
| 22 |

It is not a palindrome number

BLDEACET, Vijayapura 13 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit

Enter your choice: 5

Result
The Stack and Palindrome was successfully implemented using arrays.

Experiment No. 4:
Aim
4. Develop a Program in C for converting an Infix Expression to Postfix Expression.
Program should support for both parenthesized and free parenthesized expressions with
the operators: +, -, *, /, (Remainder), ( P ower))andalphanumericoperands.

Program Code

1 # include < stdio .h >


2

3 # include < stdlib .h >


4

5 void evaluate () ;
6 void push ( char ) ;
7 char pop () ;
8 int prec ( char ) ;
9

10 char infix [30] , postfix [30] , stack [30];


11 int top = -1;
12

13 void main ()
14 {
15 printf ( " \ n Enter the valid infix expression : " ) ;
16 scanf ( " % s " , infix ) ;
17 evaluate () ;
18 printf ( " \ nThe entered infix expression is :\ n % s \ n " , infix ) ;
19 printf ( " \ nThe corresponding postfix expression is :\ n % s \ n " ,
postfix ) ;
20 }
21

22 void evaluate ()
23 {
24 int i = 0 , j = 0;
25 char symb , temp ;

BLDEACET, Vijayapura 14 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

26

27 push ( ’# ’) ;
28

29 for ( i = 0; infix [ i ] != ’ \0 ’; i ++)


30 {
31 symb = infix [ i ];
32 switch ( symb )
33 {
34 case ’( ’:
35 push ( symb ) ;
36 break ;
37

38 case ’) ’:
39 temp = pop () ;
40 while ( temp != ’( ’)
41 {
42 postfix [ j ] = temp ;
43 j ++;
44 temp = pop () ;
45 }
46 break ;
47 case ’+ ’:
48 case ’ - ’:
49 case ’* ’:
50 case ’/ ’:
51 case ’% ’:
52 case ’^ ’:
53 case ’$ ’:
54 while ( prec ( stack [ top ]) >= prec ( symb ) )
55 {
56 temp = pop () ;
57 postfix [ j ] = temp ;
58 j ++;
59 }
60 push ( symb ) ;
61 break ;
62 default :
63 postfix [ j ] = symb ;
64 j ++;
65 }
66 }
67 while ( top > 0)
68 {
69 temp = pop () ;
70 postfix [ j ] = temp ;
71 j ++;
72 }
73 postfix [ j ] = ’ \0 ’;
74 }
75

76 void push ( char item )

BLDEACET, Vijayapura 15 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

77 {
78 top = top + 1;
79 stack [ top ] = item ;
80 }
81

82 char pop ()
83 {
84 char item ;
85 item = stack [ top ];
86 top = top - 1;
87 return item ;
88 }
89

90 int prec ( char symb )


91 {
92 int p ;
93 switch ( symb )
94 {
95 case ’# ’:
96 p = -1;
97 break ;
98

99 case ’( ’:
100 case ’) ’:
101 p = 0;
102 break ;
103

104 case ’+ ’:
105 case ’ - ’:
106 p = 1;
107 break ;
108

109 case ’* ’:
110 case ’/ ’:
111 case ’% ’:
112 p = 2;
113 break ;
114

115 case ’^ ’:
116 case ’$ ’:
117 p = 3;
118 break ;
119 }
120 return p ;
121 }

Sample Input/Output

Enter the valid infix expression:(a+b)*c/d5 %

BLDEACET, Vijayapura 16 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

T heenteredinf ixexpressionis :
(a + b) ∗ c/d5 T hecorrespondingpostf ixexpressionis :
ab + c ∗ d5/

result
the convertion of infix to postfix successfully created

Experiment number:5A
Aim
5. Develop a Program in C for the following Stack Applications. a) Evaluation of Suffix
expression with single digit operands and operators: +, -, *, /,

Program Code

1 # include < stdio .h >


2 # include < stdlib .h >
3 # include < math .h >
4 # include < ctype .h >
5

6 int i , top = -1;


7 int op1 , op2 , res , s [20];
8 char postfix [90] , symb ;
9

10 void push ( int item )


11 {
12 top = top + 1;
13 s [ top ] = item ;
14 }
15

16 int pop ()
17 {
18 int item ;
19 item = s [ top ];
20 top = top - 1;
21 return item ;
22 }
23

24 void main ()
25 {
26 printf ( " \ nEnter a valid postfix expression :\ n " ) ;
27 scanf ( " % s " , postfix ) ;
28 for ( i = 0; postfix [ i ] != ’ \0 ’; i ++)
29 {
30 symb = postfix [ i ];
31 if ( isdigit ( symb ) )
32 {
33 push ( symb - ’0 ’) ;

BLDEACET, Vijayapura 17 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

34 }
35 else
36 {
37 op2 = pop () ;
38 op1 = pop () ;
39 switch ( symb )
40 {
41 case ’+ ’:
42 push ( op1 + op2 ) ;
43 break ;
44 case ’ - ’:
45 push ( op1 - op2 ) ;
46 break ;
47 case ’* ’:
48 push ( op1 * op2 ) ;
49 break ;
50 case ’/ ’:
51 push ( op1 / op2 ) ;
52 break ;
53 case ’% ’:
54 push ( op1 % op2 ) ;
55 break ;
56 case ’$ ’:
57 case ’^ ’:
58 push ( pow ( op1 , op2 ) ) ;
59 break ;
60 default :
61 push (0) ;
62 }
63 }
64 }
65 res = pop () ;
66 printf ( " \ n Result = % d " , res ) ;
67 }

Sample Input/Output

Enter a valid postfix expression:


623+-382/+*2*3+

Result = 17

BLDEACET, Vijayapura 18 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

5.B) Develop a Program in C for the following Stack Applications. Solving Tower of
Hanoi problem with n disks.

Program Code

1 # include < stdio .h >


2 # include < math .h >
3

4 void tower ( int n , int source , int temp , int destination )


5 {
6 if ( n == 0)
7 return ;
8 tower ( n - 1 , source , destination , temp ) ;
9 printf ( " \ nMove disc % d from % c to % c " , n , source , destination
);
10 tower ( n - 1 , temp , source , destination ) ;
11 }
12 void main ()
13 {
14 int n ;
15 printf ( " \ nEnter the number of discs : \ n " ) ;
16 scanf ( " % d " , & n ) ;
17 tower (n , ’A ’ , ’B ’ , ’C ’) ;
18 printf ( " \ n \ nTotal Number of moves are : % d " , ( int ) pow (2 , n ) -
1) ;
19 }

Sample Input/Output

OUTPUT 1:
Enter the number of discs: 1

Move disc 1 from A to C


Total Number of moves are: 1

OUTPUT 2:
Enter the number of discs: 2

Move disc 1 from A to B Move disc 2 from A to C Move disc 1 from B to C


Total Number of moves are: 3

BLDEACET, Vijayapura 19 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

6. Develop a menu driven Program in C for the following operations on Circular


QUEUE of Characters (Array Implementation of Queue with maximum size MAX).
a) Insert an Element on to Circular QUEUE
b) Delete an Element from Circular QUEUE
c) Demonstrate Overflow and Underflow situations on Circular QUEUE
d) Display the status of Circular QUEUE
e) Exit
Support the program with appropriate functions for each of the above operations

Program Code

2 # include < stdio .h >


3 # include < stdlib .h >
4

5 # define MAX 5
6

7 char circular_queue [ MAX ];


8 int front = -1 , rear = -1;
9

10 int isEmpty ()
11 {
12 if ( front == -1 && rear == -1)
13 return 1;
14 else
15 return 0;
16 }
17

18 int isFull ()
19 {
20 if (( rear + 1) % MAX == front )
21 return 1;
22 else
23 return 0;
24 }
25

26 void insertElement ( char element )


27 {
28 if ( isFull () )
29 {
30 printf ( " Circular Queue Overflow \ n " ) ;
31 return ;
32 }
33 else if ( isEmpty () )
34 {
35 front = rear = 0;
36 }
37 else
38 {
39 rear = ( rear + 1) % MAX ;
40 }

BLDEACET, Vijayapura 20 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

41 circular_queue [ rear ] = element ;


42 }
43

44 void deleteElement ()
45 {
46 char deletedElements ;
47 if ( isEmpty () )
48 {
49 printf ( " Circular Queue Underflow \ n " ) ;
50 return ;
51 }
52 deletedElements = circular_queue [ front ];
53 if ( front == rear )
54 {
55 front = rear = -1;
56 }
57 else
58 {
59

60 front = ( front + 1) % MAX ;


61 }
62 printf ( " The deletedelement is : % c " , deletedElements ) ;
63 }
64

65 void display ()
66 {
67 int i ;
68 if ( isEmpty () )
69 {
70 printf ( " Circular Queue is empty \ n " ) ;
71 return ;
72 }
73 printf ( " Circular Queue elements : " ) ;
74 i = front ;
75 do
76 {
77 printf ( " % c " , circular_queue [ i ]) ;
78 i = ( i + 1) % MAX ;
79 }
80 while ( i != ( rear + 1) % MAX ) ;
81 printf ( " \ n " ) ;
82 }
83

84 int main ()
85 {
86 int choice ;
87 char element ;
88 do
89 {
90 printf ( " \ n \n - - - - Circular Queue Menu - - - -\ n " ) ;
91 printf ( " 1. Insert an Element \ n " ) ;

BLDEACET, Vijayapura 21 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

92 printf ( " 2. Delete an Element \ n " ) ;


93 printf ( " 3. Display Circular Queue \ n " ) ;
94 printf ( " 4. Exit \ n " ) ;
95 printf ( " Enter your choice : " ) ;
96 scanf ( " % d " , & choice ) ;
97

98 switch ( choice )
99 {
100 case 1:
101 printf ( " Enter element to be inserted : " ) ;
102 scanf ( " % c " , & element ) ;
103 insertElement ( element ) ;
104 break ;
105 case 2:
106 deleteElement () ;
107 break ;
108 case 3:
109 display () ;
110 break ;
111 case 4:
112 printf ( " Exiting ...\ n " ) ;
113 break ;
114 default :
115 printf ( " Invalid choice ! Please enter a valid option .\
n");
116 }
117 }
118 while ( choice != 4) ;
119

120 return 0;
121 }

Sample Input/Output

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1
Enter element to be inserted: a

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1

BLDEACET, Vijayapura 22 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

Enter element to be inserted: b

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1
Enter element to be inserted: c

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1
Enter element to be inserted: d

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1
Enter element to be inserted: e

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1
Enter element to be inserted: f
Circular Queue Overflow

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 3
Circular Queue elements: a b c d e

BLDEACET, Vijayapura 23 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
The deletedelement is : a

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
The deletedelement is : b

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
The deletedelement is : c

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
The deletedelement is : d

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
The deletedelement is : e

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
Circular Queue Underflow

BLDEACET, Vijayapura 24 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 2
Circular Queue Underflow

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 3
Circular Queue is empty

---- Circular Queue Menu ----


1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 4
Exiting...

BLDEACET, Vijayapura 25 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

7. Develop a menu driven Program in C for the following operations on Singly Linked
List (SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo.
a) Create a SLL of N Students Data by using front insertion.
b) Display the status of SLL and count the number of nodes in it
c) Perform Insertion / Deletion at End of SLL
d) Perform Insertion / Deletion at Front of SLL(Demonstration of stack) e) ExitStudent
Registration Services
Support the program with appropriate functions for each of the above operations

Program Code

2 # include < stdio .h >


3

4 # include < stdlib .h >


5

6 struct node
7 {
8 char usn [25] , name [25] , branch [25];
9 int sem ;
10 long int phone ;
11 struct node * link ;
12 };
13 typedef struct node * NODE ;
14

15 NODE start = NULL ;


16 int count = 0;
17

18 NODE create ()
19 {
20 NODE snode ;
21 snode = ( NODE ) malloc ( sizeof ( struct node ) ) ;
22

23 if ( snode == NULL )
24 {
25 printf ( " \ nMemory is not available " ) ;
26 exit (1) ;
27 }
28 printf ( " \ nEnter the usn , Name , Branch , sem , PhoneNo of the
student : " ) ;
29 scanf ( " % s % s % s % d % ld " , snode -> usn , snode -> name , snode
-> branch , & snode -> sem , & snode -> phone ) ;
30 snode -> link = NULL ;
31 count ++;
32 return snode ;
33 }
34

35 NODE insertfront ()
36 {

BLDEACET, Vijayapura 26 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

37 NODE temp ;
38 temp = create () ;
39 if ( start == NULL )
40 {
41 return temp ;
42 }
43

44 temp -> link = start ;


45 return temp ;
46 }
47

48 NODE deletefront ()
49 {
50 NODE temp ;
51 if ( start == NULL )
52 {
53 printf ( " \ nLinked list is empty " ) ;
54 return NULL ;
55 }
56

57 if ( start -> link == NULL )


58 {
59 printf ( " \ nThe Student node with usn :% s is deleted " ,
start -> usn ) ;
60 count - -;
61 free ( start ) ;
62 return NULL ;
63 }
64 temp = start ;
65 start = start -> link ;
66 printf ( " \ nThe Student node with usn :% s is deleted " , temp ->
usn ) ;
67 count - -;
68 free ( temp ) ;
69 return start ;
70 }
71

72 NODE insertend ()
73 {
74 NODE cur , temp ;
75 temp = create () ;
76

77 if ( start == NULL )
78 {
79 return temp ;
80 }
81 cur = start ;
82 while ( cur -> link != NULL )
83 {
84 cur = cur -> link ;
85 }

BLDEACET, Vijayapura 27 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

86 cur -> link = temp ;


87 return start ;
88 }
89

90 NODE deleteend ()
91 {
92 NODE cur , prev ;
93 if ( start == NULL )
94 {
95 printf ( " \ nLinked List is empty " ) ;
96 return NULL ;
97 }
98

99 if ( start -> link == NULL )


100 {
101 printf ( " \ nThe student node with the usn :% s is deleted " ,
start -> usn ) ;
102 free ( start ) ;
103 count - -;
104 return NULL ;
105 }
106

107 prev = NULL ;


108 cur = start ;
109 while ( cur -> link != NULL )
110 {
111 prev = cur ;
112 cur = cur -> link ;
113 }
114

115 printf ( " \ nThe student node with the usn :% s is deleted " , cur
-> usn ) ;
116 free ( cur ) ;
117 prev -> link = NULL ;
118 count - -;
119 return start ;
120 }
121

122 void display ()


123 {
124 NODE cur ;
125 int num = 1;
126

127 if ( start == NULL )


128 {
129

130 printf ( " \ nNo Contents to display in SLL \ n " ) ;


131 return ;
132 }
133 printf ( " \ nThe contents of SLL : \ n " ) ;
134 cur = start ;

BLDEACET, Vijayapura 28 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

135 while ( cur != NULL )


136 {
137 printf ( " \ n |% d | | USN :% s | | Name :% s | | Branch :% s | | Sem :% d | |
Ph :% ld | " , num , cur -> usn , cur -> name , cur -> branch ,
cur -> sem , cur -> phone ) ;
138 cur = cur -> link ;
139 num ++;
140 }
141 printf ( " \ n No of student nodes is % d \ n " , count ) ;
142 }
143

144 void stackdemo ()


145 {
146 int ch ;
147 while (1)
148 {
149 printf ( " \n - - - - - - - - Stack Demo using SLL - - - - - - - -\ n " ) ;
150 printf ( " \ n1 : Push operation \ n2 : Pop operation \ n3 :
Display \ n4 : Exit \ n " ) ;
151 printf ( " \ nEnter your choice for stack demo : " ) ;
152 scanf ( " % d " , & ch ) ;
153

154 switch ( ch )
155 {
156 case 1:
157 start = insertfront () ;
158 break ;
159 case 2:
160 start = deletefront () ;
161 break ;
162 case 3:
163 display () ;
164 break ;
165 default :
166 return ;
167 }
168 }
169 return ;
170 }
171

172 int main ()


173 {
174 int ch , i , n ;
175 while (1)
176 {
177 printf ( " \n - - - - - - - - Menu - - - - - - - - " ) ;
178 printf ( " \ nEnter your choice for SLL operation \ n " ) ;
179 printf ( " \ n1 : Create SLL of Student Nodes " ) ;
180 printf ( " \ n2 : DisplayStatus " ) ;
181 printf ( " \ n3 : InsertAtEnd " ) ;
182 printf ( " \ n4 : DeleteAtEnd " ) ;

BLDEACET, Vijayapura 29 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

183 printf ( " \ n5 : Stack Demo using SLL ( Insertion and Deletion
at Front ) " ) ;
184 printf ( " \ n6 : Exit \ n " ) ;
185 printf ( " \ nEnter your choice : " ) ;
186 scanf ( " % d " , & ch ) ;
187

188 switch ( ch )
189 {
190 case 1:
191 printf ( " \ nEnter the no of students : " ) ;
192 scanf ( " % d " , & n ) ;
193 for ( i = 1; i <= n ; i ++)
194 start = insertfront () ;
195 break ;
196

197 case 2:
198 display () ;
199 break ;
200

201 case 3:
202 start = insertend () ;
203 break ;
204

205 case 4:
206 start = deleteend () ;
207 break ;
208

209 case 5:
210 stackdemo () ;
211 break ;
212

213 case 6:
214 exit (0) ;
215

216 default :
217 printf ( " \ nPlease enter the valid choice " ) ;
218

219 }
220 }
221 }

Sample Input/Output
--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd

BLDEACET, Vijayapura 30 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

5:Stack Demo using SLL(Insertion and Deletion at Front)


6:Exit

Enter your choice:1

Enter the no of students: 3


Enter the usn,Name,Branch, sem,PhoneNo of the student:
1ME21CS017
Braham
CSE
5
8768586443

Enter the usn,Name,Branch, sem,PhoneNo of the student:


1ME21CS015
Bikash
CSE
5
8734687996

Enter the usn,Name,Branch, sem,PhoneNo of the student:


1ME21AI015
Shoaib
AI&ML
5
6748353877

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:

|1| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|


|2| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
|3| |USN:1ME21CS017| |Name:Braham| |Branch:CSE | |Sem:5| |Ph:8768586443|
No of student nodes is 3

--------Menu--------
Enter your choice for SLL operation

BLDEACET, Vijayapura 31 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:3

Enter the usn,Name,Branch, sem,PhoneNo of the student:


1ME21CS068
Rajan
CSE
5
3426527765

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:

|1| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|


|2| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
|3| |USN:1ME21CS017| |Name:Braham| |Branch:CSE | |Sem:5| |Ph:8768586443|
|4| |USN:1ME21CS068| |Name:Rajan | |Branch:CSE | |Sem:5| |Ph:3426527765|
No of student nodes is 4

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:4


The student node with the usn:1ME21CS068 is deleted

BLDEACET, Vijayapura 32 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:

|1| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|


|2| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
|3| |USN:1ME21CS017| |Name:Braham| |Branch:CSE | |Sem:5| |Ph:8768586443|
No of student nodes is 3

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:4


The student node with the usn:1ME21CS017 is deleted

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:5


--------Stack Demo using SLL--------

1:Push operation
2: Pop operation
3: Display
4:Exit

BLDEACET, Vijayapura 33 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

Enter your choice for stack demo:1

Enter the usn,Name,Branch, sem,PhoneNo of the student:


1ME21CS005
Aman
CSE
5
6587594335

--------Stack Demo using SLL--------

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo:3


The contents of SLL:

|1| |USN:1ME21CS005| |Name:Aman | |Branch:CSE | |Sem:5| |Ph:6587594335|


|2| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|
|3| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
No of student nodes is 3

--------Stack Demo using SLL--------

1: Push operation
2: Pop operation
3: Display
4: Exit

Enter your choice for stack demo:1

Enter the usn,Name,Branch, sem,PhoneNo of the student:


1ME21CS092
Shubham
CSE
5
9869754354

--------Stack Demo using SLL--------


1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo:3

BLDEACET, Vijayapura 34 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

The contents of SLL:

|1| |USN:1ME21CS092| |Name:Shubham| |Branch:CSE | |Sem:5| |Ph:9869754354|


|2| |USN:1ME21CS005| |Name:Aman | |Branch:CSE | |Sem:5| |Ph:6587594335|
|3| |USN:1ME21AI015| |Name:Shoaib | |Branch:AI&ML| |Sem:5| |Ph:6748353877|
|4| |USN:1ME21CS015| |Name:Bikash | |Branch:CSE | |Sem:5| |Ph:8734687996|
No of student nodes is 4

--------Stack Demo using SLL--------

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo:2


The Student node with usn:1ME21CS092 is deleted

--------Stack Demo using SLL--------

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo:3


The contents of SLL:

|1| |USN:1ME21CS005| |Name:Aman | |Branch:CSE | |Sem:5| |Ph:6587594335|


|2| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|
|3| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
No of student nodes is 3

--------Stack Demo using SLL--------

1: Push operation
2: Pop operation
3: Display
4: Exit

Enter your choice for stack demo:4

--------Menu--------
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd

BLDEACET, Vijayapura 35 Instructor: Mrs:H.N.Ingaleshwar


Data Structures Lab Manual Dept. of CSE

4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:6

BLDEACET, Vijayapura 36 Instructor: Mrs:H.N.Ingaleshwar

You might also like