Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

ARRAY

and
LINK LIST
What is an Array?

An array is a collection of homogeneous data


types where the elements are allocated contiguous
memory.
Basic Terminologies of Array
• Array Index: In an array, elements are identified by their indexes. Array
index starts from 0.
• Array element: Elements are items stored in an array and can be accessed
by their index
• Array Length: The length of an array is determined by the number of
elements it can contain.
Array Element

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

int[] intArray1 = new int[5];


Why Array Data Structures is needed?
• Assume there is a class of five students and if we have to keep records
of their marks in examination then, we can do this by declaring five
variables individual and keeping track of records but what if the
number of students becomes very large, it would be challenging to
manipulate and maintain the data.

• 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

• One Dimensional (1D) Array

• Two Dimension (2D) Array

• Multidimensional Array
One Dimensional Array

• It is a list of the variable of similar data types.

• It allows random access and all the elements can be


accessed with the help of their index.

• The size of the array is fixed.

• 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

• Traversal: Traverse through the elements of an array.


• Insertion: Inserting a new element in an array.
• Deletion: Deleting element from the array.
• Searching: Search for an element in the array.
• Sorting: Maintaining the order of elements in the
array.
Applications of Array

• They are used in the implementation of other data


structures such as array lists, heaps, hash tables,
vectors, and matrices.
• Database records are usually implemented as arrays.
• It is used in lookup tables by computer.
• It is used for different sorting algorithms such as
bubble sort insertion sort, merge sort, and quick sort.
What is a Link List?

A linear data structure which looks like a chain of


nodes, where each node is a different element. Unlike
Arrays, Linked List elements are not stored at a
contiguous location.
Terminologies of Link List
Node Structure: A node in a linked list typically consists of two
components:

• 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

A single linked list is a linear data structure in


which the elements are not stored in contiguous
memory locations and each element is connected
only to its next element using a pointer.
Double Linked List
Inserting a new node in a doubly linked list is very similar to inserting new
node in linked list. There is a little extra work required to maintain the link
of the previous node. A node can be inserted in a Doubly Linked List in four
ways:
•At the front of the DLL.
•In between two nodes
 After a given node.
 Before a given node.
•At the end of the DLL.
Double Linked List
• Add a node at the front in a Doubly Linked List: The new node is
always added before the head of the given Linked List. The task can be
performed by using the following 5 steps

1.Firstly, allocate a new node (say new_node).


2.Now put the required data in the new node.
3.Make the next of new_node point to the current head of the
doubly linked list.
4.Make the previous of the current head point to new_node.
5.Lastly, point head to new_node.
Adding a node to the front example:
Add a node between two nodes: It is further classified into the following two
parts:
• Add a node after a given node in a Doubly Linked List
• Add a node before a given node in a Doubly Linked List
Add a node after a given node in a Doubly Linked List: We are given a pointer to
a node as prev_node, and the new node is inserted after the given node. This can be
done using the following 6 steps:
1. Firstly create a new node (say new_node).
2. Now insert the data in the new node.
3. Point the next of new_node to the next of prev_node.
4. Point the next of prev_node to new_node.
5. Point the previous of new_node to prev_node.
6. Change the pointer of the new node’s previous pointer
to new_node.
Add a node between: After
Add a node before a given node in a Doubly Linked List:
Let the pointer to this given node be next_node. This can be done using the
following 6 steps:
1.Allocate memory for the new node, let it be called new_node.
2.Put the data in new_node.
3.Set the previous pointer of this new_node as the previous node of
the next_node.
4.Set the previous pointer of the next_node as the new_node.
5.Set the next pointer of this new_node as the next_node.
6.Now set the previous pointer of new_node.
• If the previous node of the new_node is not NULL, then set the next
pointer of this previous node as new_node.
• Else, if the prev of new_node is NULL, it will be the new head node.
Add a node between: Before
Add a node at the end in a Doubly Linked List:
The new node is always added after the last node of the given Linked List.
This can be done using the following 7 steps:
1.Create a new node (say new_node).
2.Put the value in the new node.
3.Make the next pointer of new_node as null.
4.If the list is empty, make new_node as the head.
5.Otherwise, travel to the end of the linked list.
6.Now make the next pointer of last node point to new_node.
7.Change the previous pointer of new_node to the last node of
the list.
Add a node at the end End
What is Circular 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

You might also like