Array & Linked List Difference between in DS
Slno. | Array | Linked List |
01. | They are static, i.e. size of an array is fixed. | They are dynamic, i.e. size of a list is not fixed. |
02. | Memory is allocated/formed in the form of a stack. | Memory is allocated/formed in the form of a heap. |
03. | Random access is efficient. | Random access is inefficient. |
04. | Element rearranging is difficult/inefficient. | Element rearranging is easy/efficient. |
05. | It is necessary to specify the number of elements for an array during declaration/ compile time. | It is not necessary to specify the number of elements for a list during declaration/ memory is allocated during run time. |
06. | It occupies less memory than a linked list for the same number of elements. | It occupies more memory. |
07. | Inserting new elements at the front is potentially expensive because existing elements need to be shifted over to make room. | Inserting a new element at any position can be carried out easily. |
08. | Deleting an element from an array is not possible. | Deleting an element from a list is possible. |
09. | Overhead per element is none. | Overhead per element is for 1 or 2 links. |
Difference between Malloc() & Calloc()
Slno. | calloc() | malloc() |
01. | The calloc() function allocates multiple blocks of requested memory. | The malloc() function allocates a single block of requested memory. |
02. | It initializes the created memory with zero, hence no garbage values are present in the memory. | It does not initialize the memory during allocation, hence garbage values are present in the memory. |
03. | It takes two arguments. | It takes only one argument. |
04. | Calloc() is better than malloc() due to no garbage values in allocated memory. | malloc() is less better than calloc() due to garbage values being present in allocated memory. |
05. | calloc() is more secure and provides more space during allocation than malloc(). | malloc() is less secure and provides less space during allocation than calloc(). |
06. | The calloc function is normally used for allocating memory to derived data types such as arrays and structures. If it fails to allocate enough space as specified, it returns a NULL pointer. | We use malloc() when we need to allocate objects that must exist beyond the lifetime of execution of the current block, or if we need to allocate memory greater than the size of the stack. |
07. | calloc() is comparatively slower. | malloc() is comparatively faster. |
08. | The calloc() is used to allocate multiple blocks of memory space having the same block size. | malloc() is used to allocate a large single block of memory space. |
Difference between Static Memory Allocation & Dynamic Memory Allocation
Slno. | Static Memory Allocation | Dynamic Memory Allocation |
01. | Memory is allocated before the execution of the program begins/starts i.e., during compilation time. | Memory is allocated during the execution of the program, i.e., at run time. |
02. | Implemented using the stack data structure. | Implemented using the heap data structure. |
03. | Less efficient method and is less used. | More efficient method and is mostly used. |
04. | There is no memory reusability. | There is memory reusability, and memory can be freed when not required. |
05. | Variables remain permanently allocated. | Memory is allocated only when the program unit is active. |
06. | In this type of allocation, memory cannot be resized again after the initial allocation. | In this type of allocation, memory can be dynamically expanded and shrunk as needed in the program. |
07. | Faster execution than Dynamic. | Slower execution than Static. |
08. | Implementation of this type of memory allocation is comparatively simple. | Implementation of this type of memory allocation is comparatively complicated. |
09. | In static memory allocation, we cannot reuse the unused memory, i.e., memory is wasted. | Memory can be freed when it is no longer needed & reused or reallocated during execution. Hence, no memory wasting occurs. |
Difference between Tree and Graph
Slno. | Tree | Graph |
01. | A Tree is a special, strict, and cycle-free type of graph. | A Graph is flexible and general, allowing all kinds of connections. |
02. | A Tree always starts with a single root node. | No concept of root, rather connections can begin anywhere. Does not necessarily have a root node. |
03. | A tree is like a family chart, showing hierarchy from top (root) to bottom. | A graph is like a social network where any node can connect to any other. |
04. | Never form/create a cyclic structure(=acyclic). | Usually form cycles or loops. |
05. | If there are N nodes in a tree, it always has N-1 edges. | A graph can have from 0 edges to many (up to N(N-1)/2 in simple graphs). |
06. | In a tree, all nodes are always connected; no isolated nodes are possible. Always, a path exists between any two nodes | In a Graph, nodes may or may not be connected; when not connected, isolated nodes are possible. |
07. | A tree usually has a natural direction of traversing (parent → child). They have clear-cut parent-child relationships. | In a Graph, there are no inherent parent-child relationships; In a Graph, nodes can be traversed in any connected direction, as needed, hence connections are more flexible. |
08. | The concept of a Tree can be used in file systems, decision trees, databases, parsing expressions, etc. | The concept of a Graph can be used in social networks, computer networks, GPS maps, transportation systems, airline routes, and web links. |
09. | They have a hierarchical structure. | They have no hierarchical structure. |
10. | They have a specific traversal order – pre-order, in order, and post-order. | They also have a specific traversal order -Breadth-First Search (BFS), Depth-First Search (DFS). |
11. | Comparatively simpler structure, generally easier to implement and traverse. | Comparatively more complex structure, can be challenging to traverse and analyze due to cycles and varied connectivity. |
0 Comments