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

6 Linked List

The document discusses linked lists, providing an introduction and overview of singly linked lists including their memory representation and uses. It also provides examples of how to implement a singly linked list in C and C++ with functions to insert nodes and display the list. Key differences between linked lists and arrays are outlined.

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

6 Linked List

The document discusses linked lists, providing an introduction and overview of singly linked lists including their memory representation and uses. It also provides examples of how to implement a singly linked list in C and C++ with functions to insert nodes and display the list. Key differences between linked lists and arrays are outlined.

Uploaded by

Amar Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Lovely Professional University, Punjab

Data Structures
Lecture : Linked List
Outlines
• Introduction
• Why Linked List?
• Types of Linked List
• Memory Representation of Linked Lists
• Difference between Singly Linked List and Arrays
• Program to implement Singly Link list
• Review Questions
Introduction
• A linked list (One-way list) is a linear collection of data
elements, called nodes, where the linear order is given by
means of pointers.

• Each node is divided into two parts.

• First part contains the information of the element.

• Second part contains the address of the next node in the list.
Linked List
• A singly linked list is a
concrete data structure
consisting of a sequence of next
nodes
• Each node stores
– element
– link to the next node node
element

A B C D
Key Points
 Linked list
• Linear collection of nodes
• Connected by pointer links
• Accessed via a pointer to the first node of the list
• Link pointer in the last node is set to null to mark the
list’s end
• Linked list contains a List Pointer Variable called
START or NAME, which contains the address of the
first node.
Why Linked List?
Arrays: pluses and minuses
+ Fast element access.
-- Impossible to resize.

Need of Linked List


• Many applications require resizing!
• Required size not always immediately available.
 Use a linked list instead of an array when
• You have an unpredictable number of data elements
• You want to insert and delete quickly.
Memory Representation
• Linked lists are maintained in memory using linear arrays or
Parallel arrays.

INFO LINK
Start 2 1
A 6
2
E 9
3
C 7
4
5
B 4
6 D 3
7
8 F 0
9
10
Memory Representation (2)
• Multiple lists in memory

INFO LINK
Start1 1 S4 0
2
2 A 6
3 E 9
Start 210 4 C 7
5 S2 8
6 B 4
7 D 3

8 S3 1
F 0
9
S1 5
10
Memory Representation (3)
• INFO part of a node may be a record with multiple data items.
• Records in memory using Linked lists

Start Name Age Sex LINK


2
1
A 19 M 6
2
E 21 M 9
3
C 20 F 7
4
5
B 19 F 4
6
D 22 M 3
7
8
F 20 F 0
9
10
Types of Linked Lists
1. Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
2. Circular, singly linked
• Pointer in the last node points
back to the first node
3. Doubly linked list
• Two “start pointers” – first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
4. Circular, doubly linked list
• Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
Difference between Singly Linked List and
Arrays

Singly linked list Array

 Elements are stored in  Elements are stored in


linear order, accessible linear order, accessible
with links. with an index.

 Do not have a fixed size.  Have a fixed size.

 Cannot access the previous  Can access the previous


element directly. element easily.

 No binary search.  Binary search.


Program( Singly Linked
List )
• #include <stdio.h> • // Function to display the linked list
• #include <stdlib.h> • void displayList(struct Node *head) {
• // Define the structure for a singly linked list node • struct Node *current = head;
• struct Node { • while (current != NULL) {
• int data; • printf("%d -> ", current->data);
• struct Node *next; • current = current->next;

• }
};
• printf("NULL\n");
• // Function to insert a new element at the beginning of
the linked list • }
• struct Node* insertAtBeginning(struct Node *head, int
value) { • int main() {
• struct Node *newNode = (struct
• struct Node *head = NULL;
Node*)malloc(sizeof(struct Node));
• if (newNode == NULL) { • // Insert elements at the beginning
• printf("Memory allocation failed!\n");
• head = insertAtBeginning(head, 3);
• head = insertAtBeginning(head, 7);
• return head;
• head = insertAtBeginning(head, 11);
• }
• newNode->data = value;
• // Display the linked list
• newNode->next = head;
• printf("Linked List: ");
• head = newNode; • displayList(head);
• return head;
• } • return 0;
• }
Program( Singly Linked List )(C++)
// Function to display the linked list
• #include <iostream> void displayList(Node* head) {
Node* current = head;
while (current != nullptr) {
• // Define the structure for a singly linked list std::cout << current->data << " -> ";
node current = current->next;
• struct Node { }
• int data; std::cout << "nullptr" << std::endl;
• Node* next; }
• };
int main() {
Node* head = nullptr;
• // Function to insert a new element at the
beginning of the linked list
// Insert elements at the beginning
• Node* insertAtBeginning(Node* head, int head = insertAtBeginning(head, 3);
value) { head = insertAtBeginning(head, 7);
• Node* newNode = new Node; head = insertAtBeginning(head, 11);
• newNode->data = value;
• newNode->next = head; // Display the linked list
• head = newNode; std::cout << "Linked List: ";
displayList(head);
• return head;
• } return 0;
}

You might also like