You are on page 1of 39

Linked Lists

1
Definition of Linked Lists
• A linked list is a sequence of items (objects) where every
item is linked to the next.
• A linked list can grow or shrink in size as the program runs
• Graphically:

data data data data

head_ptr tail_ptr

2
• Linked lists and arrays are similar since they both
store collections of data.
• The array's features all follow from its strategy of
allocating the memory for all its elements in one
block of memory.
• Linked lists use an entirely different strategy:
linked lists allocate memory for each element
separately and only when necessary.

3
Definition Details
• Each item has a data part (one or more data
members), and a link that points to the next item
• One natural way to implement the link is as a
pointer; that is, the link is the address of the next
item in the list
• It makes good sense to view each item as an
object, that is, as an instance of a class.
• We call that class: Node
• The last item does not point to anything. We set its
link member to NULL. This is denoted
graphically by a self-loop

4
Examples of Linked Lists
(A Waiting Line)
• A waiting line of customers: John, Mary, Dan, Sue
(from the head to the tail of the line)
• A linked list of strings can represent this line:

John Mary Dan Sue

head_ptr tail_ptr

5
Examples of Linked Lists
(A Stack of Numbers)
• A stack of numbers (from top to bottom): 10, 8, 6,
8, 2
• A linked list of ints can represent this stack:

10 8 6 8 2

head_ptr tail_ptr

6
Examples of Linked Lists
(A Set of Non-redundant Elements)
• A set of characters: a, b, d, f, c
• A linked list of chars can represent this set:

a b d f c

head_ptr tail_ptr

7
Examples of Linked Lists
(A Sorted Set of Non-redundant Elements)
• A set of characters: a, b, d, f, c
• The elements must be arranged in sorted order: a,
b, c, d, f
• A linked list of chars can represent this set:

a b c d f

head_ptr tail_ptr

8
Pointers
• A pointer contains the location, or address in
memory, of a memory cell
– Initially undefined, but not NULL
– A statically allocated pointer declaration
int *p;
– A dynamically allocated pointer variable
p = new int;

9
Pointers

• The expression, *p, denotes the memory cell to


which p points
• The & address-of operator places the address of a
variable into a pointer variable
– p = &x;

A pointer to an integer
10
Pointers
• The delete operator returns dynamically
allocated memory to the system for reuse, and
leaves the variable undefined
– delete p;
– A pointer to a deallocated memory cell is possible and
dangerous
• Assign the pointer q the value in p
q = p;

11
Dynamic Allocation of Arrays
• Use the new operator to allocate an array
dynamically
• An array name is a pointer to the array’s first
element
• The size of a dynamically allocated array can be
increased
double* oldArray = anArray;
anArray = new double[2*arraySize];

12
Pointer-Based Linked Lists

• A node in a linked list is usually a struct


struct Node
{ int item
Node *next; A node

}; //end struct
• A node is dynamically allocated
Node *p;
p = new Node;

13
Comparing Array-Based and Pointer-
Based Implementations
• Size
– Increasing the size of a resizable array can waste
storage and time
• Storage requirements
– Array-based implementations require less memory than
a pointer-based ones

14
Comparing Array-Based and Pointer-
Based Implementations
• Access time
– Array-based: constant access time
– Pointer-based: the time to access the ith node depends
on i
• Insertion and deletions
– Array-based: require shifting of data
– Pointer-based: require a list traversal

15
The composition of a Linked List
• Collection of components (nodes)
– Every node (except last)
• Contains address of the next node
• Node components
– Data: stores relevant information
– Link: stores address

16
• Collection of components (nodes)
– Every node (except last)
• Contains address of the next node
• Node components
– Data: stores relevant information
– Link: stores address

17
The composition of a Linked List

• A linked list is called "linked" because each node


in the series has a pointer that points to the next
node in the list.

18
Disadvantages of Arrays
1. The size of the array is fixed.
– In case of dynamically resizing the array from size S
to 2S, we need 3S units of available memory.
– Programmers allocate arrays which seem "large
enough” This strategy has two disadvantages: (a)
most of the time there are just 20% or 30% elements
in the array and 70% of the space in the array really is
wasted. (b) If the program ever needs to process more
than the declared size, the code breaks.
2. Inserting (and deleting) elements into the
middle of the array is potentially expensive
because existing elements need to be shifted over
to make room
19
Linked lists
• Linked lists are appropriate when the number of
data elements to be represented in the data
structure at once is unpredictable.
• Linked lists are dynamic, so the length of a list can
increase or decrease as necessary.
• Each node does not necessarily follow the
previous one physically in the memory.
• Linked lists can be maintained in sorted order by
inserting or deleting an element at the proper point
in the list.
20
Singly Linked Lists
head

next next next next

a b c d

First node Last node

21
Header Nodes
• One problem with the basic description: it
assumes that whenever an item x is removed (or
inserted) some previous item is always present.
• Consequently removal of the first item and
inserting an item as a new first node become
special cases to consider.
• In order to avoid dealing with special cases:
introduce a header node (dummy node).
• A header node is an extra node in the list that
holds no data but serves to satisfy the requirement
that every node has a previous node.

22
List with a header node
header

a b c d

Empty List
header

23
Doubly Linked Lists
pHead

a b c

Advantages:
• Convenient to traverse the list backwards.
• Simplifies insertion and deletion because you no longer
have to refer to the previous node.
Disadvantage:
• Increase in space requirements.

24
Saving and Restoring a Linked List by
Using a File
• Use an external file to preserve the list
• Do not write pointers to a file, only data
• Recreate the list from the file by placing each item at
the end of the list
– Use a tail pointer to facilitate adding nodes to the end of
the list
– Treat the first insertion as a special case by setting the tail
to head

25
Passing a Linked List to a Function
• A function with access to a linked list’s head
pointer has access to the entire list
• Pass the head pointer to a function as a reference
argument

26
Circular Linked Lists
• Last node references the first node
• Every node has a successor
• No node in a circular linked list contains NULL

A circular linked list

27
Circular Doubly Linked Lists
• Circular doubly linked list
– prev pointer of the dummy head node points to the
last node
– next reference of the last node points to the dummy
head node
– No special cases for insertions and deletions

28
Circular Doubly Linked Lists

(a) A circular doubly linked list with a dummy head node


(b) An empty list with a dummy head node
29
Operations on Linked Lists
• Insert a new item
– At the head of the list, or
– At the tail of the list, or
– Inside the list, in some designated position
• Search for an item in the list
– The item can be specified by position, or by some value
• Delete an item from the list
– Search for and locate the item, then remove the item,
and finally adjust the surrounding pointers
• size( );
• isEmpty( )
30
Insert– At the Head
• Insert a new data A. Call new: newPtr A
List before insertion:
data data data data

head_ptr tail_ptr
• After insertion to head:

A data data data data

head_ptr tail_ptr

•The link value in the new item = old head_ptr


•The new value of head_ptr = newPtr
31
Insert – at the Tail
• Insert a new data A. Call new: newPtr A
List before insertion
data data data data

head_ptr tail_ptr
• After insertion to tail:
data data data data A

head_ptr tail_ptr
•The link value in the new item = NULL
•The link value of the old last item = newPtr
32
Insert – inside the List
• Insert a new data A. Call new: newPtr data
List before insertion:
data data data data

head_ptr tail_ptr
• After insertion in 3rd position:
data data A data data

head_ptr tail_ptr

•The link-value in the new item = link-value of 2nd item


•The new link-value of 2nd item = newPtr
33
Delete – the Head Item
• List before deletion:

data data data data data

head_ptr tail_ptr

• List after deletion of the head item:


data data data data data

head_ptr tail_ptr
•The new value of head_ptr = link-value of the old head item
•The old head item is deleted and its memory returned
34
Delete – the Tail Item
• List before deletion:

data data data data data

head_ptr tail_ptr

• List after deletion of the tail item:


data data data data data

head_ptr tail_ptr
•New value of tail_ptr = link-value of the 3rd from last item
•New link-value of new last item = NULL.
35
Delete – an inside Item
• List before deletion:

data data data data data

head_ptr tail_ptr

• List after deletion of the 2nd item:


data data data data data

head_ptr tail_ptr
•New link-value of the item located before the deleted one =
the link-value of the deleted item
36
size() and isEmpty()
• We need to scan the items in the list from the
head_ptr to the last item marked by its link-value
being NULL
• Count the number of items in the scan, and return
the count. This is the size().
• Alternatively, keep a counter of the number of
item, which gets updated after each insert/delete.
The function size( ) returns that counter
• If head_ptr is NULL, isEmpty() returns true; else,
it returns false.

37
Searching for an Item
• Suppose you want to find the item whose data
value is A
• You have to search sequentially starting from the
head item rightward until the first item whose data
member is equal to A is found.
• At each item searched, a comparison between the
data member and A is performed.

38
#include<iostream.h>
#include <string.h>
main()
{
char s1[80];
char s2[80];
cout << "Input the first string: ";
cin>>s1;
cout << "Input the second string: ";
cin>>s2;
cout << "Length of s1= " << strlen(s1)<< endl;
cout << "Length of s2= " << strlen(s2)<< endl;
cout <<"This is a test.\n";
} 39

You might also like