When you create a variable, you reserve some space in memory. Different data types take up different amount of space. Some of the fundamental data types in C++ are : source : https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm Calculation for range of int : 1 int contains 4 bytes and 1 byte contains 8 bits So, 1 int contains = 4x8 = 32 bits n bits can have 2^n patterns So, 32 bits have 2^32 = 4294967296, which is the range of unsigned int. Size of data types can also vary by machine.
I n-Place Algorithms are required to solve those problems which have limited memory constraints. You are not allowed to use extra memory and hence all operations need to be performed on the original data structures themselves. Usually, the algorithms should use O(1), constant memory. You are allowed to declare variables but not complete data structures. For example, you are given an array [1,2,2,4] and you need to delete all values which are equal to 2. One approach could be by checking each element of the array. If it is 2 then skip otherwise store the value in a new array and at the end of the loop[ when traversed every element of array ], return the new array. But this approach is not In-Place as it requires O(n) memory [ Worst case when no element of given value in array ]. For an In-Place algorithm we would need to delete the elements with value in the original array and then return the original array as the answer to the problem.
STACK A stack is a linear data structure that only allows operations on the last added element in the stack. A stack can be visualized as a jar that is open from the top, so you can only put stuff or take out stuff from the top. Stacks are almost always with two basic operations - push and pop. Push inserts a new element in the stack from the top and Pop takes out the element at the top of the stack. Stacks follow LIFO order (Last In First Out). A stack can be implemented using array or linked list. Top in stack is the element at the top of the stack. In C++, we can use the Standard Template Library implementation of Stack as: #include <iostream> #include <stack> using namespace std; int main() { stack <int> x; x.push(1); x.push(2); x.push(4); cout<<x.top()<<endl; // 4 is at top x.pop(); // pops 4 x.pop(); // pops 2 cout<<x.top()<<endl; /...
Comments
Post a Comment