C++ Programming Code Examples
C++ > Data Structures Code Examples
C++ Program to Implement Skew Heap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* C++ Program to Implement Skew Heap
This C++ Program demonstrates the implementation of Skew Heap. */
#include <iostream>
#include <cstdlib>
using namespace std;
/* Skew Heap Class */
class Skew_Heap
{
public:
int key;
Skew_Heap *right;
Skew_Heap *left;
Skew_Heap *parent;
/* Constructor */
Skew_Heap()
{
key = 0;
right = NULL;
left = NULL;
parent = NULL;
}
/* Merging Skew Heaps */
Skew_Heap *merge(Skew_Heap *h1, Skew_Heap *h2)
{
Skew_Heap *temp;
if (h1 == NULL)
return h2;
else if (h2 == NULL)
return h1;
else
{
if (h1->key < h2->key)
{
temp = h1;
h1 = h2;
h2 = temp;
}
temp = h1->left;
h1->left = h1->right;
h1->right = temp;
h1->left = merge(h2, h1->left);
}
return h1;
}
/* Construct Skew Heap */
Skew_Heap *construct(Skew_Heap *root)
{
Skew_Heap *temp;
int input, val;
while (1)
{
cout<<"Enter the value of the node(0 to exit): ";
cin >> val;
if (val == 0)
break;
temp = new Skew_Heap;
temp->key = val;
root = merge(root, temp);
}
return root;
}
/* Insert into Skew Heap */
Skew_Heap *insert(Skew_Heap *root)
{
int val;
Skew_Heap *temp;
cout<<"Enter the value of the node: ";
cin >> val;
temp = new Skew_Heap;
temp->key = val;
root = merge(root, temp);
return root;
}
/* Delete Maximum from Skew Heap */
Skew_Heap *delete_max(Skew_Heap *root)
{
if (root == NULL)
{
cout << "The heap is empty"<<endl;
return NULL;
}
Skew_Heap *temp1, *temp2;
temp1 = root->left;
temp2 = root->right;
temp1 = merge(temp1, temp2);
cout << "Maximum Deleted "<<endl;
return temp1;
}
/* Preorder Traversal of Skew Heap */
void preorder(Skew_Heap *root)
{
if (root == NULL)
return;
else
{
cout << root->key <<" ";
preorder(root->left);
preorder(root->right);
}
return;
}
/* Inorder Traversal of Skew Heap */
void inorder(Skew_Heap *root)
{
if (root == NULL)
return;
else
{
inorder(root->left);
cout << root->key <<" ";
inorder(root->right);
}
return;
}
/* Postorder Traversal of Skew Heap */
void postorder(Skew_Heap *root)
{
if (root == NULL)
return;
else
{
postorder(root->left);
postorder(root->right);
cout << root->key <<" ";
}
return;
}
/* Display Skew Heap */
void print_heap(Skew_Heap *root)
{
cout << "Preorder traversal of the heap is " << endl;
preorder(root);
cout << endl;
cout << "Inorder traversal of the heap is " << endl;
inorder(root);
cout << endl;
cout << "Postorder traversal of the heap is " << endl;
postorder(root);
cout << endl;
}
};
/* Main Contains Menu */
int main()
{
Skew_Heap *head1, *temp1, *temp2, obj, *temp3;
int input;
head1 = NULL;
temp1 = NULL;
temp2 = NULL;
temp3 = NULL;
while(1)
{
cout << endl<< "-----------Operations on Skew Heap---------"<<endl;
cout << "1.Insert element into the heap"<<endl;
cout << "2.Delete maximum element from the heap"<<endl;
cout << "3.Merge two heaps"<<endl;
cout << "4.Print the heap"<<endl;
cout << "5.Print the maximum element of the heap"<<endl;
cout << "6.Merge the present heap with another heap"<<endl;
cout << "7.Exit"<<endl;
cout << "Enter your Choice: ";
cin >> input;
switch(input)
{
case 1:
head1 = obj.insert(head1);
break;
case 2:
head1 = obj.delete_max(head1);
break;
case 3:
cout << "Enter the first heap: "<<endl;
temp1 = obj.construct(temp1);
cout << "Enter the second heap: "<<endl;
temp2 = obj.construct(temp2);
temp1 = obj.merge(temp1, temp2);
cout << "The heap obtained after merging is: "<<endl;
obj.print_heap(temp1);
break;
case 4:
obj.print_heap(head1);
break;
case 5:
cout << "The maximum element existing in the heap is: ";
cout << head1->key << endl;
break;
case 6:
cout << "Enter the second heap"<<endl;
temp3 = obj.construct(temp3);
temp3 = obj.merge(temp3, head1);
cout << "The heap obtained after merging is: "<<endl;
obj.print_heap(temp3);
break;
case 7:
exit(1);
default:
cout<<"Enter Correct Choice"<<endl;
break;
}
}
return 0;
}
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.
In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.
The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)
Allocate storage space. Default allocation functions (single-object form). A new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the program.
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
In C++ language, Multilevel Inheritance The class A serves as a base class for the derived class B, which in turn serves as a 'base class' for the derived class C. The class B is known
Program 'takes the input' of a set of integers or characters. It firstly generates the random partition of the "Length of the Set". Starting from the beginning, it 'prints the number' of
You enter number size then ask to enter the numbers of that size. To calculate arithmetic 'mean of all numbers', first perform addition of all the numbers, and then make a variable