0% found this document useful (0 votes)
3 views

Linked List

The document provides an overview of arrays and linked lists, highlighting their structures, advantages, and disadvantages. Arrays allow for random access and are fixed in size, while linked lists are dynamic and facilitate efficient insertions and deletions. It also covers dynamic arrays using std::vector in C++, along with basic operations and examples for both data structures.

Uploaded by

Nolawi Getye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linked List

The document provides an overview of arrays and linked lists, highlighting their structures, advantages, and disadvantages. Arrays allow for random access and are fixed in size, while linked lists are dynamic and facilitate efficient insertions and deletions. It also covers dynamic arrays using std::vector in C++, along with basic operations and examples for both data structures.

Uploaded by

Nolawi Getye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Arrays and Linked Lists

Arrays
● An array is a linear data structure that stores elements in contiguous memory locations.

● Each element can be accessed by its index in O(1) time (random access).

● Declaration & Initialization.


int a[5] = {1, 2, 4, 8, 16};
Types of Arrays
● Arrays can be classified in two ways:
○ On the basis of Size
○ On the basis of Dimensions
Dynamic Arrays
● A dynamic array in C++ can be implemented with std::vector, which automatically
manages memory (resizing, allocation, deallocation) while keeping elements in
contiguous storage.

● Commonly Used Functions

○ push_back(const T& val): Add an element to the end, resizing if necessary.

○ pop_back(): Remove the last element.

○ size(): Get the current number of elements.

○ operator[] / at(): Index-based access (at() includes bounds checking)


#include <iostream>
#include <vector>

using namespace std;

int main() {
vector<int> numbers;

numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);

cout << "Size after push_backs: " << numbers.size() << endl;

cout << "numbers[1] = " << numbers[1] << endl;

cout << "numbers.at(0) = " << numbers.at(0) << endl;

numbers.pop_back(); // removes the element "30"


cout << "Size after pop_back: " << numbers.size() << endl;

return 0;
}
Advantages
● Automatic Resizing: No manual memory management required.

● Constant-Time Index Access: Random access in O(1).

● Contiguous Storage: Better cache locality, faster iteration.

● Amortized O(1) Insertions at the end (via push_back()).

Disadvantages
● Reallocations: When capacity is exceeded, existing elements are copied/moved to a
new block.

● Insertion/Deletion in Middle: Requires shifting elements, which costs O(n).

● Iterator Invalidation: On reallocation, existing pointers/iterators become invalid.


Linked Lists
● Linked List is a linear data structure that stores value and grows dynamically

● Linked List consists of nodes where each node contains a data field and a
reference(link) to the next node in the list
Node
● Stores the value and the reference to the next node.
The simplest linked list example
struct Node {
int data; // Value stored in the node
Node* next; // Pointer to the next node

Node(int data1, Node* next1){


data = data1;
next = next1;
};
● Singly Linked List is when nodes have only next’s node reference.

● Doubly Linked List is when nodes have both previous and next node reference.
Why Linked List when you have Arrays?

● Arrays by default don’t grow dynamically


● Inserting in the middle of an array is costly
● Removing elements from the middle of array is costly
Array Linked Lists

Fixed size Dynamic size

Insertions and Deletions are inefficient Insertions and Deletions are efficient

Random access No random access

Possible waste of memory No waste of memory

Sequential access is faster Sequential access is slow


Traversing a Linked List
● Start with the head of the list. Access the content of the head node if it is not
null
● Go to the next node(if exists) and access the node information
● Continue until no more nodes (that is, you have reached the last node)
Inserting node at the beginning in linked list
● If list is empty
○ make new node the head of the list

● Otherwise

○ connect new node to the current head

○ make new node the head of the list.


Insert at any position
● Find the insert position and the previous node

● And then make the next of new node as the next of previous node

● Finally, make the next of the previous node the new node
Deleting node at the beginning in linked list
● Make the second node as head

● Discard the memory allocated for the first node.


Delete a node at any position
● Find a match the node to be deleted

● Get the previous node

● Make the previous node next point to the next of the deleted node
Circular Linked Lists
● A circular linked list is a variation of the linked list where the last node links back to the
first node, forming a ring-like structure rather than ending with a nullptr.

You might also like