DSA Lab Manual
DSA Lab Manual
Prepared by
Mrs. H.N.Ingaleshwar
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
5 struct Day {
6 char * dayName ;
7 int date ;
8 char * activity ;
9 };
10
50 int main () {
51 int size ;
52 printf ( " Enter the number of days in the week : " ) ;
53 scanf ( " % d " , & size ) ;
54
57 if ( calendar == NULL ) {
58 printf ( " Memory allocation failed . Exiting program .\ n " ) ;
59 return 1;
60 }
61
68 return 0;
69 }
Sample Input/Output
Enter the number of days in the week: 7
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
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
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
Result
The Pattern Matching was successfully implemented using arrays.
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
7 # define MAX 3
8
9 int s [ MAX ];
10 int top = -1;
11
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 : " ) ;
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 ;
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
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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------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
-----------Menu----------- :
-----------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
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
-----------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
-----------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
-----------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
-----------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
-----------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
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
5 void evaluate () ;
6 void push ( char ) ;
7 char pop () ;
8 int prec ( char ) ;
9
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 ;
26
27 push ( ’# ’) ;
28
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
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
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
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
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 ’) ;
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
Result = 17
5.B) Develop a Program in C for the following Stack Applications. Solving Tower of
Hanoi problem with n disks.
Program Code
Sample Input/Output
OUTPUT 1:
Enter the number of discs: 1
OUTPUT 2:
Enter the number of discs: 2
Program Code
5 # define MAX 5
6
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
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
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 " ) ;
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
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
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
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 {
37 NODE temp ;
38 temp = create () ;
39 if ( start == NULL )
40 {
41 return temp ;
42 }
43
48 NODE deletefront ()
49 {
50 NODE temp ;
51 if ( start == NULL )
52 {
53 printf ( " \ nLinked list is empty " ) ;
54 return NULL ;
55 }
56
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 }
90 NODE deleteend ()
91 {
92 NODE cur , prev ;
93 if ( start == NULL )
94 {
95 printf ( " \ nLinked List is empty " ) ;
96 return NULL ;
97 }
98
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
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
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
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
1:Push operation
2: Pop operation
3: Display
4:Exit
1:Push operation
2: Pop operation
3: Display
4:Exit
1: Push operation
2: Pop operation
3: Display
4: Exit
1:Push operation
2: Pop operation
3: Display
4:Exit
1:Push operation
2: Pop operation
3: Display
4:Exit
1: Push operation
2: Pop operation
3: Display
4: Exit
--------Menu--------
Enter your choice for SLL operation
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit