You are on page 1of 213

MUMBAI EDUCATIONAL TRUST

MET Institute of Computer Science

Data Structures Lab


(JOURNAL)

NAME: SHARVARI BIRAJDAR


ROLL NO: 1409
CLASS: FY MCA (2023-25)

1|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Data Structures Lab – Batch B1

Unit 1 Sorting Date


1. Bubble Sort 01.09.2023
2. Selection Sort 04.09.2023
3. Insertion Sort 09.09.2023
4. Radix Sort 11.09.2023
5. Shell Sort 16.09.2023

Unit 2 Searching Date


6. Linear Search 25.08.2023
7. Binary Search 28.08.2023

Unit 2 Hashing Date


8. Modulo Division with Linear Probe 11.12.2023
9. Digit Extraction with Linear Probe 25.12.2023
10. Fold Shift with Linear Probe 25.12.2023
11. Fold Boundary with Linear Probe 25.12.2023

Unit 3 Stacks Date


12. Array Based Stack 30.10.2023
13. List Based Stack 04.11.2023
14. Evaluation of Postfix Expression using Stacks 06.11.2023
15. Balancing of Parenthesis using Stacks 09.11.2023

2|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit 4 Queues Date


16. Ordinary Queue 09.11.2023
17. Circular Queue 25.11.2023
18. Priority Queue 04.12.2023
19. Double-Ended Queue 02.12.2023

Unit 5 Linked Lists Date


20. Singly Linked List 18.09.2023
30.09.2023
21. Circular Linked List 09.10.2023
22. Doubly Linked List 14.10.2023
16.10.2023
23. Polynomial Addition using Lists 21.10.2023
28.10.2023

Unit 6 Trees Date


24. Binary Search Trees 05.12.2023
25. Max-Heaps 11.12.2023
26. Min-Heaps 25.12.2023

Unit 7 Graphs Date


27. Representation of Graphs using Adjacency Matrix 07.12.2023
28. Perform Breadth-First Traversal on a Graph using Queues 07.12.2023
29. Find MST using Kruskal’s Algorithm 09.12.2023

3|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit-1 : Sorting
Program No: 1.
Roll No : 1409
Title of Program : Bubble Sort
Objective : Unit-1: Sorting

Date: 01.09.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 1: Sorting
4 Program: Bubble Sort*/
5
6 #include<iostream>
7
8 using namespace std;
9
10 int main()
11 {
12 //Variables
13 int A[20], n;
14 int i,j,temp=0;
15
16 cout << "*** Bubble Sort ***" << endl << endl;
17
18 cout << "Enter the size of the array: ";
19 cin >> n;
20
21 cout <<"Enter " << n << " elements: " << endl;
22 for(i=0; i<n; i++)
23 {
24 cin >> A[i];
25 }
26
27 //Bubble Sort
28 for(i=0 ; i<n-1; i++)
29 {
30 for(j=i+1; j<n; j++)
4|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 {
32 if(A[i] > A[j])
33 {
34 temp = A[i];
35 A[i] = A[j];
36 A[j] = temp;
37 }
38 }//end of for j
39 }//end of for i
40
41 //Display the sorted array
42 cout << "After Bubble Sort: ";
43 for(i=0 ; i<n; i++)
44 {
45 cout << A[i] << " ";
46 }
47
48 }//end of main
49

Output:

5|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 2.
Roll No : 1409
Title of Program : Selection Sort
Objective : Unit-1: Sorting

Date: 04.09.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 1: Sorting
4 Program: Selection Sort*/
5
6 #include<iostream>
7
8 using namespace std;
9
10 int main()
11 {
12 //Variables
13 int a[20], n,temp,loc;
14 int i,j;
15 int min;
16
17 cout << "*** Selection Sort ***" << endl << endl;
18
19 cout << "Enter the size of the array: ";
20 cin >> n;
21
22 cout << "Enter " << n <<" values in the array: " << endl;
23 for(i=0; i<n; i++)
24 {
25 cin >> a[i];
26 }
27
28 //Selection Sort
29 for(i=0; i<n; i++)
30 {
31 min = a[i]; // assume min value is first value in array
32 loc = i; // and its location is i

6|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33
34 for(j=i+1; j<n; j++) // check through array for a smaller
35 { // value than min
36 if(a[j] < min)
37 {
38 min = a[j];
39 loc = j;
40 }
41 }//end of inner for j
42
43 //For this iteration, you have identified the min and its loc
44 temp = a[i];
45 a[i] = a[loc];
46 a[loc] = temp;
47 }//end for i
48
49 cout << endl << "Sorted Array: ";
50 for(i=0; i<n; i++)
51 {
52 cout << a[i] << " ";
53 }
54 }//end of main

Output:

7|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 3.
Roll No : 1409
Title of Program : Insertion Sort
Objective : Unit-1: Sorting

Date: 09.09.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 1: Sorting
4 Program: Insertion Sort*/
5
6 #include<iostream>
7 using namespace std;
8
9 int main()
10 {
11 //Variables
12 int a[20],n;
13 int i,temp,loc;
14
15 cout << "*** Insertion Sort ***"<< endl << endl;
16
17 cout << "Enter the size of the array: " << endl;
18 cin >> n;
19
20 cout << "Enter " <<n<<" elements in the array: " << endl;
21 for(i=0;i<n;i++)
22 {
23 cin>>a[i];
24 }
25
26 //Insertion sort
27 //i=1 as the first element is assumed to be sorted.
28 for(i=1;i<n;i++)
29 {
30 temp=a[i]; //first element of unsorted region
31 loc=i-1; //location to the left of unsorted region
32 //Start with assuming that the first element is sorted already

8|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33
34 while(temp<a[loc] && loc>=0)
35 {
36 a[loc+1]=a[loc];
37 loc--;
38 }//end of while
39
40 a[loc+1] = temp; //Write temp to its final position
41
42 }//end of for i
43
44
45 cout << "Sorted array: ";
46 for(i=0;i<n;i++)
47 {
48 cout << a[i] << " ";
49 }
50 }//end of main
51

Output:

9|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 4.
Roll No : 1409
Title of Program : Radix Sort
Objective : Unit-1: Sorting

Date: 11.09.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 1: Sorting
4 Program: Radix Sort*/
5
6 #include<iostream>
7 using namespace std;
8
9 int main()
10 {
11 //Variables
12 int a[20], n, i,j;
13 int max,r;
14 int passes=0,pass_no,divisor=1;
15 int k,bucket[10][20], b_count[10];
16
17 cout << "*** Radix Sort ***\n\n";
18
19 cout << "Enter the size of the array: " << endl;
20 cin >> n;
21
22 cout << "Enter " << n << " elements in the array: " << endl;
23 for(int i=0;i<n;i++)
24 {
25 cin>>a[i];
26 }
27
28 //Radix Sort
29
30 //1. Identify the maximum number in the array
31 max = a[0];
32 for(i=1; i<n; i++)

10 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33 {
34 if(a[i] > max)
35 {
36 max=a[i];
37 }
38 }
39
40 //2. Count digits in max
41 while(max>0)
42 {
43 passes++;
44 max = max/10;
45 }
46
47 //3. Radix Sort Process
48 for(pass_no=1; pass_no<=passes; pass_no++)
49 {
50 for(k=0; k<10; k++)//10 for 0-9 bins
51 {
52 b_count[k] = 0; //Initialize b_count to 0 to indicate elements in the
53 row,set to all 0 i.e to set empty bucket
54 }
55
56 for(i=0; i<n; i++) // Assign elements to the bin
57 {
58 //r-units position
59 //bucket- to track counter, avoid overwriting
60
61 r=(a[i]/divisor)%10;
62 bucket[r][b_count[r]] = a[i];
63 b_count[r]++;
64 }
65
66 //collecting elements from the bucket or bins
67 i=0;
68 for(k=0; k<10;k++)
69 {
70 for(j=0; j<b_count[k]; j++)
71 {
72 a[i] = bucket[k][j];
73 i++;

11 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

74 }//inner for j
75 }//outer for k
76
77 divisor=divisor*10; // to make it work for every digit
78
79 }//end of for pass_no
80
81 cout << "Sorted Array: ";
82 for(i=0; i<n;i++)
83 {
84 cout << a[i] << " ";
85 }
86
}//end of main

Output:

12 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 5.
Roll No : 1409
Title of Program : Shell Sort
Objective : Unit-1: Sorting

Date: 16.09.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 1: Sorting
4 Program: Shell Sort*/
5
6 #include<iostream>
7 #include<math.h>
8
9 using namespace std;
10
11 int main()
12 {
13 int A[20],n,i;
14 int gap,pos,extractItem;
15
16 cout << "*** Shell Sort ***" << endl << endl;
17 cout << "Enter the size of the array: ";
18 cin >> n;
19
20 cout << "Enter " << n << " elements in the array:\n";
21 for(i=0; i<n; i++)
22 {
23 cin >> A[i];
24 }
25
26 //Shell Sort
27 gap = floor(n/2);
28 while(gap>0)
29 {
30 for(i=0; i<n-gap; i++)
31 {
32 extractItem = A[i+gap];

13 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33 pos = i+gap;
34
35 while((pos-gap)>=0 && extractItem<A[pos-gap])
36 {
37 A[pos] = A[pos-gap];
38 pos = pos-gap;
39 }//end of inner while
40
41 A[pos] = extractItem;
42 }//end of i
43 gap = floor(gap/2);
44 }//end of while gap
45
46 cout << "Sorted Array: ";
47 for(i=0; i<n; i++)
48 {
49 cout << A[i] << " ";
50 }
51 }//end of main

Output:

14 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit - 2 : Searching

Program No: 6.
Roll No : 1409
Title of Program : Linear Search
Objective : Unit-2: Searching

Date: 25.08.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit: 2
4 Program: Linear Search */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 int main()
12 {
13 int num[10];
14 int target;
15 int size, i;
16 int flag=0; // Flag down - target not found
17
18 cout << "*** Linear Search ***" << endl << endl;
19
20 cout << "Enter the size of the array: ";
21 cin >> size;
22
23 cout << "Enter " << size << " values in the array: " << endl;
24 for(i=0 ; i<size ; i++)
25 {
26 cin >> num[i];
27 }
28
29 cout << "Enter the target value: ";

15 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

30 cin >> target;


31
32 //Linear Search
33 for(i=0 ; i<size ; i++)
34 {
35 if(num[i] == target)
36 {
37 flag = 1; //target is found - raise the flag
38 break;
39 }
40 }
41
42 if(flag == 0)
43 {
44 cout << target << " not found!";
45 }
46 else
47 {
48 cout << target << " found at index " << i;
49 }
50 }//end of main

Output:
(Target Found)

16 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Target Not Found)

17 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 7.
Roll No : 1409
Title of Program : Binary Search
Objective : Unit-2: Searching

Date: 28.08.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 2: Searching
4 Program: Binary Search */
5
6 #include<iostream>
7
8 using namespace std;
9
10 int main()
11 {
12 //variables
13 int num[10], size, target, first, last, mid, i, flag=0;
14
15 cout << "*** Binary Search ***" << endl << endl;
16
17 cout << "Enter the size of the array: ";
18 cin >> size;
19
20 cout << "Enter " << size << " elements in sorted order:" << endl;
21
22 //reading elements
23 for(i=0 ; i <size ; i++)
24 {
25 cin >> num[i];
26 }
27
28 cout << "Enter the target value: ";
29 cin >> target;
30
31 //Binary Search
32 first = 0;

18 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33 last = size-1;
34
35 while(first<=last)
36 {
37 mid = (first+last)/2;
38
39 if(target == num[mid])
40 {
41 flag = 1; //target is found - raise the flag
42 break;
43 }
44
45 if(target < num[mid])
46 {
47 last = mid-1;
48 }
49 else //control reaches here only if target num[mid]
50 {
51 first = mid+1;
52 }
53 }//end of while
54
55 if(flag == 0)
56 {
57 cout << target << " is not found";
58 }
59 else
60 {
61 cout << target << " is found at index " << mid;
62 }
63
64 }//end of main

19 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Output:
(Target Found)

(Target Not Found)

20 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit - 2 : Hashing

Program No: 8.
Roll No : 1409
Title of Program : Modulo Division with Linear Probe
Objective : Unit-2: Hashing

Date: 11.12.2023
Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 2: Hashing
4 Program: Modulo division hashing with linear probe for collision*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 10
10 #define EMPTY -1
11
12 using namespace std;
13
14 /*----------1. Node Template -Not required----------*/
15
16
17 /*----------2.Array Based Template for Hash Table--------*/
18 class Hash
19 {
20 int h[MAX];
21
22 public:
23 Hash()
24 {
25 int i;
26 for(i=0; i<MAX; i++)
27 {
28 h[i] = EMPTY;
29 }
30 }
31
21 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

32 void Insert(int key);


33 void Display();
34 void Search(int skey);
35 };
36
37 /*----------------3.Functions------------------*/
38
39 /*----------------Insert------------------*/
40 void Hash::Insert(int key)
41 {
42 //HASH FNC
43 int addr = key % MAX; //Modulo Division Hash Function
44
45 int oaddr = addr; ///saving original address
46
47 if(h[addr] == EMPTY ) //addr is available
48 {
49 h[addr] = key;
50 cout << key << " is inserted at address " << addr << endl;
51 }
52 else
53 {
54 //Collision
55 do
56 {
57 addr = (addr+1) % MAX;
58 if(addr == oaddr)
59 {
60 cout << "Hash Table is Full!";
61 return;
62 }
63 }
64 while(h[addr]!=EMPTY);
65
66 h[addr] = key;
67 cout << key << " is inserted at address " << addr << endl;
68
69 }//end of if-else
70
71 }//end of Insert
72

22 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

73 /*----------------Display------------------*/
74 void Hash::Display()
75 {
76 int i;
77
78 for(i=0;i<MAX;i++)
79 {
80 cout << "h[" << i << "] = ";
81 if(h[i] == EMPTY)
82 {
83 cout << "EMPTY" << endl;
84 }
85 else
86 {
87 cout << h[i] << endl;
88 }
89 }
90 }//end of Display
91
92
93 /*----------------Search------------------*/
94 void Hash::Search(int skey)
95 {
96 int saddr = skey%MAX;
97 int oaddr = saddr;
98
99 if((h[saddr]) == skey)
100 {
101 cout << "Key: " << skey << " is found at address " << saddr << endl;
102 }
103 else
104 {
105 do
106 {
107 saddr = (saddr+1) % MAX;
108 if(saddr == oaddr)
109 {
110 break;
111 }
112 }
113 while(h[saddr]!=skey);

23 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

114
115 if((h[saddr]) == skey)
116 {
117 cout << "Key: " << skey << " is found at address " << saddr << endl;
118 }
119 else
120 {
121 cout << "Key: " << skey << " is not found" << endl;
122 }
123 }
124
125 }//end of Search
126
127 /*------------------4.Menu--------------*/
128 int main()
129 {
130 int ch,num;
131 Hash h;
132
133 while(1)
134 {
135 system("cls");
136 cout << "*** Hashing with Modulo Division & Linear Probe ***\n\n";
137
138 cout << "1.Insert the value on the Hash Table\n";
139 cout << "2.Display the Hash Table\n";
140 cout << "3.Search for a key in the Hash Table\n";
141 cout << "4.Exit\n";
142
143 cout << "Enter your choice: ";
144 cin >> ch;
145
146 switch(ch)
147 {
148
149 case 1:
150 cout << "Enter the key: ";
151 cin >> num;
152 h.Insert(num);
153 getch();
154 break;

24 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

155
156 case 2:
157 h.Display();
158 getch();
159 break;
160
161 case 3:
162 cout << "Enter the element to be searched ";
163 cin >> num;
164 h.Search(num);
165 getch();
166 break;
167
168 case 4:
169 exit(1);
170
171 default:
172 cout << "Incorrect Choice!";
173 getch();
174 }//end of switlch
175
176 }//end of while
177
178 }//end of main

Output:

25 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

26 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

27 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 9.
Roll No : 1409
Title of Program : Digit Extraction with Linear Probe
Objective : Unit-2: Hashing

Date: 25.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 2: Hashing
4 Program: Digit Extraction Hashing with linear probe for collision*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 4
10 #define EMPTY -1
11
12 using namespace std;
13
14 /*----------1. Node Template -Not required----------*/
15
16
17 /*----------2.Array Based Template for Hash Table--------*/
18 class Hash
19 {
20 int h[MAX];
21
22 public:
23 Hash()
24 {
25 int i;
26 for(i=0; i<MAX; i++)
27 {
28 h[i] = EMPTY;
29 }
30 }
31
32 void Insert(int key);

28 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33 void Display();
34 void Search(int skey);
35 int DigitExtractionHash(int key);
36 };
37
38 /*----------------3.Functions------------------*/
39
40 /*----------------Insert------------------*/
41 void Hash::Insert(int key)
42 {
43 //Digit Extraction Hash Function
44 int addr = DigitExtractionHash(key);
45
46 int oaddr = addr; //saving original address
47
48 if(h[addr] == EMPTY ) //addr is available
49 {
50 h[addr] = key;
51 cout << key << " is inserted at address " << addr << endl;
52 }
53 else
54 {
55 //Collision
56 do
57 {
58 addr = (addr+1) % MAX;
59 if(addr == oaddr)
60 {
61 cout << "Hash Table is Full!";
62 return;
63 }
64 }
65 while(h[addr]!=EMPTY);
66
67 h[addr] = key;
68 cout << key << " is inserted at address " << addr << endl;
69
70 }//end of if-else
71
72 }//end of Insert
73

29 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

74 /*----------------Display------------------*/
75 void Hash::Display()
76 {
77 int i;
78
79 for(i=0;i<MAX;i++)
80 {
81 cout << "h[" << i << "] = ";
82 if(h[i] == EMPTY)
83 {
84 cout << "EMPTY" << endl;
85 }
86 else
87 {
88 cout << h[i] << endl;
89 }
90 }
91 }//end of Display
92
93
94 /*----------------Search------------------*/
95 void Hash::Search(int skey)
96 {
97 int saddr = DigitExtractionHash(skey);
98 int oaddr = saddr;
99
100 if((h[saddr]) == skey)
101 {
102 cout << "Key: " << skey << " is found at address " << saddr << endl;
103 }
104 else
105 {
106 do
107 {
108 saddr = (saddr+1) % MAX;
109 if(saddr == oaddr)
110 {
111 break;
112 }
113 }
114 while(h[saddr]!=skey);

30 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

115
116 if((h[saddr]) == skey)
117 {
118 cout << "Key: " << skey << " is found at address " << saddr << endl;
119 }
120 else
121 {
122 cout << "Key: " << skey << " is not found" << endl;
123 }
124 }
125
126 }//end of Search
127
128 /*-----------DigitExtractionHash---------------*/
129 int Hash::DigitExtractionHash(int key)
130 {
131 //Digit Extraction Hash Function: Sum of the digits
132 int sum=0;
133 while(key>0)
134 {
135 sum = sum + (key%10);
136 key = key/10;
137
138 }//end of while
139
140 return (sum % MAX);
141
142 }//end ofDigitExtractionHash
143
144
145 /*------------------4.Menu--------------*/
146 int main()
147 {
148 int ch,num;
149 Hash h;
150
151 while(1)
152 {
153 system("cls");
154 cout << "*** Hashing with Digit Extraction & Linear Probe ***\n\n";
155

31 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

156 cout << "1.Insert the value on the Hash Table\n";


157 cout << "2.Display the Hash Table\n";
158 cout << "3.Search for a key in the Hash Table\n";
159 cout << "4.Exit\n";
160
161 cout << "Enter your choice: ";
162 cin >> ch;
163
164 switch(ch)
165 {
166
167 case 1:
168 cout << "Enter the key: ";
169 cin >> num;
170 h.Insert(num);
171 getch();
172 break;
173
174 case 2:
175 h.Display();
176 getch();
177 break;
178
179 case 3:
180 cout << "Enter the element to be searched ";
181 cin >> num;
182 h.Search(num);
183 getch();
184 break;
185
186 case 4:
187 exit(1);
188
189 default:
190 cout << "Incorrect Choice!";
191 getch();
192 }//end of switlch
193
194 }//end of while
195
196 }//end of main

32 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Output:

(Insertion)

33 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display)

(Target Found)

34 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Target Not Found)

35 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 10.


Roll No : 1409
Title of Program : Fold Shift with Linear Probe
Objective : Unit-2: Hashing

Date: 25.12.2023
Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 2: Hashing
4 Program: Fold Shift Hashing with linear probe for collision*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 19
10 #define EMPTY -1
11
12
13 using namespace std;
14
15 /*----------1. Node Template -Not required----------*/
16
17
18 /*----------2.Array Based Template for Hash Table--------*/
19 class Hash
20 {
21 int h[MAX];
22
23 public:
24 Hash()
25 {
26 for (int i = 0; i < MAX; i++)
27 {
28 h[i] = EMPTY;
29 }
30 }
31
32 void Insert(int key);
33 void Display();

36 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

34 void Search(int skey);


35 int FoldShiftHash(int key);
36 };
37
38
39 /*----------------3.Functions------------------*/
40
41 /*----------------Insert------------------*/
42 void Hash::Insert(int key)
43 {
44 // Fold Shift Hash Function
45 int addr = FoldShiftHash(key);
46
47 int oaddr = addr; // saving original address
48
49 if (h[addr] == EMPTY)
50 {
51 h[addr] = key;
52 cout << key << " is inserted at address " << addr << endl;
53 }
54 else
55 {
56 // Collision
57 do
58 {
59 addr = (addr + 1) % MAX;
60 if (addr == oaddr)
61 {
62 cout << "Hash Table is Full!";
63 return;
64 }
65 } while (h[addr] != EMPTY);
66
67 h[addr] = key;
68 cout << key << " is inserted at address " << addr << endl;
69 }//end of if-else
70
71 }//end of Insert
72
73
74 /*----------------Display------------------*/
void Hash::Display()
37 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

75 {
76 int i;
77
78 for (i = 0; i < MAX; i++)
79 {
80 cout << "h[" << i << "] = ";
81 if (h[i] == EMPTY)
82 {
83 cout << "EMPTY" << endl;
84 }
85 else
86 {
87 cout << h[i] << endl;
88 }
89 }
90 }//end of Display
91
92
93 /*----------------Search------------------*/
94
95 void Hash::Search(int skey)
96 {
97 int saddr = FoldShiftHash(skey);
98 int oaddr = saddr;
99
100 if ((h[saddr]) == skey)
101 {
102 cout << "Key: " << skey << " is found at address " << saddr << endl;
103 }
104 else
105 {
106 do {
107 saddr = (saddr + 1) % MAX;
108
109 if (saddr == oaddr)
110 {
111 break;
112 }
113 } while (h[saddr] != skey);
114
115 if ((h[saddr]) == skey)

38 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

116 {
117 cout << "Key: " << skey << " is found at address " << saddr << endl;
118 }
119 else
120 {
121 cout << "Key: " << skey << " is not found" << endl;
122 }
123 }
124 }//end of search
125
126
127 /*------------FoldShiftHash---------------*/
128
129 int Hash::FoldShiftHash(int key)
130 {
131 // Fold Shift Hash Function:
132 // Sum two digits at a time, discard MSB if three digits
133 int sum = 0;
134 int tempKey = key;
135
136 while (tempKey > 0)
137 {
138 // Extract the last two digits
139 int twoDigits = (tempKey % 100);
140 sum += twoDigits;
141
142 // If sum becomes a three-digit number, discard the MSB
143 if (sum >= 100)
144 {
145 sum %= 100;
146 }
147
148 tempKey /= 100; // Move to the next two digits
149 }
150
151 return (sum % MAX);
152 }//end of FoldShiftHash
153
154
155 /*------------------4.Menu--------------*/
156 int main()
{
39 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

157 int ch, num;


158 Hash h;
159
160 while (1)
161 {
162 system("cls");
163 cout << "*** Hashing with Fold Shift & Linear Probe ***\n\n";
164
165 cout << "1.Insert the value on the Hash Table\n";
166 cout << "2.Display the Hash Table\n";
167 cout << "3.Search for a key in the Hash Table\n";
168 cout << "4.Exit\n";
169
170 cout << "Enter your choice: ";
171 cin >> ch;
172
173 switch (ch)
174 {
175 case 1:
176 cout << "Enter the key: ";
177 cin >> num;
178 h.Insert(num);
179 getch();
180 break;
181
182 case 2:
183 h.Display();
184 getch();
185 break;
186
187 case 3:
188 cout << "Enter the element to be searched: ";
189 cin >> num;
190 h.Search(num);
191 getch();
192 break;
193
194 case 4:
195 exit(1);
196
197 default:

40 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

198 cout << "Incorrect Choice!";


199 getch();
200 }//end of switch
201 }//end of while
202
203 return 0;
204 }//end of main
205
206
207

Output:

(Insertion)

41 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display)

42 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Target Found)

43 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Target Not Found)

44 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 11.


Roll No : 1409
Title of Program : Fold Boundary with Linear Probe
Objective : Unit-2: Hashing

Date: 25.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 2: Hashing
4 Program: Fold Boundary Hashing with linear probe for collision*/
5
6 #define MAX 19
7 #define EMPTY -1
8
9 #include<iostream>
10 #include<conio.h>
11 using namespace std;
12
13 //1. Node template - Not required
14
15
16 //2. Array based template for hash table
17 class Hash
18 {
19 int h[MAX];
20
21 public:
22 Hash()
23 {
24 for (int i=0; i<MAX; i++)
25 {
26 h[i] = EMPTY;
27 }
28 }
29 void Insert(int key);
30 void Display();
31 void Search(int key);
32 };

45 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33
34
35 //3. Function
36 //reverse Function
37 int revNum(int num)
38 {
39 int rev = 0;
40
41 if (num < 10)
42 {
43 rev = num * 10;
44 return rev;
45 }
46 while (num != 0)
47 {
48 int digit = num % 10;
49 rev = rev * 10 + digit;
50 num /= 10;
51 }
52
53 return rev;
54 }//reverse ends here
55
56
57 //-----------------Insert-------------------------
58 void Hash :: Insert(int key)
59 {
60 //Hash fnc
61 int addr = (revNum((key / 10000) % 100) + ((key / 100) % 100) +
62 revNum(key%100)) % 100;
63 int oaddr = addr;
64
65 if (h[addr] == EMPTY) //addr is available
66 {
67 h[addr] = key;
68 cout<<key<<" is inserted at the address: "<<addr<<endl;
69 }
70 else{
71 //Colliison
72 do
73 {

46 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

74 addr = (addr+1) % MAX;


75
76 if (addr == oaddr)
77 {
78 cout<<"Hash table is Full";
79 return;
80 }
81 }while(h[addr] != EMPTY);
82
83 h[addr] = key;
84
85 cout<<key<<" is inserted at the address: "<<addr<<endl;
86 }// end of if-else
87 }//endo of insert
88
89
90 //-----------------Display-------------------------
91 void Hash :: Display()
92 {
93 for (int i=0; i<MAX; i++)
94 {
95 if (h[i] == EMPTY)
96 {
97 cout<<"arr["<<i<<"]: "<<"EMPTY"<<endl;
98 continue;
99 }
100 cout<<"arr["<<i<<"]: "<<h[i]<<endl;
101 }
102 }//end of Display
103
104
105 //-----------------Search-------------------------
106 void Hash :: Search(int key)
107 {
108 int saddr = key%MAX;
109 int oaddr = saddr;
110 int i;
111 if (h[saddr] == key)
112 {
113 cout<<"key found at address: "<<saddr<<endl;
114 } else

47 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

115 {
116 do
117 {
118 saddr = (saddr+1) % MAX;
119 if (saddr == oaddr)
120 {
121 break;
122 }
123 }while(h[saddr] != key);
124
125 if (h[saddr] == key)
126 {
127 cout<<"Key found at address: "<<saddr<<endl;
128 } else
129 {
130 cout<<"Key not found"<<endl;
131 }
132 }
133 }
134
135
136 //4. Main Menu
137 int main()
138 {
139 Hash h;
140 int choice, num;
141
142 while(true)
143 {
144 system("cls");
145 cout<<"*** Hashing - Fold Bundary & Linear Probe ***"<<endl;
146 cout<<"1. Insert"<<endl;
147 cout<<"2. Display"<<endl;
148 cout<<"3. Search"<<endl;
149 cout<<"4. Exit"<<endl;
150
151 cout<<"Enter your choice: ";
152 cin>>choice;
153
154 switch(choice)
155 {

48 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

156 case 1:
157 cout<<"Enter Key: ";
158 cin>>num;
159 h.Insert(num);
160 getch();
161 break;
162
163 case 2:
164 h.Display();
165 getch();
166 break;
167
168 case 3:
169 cout<<"Enter key to Search: ";
170 cin>>num;
171 h.Search(num);
172 getch();
173 break;
174
175 case 4:
176 exit(true);
177
178 default:
179 cout<<"Incorrect choice:(";
180 getch();
181 break;
182 } // end of switch
} // end of while
} // end of main

Output:

49 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

50 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

51 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

52 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit - 3 : Stacks
Program No: 12.
Roll No : 1409
Title of Program : Array Based Stack
Objective : Unit-3: Stacks

Date: 30.10.2023

Source Code:

1 /*Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 3: Stacks
4 Program: Array-Based Stack */
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define SIZE 5 // Declaring a constant SIZE
10 using namespace std;
11
12 /*----------1. Node Template - NOT REQUIRED FOR ARRAY BASED STRUCTURE------
13 --*/
14
15 /*----------2. Array Based Template--------*/
16 class AStack
17 {
18 int A[SIZE];
19 int tos;
20
21 public:
22 AStack()
23 {
24 tos = -1; //Empty Stack
25 }
26
27 void Push(int x);
28 void Pop();
29 void Peek();
30 void Display();

53 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 int Full();
32 int Empty();
33
34 };
35
36
37 /*---------------3.Functions--------------*/
38
39 /*----------------FULL()---------------*/
40 int AStack :: Full()
41 {
42 if(tos == SIZE-1)
43 {
44 return 1;
45 }
46 else
47 {
48 return 0;
49 }
50 }//end of Full
51
52
53 /*----------------Empty()---------------*/
54 int AStack :: Empty()
55 {
56 if(tos == -1)
57 {
58 return 1;
59 }
60 else
61 {
62 return 0;
63 }
64
65 }//end of Empty
66
67
68 /*----------------Push()---------------*/
69 void AStack :: Push(int x)
70 {
71 if(Full())

54 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 {
73 cout << "Stack Overflow";
74 return;
75 }
76
77 tos++;
78 A[tos] = x;
79 cout << "Element inserted successfully!";
80
81 }//end of Push
82
83
84 /*----------------Peek()---------------*/
85 void AStack :: Peek()
86 {
87 if(Empty())
88 {
89 cout << "Stack Underflow!";
90 return;
91 }
92
93 cout << "Element at the TOS is: " << A[tos];
94
95 }//end of Peek
96
97
98 /*----------------Display()---------------*/
99 void AStack :: Display()
100 {
101 if(Empty())
102 {
103 cout << "Stack underflow!";
104 return;
105 }
106
107 int i;
108 cout<<"The Stack is: \n";
109 for(i=tos;i>=0;i--)
110 {
111 cout << A[i] <<endl;
112 }

55 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113
114 }//end of Display
115
116 /*----------------Pop()---------------*/
117 void AStack :: Pop()
118 {
119 if(Empty())
120 {
121 cout << "Stack Underflow !!!";
122 return;
123 }
124
125 int temp = A[tos];
126 tos--;
127
128 cout << "Element popped is: " << temp;
129
130 }//end of pop
131
132
133
134 /*---------------4.Menu--------------*/
135
136 int main()
137 {
138 AStack s;
139 int ch,num;
140
141 while(1)
142 {
143 system("cls");
144
145 cout << "*** Array-Based Stack ***\n\n";
146
147 cout << "1. Push an element on the stack\n";
148 cout << "2. Pop the element at the TOS\n";
149 cout << "3. Peek at the element on the TOS\n";
150 cout << "4. Display the stack\n";
151 cout << "5. Exit\n\n";
152
153 cout<<"Enter your choice: ";

56 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 cin>>ch;
155
156 switch(ch)
157 {
158 case 1:
159 cout<<"Enter the element: ";
160 cin>>num;
161 s.Push(num);
162
163 getch();
164 break;
165
166 case 2:
167 s.Pop();
168 getch();
169 break;
170
171 case 3:
172 s.Peek();
173 getch();
174 break;
175
176 case 4:
177 s.Display();
178 getch();
179 break;
180
181 case 5:
182 exit(1);
183
184 default:
185 cout<<"Incorrect choice!!!";
186 getch();
187
188 }//end of switch
189
190 }//end of while
191 }//end of main

57 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Output:
(Push)

(Pop)

58 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek at Top of Stack)

(Display)

59 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

60 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 13.


Roll No : 1409
Title of Program : List Based Stack
Objective : Unit-3: Stacks

Date: 04.11.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no:1409
3 Unit 3: Stacks
4 Program: List-Based Stack */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 /*----------------1.Node Template------------------------*/
12 class SNode
13 {
14 public:
15 int data;
16 SNode *next;
17 };
18
19 /*-------------------2.List-Based Template--------------------*/
20 class LStack
21 {
22 SNode *tos;
23
24 public:
25 LStack()
26 {
27 tos = NULL;
28 }
29
30 void Push(int x);
31 void Pop();
32 void Peek();

61 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33 void Display();
34 int Empty();
35 };
36
37
38 /*----------------------3.Functions----------------------*/
39 /*-----------------Push()---------------------*/
40 void LStack :: Push(int x)
41 {
42 //Make a new node t using x
43 SNode *t = new SNode;
44 t->data = x;
45 t->next = NULL;
46
47 //First Node to the stack
48 if(tos == NULL)
49 {
50 tos=t;
51 }
52 else //Attach t to the start of the stack
53 {
54 t->next = tos;
55 tos = t;
56 }
57 }//end of Push
58
59
60 /*----------------Empty()---------------*/
61 int LStack :: Empty()
62 {
63 if(tos == NULL)
64 {
65 return 1;
66 }
67 else
68 {
69 return 0;
70 }
71
72 }//end of Empty
73

62 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

74 /*------------------Pop()-------------------*/
75 void LStack :: Pop()
76 {
77 //check it is empty
78 if(Empty())
79 {
80 cout<<"Stack Underflow";
81 return;
82 }
83
84 //otherwise pop the element
85 SNode *temp=tos; //set a temp pointer to tos
86 tos = tos->next; //Shift tos to the next node - goes to NULL for single node
87 cout<<"Element popped: "<<temp->data; //Display the popped element
88 delete temp;
89
90 }//end of Pop
91
92
93 /*------------------Peek()-------------------*/
94 void LStack :: Peek()
95 {
96 //check it is empty
97 if(Empty())
98 {
99 cout<<"Stack Underflow";
100 return;
101 }
102
103 cout<<"Element at the TOS is: "<<tos->data;
104
105 }//end of peek
106
107 /*------------------Display()-------------------*/
108 void LStack :: Display()
109 {
110 //check it is empty
111 if(Empty())
112 {
113 cout<<"Stack Underflow";
114 return;

63 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

115 }
116
117 SNode *temp=tos;
118
119 while(temp!=NULL)
120 {
121 cout << "|"<< temp->data << "| " << endl;
122 cout<<"----"<< ;
123 temp=temp->next;
124 }
125
126 }//end of Display
127
128
129 /*------------------------4.Main Menu----------------------*/
130 int main()
131 {
132 LStack s;
133 int num,choice;
134 while(1)
135 {
136 system("cls");
137 cout << "*** List-Based Stack***\n\n";
138
139 cout << "1. Push an element in the stack\n";
140 cout << "2. Pop the element at the tos\n";
141 cout << "3. Peek at the stack\n";
142 cout << "4. Display the stack\n";
143 cout << "5. Exit\n\n";
144
145 cout << "Enter your choice: ";
146 cin >> choice;
147
148 switch(choice)
149 {
150 case 1:
151 cout<<"Enter the element: ";
152 cin>>num;
153 s.Push(num);
154 getch();
155 break;

64 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

156 case 2:
157 s.Pop();
158 getch();
159 break;
160 case 3:
161 s.Peek();
162 getch();
163 break;
164 case 4:
165 s.Display();
166 getch();
167 break;
168 case 5:
169 exit(1);
170 default:
171 cout<<"Incorrect Choice!";
172 getch();
173 }//end of switch
174
175 }//end of while
176
177 }//end of main

Output:
(Push)

65 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek)

(Display)

66 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 14.


Roll No : 1409
Title of Program : Evaluation of Postfix Expression using Stacks
Objective : Unit-3: Stacks

Date: 06.11.2023

Source Code:

(F_Stack.cpp) :
1 /* Name: Sharvari Birajdar
2 Roll no: 1409
3 Unit 3: Stacks
4 Program: Stack Template for Evaluation of Postfix Expression */
5
6 #include<iostream>
7 #include<conio.h>
8 #include<ctype.h>
9
10 using namespace std;
11 #define SIZE 20
12
13 /*Create a Stack*/
14 class Stack
15 {
16 float A[SIZE];
17 int tos;
18
19 public:
20 Stack()
21 {
22 tos=-1;
23 }
24
25 void Push(float x);
26 float Pop();
27 int Full();
28 int Empty();
29 };
30
31 /*-----------------Full()---------------------*/
67 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

32 int Stack :: Full()


33 {
34 if(tos==SIZE-1)
35 {
36 return 1;
37 }
38 else
39 {
40 return 0;
41 }
42 }//end of Full
43
44 /*-----------------Empty()---------------------*/
45 int Stack :: Empty()
46 {
47 if(tos==-1)
48 {
49 return 1;
50 }
51 else
52 {
53 return 0;
54 }
55 }//end of Empty
56
57
58 /*-----------------Push()---------------------*/
59 void Stack :: Push(float x)
60 {
61 if(Full())
62 {
63 cout<<"Stack Overflow";
64 return;
65 }
66 tos++;
67 A[tos]=x;
68 }//end of Push
69
70 /*-----------------Pop()---------------------*/
71 float Stack :: Pop()
72 {

68 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

73 if(Empty())
74 {
75 cout<<"Stack Underflow";
76 return -1 ;
77 }
78 float temp = A[tos];
79 tos--;
80 return temp;
81
82 }//end of Push
83

(Evaluation_Of_Postfix.cpp):
1 /* Name: Sharvari Birajdar
2 Roll no: 1409
3 Unit 3: Stacks
4 Program: Evaluation of Postfix Expression */
5
6 #include"F_Stack.cpp"
7
8 int main()
9 {
10 char postfix[20];
11 Stack s;
12 float result,n1,n2;
13 int i=0;
14
15 cout<<"*** Evaluation of Postfix Expression ***\n\n";
16
17 cout<<"Enter a postfix expression: ";
18 cin>>postfix;
19
20 while(postfix[i]!='\0')
21 {
22 if(postfix[i] == ' ')
23 {
24 i++;
25 continue;
26 }
27

69 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

28 if(isdigit(postfix[i]))
29 {
30 float x;
31 x = postfix[i] - 48.0;
32 s.Push(x);
33 }
34 else //Operator
35 {
36 n2 = s.Pop();
37 n1 = s.Pop();
38
39 switch(postfix[i])
40 {
41 case '+':
42 result = n1 + n2;
43 break;
44 case '-':
45 result = n1 - n2;
46 break;
47 case '*':
48 result = n1 * n2;
49 break;
50 case '/':
51 result = n1 / n2;
52 break;
53 }//end of switch
54
55 s.Push(result);
56
57 }//end of postfix[i] is an operator
58
59 i++;
60
61 }//end of while
62
63 cout << "Result of postfix evaluation: "<<s.Pop();
64
65 }//end of main

70 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Output:

71 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 15.


Roll No : 1409
Title of Program : Balancing of Parenthesis using Stacks
Objective : Unit-3: Stacks

Date: 09.11.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no:1409
3 Unit 5: Stacks
4 Program: Balancing of Parenthesis*/
5
6 #include<iostream>
7
8 using namespace std;
9 #define SIZE 20
10
11 /* Create a stack */
12 class Stack
13 {
14 char A[SIZE];
15 int tos;
16
17 public:
18 Stack()
19 {
20 tos=-1;
21 }
22
23 void Push(char x);
24 char Pop();
25 int Full();
26 int Empty();
27 };
28
29 /*-----------------Push()---------------------*/
30 void Stack :: Push(char x)
31 {
32 if(Full())

72 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

33 {
34 cout<<"Stack Overflow";
35 return;
36 }
37
38 tos++;
39 A[tos] = x;
40 }//end of Push
41
42 /*-----------------Pop()---------------------*/
43 char Stack :: Pop()
44 {
45 if(Empty())
46 {
47 cout<<"Stack Undeflow";
48 return -1;
49 }
50
51 char temp = A[tos];
52 tos--;
53 return temp;
54
55 }//end of Pop
56
57 /*-----------------Full()---------------------*/
58 int Stack :: Full()
59 {
60 if(tos== SIZE-1)
61 {
62 return 1;
63 }
64 else
65 {
66 return 0;
67 }
68 }//end of Full
69
70 /*-----------------Empty()---------------------*/
71 int Stack :: Empty()
72 {
73 if(tos==-1)

73 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

74 {
75 return 1;
76 }
77 else
78 {
79 return 0;
80 }
81 }//end of Empty
82
83
84
85 int main()
86 {
87 char expr[20]; //manage exp
88 char temp; //to hold
89 int i=0,flag=0;
90 Stack s;
91
92 cout << "*** Balancing of Parenthesis using a Stack ***\n\n";
93
94 cout << "Enter an expression: ";
95 cin>>expr;
96
97 while(expr[i] != '\0')
98 {
99 if(expr[i] == '[' || expr[i]=='(' || expr[i]=='{')
100 {
101 s.Push(expr[i]);
102 }//end of if opening bracket
103
104 if(expr[i]==']' || expr[i]==')' || expr[i]=='}')
105 {
106 if(s.Empty()) //when extra close bracket
107 {
108 flag=1;
109 break;
110 }//end of if stack Empty
111
112 temp = s.Pop();
113
114 if((temp=='[' && expr[i]==']') ||

74 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

115 (temp=='(' && expr[i]==')') ||


116 (temp=='{' && expr[i]=='}'))
117 {
118 i++;
119 continue;
120 }//end of matching brackets
121 else
122 {
123 flag=1;
124 break;
125 }//end of brackets do not match
126
127 }//end of if closing bracket
128
129 i++;
130
131
132 }//end of while
133
134 if(s.Empty() && flag==0)
135 {
136 cout<<"Expression is balanced";
137 }
138 else
139 {
140 cout<<"Expression is not balanced";
141 }
142
143
144 }//end of main

75 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Output:

76 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit - 4 : Queues
Program No: 16.
Roll No : 1409
Title of Program : Ordinary Queue
Objective : Unit-4: Queues

Date: 09.11.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 4: queues
4 Program: Ordinary Queue */
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define SIZE 4 // Declaring a constant SIZE
10 using namespace std;
11
12 /*----------1. Node Template - NOT REQUIRED FOR ARRAY BASED STRUCTURE---
13 -----*/
14
15 /*----------2. Array Based Template--------*/
16 class AQueue
17 {
18 int A[SIZE];
19 int front;
20 int rear;
21
22 public:
23 AQueue()
24 {
25 front = -1;
26 rear = -1;
27 }
28 void Enqueue(int x);
29 void Dequeue();
30 void PeekFront();

77 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 void PeekRear();
32 void Display();
33 int Full();
34 int Empty();
35 };
36
37 /*---------------3.Functions--------------*/
38
39 /*----------Enqueue----------*/
40 void AQueue :: Enqueue(int x)
41 {
42 if(Full())
43 {
44 cout << "Queue Overflow!";
45 return;
46 }
47
48 rear++;
49 A[rear] = x;
50
51 //First Element
52 if(front==-1)
53 {
54 front++;
55 }
56
57 }//end of Enqueue
58
59 /*---------------Full-------------------*/
60 int AQueue :: Full()
61 {
62 return(rear == SIZE-1 ? 1 : 0);
63 }//end of Full
64
65 /*--------------Empty-----------------*/
66 int AQueue :: Empty()
67 {
68 return(front == -1 ? 1 : 0);
69 }//end of Empty
70
71

78 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 /*-----------------Dequeue()----------------*/
73 void AQueue :: Dequeue()
74 {
75 if(Empty())
76 {
77 cout<<"Queue Underflow!";
78 return;
79 }
80
81
82 int temp=A[front]; //hold element in the temp variable
83
84 if(front==rear) //Single element in the queue
85 {
86 front=rear=-1; //Empty queue
87 }
88 else
89 {
90 front++;
91 }
92
93 cout<<"Element removed is: "<<temp;
94 }//end of Dequeue
95
96 /*-------------Display-------------*/
97 void AQueue :: Display()
98 {
99 if(Empty())
100 {
101 cout << "Queue Underflow!";
102 return;
103 }
104
105 int i;
106
107 cout<<"Queue contains: \n";
108 for(i=front; i<=rear; i++)
109 {
110 cout << A[i] <<" ";
111 }
112 }//end of Display

79 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113
114
115 /*-------------Peek Front-------------*/
116 void AQueue :: PeekFront()
117 {
118 if(Empty())
119 {
120 cout <<"Queue Underflow!";
121 return;
122 }
123 cout<<"Element at the front is: "<<A[front];
124
125 }//end of Peek Front
126
127
128 /*-------------Peek Rear-------------*/
129 void AQueue :: PeekRear()
130 {
131 if(Empty())
132 {
133 cout <<"Queue Underflow!";
134 return;
135 }
136 cout<<"Element at the rear is: "<<A[rear];
137
138 }//end of Peek Rear
139
140
141
142
143 /*---------------4.Main Menu---------------*/
144
145 int main()
146 {
147 AQueue q;
148 int ch,num;
149 while(1)
150 {
151 system("cls");
152
153

80 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 cout << "*** Ordinary Queue***\n\n";


155
156 cout << "1. Enqueue an element\n";
157 cout << "2. Dequeue\n";
158 cout << "3. Peek Front \n";
159 cout << "4. Peek Rear \n";
160 cout << "5. Display the queue \n";
161 cout << "6. Exit \n\n";
162
163 cout << "Enter your choice: ";
164 cin>>ch;
165
166 switch(ch)
167 {
168 case 1:
169 cout << "Enter an element: ";
170 cin>>num;
171 q.Enqueue(num);
172 getch();
173 break;
174
175 case 2:
176 q.Dequeue();
177 getch();
178 break;
179
180 case 3:
181 q.PeekFront();
182 getch();
183 break;
184
185 case 4:
186 q.PeekRear();
187 getch();
188 break;
189
190 case 5:
191 q.Display();
192 getch();
193 break;
194

81 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 case 6:
196 exit(1);
197
198 default:
199 cout << "Incorrect Choice!";
200 getch();
201
202 }//end of switch
203 }//end of while
204 }//end of main

Output:
(Enqueue)

82 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek Front)

(Peek Rear)

(Display)

83 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Dequeue)

84 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 17.


Roll No : 1409
Title of Program : Circular Queue
Objective : Unit-4: Queues

Date: 25.11.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 4: queues
4 Program: Circular Queue */
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define SIZE 4
10 using namespace std;
11 /*----------1. Node Template - NOT REQUIRED FOR ARRAY BASED STRUCTURE------
12 --*/
13
14 /*----------2. Array Based Template--------*/
15
16 class CQueue
17 {
18 int A[SIZE];
19 int rear,front;
20 int count;
21
22 public:
23 CQueue()
24 {
25 front=-1;
26 rear=-1;
27 count=0;
28 }
29
30 void Enqueue(int x);

85 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 void Dequeue();
32 void PeekFront();
33 void PeekRear();
34 void Display();
35 int Full();
36 int Empty();
37 };
38
39
40 /*---------------3.Functions--------------*/
41
42 /*---------------Full-------------------*/
43 int CQueue :: Full()
44 {
45 if(count == SIZE)
46 {
47 return 1;
48 }
49 else
50 {
51 return 0;
52 }
53 }//end of full
54
55 /*---------------Empty-------------------*/
56 int CQueue :: Empty()
57 {
58 if(count == 0)
59 {
60 return 1;
61 }
62 else
63 {
64 return 0;
65 }
66 }//end of empty
67
68
69 /*---------------Enqueue(int x)-------------------*/
70 void CQueue :: Enqueue(int x)
71 {

86 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 if(Full())
73 {
74 cout<<"Queue Overflow!";
75 return;
76 }
77
78
79 if(rear == SIZE-1)//wrap rare index around the queue
80 {
81 rear = 0;
82 }
83 else
84 {
85 rear++;
86 }
87
88 A[rear]=x;
89
90 if(front == -1) // Special case-first element in the queue
91 {
92 front++;
93 }
94 count++;
95
96 }//end of Enqueue
97
98
99 /*-----------------Dequeue()----------------*/
100 void CQueue :: Dequeue()
101 {
102 if(Empty())
103 {
104 cout<<"Queue Underflow!";
105 return;
106 }
107
108 int temp=A[front];
109 if(front == rear) //Single Element Deletion
110 {
111 front=-1;
112 rear=-1;

87 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 }
114 else if(front == SIZE-1) //Implement Circularity and update front
115 {
116 front=0;
117 }
118 else
119 {
120 front++;
121 }
122
123 cout << "Element removed: " << temp;
124 count--;
125
126 }//end of Dequeue
127
128 /*-----------------Display()----------------*/
129 void CQueue :: Display()
130 {
131 if(Empty())
132 {
133 cout << "Queue Underflow!";
134 return;
135 }
136
137 int i=front;
138 for(int j=1; j<=count;j++)
139 {
140 cout << A[i] << "|";
141 if(i== SIZE-1)
142 {
143 i=0;
144 }
145 else
146 {
147 i++;
148 }
149 }
150
151
152 }//end of Display
153

88 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 /*-----------------PeekFront()----------------*/
155 void CQueue :: PeekFront()
156 {
157 if(Empty())
158 {
159 cout<<"Queue Underflow!";
160 return;
161 }
162
163 cout<<"Element at the front is: "<<A[front];
164 }//end of PeekFront
165
166 /*-----------------PeekRare()----------------*/
167 void CQueue :: PeekRear()
168 {
169 if(Empty())
170 {
171 cout<<"Queue Underflow!";
172 return;
173 }
174
175 cout<<"Element at the front is: "<<A[rear];
176
177 }//end of PeekRare
178
179
180 /*---------------4.Main Menu---------------*/
181
182 int main()
183 {
184 int ch,num;
185 CQueue c;
186
187 while(1)
188 {
189 system("cls");
190 cout << "*** Circular Queue ***\n\n";
191 cout << "1. Enqueue Operation\n";
192 cout << "2. Dequque Operation\n";
193 cout << "3. PeekFront Operation\n";
194 cout << "4. PeekRare Operation\n";

89 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 cout << "5. Display Operation\n";


196 cout << "6. Exit \n\n";
197
198 cout << "Enter your choice: ";
199 cin >> ch;
200
201 switch(ch)
202 {
203 case 1:
204 cout << "Enter an element: ";
205 cin>>num;
206 c.Enqueue(num);
207 getch();
208 break;
209
210 case 2:
211 c.Dequeue();
212 getch();
213 break;
214
215 case 3:
216 c.PeekFront();
217 getch();
218 break;
219
220 case 4:
221 c.PeekRear();
222 getch();
223 break;
224
225 case 5:
226 c.Display();
227 getch();
228 break;
229
230 case 6:
231 exit(1);
232
233 default:
234 cout << "Incorrect Choice!";
235 getch();

90 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 }//end of switch


237
238 }//end of while
239 }//end of main

Output:

(Enqueue)

91 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display)

(Peek Front)

92 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek Rare)

(Dequeue)

93 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

94 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 18.


Roll No : 1409
Title of Program : Priority Queue
Objective : Unit-4: Queues

Date: 04.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 4: queues
4 Program: Priority Queue */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 /*----------1. Node Template - NOT REQUIRED FOR ARRAY BASED STRUCTURE--------
12 */
13 class PQNode
14 {
15 public:
16 int data;
17 int priority;
18 PQNode *next;
19 };
20 //memory address initialization will be null
21
22 /*----------2.List Template--------*/
23 class PQueue
24 {
25 PQNode *front;
26 PQNode *rear;
27 public:
28
29 PQueue()
30 {

95 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 front=NULL;
32 rear=NULL;
33 }
34 void Enqueue(int x,int p);
35 void Dequeue();
36 void PeekFront();
37 void PeekRear();
38 void Display();
39 int Empty();
40 };
41
42 /*----------3.Functions-------*/
43 /*----------Enqueue-------------*/
44 void PQueue :: Enqueue(int x,int p)
45 {
46 //1.Make a new Node
47 PQNode *t = new PQNode;
48 t->data=x;
49 t->priority=p;
50 t->next=NULL;
51
52 //2.1st Node in the queue
53 if(front== NULL)
54 {
55 front=t;
56 rear=t;
57 return;
58 }
59
60 //3.Traverse
61 PQNode *temp= front;
62 PQNode *prev=NULL;
63
64 while(temp!=NULL && temp->priority < t->priority)
65 {
66 prev=temp;
67 temp=temp->next;
68 }
69
70 //4.Attach t
71 if(temp==front) //Front Node Insertion

96 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 {
73 t->next=front;
74 front=t;
75 }
76 else if (temp==NULL) //Rear Node Insertion
77 {
78 rear->next = t;
79 rear=t;
80 }
81 else //Any Other Node
82 {
83 prev->next=t;
84 t->next=temp;
85 }
86
87 }//end of Enqueue
88 /*---------------Empty-------------------*/
89 int PQueue :: Empty()
90 {
91 if(front == NULL)
92 {
93 return 1;
94 }
95 else
96 {
97 return 0;
98 }
99 }//end of empty
100
101
102 /*-----------------Display()----------------*/
103 void PQueue :: Display()
104 {
105 if(Empty())
106 {
107 cout<<"Queue Underflow!!";
108 return;
109 }
110 PQNode *temp=front;
111
112 while(temp!=NULL)

97 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 {
114 cout << "Data: " <<temp->data <<" Priority: "<<temp->priority<<endl;
115 temp=temp->next;
116 }
117
118 cout<<"NULL";
119
120 }//end of Display
121
122
123 /*-----------------PeekFront()----------------*/
124 void PQueue :: PeekFront()
125 {
126 if(Empty())
127 {
128 cout<<"Queue Underflow!";
129 return;
130 }
131
132 cout<<"Element at the front is: "<<endl;
133 cout << "Data: " <<front->data <<" Priority: "<<front->priority;
134
135 }//end of PeekFront
136
137
138 /*-----------------PeekRear()----------------*/
139 void PQueue :: PeekRear()
140 {
141 if(Empty())
142 {
143 cout<<"Queue Underflow!";
144 return;
145 }
146
147 cout<<"Element at the rear is: "<<endl;
148 cout << "Data: " <<rear->data <<" Priority: "<<rear->priority;
149
150 }//end of PeekRear
151
152
153
/*-----------------Dequeue()----------------*/
98 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 void PQueue :: Dequeue()


155 {
156 if(Empty())
157 {
158 cout<<"Queue Undeflow!";
159 return;
160 }
161 PQNode *temp=front;
162 if(front==rear)
163 {
164 front = NULL;
165 rear = NULL;
166 }
167 else
168 {
169 front=front->next;
170 }
171
172 cout <<"Element removed is : "<<temp->data<<" with priority " <<temp->priority;
173
174 delete temp;
175
176
177 }//end of Dequeue
178
179
180 /*----------4.Menu--------*/
181 int main()
182 {
183 int ch,num,pr;
184 PQueue p;
185
186 while(1)
187 {
188 system("cls");
189 cout << "***Priority Queue***\n\n";
190 cout << "1. Enqueue Operation\n";
191 cout << "2. Dequeue Operation\n";
192 cout << "3. PeekFront Operation\n";
193 cout << "4. PeekRare Operation\n";
194 cout << "5. Display Operation\n";
cout << "6. Exit \n\n";
99 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195
196 cout << "Enter your choice: ";
197 cin >> ch;
198 switch(ch)
199 {
200 case 1:
201 cout << "Enter an element: ";
202 cin>>num;
203 cout << "Enter the priority: ";
204 cin>>pr;
205 p.Enqueue(num,pr);
206 getch();
207 break;
208
209 case 2:
210 p.Dequeue();
211 getch();
212 break;
213
214 case 3:
215 p.PeekFront();
216 getch();
217 break;
218
219 case 4:
220 p.PeekRear();
221 getch();
222 break;
223
224 case 5:
225 p.Display();
226 getch();
227 break;
228 case 6:
229 exit(1);
230
231 default:
232 cout << "Incorrect Choice!";
233 getch();
234 }
235

100 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 }//end of while


237 }//end of main
238

Output:

(Enqueue)

(Display)

101 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Dequeue)

102 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek Front)

103 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek Rare)

(Exit)

104 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 19.


Roll No : 1409
Title of Program : Double-Ended Queue
Objective : Unit-4: Queues

Date: 02.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 4: queues
4 Program: Double Ended Queues */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10 /*----------1. Node Template -*/
11 class DQNode
12 {
13 public:
14 int data;
15 DQNode *right;
16 DQNode *left;
17 };
18
19 /*----------2.List Template--------*/
20 class DQueue
21 {
22 DQNode *front;
23 DQNode *rear;
24
25 public:
26 DQueue()
27 {
28 front = rear = NULL;
29 }
30

105 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 void EnqueueFront(int x);


32 void EnqueueRear(int x);
33 void DequeueFront();
34 void DequeueRear();
35 void PeekFront();
36 void PeekRear();
37 void Display();
38 int Empty();
39 };
40
41 /*----------------3.Functions-------------*/
42
43 /*----------------EnqueueFront--------------*/
44 void DQueue :: EnqueueFront(int x)
45 {
46 //Make a new node
47 DQNode *t = new DQNode;
48 t->data = x;
49 t->left = NULL;
50 t->right = NULL;
51
52 //Special case- 1st node in the queue
53 if(front == NULL)
54 {
55 front = t;
56 rear = t;
57 return;
58 }
59
60 //Attach t to the front of the queue
61 t->right = front;
62 front->left = t;
63 front = t;
64
65 }//end of enqueue
66
67 /*----------------EnqueueRear--------------*/
68 void DQueue :: EnqueueRear(int x)
69 {
70 //Make a new node
71 DQNode *t = new DQNode;

106 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 t->data = x;
73 t->right = NULL;
74 t->left = NULL;
75
76 //1st Node in the queue
77 if(front == NULL)
78 {
79 front = t;
80 rear = t;
81 return;
82 }
83
84 //Attach t to the rear of the queue
85 rear->right = t;
86 t->left = rear;
87 rear = t;
88
89 }//end of EnqueueRear
90
91 /*---------------Empty-------------------*/
92 int DQueue :: Empty()
93 {
94 if(front == NULL)
95 {
96 return 1;
97 }
98 else
99 {
100 return 0;
101 }
102 }//end of empty
103
104 /*--------------------Display()------------------*/
105 void DQueue :: Display()
106 {
107 if(Empty())
108 {
109 cout << "Queue Underflow!";
110 return;
111 }
112

107 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 DQNode *temp = front;


114
115 cout<< "The double ended queue is: " <<endl;
116
117 while(temp!=NULL)
118 {
119 cout << temp->data << " | ";
120 temp = temp->right;
121 }
122
123 }//end of Display
124
125
126 /*-------------------PeekFront()------------------*/
127 void DQueue :: PeekFront()
128 {
129 if(Empty())
130 {
131 cout<<"Queue Underflow!";
132 return;
133 }
134 cout << "Element at the front is: "<<front->data;
135
136
137 }//end of PeekFront
138
139
140 /*-----------------PeekRear()----------------*/
141 void DQueue :: PeekRear()
142 {
143 if(Empty())
144 {
145 cout << "Queue Underflow!";
146 return;
147 }
148 cout << "Element at the front is: "<<rear->data;
149 }//end of PeekRear
150
151 /*-----------------DequeueFront()----------------*/
152 void DQueue :: DequeueFront()
153 {
if(Empty())
108 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 {
155 cout<<"Queue Underflow!";
156 return;
157 }
158
159 DQNode *temp = front;
160 if(front == rear)
161 { front = rear = NULL;
162 }
163 else
164 {
165 front = front->right;
166 front->left=NULL;
167 }
168
169 cout << "Element removed is: " << temp->data << endl;
170 delete temp;
171
172
173 }//end of DequeueFront
174
175 /*-----------------DequeueRear()----------------*/
176 void DQueue :: DequeueRear()
177 {
178 if(Empty())
179 {
180 cout<<"Queue Underflow!";
181 return;
182 }
183
184
185 DQNode *temp = rear;
186 if(front == rear)
187 { front = rear = NULL;
188 }
189 else
190 {
191 rear = rear->left;
192 rear->right=NULL;
193 }
194

109 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 cout << "Element removed is: " << temp->data << endl;
196 delete temp;
197
198 }//end of DequeueRear
199
200
201 /*----------4.Menu--------*/
202 int main()
203 {
204 int ch,num;
205 DQueue d;
206
207 while(1)
208 {
209 system("cls");
210 cout <<" **** Double-Ended Queues ***\n\n";
211 cout << "1. Enqueue Front\n";
212 cout << "2. Enqueue Rear\n";
213 cout << "3. Dequeue Front\n";
214 cout << "4. Dequeue Rear\n";
215 cout << "5. Peek Front\n";
216 cout << "6. Peek Rear\n";
217 cout << "7. Display\n";
218 cout << "8. Exit\n";
219
220 cout << "Enter your choice: ";
221 cin >> ch;
222 switch(ch)
223 {
224 case 1:
225 cout << "Enter an element: ";
226 cin>>num;
227 d.EnqueueFront(num);
228 getch();
229 break;
230
231 case 2:
232 cout << "Enter an element: ";
233 cin>>num;
234 d.EnqueueRear(num);
235 getch();

110 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 break;
237
238 case 3:
239 d.DequeueFront();
240 d.Display();
241 getch();
242 break;
243
244 case 4:
245 d.DequeueRear();
246 d.Display();
247 getch();
248 break;
249
250 case 5:
251 d.PeekFront();
252 getch();
253 break;
254
255 case 6:
256 d.PeekRear();
257 getch();
258 break;
259
260 case 7:
261 d.Display();
262 getch();
263 break;
264
265 case 8:
266 exit(1);
267
268 default:
269 cout << "Incorrect Choice!";
270 getch();
271
272
273 }//end of switch
274
275 }//end of while
276 }

111 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

277

Output:

(Enqueue Front)

112 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Enqueue Rare)

113 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Dequeue Front)

(Dequeue Rear)
114 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek Front)

115 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Peek Rear)

(Display)

116 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit - 5 : Linked Lists


Program No: 20.
Roll No : 1409
Title of Program : Singly Linked List
Objective : Unit-5: Linked Lists

Date: 30.09.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 5:Linked Lists
4 Program: Singly Linked List */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 /*------------------1. Node Template------------------*/
12 class Node
13 {
14 public:
15 int data;
16 Node *next;
17 };
18
19 /*------------------2. List Template------------------*/
20 class List
21 {
22 Node *head;
23
24 public:
25 List()
26 {
27 head = NULL;
28 }
29 void Insert(int x);
30 void Del(int x);

117 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 void Search(int x);


32 void Length();
33 void Display();
34 void Reverse();
35 };
36
37 /*------------------3. Function------------------*/
38 /*-------------Insert Function---------------*/
39 void List :: Insert(int x)
40 {
41 //Step-1: Make a new node t
42 Node *t = new Node;
43 t->data = x;
44 t->next = NULL;
45
46 //Step-2 Special Case: First node in the SLL
47 if(head == NULL)
48 {
49 head = t;
50 }
51 else //Step-3: Traverse till the last node & attach t
52 {
53 Node *temp = head; //Creating a temporary pointer
54
55 while(temp->next != NULL)
56 {
57 temp = temp->next;//assign the next address's node
58 }
59
60 temp->next = t;
61 }
62
63 }//end of insert
64
65 /*-------------Display Function---------------*/
66 void List :: Display()
67 {
68 Node *temp = head;
69
70 while(temp != NULL)
71 {

118 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 cout <<temp->data << "->";


73 temp = temp->next;
74 }
75 cout << "NULL";
76 }//end of display
77
78 /*-------------Length Function---------------*/
79 void List :: Length()
80 {
81 Node *temp = head;
82 int count=0;
83
84 while(temp != NULL)
85 {
86 count++;
87 temp = temp -> next;
88 }
89
90 cout << "Length of list is: " << count;
91 }//end of Length
92
93 /*-------------Search Function---------------*/
94 void List :: Search(int x)
95 {
96 Node *temp = head;
97 int flag = 0;
98 int pos=0;
99
100 while(temp != NULL)
101 {
102 pos++;
103 if(temp->data == x)
104 {
105 flag = 1;
106 break;
107 }
108 temp = temp -> next;
109 }
110
111
112 if (flag==1)

119 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 {
114 cout << x << " is found at position " << pos ;
115 }
116 else
117 {
118 cout << x << " is not found";
119 }
120 }//end of Search
121
122 /*-------------Delete Function---------------*/
123 void List :: Del(int x)
124 {
125
126 //1.Empty List - RETURN CONTROL
127 if(head==NULL)
128 {
129 cout << "Empty List!";
130 return;
131 }
132
133 //2. Search for x
134 Node *temp = head;
135 Node *prev = NULL;//first prev moves ,then temp moves
136 int flag=0;
137
138
139 while(temp != NULL)
140 {
141 if(temp->data == x)
142 {
143 flag=1;
144 break;
145 }
146 prev = temp;
147 temp = temp->next;
148 }
149
150 //3.Unsuccessful Search - RETURN CONTROL
151 if(flag == 0)
152 {
153 cout << x << " is not found";

120 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 return;
155 }
156
157 //4.Successful Search
158 //a.Single node deletion
159 if(temp == head && temp->next==NULL)
160 {
161 head=NULL;
162 }
163 else if (temp==head)//b.head node deletion
164 {
165 head = temp->next;
166 }
167 else if(temp->next == NULL)//c.tail/last node deletion
168 {
169 prev->next = NULL;
170 }
171 else//d.delete any other node in the middle
172 {
173 prev->next = temp->next;
174 }
175 //5.Delete memory occupied by temp
176 delete temp;
177
178 //Displaying the updated list after the node deletion
179 cout<<"The updated list is: "<<endl;
180 Display();
181
182 }//end of Del
183
184
185 /*-------------Reverse Function---------------*/
186 void List :: Reverse()
187 {
188 if(head == NULL)
189 {
190 cout << "Empty SLL";
191 return;
192 }
193
194 Node *temp = head;

121 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 Node *revHead = NULL;


196
197 //Head Node Insertion
198 while(temp)
199 {
200 //create a new node with the contents of original SLL node
201 Node *t = new Node;
202 t->data = temp->data;
203 t->next = revHead; // for the first time node revHead is already NULL
204
205 //update revHead
206 revHead = t;
207
208 //Advance to th enect node in the original SLL
209 temp = temp->next;
210 }
211
212 head = revHead; //set head to the reversed list
213
214 }//end of reverse
215
216 /*------------------4. Main Menu------------------*/
217 int main()
218 {
219 List s;
220 int ch,num;
221
222 while(1)
223 {
224 system("cls");
225
226 cout << "*** Singly Linked List ***\n\n";
227
228 cout << "1. Insert a node" << endl;
229 cout << "2. Delete a node" << endl;
230 cout << "3. Search for a node" << endl;
231 cout << "4. Length of the list" << endl;
232 cout << "5. Display the list" << endl;
233 cout << "6. Reverse the list" << endl;
234 cout << "7. Exit" << endl << endl;
235

122 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 cout << "Enter your choice: ";


237 cin >>ch;
238
239 switch(ch)
240 {
241 case 1:
242 cout << "Enter a value: ";
243 cin >> num;
244
245 s.Insert(num);
246 getch();
247 break;
248
249 case 2:
250 cout << "Enter the element to be deleted: ";
251 cin >> num;
252 s.Del(num);
253 getch();
254 break;
255
256 case 3:
257 cout << "Enter the element to be searched: ";
258 cin >> num;
259 s.Search(num);
260 getch();
261 break;
262
263 case 4:
264 s.Length();
265 getch();
266 break;
267
268 case 5:
269 s.Display();
270 getch();
271 break;
272
273 case 6:
274 s.Reverse();
275 cout<<"The reversed list is: ";
276 s.Display();

123 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

277 getch();
278 break;
279
280 case 7:
281 exit(1);
282
283 default:
284 cout << "Incorrect Choice!";
285 getch();
286
287 }//end of switch
288 }//end of while
289 }//end of main

Output:
(Insertion)

(Display)

124 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Search)

(Delete)

125 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Length)

(Reverse)

126 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

127 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 21.


Roll No : 1409
Title of Program : Circular Linked List
Objective : Unit-5: Linked Lists

Date: 09.10.2023

Source Code:

1 /*Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 5:Linked Lists
4 Program: Circular Linked List */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 /*------------------1. Node Template------------------*/
12 class CNode
13 {
14 public:
15 int data;
16 CNode *next;
17 };
18
19 /*------------------2. List Template------------------*/
20 class CList
21 {
22 CNode *first;
23 CNode *last;
24
25 public:
26 CList()
27 {
28 first = last = NULL;
29 }
30 void Insert(int x);

128 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 void Del(int x);


32 void Search(int x);
33 void Display();
34 void Length();
35
36 };
37
38 /*------------------3. Function------------------*/
39 //----------------Insert Function-----------------
40 void CList :: Insert(int x)
41 {
42 //Make a new node t
43 CNode *t = new CNode;
44 t->data = x;
45 t->next = NULL;
46
47 //First node in the CLL
48 if(first == NULL)
49 {
50 first = t;
51 last = t;
52 last->next = first;
53 }
54 else //Attach t to the last node
55 {
56 last->next = t; //Link last node to new node t
57 last = t; //Shift last to t
58 last->next = first; //to maintain circularity
59 }
60 }//end of insert
61
62 //----------------Display Function-----------------
63 void CList :: Display()
64 {
65 if(first == NULL)
66 {
67 cout << "Empty CLL!";
68 return;
69 }
70 CNode *temp = first;
71

129 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 do
73 {
74 cout << temp->data << "->";
75 temp = temp->next;
76 }
77 while(temp != first);
78 cout<<"Back to first node";
79 }//end of Display
80
81 //----------------Length Function-----------------
82 void CList :: Length()
83 {
84
85 if(first == NULL)
86 {
87 cout << "Empty CLL!";
88 return;
89 }
90
91 CNode *temp = first;
92 int count=0;
93
94 do
95 {
96 count++;
97 temp=temp->next;
98
99 }
100 while(temp != first);
101
102 cout << "Number of nodes: " << count;
103
104 }//end of length
105
106
107 //----------------Search Function-----------------
108 void CList :: Search(int x)
109 {
110 if(first == NULL)
111 {
112 cout<<"Empty CLL";

130 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 return;
114 }
115
116 CNode *temp=first;
117 int flag=0;
118 int pos=0;
119
120 do
121 {
122 pos++;
123 if(temp->data ==x)
124 {
125 flag=1;
126 break;
127 }
128 temp=temp->next;
129 }
130 while(temp != first);
131
132 if (flag==1)
133 {
134 cout << x << " is found at position " << pos ;
135 }
136 else
137 {
138 cout << x << " is not found";
139 }
140 }//end of Search
141
142 //----------------Delete Function-----------------
143 void CList :: Del(int x)
144 {
145 //1.Empty List - RETURN CONTROL
146 if(first==NULL)
147 {
148 cout<<"Empty CLL";
149 return;
150 }
151
152 //2.Search for x
153 CNode *temp=first;

131 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 CNode *prev=NULL;


155 int flag=0;
156
157 do
158 {
159 if(temp->data == x)
160 {
161 flag=1;
162 break;
163 }
164
165 prev=temp;
166 temp=temp->next;
167 }
168 while(temp!=first);
169
170 //3.Unsuccesful Search - RETURN CONTROL
171 if(flag == 0)
172 {
173 cout<<"Unsuccessful Search";
174 return;
175 }
176
177 //4.Successful Search
178 //4a.Single Node Deletion
179 if(temp==first && temp==last)
180 {
181 first=NULL;
182 last=NULL;
183 }
184 //b.first node deletion (Update Cicularity)
185 else if (temp==first)
186 {
187 first=first->next;
188 last->next=first;
189 }
190 //c.last node deletion (Update Cicularity)
191 else if(temp==last)
192 {
193 last=prev;
194 last->next=first;

132 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 }
196 //d.delete any other node in the middle
197 else
198 {
199 prev->next = temp->next;
200 }
201 //5.Delete memory occupied by temp
202 delete temp;
203
204 //Displaying the updated list after the node deletion
205 cout<<"The updated list is: "<<endl;
206 Display();
207
208
209 }//end of Del
210 /*------------------4. Main Menu------------------*/
211 int main()
212 {
213 CList c;
214 int ch,num;
215
216 while(true)
217 {
218 system("cls");
219 cout << "*** Cicular Linked List Menu ***\n\n";
220
221 cout << "1.Insert a node in the CLL\n";
222 cout << "2.Delete a node from the CLL\n";
223 cout << "3.Search a node in the CLL\n";
224 cout << "4.Length of the CLL\n";
225 cout << "5.Display the CLL\n";
226 cout << "6.Exit\n\n";
227
228 cout << "Enter your choice: ";
229 cin >> ch;
230
231 switch(ch)
232 {
233 case 1:
234 cout << "Enter a value: ";
235 cin >> num;

133 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236
237 c.Insert(num);
238
239 getch();
240 break;
241 case 2:
242 cout << "Enter the element to be deleted: ";
243 cin >> num;
244 c.Del(num);
245 getch();
246 break;
247 case 3:
248 cout << "Enter the element to be searched: ";
249 cin >> num;
250 c.Search(num);
251 getch();
252 break;
253 case 4:
254 c.Length();
255 getch();
256 break;
257 case 5:
258 c.Display();
259 getch();
260 break;
261 case 6:
262 exit(1);
263 default:
264 cout << "Incorrect choice";
265 getch();
266
267 }//end of switch
268 }//end of while
269 }//end of main
270

Output:

(Insertion)

134 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display)

(Search)

135 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Delete)

(Length)

136 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

137 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 22.


Roll No : 1409
Title of Program : Doubly Linked List
Objective : Unit-5: Linked Lists

Date: 16.10.2023

Source Code:

1 /*Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 5:Linked Lists
4 Program: Doubly Linked List */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 /*------------------1. Node Template------------------*/
12 class DNode
13 {
14 public:
15 DNode *left;
16 int data;
17 DNode *right;
18 };
19
20 /*------------------2. List Template------------------*/
21 class DList
22 {
23 DNode *head;
24 DNode *tail;
25
26 public:
27 DList()
28 {
29 head = tail = NULL;
30 }

138 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31
32 void Insert(int x);
33 void Del(int x);
34 void Search(int x);
35 void Length();
36 void Display();
37
38 };
39
40 /*------------------3. Function------------------*/
41 /*-------------Insert Function---------------*/
42 void DList::Insert(int x)
43 {
44 //Step-1: Make a new node t
45 DNode *t = new DNode;
46 t->data = x;
47 t->left = NULL;
48 t->right = NULL;
49
50 //Step-2 Special Case: First node in the DLL
51 if(head == NULL)
52 {
53 head=t;
54 tail=t;
55 }
56 else //when two nodes are there
57 {
58 tail->right=t; //attach t to the right of the tail
59 t->left=tail; //attach tail to th left of t
60 tail=t; //Shiting of tail
61 }
62
63 }//end of insert
64
65 /*-------------Display Function---------------*/
66 void DList :: Display()
67 {
68 DNode *temp = head;
69
70 cout << "DLL in forward direction: ";
71 cout << "NULL<->";

139 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 while(temp != NULL)
73 {
74 // temp = temp->left; ....this will be NULL
75 cout << temp->data << "<->";
76 temp = temp->right;
77 }
78 cout << "NULL\n\n";
79
80 //setting tail to display reverse direction
81 temp = tail;
82 cout << "DLL in reverse direction: ";
83 cout <<"NULL<->";
84 while(temp != NULL)
85 {
86 cout << temp->data << "<->";
87 temp = temp->left;
88 // temp = temp->right; ....this will be NULL
89 }
90
91 cout << "NULL";
92 }//end of display
93
94 /*-------------Length Function---------------*/
95 void DList :: Length()
96 {
97 DNode *temp=head;
98 int count=0;
99
100 while(temp != NULL)
101 {
102 count++;
103 temp= temp->right;
104 }
105
106 cout << "Length of list is: " << count;
107 }//end of Length
108
109
110 /*-------------Search Function---------------*/
111 void DList :: Search(int x)
112 {

140 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 //1.Empty List - RETURN CONTROL


114 if(head==NULL && tail==NULL)
115 {
116 cout<<"Empty List!";
117 return;
118 }
119
120 //2.Search for x
121 DNode *temp = head;
122 int flag=0;
123 int pos=0;
124
125 while(temp != NULL)
126 {
127 pos++;
128 if(temp->data == x)
129 {
130 flag=1;
131 break;
132 }
133 temp = temp->right;
134 }
135
136 if(flag == 1)
137 {
138 cout << x << " is found at position in forward direction " <<pos;
139 }
140 else
141 {
142 cout << x << " is not found";
143 }
144 }//end of Search
145
146 /*-------------Del Function---------------*/
147 void DList :: Del(int x)
148 {
149 //1.Empty List - RETURN CONTROL
150 if(head==NULL && tail==NULL)
151 {
152 cout<<"Empty List!";
153 return;

141 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 }
155
156 //2.Search for x
157 DNode *temp = head;
158 DNode *prev = NULL;
159 int flag=0;
160
161 while(temp != NULL)
162 {
163 if(temp->data == x)
164 {
165 flag=1;
166 break;
167 }
168 prev = temp;
169 temp = temp->right;
170 }
171
172 //3.Unsuccessful Search - RETURN CONTROL
173 if(flag==0)
174 {
175 cout << x <<" is not found";
176 return;
177 }
178
179 //4.Successful Search
180 //a.Single node deletion
181 if(temp == head && temp == tail)
182 {
183 head=NULL;
184 tail=NULL;
185 }
186
187 //b.head node deletion
188 else if(temp == head)
189 {
190 head = head->right;
191 head->left= NULL;
192 }
193
194 //c.tail/last node deletion

142 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 else if(temp == tail)


196 {
197 tail = prev;
198 tail->right = NULL;
199 }
200
201 //d.delete any other node in the middle
202 else
203 {
204 prev->right = temp->right;
205 (temp->right)->left = prev;
206 }
207
208 //5.Delete memory occupied by temp
209 delete temp;
210
211 //Displaying the updated list after the node deletion
212 cout<<"The updated list is: "<<endl;
213 Display();
214
215 }//end of Del
216 /*------------------4. Main Menu------------------*/
217
218 int main()
219 {
220 DList d;
221 int ch,num;
222
223 while(true)
224 {
225 system("cls");
226 cout << "*** Doubly Linked List ***\n\n";
227
228 cout << "1. Insert a node" << endl;
229 cout << "2. Delete a node" << endl;
230 cout << "3. Search for a node" << endl;
231 cout << "4. Length of the list" << endl;
232 cout << "5. Display the list" << endl;
233 cout << "6. Exit" << endl << endl;
234
235 cout << "Enter your choice: ";

143 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 cin >>ch;


237
238 switch(ch)
239 {
240 case 1:
241 cout << "Enter a value: ";
242 cin >> num;
243
244 d.Insert(num);
245 getch();
246 break;
247
248 case 2:
249 cout << "Enter the element to be deleted: ";
250 cin >> num;
251 d.Del(num);
252 getch();
253 break;
254
255 case 3:
256 cout << "Enter the element to be searched: ";
257 cin >> num;
258 d.Search(num);
259 getch();
260 break;
261
262 case 4:
263 d.Length();
264 getch();
265 break;
266
267 case 5:
268 d.Display();
269 getch();
270 break;
271
272 case 6:
273 exit(1);
274
275 default:
276 cout << "Incorrect Choice!";

144 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

277 getch();
278
279 }//end of switch
280 }//end of while
281
282 }//end of main

Output:

(Insertion)

(Display)

145 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Search)

(Length)

146 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Delete)

147 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 23.


Roll No : 1409
Title of Program : Polynomial Addition using Lists
Objective : Unit-5: Linked Lists

Date: 28.10.2023

Source Code:

1 /*Name: Sharvari Birajdar


2 Roll No: 1409
3 Unit 5: Lists
4 Program: Addition of Polynomials */
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10
11 /*----------1. Node Template--------*/
12 class PNode
13 {
14 public:
15 int Coeff;
16 int Exp;
17 PNode *next;
18 };
19
20
21 /*----------2.List Template-----------*/
22
23 class PList
24 {
25 PNode *head;
26
27 public:
28 PList()
29 {
30 head = NULL;

148 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 }
32 void Insert(int c, int e);
33 void Display();
34 void Add(PList P, PList Q);
35 void Sub(PList P, PList Q);
36 };
37
38
39 /*--------------3.Functions----------------*/
40 /*-------------Insert Function---------------*/
41 void PList :: Insert(int c,int e)
42 {
43 //Step-1: Make a new node t
44 PNode *t = new PNode;
45 t->Coeff = c;
46 t->Exp = e;
47 t->next = NULL;
48
49 //Step-2 Special Case : add first node in the list
50 if(head == NULL)
51 {
52 head = t;
53 }
54
55 else//Step-3 : traverse till the last node and attach t
56 {
57 PNode *temp = head; //Creating a temporary pointer
58 PNode *prev = NULL;
59
60 while(temp != NULL && temp->Exp > t->Exp)
61 {
62 prev = temp;
63 temp = temp->next;
64 }
65
66 if(temp==head) // 3a. Head Node Insertion
67 {
68 t->next = head;
69 head = t;
70 }
71

149 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 else if (temp==NULL) //3b.Tail Node Deletion


73 {
74 prev->next = t;
75 }
76 else //3c.Insertion of any other nodes
77 {
78 prev->next = t;
79 t->next = temp;
80 }
81 }
82 }//end of Insert
83
84 /*-------------Display Function---------------*/
85 void PList :: Display()
86 {
87 cout << "The polynomial is : ";
88 PNode *temp = head;
89
90 while(temp!= NULL)
91 {
92 if(temp->Coeff > 0)
93 {
94 cout << "+";
95 }
96 cout << temp->Coeff << "x^" << temp->Exp << " ";
97 temp= temp->next;
98 }//end of while
99
100
101 }//end of display
102
103 /*-------------Add Function---------------*/
104 void PList :: Add(PList x,PList y)
105 {
106 this-> head = NULL; //to eliminate extra terms
107
108 PNode *P = x.head;
109 PNode *Q= y.head;
110
111 int c,e;
112

150 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 while(P!=NULL && Q!=NULL)


114 {
115 if(P->Exp == Q->Exp)
116 {
117 c = P->Coeff + Q->Coeff;
118
119 if(c!=0)
120 {
121 e = P->Exp; //can be Q->Exp also
122 Insert(c,e);
123 }
124
125 P = P->next;
126 Q = Q->next;
127
128 }//end of if(P==Q)
129
130
131 else if(P->Exp > Q->Exp)
132 {
133 c = P->Coeff;
134 e = P->Exp;
135 Insert(c,e);
136
137 P = P->next;
138 }//end of if(P>Q)
139
140
141 else //(P->Exp < Q->Exp)
142 {
143 c = Q->Coeff;
144 e = Q->Exp;
145 Insert(c,e);
146
147 Q = Q->next;
148 }//end of if(P<Q)
149
150 }//P&&Q
151
152 while(P!=NULL)
153 {

151 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 c = P->Coeff;
155 e = P->Exp;
156 Insert(c,e);
157
158 P = P->next;
159 }
160
161 while(Q!=NULL)
162 {
163 c = Q->Coeff;
164 e = Q->Exp;
165 Insert(c,e);
166
167 Q = Q->next;
168 }
169
170
171 }//end of add
172
173
174 /*-------------Add Function---------------*/
175 void PList :: Sub(PList x,PList y)
176 {
177 this-> head = NULL; //to eliminate extra terms
178
179
180 PNode *P = x.head;
181 PNode *Q= y.head;
182
183 int c,e;
184
185 while(P!=NULL && Q!=NULL)
186 {
187 if(P->Exp == Q->Exp)
188 {
189 c = P->Coeff - Q->Coeff;
190
191 if(c!=0)
192 {
193 e = P->Exp; //can be Q->Exp also
194 Insert(c,e);

152 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 }
196
197 //skip the term with 0 coefficient
198 P = P->next;
199 Q = Q->next;
200
201 }//end of if(P==Q)
202
203
204 else if(P->Exp > Q->Exp)
205 {
206 c = P->Coeff;
207 e = P->Exp;
208 Insert(c,e);
209
210 P = P->next;
211 }//end of if(P>Q)
212
213
214 else //(P->Exp < Q->Exp)
215 {
216 c = Q->Coeff;
217 e = Q->Exp;
218 Insert(c,e);
219
220 Q = Q->next;
221 }//end of if(P<Q)
222
223 }//P&&Q
224
225 while(P!=NULL)
226 {
227 c = P->Coeff;
228 e = P->Exp;
229 Insert(c,e);
230
231 P = P->next;
232 }
233
234 while(Q!=NULL)
235 {

153 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 c = Q->Coeff;
237 e = Q->Exp;
238 Insert(c,e);
239
240 Q = Q->next;
241 }
242
243 }//end of sub
244
245 /*-------------------4.Menu---------------*/
246 int main()
247 {
248 int ch;
249
250 PList p1,p2,p3,p4;
251 int co,ex;
252
253 while(1)
254 {
255 system("cls");
256
257 cout << " *** Operations on Polynomials ***\n\n";
258 cout << "1. Create 1st Polynomial\n";
259 cout << "2. Create 2nd Polynomial\n";
260 cout << "3. Display the 1st Polynomial\n";
261 cout << "4. Display the 2nd Polynomial\n";
262 cout << "5. Add the Polynomials\n";
263 cout << "6. Subtract the Polynomials\n";
264 cout << "7. Exit\n\n";
265
266 cout << "Enter your choice: ";
267 cin>>ch;
268
269 switch(ch)
270 {
271 case 1:
272 cout << "Enter the coefficient: ";
273 cin >> co;
274 cout << "Enter the exponent: ";
275 cin >> ex;
276 p1.Insert(co,ex);

154 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

277 getch();
278 break;
279
280 case 2:
281 cout << "Enter the coefficient: ";
282 cin >> co;
283 cout << "Enter the exponent: ";
284 cin >> ex;
285 p2.Insert(co,ex);
286 getch();
287 break;
288
289 case 3:
290 p1.Display();
291 getch();
292 break;
293
294 case 4:
295 p2.Display();
296 getch();
297 break;
298
299 case 5:
300 p3.Add(p1,p2);
301 p3.Display();
302 getch();
303 break;
304
305 case 6:
306 p4.Sub(p1,p2);
307 p4.Display();
308 getch();
309 break;
310
311 case 7:
312 exit(1);
313
314 default:
315 cout << "Incorrect Choice!";
316 getch();
317

155 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

318 }//end of switch


319 }//end of while
320 }//end of main

Output:

(Create 1st polynomial)

156 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display 1st polynomial)

(Create 2nd Polynomial)

157 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display 2nd Polynomial)

158 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Addition)

(Subtraction)

159 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

160 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit - 6 : Trees
Program No: 24.
Roll No : 1409
Title of Program : Binary Search Trees
Objective : Unit-6: Trees

Date: 05.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 6: Trees
4 Program: Binary Search Tree*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 using namespace std;
10 /*----------1. Node Template ----------*/
11
12 class BSTNode
13 {
14 public:
15 int data;
16 BSTNode *right;
17 BSTNode *left;
18 };
19
20 /*----------2.List Template--------*/
21 class BST
22 {
23 BSTNode *root;
24 int count;
25
26 public:
27 BST()
28 {
29 root = NULL;
30 count = 0;

161 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 }
32
33 void Insert(int x);
34 void Search(int x);
35 void Display();
36 void Inorder(BSTNode *p);
37 void Preorder(BSTNode *p);
38 void Postorder(BSTNode *p);
39 void Count();
40 void FindMax();
41 void FindMin();
42
43 };
44
45
46 /*----------------3.Functions-------------*/
47
48 /*---------------Insert-------------*/
49 void BST :: Insert(int x)
50 {
51 //Make a new Node
52 BSTNode *t = new BSTNode;
53 t->data = x;
54 t->right = NULL;
55 t->left = NULL;
56
57 //Special case - 1st node in the structure
58 if(root == NULL)
59 {
60 root = t;
61 count++;
62 return;
63 }
64
65 //Traverse through the BST
66 BSTNode *temp = root;
67 BSTNode *prev = NULL;
68
69 while(temp != NULL)
70 {
71 prev = temp;

162 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 if(t->data < temp->data)


73 {
74 temp = temp->left;
75 }
76 else if(t->data > temp->data)
77 {
78 temp = temp->right;
79 }
80 else
81 {
82 cout << "Duplicate values are not allowed";
83 getch();
84 return;
85 }
86 }//end of while
87
88
89 //Attach t to prev
90 if(t->data < prev->data)
91 {
92 prev->left = t;
93 }
94 else
95 {
96 prev->right = t;
97 }
98
99 count++;
100
101 }//end of Insert
102
103 /*-------------------Display--------------*/
104 void BST :: Display()
105 {
106
107 cout << "Inorder Traversal: " << endl;
108 Inorder(root);
109 cout << endl;
110
111 cout << "Preorder Traversal: " << endl;
112 Preorder(root);

163 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 cout << endl;


114
115 cout << "Postorder Traversal: " << endl;
116 Postorder(root);
117 cout << endl;
118
119 }//end of Display
120
121 /*-------------------Inorder--------------*/
122 void BST :: Inorder(BSTNode *p)
123 {
124 if(p!= NULL)
125 {
126 Inorder(p->left);
127 cout << p->data << " ";
128 Inorder(p->right);
129 }
130 }//end of Inorder
131
132 /*-------------------Preorder--------------*/
133 void BST :: Preorder(BSTNode *p)
134 {
135 if(p!= NULL)
136 {
137 cout << p->data << " ";
138 Preorder(p->left);
139 Preorder(p->right);
140 }
141 }//end of Preorder
142
143
144 /*-------------------Postorder--------------*/
145 void BST :: Postorder(BSTNode *p)
146 {
147 if(p!= NULL)
148 {
149 Postorder(p->left);
150 Postorder(p->right);
151 cout << p->data << " ";
152 }
153 }//end of Postorder

164 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154
155 /*-------------------Count-----------------*/
156 void BST :: Count()
157 {
158 cout << "The number of elements in the tree are: " << count;
159
160 }//end of Count
161
162
163 /*------------------Search------------------*/
164 void BST :: Search(int x)
165 {
166 BSTNode *temp = root;
167 int flag=0;
168
169 while(temp != NULL)
170 {
171 if(x < temp->data)
172 {
173 temp = temp->left;
174 }
175 else if(x > temp->data)
176 {
177 temp = temp->right;
178 }
179 else //Mil gayaaa
180 {
181 flag=1;
182 break;
183 }
184
185 }//end of while
186
187 if(flag==1)
188 {
189 cout << "The Element "<< x << " is found";
190 }
191 else
192 {
193 cout << "The Element "<< x << " is not found";
194 }

165 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 }//end of Search


196
197 /*------------------FindMax------------------*/
198 void BST :: FindMax()
199 {
200 BSTNode *temp = root;
201
202 while(temp->right != NULL)
203 {
204 temp = temp->right;
205 }
206
207 cout << "Maximum value in the BST: " << temp->data;
208 }//end of FindMax
209
210
211 /*------------------FindMin------------------*/
212 void BST :: FindMin()
213 {
214 BSTNode *temp = root;
215
216 while(temp->left != NULL)
217 {
218 temp = temp->left;
219 }
220
221 cout << "Minimum value in the BST: " << temp->data;
222 }//end of FindMax
223
224 /*------------------FindMin------------------*/
225
226
227
228 /*----------4.Menu--------*/
229 int main()
230 {
231 int ch,num;
232 BST b;
233
234 while(1)
235 {
system("cls");
166 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 cout <<" **** Binary Search tree ***\n\n";


237 cout << "1. Insert the element\n";
238 cout << "2. Search for the element\n";
239 cout << "3. Display the tree\n";
240 cout << "4. Count\n";
241 cout << "5. Find the maximum element\n";
242 cout << "6. Find the minimum element\n";
243 cout << "7. Exit\n";
244
245 cout << "Enter your choice: ";
246 cin >> ch;
247 switch(ch)
248 {
249 case 1:
250 cout << "Enter an element: ";
251 cin>>num;
252 b.Insert(num);
253 getch();
254 break;
255
256 case 2:
257 cout << "Enter an element to be searched: ";
258 cin>>num;
259 b.Search(num);
260 getch();
261 break;
262
263 case 3:
264 b.Display();
265 getch();
266 break;
267
268 case 4:
269 b.Count();
270 getch();
271 break;
272
273 case 5:
274 b.FindMax();
275 getch();
276 break;

167 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

277
278 case 6:
279 b.FindMin();
280 getch();
281 break;
282
283 case 7:
284 exit(1);
285
286 default:
287 cout << "Incorrect Choice!";
288 getch();
289 }//end of switch
290
291 }//end of while
292 }//end of main
293

Output:

(Insertion)

168 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display)

(Search)

169 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Count)

170 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Maximum Element)

(Minimum Element)

171 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

172 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 25.


Roll No : 1409
Title of Program : Max Heaps
Objective : Unit-6: Trees

Date: 11.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 6: Trees
4 Program: Max-Heap*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 20
10
11 using namespace std;
12
13 /*----------1. Node Template -Not required----------*/
14
15
16 /*----------2.Array Based Template for Max-Heap--------*/
17 class Heap
18 {
19 int arr[MAX];
20 int n;
21
22 public:
23 Heap()
24 {
25 int i;
26 for(i=0;i<MAX;i++)
27 {
28 arr[i] = 0;
29 }
30 n=0;

173 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 }
32
33
34 void CreateHeap();
35 void BuildHeap();
36 void InsertHeap(int x);
37 void DeleteHeap();
38 void Display();
39 void reheapUp(int i);
40 void reheapDown(int pos);
41
42 };
43
44 /*----------------3.Functions------------------*/
45
46 /*---------------CreateHeap--------------*/
47 void Heap::CreateHeap()
48 {
49 int i;
50
51 cout << "Enter the number of elements in the heap: ";
52 cin >> n; //n declared in the Heap class
53
54 cout << "Enter the elements:\n ";
55
56 for(i=0; i<n; i++)
57 {
58 cout << "arr[" << i << "] = ";
59 cin >> arr[i];
60 }
61
62 BuildHeap();
63
64 }//end of CreateHeap
65
66
67
68 /*----------------BuildHeap----------------*/
69 void Heap::BuildHeap()
70 {
71 int i;

174 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72
73 for(i=(n/2)-1; i>=0; i--)
74 {
75 reheapDown(i);
76 }
77
78 }//end of BuildHeap
79
80
81 /*----------------reheapDown--------------*/
82 void Heap::reheapDown(int pos)
83 {
84 int i,val;
85
86 val = arr[pos];//Element to be floated down
87
88 while(pos <= (n/2)-1) // till last non-leaf position
89 {
90 i = (2*pos) + 1; //left child index
91
92 //Find the greater of left and right child
93 if(arr[i] < arr[i+1])
94 {
95 i++; //maintain larger child in i
96 }
97
98 //Compare val with larger child
99 if(val > arr[i])
100 {
101 break; //Nothing to be done
102 }
103
104 arr[pos] = arr[i]; //Shift larger child upwards
105
106
107 //Repeat the same steps on the ith index
108 pos = i;
109
110 }//end of while
111
112 //Copy val to its correct location

175 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 arr[pos] = val;


114
115 }//end of reheapDown
116
117 /*-------------------Display--------------*/
118 void Heap::Display()
119 {
120 if(n == 0)
121 {
122 cout << "Heap is empty";
123 return;
124 }
125
126 int i;
127 for(i=0;i<n;i++)
128 {
129 cout << "arr[" << i << "] = " << arr[i] << endl;
130 }
131
132 }//end of Display
133
134 /*------------------DeleteHeap--------------*/
135 void Heap::DeleteHeap()
136 {
137 //Empty Heap
138 if(n==0)
139 {
140 cout << "Empty Heap!";
141 return;
142 }
143
144 int temp = arr[0]; //Copy the root element to temp
145
146 arr[0] = arr[n-1]; //Copy last element to root
147 arr[n-1] = 0; //Update last element to NIL
148 n--; //Decrement total number of elements
149
150 reheapDown(0); //reheapDown on root
151
152 cout << "Element Deleted: " << temp << endl;
153

176 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 }//end of DeleteHeap


155
156 /*------------------InsertHeap--------------*/
157 void Heap::InsertHeap(int x)
158 {
159 arr[n] = x; //Insert x at nth position
160 n++; //Increment total number of elements
161
162 reheapUp(n-1);
163 }//end of InsertHeap
164
165
166 /*------------------reheapUp--------------*/
167 void Heap::reheapUp(int i)
168 {
169 int val = arr[i]; //Copy element to be floated up in val
170
171 //arr[(i-1)/2] is the parent node
172
173 while(i>0 && arr[(i-1)/2] <= val)
174 {
175 arr[i] = arr[(i-1)/2];
176 i = (i-1)/2; //Repeat the same steps on the parent index
177 }//end of while
178
179 arr[i] = val; //Copy val to its final position
180
181 }//end of reheapUp
182
183 /*------------------4.Menu--------------*/
184 int main()
185 {
186 int ch,num;
187 Heap h;
188
189 while(1)
190 {
191 system("cls");
192 cout << "*** Max-Heap***\n\n";
193
194 cout << "1.Create a Heap\n";

177 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 cout << "2.Insert an element in the Heap\n";


196 cout << "3.Delete Heap\n";
197 cout << "4.Display the Heap\n";
198 cout << "5.Exit\n";
199 cout << "Enter your choice: ";
200 cin >> ch;
201
202
203 switch(ch)
204 {
205
206 case 1:
207 h.CreateHeap();
208 getch();
209 break;
210
211 case 2:
212 cout << "Enter the element to be inserted: ";
213 cin >> num;
214 h.InsertHeap(num);
215 getch();
216 break;
217
218 case 3:
219 h.DeleteHeap();
220 getch();
221 break;
222
223 case 4:
224 h.Display();
225 getch();
226 break;
227
228 case 5:
229 exit(1);
230
231 default:
232 cout << "Incorrect Choice!";
233 getch();
234 }//end of switlch
235 }//end of while

178 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236
237 }//end of main

Output:

(Create)

(Display)

179 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Delete - 37)

180 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Delete root - 18)

181 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Insert)

182 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

183 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 26.


Roll No : 1409
Title of Program : Min Heaps
Objective : Unit-6: Trees

Date: 25.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 6: Trees
4 Program: Min-Heap*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 20
10
11 using namespace std;
12
13 /*----------1. Node Template -Not required----------*/
14
15
16 /*----------2.Array Based Template for Max-Heap--------*/
17 class Heap
18 {
19 int arr[MAX];
20 int n;
21
22 public:
23 Heap()
24 {
25 int i;
26 for(i=0;i<MAX;i++)
27 {
28 arr[i] = 0;
29 }
30 n=0;

184 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 }
32
33
34 void CreateHeap();
35 void BuildHeap();
36 void InsertHeap(int x);
37 void DeleteHeap();
38 void Display();
39 void reheapUp(int i);
40 void reheapDown(int pos);
41
42 };
43
44 /*----------------3.Functions------------------*/
45
46 /*---------------CreateHeap--------------*/
47 void Heap::CreateHeap()
48 {
49 int i;
50
51 cout << "Enter the number of elements in the heap: ";
52 cin >> n; //n declared in the Heap class
53
54 cout << "Enter the elements:\n ";
55
56 for(i=0; i<n; i++)
57 {
58 cout << "arr[" << i << "] = ";
59 cin >> arr[i];
60 }
61
62 BuildHeap();
63
64 }//end of CreateHeap
65
66
67
68 /*----------------BuildHeap----------------*/
69 void Heap::BuildHeap()
70 {
71 int i;

185 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72
73 for(i=(n/2)-1; i>=0; i--)
74 {
75 reheapDown(i);
76 }
77
78 }//end of BuildHeap
79
80
81 /*----------------reheapDown--------------*/
82 void Heap::reheapDown(int pos)
83 {
84 int i,val;
85
86 val = arr[pos];//Element to be floated down
87
88 while(pos <= (n/2)-1) // till last non-leaf position
89 {
90 i = (2*pos) + 1; //left child index
91
92 //Find the smaller of left and right child
93 if(i+1<n && arr[i] > arr[i+1])
94 {
95 i++; //maintain smaller child in i
96 }
97
98 //Compare val with smaller child
99 if(val <= arr[i])
100 {
101 break; //Nothing to be done
102 }
103
104 arr[pos] = arr[i]; //Shift smaller child upwards
105
106
107 //Repeat the same steps on the ith index
108 pos = i;
109
110 }//end of while
111
112 //Copy val to its correct location

186 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 arr[pos] = val;


114
115 }//end of reheapDown
116
117 /*-------------------Display--------------*/
118 void Heap::Display()
119 {
120 if(n == 0)
121 {
122 cout << "Heap is empty";
123 return;
124 }
125
126 int i;
127 for(i=0;i<n;i++)
128 {
129 cout << "arr[" << i << "] = " << arr[i] << endl;
130 }
131
132 }//end of Display
133
134 /*------------------DeleteHeap--------------*/
135 void Heap::DeleteHeap()
136 {
137 //Empty Heap
138 if(n==0)
139 {
140 cout << "Empty Heap!";
141 return;
142 }
143
144 int temp = arr[0]; //Copy the root element to temp
145
146 arr[0] = arr[n-1]; //Copy last element to root
147 arr[n-1] = 0; //Update last element to NIL
148 n--; //Decrement total number of elements
149
150 reheapDown(0); //reheapDown on root
151
152 cout << "Element Deleted: " << temp << endl;
153

187 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 }//end of DeleteHeap


155
156 /*------------------InsertHeap--------------*/
157 void Heap::InsertHeap(int x)
158 {
159 arr[n] = x; //Insert x at nth position
160 n++; //Increment total number of elements
161
162 reheapUp(n-1);
163 }//end of InsertHeap
164
165
166 /*------------------reheapUp--------------*/
167 void Heap::reheapUp(int i)
168 {
169 int val = arr[i]; //Copy element to be floated up in val
170
171 //arr[(i-1)/2] is the parent node
172
173 while(i>0 && arr[(i-1)/2] > val)
174 {
175 arr[i] = arr[(i-1)/2];
176 i = (i-1)/2; //Repeat the same steps on the parent index
177 }//end of while
178
179 arr[i] = val; //Copy val to its final position
180
181 }//end of reheapUp
182
183 /*------------------4.Menu--------------*/
184 int main()
185 {
186 int ch,num;
187 Heap h;
188
189 while(1)
190 {
191 system("cls");
192 cout << "*** Min-Heap***\n\n";
193
194 cout << "1.Create a Heap\n";

188 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 cout << "2.Insert an element in the Heap\n";


196 cout << "3.Delete Heap\n";
197 cout << "4.Display the Heap\n";
198 cout << "5.Exit\n";
199 cout << "Enter your choice: ";
200 cin >> ch;
201
202
203 switch(ch)
204 {
205
206 case 1:
207 h.CreateHeap();
208 getch();
209 break;
210
211 case 2:
212 cout << "Enter the element to be inserted: ";
213 cin >> num;
214 h.InsertHeap(num);
215 getch();
216 break;
217
218 case 3:
219 h.DeleteHeap();
220 getch();
221 break;
222
223 case 4:
224 h.Display();
225 getch();
226 break;
227
228 case 5:
229 exit(1);
230
231 default:
232 cout << "Incorrect Choice!";
233 getch();
234 }//end of switch
235 }//end of while

189 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236
237 }//end of main

Output:

(Create a heap)

(Display)

190 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Insert)

191 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Delete heap - root 10 is deleted)

192 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Unit-7:Graphs
Program No: 27.
Roll No : 1409
Title of Program : Representation of Graphs using Adjacency Matrix
Objective : Unit-7: Graphs

Date: 07.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 7: Graphs
4 Program: Adjacency Matrix*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 10
10 using namespace std;
11
12
13 /*----------1. Node Template -Not required----------*/
14
15
16 /*----------2.Array Based Template for Graph--------*/
17 class Graph
18 {
19 int adj[MAX][MAX];
20 int n,e;
21
22 public:
23 Graph()
24 {
25 int i,j;
26
27 for(i=0; i<MAX; i++)
28 {
29 for(j=0; j<MAX; j++)

193 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

30 {
31 adj[i][j] = 0;
32 }
33 }
34
35 n = 0;
36 e = 0;
37 }
38
39 void CreateGraph();
40 void Display();
41 };
42
43 /*------------3.Functions-------------*/
44
45 /*---------------CreateGraph-------------*/
46 void Graph :: CreateGraph()
47 {
48 cout << "Enter the number of vertices: ";
49 cin >> n; //n is the declared in the Graph Class
50
51 cout << "Enter the number of edges: ";
52 cin >> e; //e is declared in the Graph Class
53
54
55 int source,dest;
56 int ne=1;
57
58 while(ne<=e)
59 {
60 cout << "Enter Edge " << ne << endl;
61 cout << "Enter the source vertex: ";
62 cin >> source;
63
64 cout << "Enter the destination vertex: ";
65 cin >> dest;
66
67 adj[source][dest] = 1;
68 adj[dest][source] = 1;
69
70 ne++;

194 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

71 }
72
73 }//end of CreateGraph
74
75 /*-------------------Display----------------*/
76 void Graph :: Display()
77 {
78 int i,j;
79 cout << "The Adjacency Matrix of the graph is: " << endl;
80 for(i=0;i<n;i++)
81 {
82 for(j=0;j<n;j++)
83 {
84 cout << adj[i][j] << " ";
85 }
86 cout<<endl;
87 }
88
89 }//end of Display
90
91 /*------------------4.Menu--------------*/
92 int main()
93 {
94 int ch;
95 Graph g;
96
97 while(1)
98 {
99 system("cls");
100
101 cout << "*** Graph - Adjacency Matrix ***\n\n";
102 cout << "1.Create a graph\n";
103 cout << "2.Display the graph\n";
104 cout << "3.Exit\n";
105
106 cout << "Enter your choice: ";
107 cin >> ch;
108
109 switch(ch)
110 {
111 case 1:

195 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

112 g.CreateGraph();
113 getch();
114 break;
115
116 case 2:
117 g.Display();
118 getch();
119 break;
120 case 3:
121 exit(1);
122 default:
123 cout << "Incorrect Choice!";
124 getch();
125 }//end of switch
126 }//end of while
127 }//end of main

Output:

(Graph)

196 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Create a graph)

(Display)

197 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 28.


Roll No : 1409
Title of Program : Perform Breadth-First Traversal on a Graph using Queues
Objective : Unit-7: Graphs

Date: 07.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 7: Graphs
4 Program: Breadth First Traversal*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 10
10 using namespace std;
11
12
13 /*---------- Queue Template ----------*/
14 class Queue
15 {
16 int a[MAX];
17 int front,rear;
18
19 public:
20 Queue()
21 {
22 front = -1;
23 rear = -1;
24 }
25
26 void Enqueue(int x);
27 int Dequeue();
28 int Empty();
29 };
30

198 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 int Queue :: Empty()


32 {
33 if(front == -1)
34 {
35 return 1;
36 }
37 else
38 {
39 return 0;
40 }
41 }
42
43 void Queue :: Enqueue(int x)
44 {
45 rear++;
46 a[rear] = x;
47
48 if(front == -1)
49 {
50 front++;
51 }
52 }
53
54 int Queue :: Dequeue()
55 {
56 if(Empty())
57 {
58 cout << "Queue Underflow!";
59 return -1;
60 }
61
62 int temp = a[front];
63
64 if(front == rear)
65 {
66 front = rear = -1;
67 }
68 else
69 {
70 front++;
71 }

199 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 return temp;
73
74 }
75
76 /*---------- Graph Template ----------*/
77 /*----------1. Node Template -Not required----------*/
78
79 /*----------2.Array Based Template for Graph--------*/
80 class Graph
81 {
82 int adj[MAX][MAX];
83 int visited[MAX];
84 int n,e;
85
86 public:
87 Graph()
88 {
89 int i,j;
90
91 for(i=0; i<MAX; i++)
92 {
93 for(j=0; j<MAX; j++)
94 {
95 adj[i][j] = 0;
96 }
97 }
98
99 n = 0;
100 e = 0;
101 }
102
103 void CreateGraph();
104 void Display();
105 void BFT(int x);
106 };
107
108 /*------------3.Functions-------------*/
109
110 /*---------------CreateGraph-------------*/
111 void Graph :: CreateGraph()
112 {

200 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 cout << "Enter the number of vertices: ";


114 cin >> n; //n is the declared in the Graph Class
115
116 cout << "Enter the number of edges: ";
117 cin >> e; //e is declared in the Graph Class
118
119
120 int source,dest;
121 int ne=1;
122
123 while(ne<=e)
124 {
125 cout << "Enter Edge " << ne << endl;
126 cout << "Enter the source vertex: ";
127 cin >> source;
128
129 cout << "Enter the destination vertex: ";
130 cin >> dest;
131
132 adj[source][dest] = 1;
133 adj[dest][source] = 1;
134
135 ne++;
136 }
137
138 }//end of CreateGraph
139
140 /*-------------------Display----------------*/
141 void Graph :: Display()
142 {
143 int i,j;
144 cout << "The Adjacency Matrix of the graph is: " << endl;
145 for(i=0;i<n;i++)
146 {
147 for(j=0;j<n;j++)
148 {
149 cout << adj[i][j] << " ";
150 }
151 cout<<endl;
152 }
153

201 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 }//end of Display


155
156
157 void Graph :: BFT(int x)
158 {
159
160 Queue q;
161 int i;
162
163 for(i=0; i<MAX; i++)
164 {
165 visited[i] = 0;
166 }
167
168 //Update the starting vertex
169 visited[x] = 1;
170 q.Enqueue(x);
171
172 cout << "Order of Traversal: ";
173
174 while(!q.Empty())
175 {
176 x = q.Dequeue();
177
178 cout << x << " ";
179
180 for(i=0; i<n; i++)
181 {
182 if(adj[x][i] == 1 && visited[i] == 0) //Neighbour & Visited Status
183 {
184 visited[i] = 1;
185 q.Enqueue(i);
186 }
187 }//end of i
188 }//end of while
189
190
191 }//end of BFT
192
193
194 /*------------------4.Menu--------------*/
int main()
202 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 {
196 int ch,num;
197 Graph g;
198
199 while(1)
200 {
201 system("cls");
202
203 cout << "*** Graph - Breadth First Traversal ***\n\n";
204 cout << "1.Create a graph\n";
205 cout << "2.Display the graph\n";
206 cout << "3.Breadth First Traversal\n";
207 cout << "4.Exit\n";
208
209 cout << "Enter your choice: ";
210 cin >> ch;
211
212 switch(ch)
213 {
214 case 1:
215 g.CreateGraph();
216 getch();
217 break;
218
219 case 2:
220 g.Display();
221 getch();
222 break;
223 case 3:
224 cout << "Enter the starting vertex: ";
225 cin >> num;
226
227 g.BFT(num);
228 getch();
229 break;
230 case 4:
231 exit(1);
232 default:
233 cout << "Incorrect Choice!";
234 getch();
235 }//end of switch

203 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

236 }//end of while


237 }//end of main
238

Output:
(Graph)

(Create a graph)

204 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Display)

(Breadth First Traversal)

205 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

Program No: 29.


Roll No : 1409
Title of Program : Find MST using Kruskal’s Algorithm
Objective : Unit-7: Graphs

Date: 09.12.2023

Source Code:

1 /* Name: Sharvari Birajdar


2 Roll no: 1409
3 Unit 7: Graphs
4 Program: Kruskal's Algorithm for MST*/
5
6 #include<iostream>
7 #include<conio.h>
8
9 #define MAX 10
10 #define INF 999
11 using namespace std;
12
13
14 /*----------1. Node Template -Not required----------*/
15
16
17 /*----------2.Array Based Template for Graph--------*/
18 class Graph
19 {
20 int adj[MAX][MAX];
21 int n,e;
22 int parent[MAX];
23
24 public:
25 Graph()
26 {
27 int i,j;
28
29 for(i=0; i<MAX; i++)
30 {

206 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

31 for(j=0; j<MAX; j++)


32 {
33 adj[i][j] = INF;
34 }
35 }
36 for(i=0;i<MAX;i++)
37 {
38 parent[i] = -1;
39 }
40 n = 0;
41 e = 0;
42 }
43
44 void CreateGraph();
45 void Display();
46 void Kruskal();
47 int Find(int i);
48 int Union(int i,int j);
49 };
50
51
52 /*------------3.Functions-------------*/
53
54 /*---------------CreateGraph-------------*/
55 void Graph :: CreateGraph()
56 {
57 cout << "Enter the number of vertices: ";
58 cin >> n; //n is the declared in the Graph Class
59
60 cout << "Enter the number of edges: ";
61 cin >> e; //e is declared in the Graph Class
62
63 int source, dest, weight;
64 int ne=1;
65
66 while(ne<=e)
67 {
68 cout << "Enter Edge " << ne << endl;
69 cout << "Enter the source vertex: ";
70 cin >> source;
71

207 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

72 cout << "Enter the destination vertex: ";


73 cin >> dest;
74
75 cout << "Enter weight: ";
76 cin >> weight;
77
78 adj[source][dest] = weight;
79 adj[dest][source] = weight;
80
81 ne++;
82 }
83
84 }//end of CreateGraph
85
86 /*-------------------Display----------------*/
87 void Graph :: Display()
88 {
89 int i,j;
90 cout << "The Adjacency Matrix of the graph is: " << endl;
91 for(i=0;i<n;i++)
92 {
93 for(j=0;j<n;j++)
94 {
95 cout << adj[i][j] << " ";
96 }
97 cout<<endl;
98 }
99
100 }//end of Display
101
102
103 /*-----------------Kruskal----------------*/
104 void Graph :: Kruskal()
105 {
106 int minimum,i,j;
107 int ne=1;//ne- loop
108 int a,b,u,v;
109 int mincost=0;
110
111
112 while(ne < n)

208 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

113 {
114 //Find the minimum edge
115 for(i=0,minimum=999; i<n; i++)
116 {
117 for(j=0; j<n; j++)
118 {
119 if(adj[i][j] < minimum)
120 {
121 minimum = adj[i][j];
122 a = u = i;
123 b = v = j;
124 }//end of j
125 }
126 }//end of i
127
128 u = Find(u);
129 v = Find(v);
130
131 if(Union(u,v))
132 {
133 cout << "Edge: " << ne++ << ": (" << a << " , " << b << ") = " << adj[a][b] << endl;
134 mincost += minimum;
135 }
136
137 //To ensure Edge is not selected again
138 adj[a][b] = adj[b][a] = INF;
139
140 }//end of while
141
142 cout << "MST Total Cost is: " << mincost;
143
144 }//end of Kruskal
145
146
147 /*------------------Find------------------*/
148 int Graph :: Find(int i)
149 {
150 while(parent[i]!=-1)
151 {
152 i = parent[i];
153 }

209 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

154 return i;
155 }
156
157 /*----------------Union--------------------*/
158 int Graph :: Union(int i,int j)
159 {
160 if(i!=j)
161 {
162 parent[j] = i;
163 return 1;
164 }
165 }
166
167
168 /*------------------4.Menu--------------*/
169 int main()
170 {
171 int ch;
172 Graph g;
173
174 while(1)
175 {
176 system("cls");
177
178 cout << "*** Graph - Minimum Spanning Tree ***\n\n";
179
180 cout << "1.Create a graph\n";
181 cout << "2.Display the graph\n";
182 cout << "3.MST-Kruskal's Algorithm'\n";
183 cout << "4.Exit\n";
184
185 cout << "Enter your choice: ";
186 cin >> ch;
187
188 switch(ch)
189 {
190 case 1:
191 g.CreateGraph();
192 getch();
193 break;
194

210 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

195 case 2:
196 g.Display();
197 getch();
198 break;
199
200 case 3:
201 g.Kruskal();
202 getch();
203 break;
204
205 case 4:
206 exit(1);
207
208 default:
209 cout << "Incorrect Choice!";
210 getch();
211 }//end of switch
212 }//end of while
213 }//end of main

Output:

(Graph)

211 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(Create a graph)

(Display the adjacency matrix)

212 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science

(MST-Kruskal’s Algorithm)

213 |

You might also like