Professional Documents
Culture Documents
DS 1409
DS 1409
1|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
2|
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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:
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:
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:
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:
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:
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:
15 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
Output:
(Target Found)
16 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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:
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)
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:
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:
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
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
35 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
Date: 25.12.2023
Source Code:
36 |
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
40 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
44 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
Date: 25.12.2023
Source Code:
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
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:
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
(Display)
59 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
60 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
Date: 04.11.2023
Source Code:
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
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
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
Date: 09.11.2023
Source Code:
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
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:
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
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
Date: 25.11.2023
Source Code:
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
90 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
Date: 04.12.2023
Source Code:
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
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
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
Date: 02.12.2023
Source Code:
105 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
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
Date: 30.09.2023
Source Code:
117 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
118 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
122 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
Date: 09.10.2023
Source Code:
128 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
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
Date: 16.10.2023
Source Code:
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
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
143 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
Date: 28.10.2023
Source Code:
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
150 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
Output:
156 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
157 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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:
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
163 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
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
Date: 11.12.2023
Source Code:
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
176 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
177 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
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
Date: 25.12.2023
Source Code:
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
187 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
188 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
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:
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
Date: 07.12.2023
Source Code:
198 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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
201 |
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
Output:
(Graph)
(Create a graph)
204 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
(Display)
205 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
Date: 09.12.2023
Source Code:
206 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
207 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
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)
212 |
MUMBAI EDUCATIONAL TRUST
MET Institute of Computer Science
(MST-Kruskal’s Algorithm)
213 |