You are on page 1of 12

Contents

Advantage and disadvantage of linked list........................................................................................2


Queue concept implementation........................................................................................................2
Stack implementation........................................................................................................................2
Advantages of queue in linked list.....................................................................................................3
Advantages of queue concept in array..............................................................................................3
Disadvantages of queue in linked list................................................................................................4
Disadvantages of queue in array.......................................................................................................4
Basic operation of array and linked list..............................................................................................5
Binary search tree for linked list insert, delete and search................................................................6
Traversal method in binary tree( preorder, postorder, inorder, levelorder).....................................7
Graph data structure vectex / voice / weight....................................................................................9
Static memory allocation in c++.......................................................................................................10
dynamic memory allocation in c++..................................................................................................11
When to use normal variable and when to use pointer variable in c++..........................................11
How the pointer works in c++..........................................................................................................12
Linked list is a dynamic data structure

Linked list are faster at insertion and deletion

Traversal is hard in linked list

Linked list is good at implementing other data structures

Advantage and disadvantage of linked list


Advantages -it is a dynamic data structure – insertion and deletion is easier than in other linear data
structure – no memory wastage – helpful in implementing different data structure like stack and
queue.

Disadvantages – It takes a lot of memory to store data on node – traversal is harder than other data
structure – reverse traversing is easy in doubly linked list but it takes extra memory to store previous
pointer.

Queue concept implementation


A queue is an abstract data type that stores elements in a particular order. In a queue, the elements
are inserted at one end (the rear) and removed from the other end (the front). The two primary
ways of implementing a queue are using a linked list and an array.

Linked List Implementation: In a linked list implementation of a queue, each node of the linked list
contains the data and a pointer to the next node. The front of the queue is represented by the head
of the linked list, and the rear of the queue is represented by the tail of the linked list. When an
element is enqueued, it is added to the tail of the linked list. When an element is dequeued, it is
removed from the head of the linked list. This implementation has the advantage that it can easily
handle queues of any size, and the enqueue and dequeue operations take constant time O(1) in the
average case. However, it requires more memory than the array implementation because of the
extra space required for the pointers.

Array Implementation: In an array implementation of a queue, an array is used to store the elements
of the queue. The front of the queue is represented by the first element of the array, and the rear of
the queue is represented by the last element of the array. When an element is enqueued, it is added
to the rear of the array. When an element is dequeued, it is removed from the front of the array.
This implementation has the advantage that it uses less memory than the linked list implementation
because it does not require pointers. However, it requires a fixed-size array, and if the queue
becomes full, it cannot accommodate any more elements without resizing the array, which can be
expensive in terms of time complexity O(n). Additionally, the dequeue operation can be expensive in
the worst-case scenario when the front of the array has to be shifted to make room for the next
element.

Stack implementation
A stack is a linear data structure that stores a collection of elements and allows access to them in a
last-in-first-out (LIFO) order. In other words, the element that was most recently added to the stack
is the first one to be removed. The two primary ways of implementing a stack are using a linked list
and an array.
Linked List Implementation: In a linked list implementation of a stack, each node of the linked list
contains the data and a pointer to the next node. The top of the stack is represented by the head of
the linked list. When an element is pushed onto the stack, it is added to the head of the linked list.
When an element is popped from the stack, it is removed from the head of the linked list. This
implementation has the advantage that it can easily handle stacks of any size, and the push and pop
operations take constant time O(1) in the average case. However, it requires more memory than the
array implementation because of the extra space required for the pointers.

Array Implementation: In an array implementation of a stack, an array is used to store the elements
of the stack. The top of the stack is represented by the last element of the array. When an element is
pushed onto the stack, it is added to the next available position in the array. When an element is
popped from the stack, it is removed from the last position of the array. This implementation has the
advantage that it uses less memory than the linked list implementation because it does not require
pointers. However, it requires a fixed-size array, and if the stack becomes full, it cannot
accommodate any more elements without resizing the array, which can be expensive in terms of
time complexity O(n).

Both linked list and array implementations have their own advantages and disadvantages, and the
choice of implementation depends on the specific requirements of the application.

Advantages of queue in linked list


There are several advantages of implementing a queue using a linked list:

1. Dynamic Size: Linked lists can grow or shrink dynamically as elements are added or removed.
This makes it easy to implement a queue of any size without worrying about the fixed
capacity of an array.

2. Efficient Insertion and Removal: In a linked list implementation, adding an element to the
rear of the queue and removing an element from the front of the queue can be done in
constant time O(1) because the pointers only need to be updated.

3. Memory Efficiency: Linked list implementations of a queue use memory more efficiently
than array implementations because they only allocate memory as needed. This is
particularly useful for applications with unpredictable workloads or memory constraints.

4. Flexibility: Linked list implementations can be easily modified to include additional features
such as sorting, searching, or indexing.

5. Easy Implementation: Linked list implementations are relatively easy to understand and
implement, making them an ideal choice for beginners or projects with tight deadlines.

Overall, linked list implementations of queues are well-suited for dynamic applications where
efficient insertion and removal are critical, and memory efficiency is a concern.

Advantages of queue concept in array


There are several advantages of implementing a queue using an array:

1. Constant Time Access: In an array implementation, elements can be accessed in constant


time O(1) because they are stored in contiguous memory locations. This makes array
implementations of queues more efficient than linked list implementations for random
access.

2. Cache Efficiency: Arrays are more cache-efficient than linked lists because the elements are
stored in contiguous memory locations. This can result in faster memory access times and
improved performance.

3. Easy Implementation: Array implementations of queues are relatively easy to implement and
understand, making them a good choice for beginners or projects with tight deadlines.

4. Memory Efficiency: Array implementations of queues use memory more efficiently than
linked list implementations because they do not require extra memory for pointers.

5. Fixed Size: Arrays have a fixed size, which can be an advantage in certain applications where
the maximum size of the queue is known in advance. This eliminates the need to allocate
additional memory dynamically, which can be time-consuming and computationally
expensive.

Overall, array implementations of queues are well-suited for applications where constant time
access and cache efficiency are critical, and the maximum size of the queue is known in advance.

Disadvantages of queue in linked list


There are also some disadvantages to implementing a queue using a linked list:

1. Extra Memory: Linked lists require additional memory for storing pointers, which can be a
disadvantage in applications with limited memory resources.

2. Slower Access: Accessing elements in a linked list is slower than accessing elements in an
array because linked list elements are not stored in contiguous memory locations. This can
result in slower performance for random access operations.

3. No Constant Time Access: Unlike array implementations of queues, linked list


implementations do not support constant time access to elements. Accessing an element in
a linked list requires traversing the list from the beginning to the desired position, which can
be computationally expensive for large queues.

4. Complexity: Linked lists are more complex to implement and understand than arrays. This
can be a disadvantage for beginners or projects with tight deadlines.

5. Fragile: Linked lists are more fragile than arrays because they are composed of pointers that
must be updated carefully to avoid errors or memory leaks.

Overall, linked list implementations of queues are well-suited for dynamic applications where
memory efficiency and efficient insertion and removal are more important than constant time access
and cache efficiency.

Disadvantages of queue in array


There are also some disadvantages to implementing a queue using an array:
1. Fixed Size: Arrays have a fixed size, which can be a disadvantage in applications where the
maximum size of the queue is not known in advance. If the queue becomes full, additional
memory must be allocated, which can be time-consuming and computationally expensive.

2. Inefficient Removal: Removing an element from the front of an array-based queue requires
shifting all remaining elements down by one position, which can be computationally
expensive for large queues.

3. Memory Wastage: Arrays require a fixed amount of memory to be allocated, even if the
queue is not full. This can result in memory wastage for small or infrequently used queues.

4. No Dynamic Resizing: Once an array is created with a certain size, it cannot be resized
dynamically. This means that the maximum size of the queue is fixed and cannot be changed
easily.

5. Cache Inefficiency: If the size of the array is large, cache inefficiency can occur because the
elements are not stored in contiguous memory locations.

Overall, array implementations of queues are well-suited for applications where constant time
access and cache efficiency are critical, and the maximum size of the queue is known in advance.
However, they can be inefficient for dynamic or large queues, and require careful management of
memory allocation and resizing.

Basic operation of array and linked list


There are also some disadvantages to implementing a queue using an array:

1. Fixed Size: Arrays have a fixed size, which can be a disadvantage in applications where the
maximum size of the queue is not known in advance. If the queue becomes full, additional
memory must be allocated, which can be time-consuming and computationally expensive.

2. Inefficient Removal: Removing an element from the front of an array-based queue requires
shifting all remaining elements down by one position, which can be computationally
expensive for large queues.

3. Memory Wastage: Arrays require a fixed amount of memory to be allocated, even if the
queue is not full. This can result in memory wastage for small or infrequently used queues.

4. No Dynamic Resizing: Once an array is created with a certain size, it cannot be resized
dynamically. This means that the maximum size of the queue is fixed and cannot be changed
easily.

5. Cache Inefficiency: If the size of the array is large, cache inefficiency can occur because the
elements are not stored in contiguous memory locations.

Overall, array implementations of queues are well-suited for applications where constant time
access and cache efficiency are critical, and the maximum size of the queue is known in advance.
However, they can be inefficient for dynamic or large queues, and require careful management of
memory allocation and resizing.
Binary search tree for linked list insert, delete and search
A binary search tree (BST) is a data structure that stores a collection of elements in a tree-like
structure. Each node in the tree has at most two child nodes: a left child and a right child. In a BST,
the elements are ordered such that for each node, all elements in its left subtree are less than the
node's element, and all elements in its right subtree are greater than the node's element. Here are
the insert, delete, and search functions for a binary search tree implemented using linked lists:

1. Insertion: To insert a new element into the binary search tree, we start at the root and
traverse the tree, comparing the new element to each node's element. If the new element is
less than the current node's element, we move to the left child. If the new element is
greater than the current node's element, we move to the right child. We continue traversing
the tree until we reach a leaf node, where we insert the new element as a new node.

2. Deletion: To delete an element from the binary search tree, we first search for the element
to be deleted. If the element is found, we consider three cases:

 The node has no children: we simply delete the node.

 The node has one child: we replace the node with its child.

 The node has two children: we find the node with the smallest element in the right subtree,
swap its value with the node to be deleted, and delete the smallest node.

3. Search: To search for an element in the binary search tree, we start at the root and compare
the element to the current node's element. If the element is less than the current node's
element, we move to the left child. If the element is greater than the current node's
element, we move to the right child. We continue traversing the tree until we find the
element or reach a leaf node.
Traversal method in binary tree( preorder, postorder, inorder, levelorder)
Binary tree traversal refers to the process of visiting all the nodes in a binary tree exactly once in a
specific order. There are four common traversal methods:

1. Preorder Traversal: In preorder traversal, we visit the current node, then its left subtree, and
then its right subtree. This traversal method is useful for creating a copy of the tree.
2. Inorder Traversal: In inorder traversal, we visit the left subtree, then the current node, and
then the right subtree. This traversal method is useful for visiting the nodes in increasing
order.

3. Postorder Traversal: In postorder traversal, we visit the left subtree, then the right subtree,
and then the current node. This traversal method is useful for deleting the tree.

4. Levelorder Traversal: In levelorder traversal, we visit all the nodes at a given depth before
moving on to the nodes at the next depth. This traversal method is useful for finding the
shortest path between two nodes.

Note that the visit function performs some operation on the current node, such as printing its value
or adding it to a list. The specific operation depends on the application of the traversal.
Graph data structure vectex / voice / weight
A graph data structure consists of vertices (also known as nodes), edges, and weights (also known as
costs or distances). Here is a brief explanation of each of these components:

1. Vertex: A vertex is a fundamental unit of a graph. It represents a point or location in the


graph, and it can have a name, value, or any other property associated with it. Vertices can
be connected to other vertices via edges.

2. Edge: An edge is a line or a link that connects two vertices in a graph. It represents a
relationship or a connection between two vertices. An edge can be directed or undirected. A
directed edge has a direction, whereas an undirected edge does not. In a directed graph, an
edge is represented by an arrow that goes from one vertex to another. In an undirected
graph, an edge is represented by a line that connects two vertices.
3. Weight: A weight is a value that is associated with an edge in a graph. It represents the cost,
distance, or some other property associated with the edge. For example, in a map, the
weight of an edge could represent the distance between two cities.

In addition to these basic components, a graph can also have the following features:

1. Degree: The degree of a vertex is the number of edges that are connected to it. In a directed
graph, the degree can be further classified into indegree and outdegree, depending on
whether the edges are directed towards or away from the vertex.

2. Path: A path is a sequence of edges that connect two vertices in a graph. The length of a
path is the sum of the weights of its edges.

3. Cycle: A cycle is a path that starts and ends at the same vertex. A graph that contains no
cycles is called an acyclic graph.

4. Connected Components: A connected component is a subset of vertices in a graph that are


all connected to each other by paths. A graph can have multiple connected components.

Overall, a graph data structure is a powerful way to represent complex relationships between
objects or entities, and it has many applications in computer science, including network routing,
social network analysis, and data visualization.

Static memory allocation in c++


Static memory allocation in C++ is a way of allocating memory for variables and arrays at compile-
time. In static memory allocation, the size of the memory is determined at the time the program is
compiled and the memory is reserved for the entire duration of the program execution. The memory
is allocated on the stack, which is a region of memory that is managed by the operating system.

In C++, variables can be declared as static by using the keyword "static". For example, the following
code declares a static integer variable "count":

When a variable is declared as static, its value persists across function calls. In other words, the
variable is not destroyed when the function returns, but remains in memory for the entire duration
of the program execution.

Similarly, arrays can be declared as static by specifying the size of the array at compile-time. For
example, the following code declares a static array of integers with size 10:

In static memory allocation, the size of the array cannot be changed at runtime, and the memory is
allocated for the entire duration of the program execution.

Static memory allocation has some advantages, such as fast memory access and efficient memory
management. However, it also has some limitations, such as limited memory size and the inability to
change the size of the memory at runtime. As a result, dynamic memory allocation is often used in
C++ for more complex programs that require dynamic memory management.
dynamic memory allocation in c++
Dynamic memory allocation in C++ is a way of allocating memory at runtime, instead of at compile-
time. Dynamic memory allocation allows programs to allocate memory as needed, which can be
useful for programs that require flexible memory management.

In C++, dynamic memory allocation is done using the "new" operator. The new operator allocates
memory on the heap, which is a region of memory that is managed by the program.

To allocate memory for a single object, the syntax is:

This code allocates memory for an integer on the heap, and assigns the address of the allocated
memory to the pointer "p". The "new" operator returns a pointer to the allocated memory.

To allocate memory for an array, the syntax is:

This code allocates memory for an array of 10 integers on the heap, and assigns the address of the
allocated memory to the pointer "p".

Once memory is allocated using the "new" operator, it must be deallocated using the "delete"
operator when it is no longer needed. For example, to deallocate memory for a single object:

And to deallocate memory for an array:

Dynamic memory allocation allows programs to manage memory more efficiently, but it can also
lead to memory leaks and other problems if not used carefully. Programs that use dynamic memory
allocation should be designed with memory management in mind, and should ensure that all
allocated memory is properly deallocated to avoid memory leaks and other issues.

When to use normal variable and when to use pointer variable in c++
In C++, a variable is a name given to a memory location that stores a value of a specific data type. A
pointer variable, on the other hand, is a variable that stores the memory address of another
variable. In general, normal variables are used when the value of the variable is known and fixed,
while pointer variables are used when the address of a variable is required.

Here are some guidelines for when to use normal variables and when to use pointer variables in C++:

1. Use normal variables when the value of the variable is known and fixed at compile-time. For
example, if you want to store the value of an integer, you can use a normal integer variable:

2. Use pointer variables when you need to refer to the memory address of a variable. For
example, if you want to pass a variable to a function by reference, you can use a pointer
variable:

3. Use normal variables when you need to perform simple operations on a variable, such as
arithmetic operations or logical operations. For example, if you want to add two integers,
you can use normal integer variables:

4. Use pointer variables when you need to perform more complex operations on a variable,
such as accessing specific elements of an array or traversing a linked list. For example, if you
want to access the first element of an array, you can use a pointer variable:
In general, normal variables are simpler and easier to use, while pointer variables provide more
flexibility and control over memory management. It is important to use the appropriate type of
variable for each situation to ensure efficient and effective programming.

How the pointer works in c++


In C++, a pointer is a variable that stores the memory address of another variable. Pointers provide a
way to refer to a variable indirectly, by its memory address, rather than directly by its value.

Here are some key concepts related to how pointers work in C++:

1. Declaring a pointer variable: To declare a pointer variable in C++, you need to specify the
data type of the variable that the pointer points to, followed by an asterisk (*), and then the
name of the pointer variable. For example, to declare This creates a pointer variable named
"p" that can store the memory address of an integer variable.

2. Assigning a value to a pointer variable: To assign a value to a pointer variable, you can use
the "address-of" operator (&) to get the memory address of a variable, and then assign that
value to the pointer variable. For example, to assign the memory address of an integer
variable "x" to a pointer variable "p", you would use the following syntax:

This assigns the memory address of "x" to "p".

3. Dereferencing a pointer: To access the value of the variable that a pointer points to, you can
use the dereference operator (*). For example, to access the value of the integer variable
that "p" points to, you would use the following syntax:

This assigns the value of the integer variable that "p" points to, to the variable "x".

4. Modifying the value of a variable through a pointer: To modify the value of a variable
through a pointer, you can use the dereference operator (*) to access the value of the
variable, and then modify it as needed. For example, to increment the value of the integer
variable that "p" points to, you would use the following syntax:

This increments the value of the integer variable that "p" points to.

Pointers are a powerful and flexible feature of C++, but they can also be complex and error-prone if
not used carefully. It is important to understand how pointers work and to use them judiciously to
ensure efficient and effective programming.

You might also like