Array and Linked List
Array and Linked List
and
LINK LIST
What is an Array?
1 30 6 20 54 4
0 1 2 3 Array Index
4 5
Array Syntax in C#
Array Declaration:
< Data Type > [ ] < Name_Array >
//Note: Only Declaration of an array and doesn’t allocate memory to the array. For that array
must be initialized.
Values:
int[] x; // can store int values
string[] stud1; // can store string values
double[] d; // can store double values
char [] cHar;// can store char values
Array Syntax in C#
Adding a size on array
type [ ] < Name_Array > = new < datatype > [size];
Example:
// defining array with size 5.
// But not assigning values
• What it means is that, we can use normal variables (v1, v2, v3, ..)
when we have a small number of objects. But if we want to store a
large number of instances, it becomes difficult to manage them with
normal variables. The idea of an array is to represent many
instances in one variable.
Types of Arrays
• Multidimensional Array
One Dimensional Array
• Representation of 1D array.
Two Dimensional Array
• It is a list of lists of the variable of the same data type.
• It also allows random access and all the elements can be
accessed with the help of their index.
• It can also be seen as a collection of 1D arrays. It is also
known as the Matrix.
• Its dimension can be increased from 2 to 3 and 4 so on
and they all are referred to as a multi-dimension array.
• The most common multidimensional array is a 2D array.
Types of Array Operations
• Data: It holds the actual value or data associated with the node.
• Next Pointer: It stores the memory address (reference) of the next node in
the sequence.
Head and Tail: The linked list is accessed through the head node, which
points to the first node in the list. The last node in the list points to NULL or
nullptr, indicating the end of the list. This node is known as the tail node.
Types of Linked List
1.Single-linked list
2.Double linked list
3.Circular linked list
Single Linked List
The circular linked list is a linked list where all nodes are connected to
form a circle. In a circular linked list, the first node and the last node are
connected to each other which forms a circle. There is no NULL at the end.
2 Types of Circular Linked List
Circular single linked list: In a circular Singly linked list, the last node of the
list contains a pointer to the first node of the list. We traverse the circular
singly linked list until we reach the same node where we started. The
circular singly linked list has no beginning or end. No null value is present in
the next part of any of the nodes.
2 Types of Circular Linked List
• Circular Double linked list: has properties of both double linked list and
circular linked list in which two consecutive elements are linked or
connected by the previous and next pointer and the last node points to the
first node by the next pointer and also the first node points to the last node
by the previous pointer.
Types of Linked List Operations:
1.Insertion: Adding a new node to a linked list involves adjusting the
pointers of the existing nodes to maintain the proper sequence. Insertion
can be performed at the beginning, end, or any position within the list
2.Deletion: Removing a node from a linked list requires adjusting the
pointers of the neighboring nodes to bridge the gap left by the deleted
node. Deletion can be performed at the beginning, end, or any position
within the list.
3.Searching: Searching for a specific value in a linked list involves
traversing the list from the head node until the value is found or the end of
the list is reached.
Why Link List data structure is needed
• Dynamic Data structure: The size of memory can be allocated or de-
allocated at run time based on the operation insertion or deletion.
• Ease of Insertion/Deletion: The insertion and deletion of elements
are simpler than arrays since no elements need to be shifted after
insertion and deletion, Just the address needed to be updated.
• Efficient Memory Utilization: As we know Linked List is a dynamic
data structure the size increases or decreases as per the
requirement so this avoids the wastage of memory.
• Implementation: Various advanced data structures can be
implemented using a linked list like a stack, queue, graph, hash
maps, etc.
Link List vs. Array