Professional Documents
Culture Documents
ALGORITHM
INTRODUCTION TO LINKED LIST 1
Linked Lists
BOOK READINGS
Linked Lists 2
PREREQUISITE CONCEPTS
• Arrays
• Dynamic Memory
• Classes and Structures
• Deep copy
Linked Lists 3
DATA STRUCTURES
Linked Lists 4
STATIC ARRAYS: PROS AND CONS
Linked Lists 5
LINKED LIST
Linked list
Linked Lists 6
Node
Linked Lists 7
SINGLY LINKED LISTS
• A singly linked list is
a data structure next
consisting of a
sequence of nodes
• Each node stores elem node
• element
• link to the next node
A B C D
Linked Lists 8
MEMORY REPRESENTATION OF LINKED
LIST & ARRAY
Linked Lists 9
BASIC OPERATIONS IN A SIMPLE LINKED
LIST:
• Insertion
• Deletion
• Searching or Iterating through the list to
display items.
Linked Lists 10
ARRAY VS LINKED LIST
Linked Lists 11
REPRESENTATION OF NODE IN C++
struct Node
{
int data; // data can be of any type
Node *next;
}
Linked Lists 12
A SIMPLE LINKED LIST CLASS
• Operations of List
• Insert / Delete
• AtStart: insert/delete a new node at start
• AtEnd: insert/delete a new node at end
• AtAnyPosition: insert/delete a new node at a
particular position
• IsEmpty: determine whether or not the list is
empty
• FindNode: find a node with a given value
• Traverse: print all the nodes in the list
Linked Lists 13
REPRESENTATION OF LINKED LIST IN C+
+
Class linkedList
{
// attributes
Node *head;
public:
// member functions
void insertStart(int v);
void insertEnd(int v);
void deleteStart ();
void deleteEnd();
void traverse();
linkedList();
linkedList(const linkedList& l);
~linkedList();
Linked Lists 14
};
INSERTING AT THE HEAD
1. Allocate a new
node
2. Insert new element
3. Make new node
point to old head
4. Update head to
point to new node
Linked Lists 15
REMOVING AT THE HEAD
1. Hold address of
head node in
temporary pointer
2. Update head to
point to next node
in the list
3. Delete the previous
head node whose
address is in
temporary node
Linked Lists 16
CAUTION!
EMPTY LIST
TRAVERSING A SLL (ANIMATION)
temp
head
Linked Lists 18
APPLICATION OF LINKED LIST
Linked Lists 19
MODIFICATIONS OF LINKED LISTS
• Circular Lists
• Doubly Linked Lists
• Circular Doubly Linked Lists
Linked Lists 20
SUMMARY
Linked Lists 22