Linked List (Part1)
Linked List (Part1)
Data Structures
Linked lists – Part 1
1
Linear List
The sequential property of a linear list is basic to its
definition and use.
2
Linear List
3
Linear List
Linear lists can be categorized into:
General List
Data can be inserted and deleted anywhere
There are no restrictions on the operations that can
be used to process the list.
Restricted List
Data can only be inserted or deleted at the ends of
structures
Processing is restricted to operations on the data at
the ends of the list
4
Linear List
General List Structures
Un-ordered (Random) list
There is no ordering of the data.
Ordered list
The data arranged according a key.
A key is one or more fields within a structure that
are used to identify the data or control their use.
Restricted List Structures
FIFO list
The first in first out. Generally called a queue.
LIFO list
The last in first out. Generally called a stack.
5
Linked List
6
Linked List
Each element contains:
Data to be processed, useful information
Link, a pointer that identifies the next element in
the list, used to chain the data together
Elements in Linked List are called nodes.
Nodes are self-referential structures: contains a
pointer member that points to a class object of the
same class type.
7
Linked List
Advantages
Data easily inserted and deleted
No need to shift elements of LL to make room for a
new element or to delete an element
Disadvantages
We are limited to a sequential search
8
Linked List Terminology
Head pointer points to the beginning of the list
A node’s successor is the next node in the sequence
The last node has no successor. (link pointer = null)
A node’s predecessor is the previous node in the sequence
The first node has no predecessor
A list s length is the number of elements in it
A list may be empty (contain no elements)
Linked List
ALWAYS REMEMBER TO SET next TO SOME VALUE: EITHER ANOTHER
NODE OR TO NULL!!!
Linked List examples (Single)
Linked List: Element (Data) Node
Data Node Structure
Data type for the list elements depends entirely on
the application.
12
Linked List: Element Node
(implementation)
Linked List: Head Node
15
Singly Linked List ADT
head = NULL
Tail = Null
Size = 0
18
1- Create List (Constructor)
implementation
19
2- Insert Node
We need only the logical predecessor to insert a node
into the list.
There are three steps for insertion:
1. Allocate memory for the new node and insert
data
2. Point the new node to its successor.
3. Point the new node’s predecessor to the new
node.
20
2- Insert Node
Insertion cases:
Insert into empty list
Insert at beginning
Insert in middle
Insert at end
21
2- Insert Node
Insert into empty list
Algorithm addFirst(e):
…
newest = Node(e)
newest.next= head
head = newest
Tail=head
size = size+1
22
2- Insert Node
Insert at beginning (non-empty list)
Algorithm addFirst(e):
…
newest = Node(e)
newest.next= head
head = newest
size = size+1
…
Note: Tail not changed
23
Insert Node
Insert into an empty LL or at the beginning of the LL is an
insertion at the head.
2- Insert Node
Insert in middle
25
2- Insert Node (in middle)
add( e,i ): adding new node at index i
We have to make sure that the value of index i is within the
range
We need a reference to traverse the list
Contain:
1. Reference to head node. public class SinglyLinkedList<E> {
private Node<E> head;
2. Reference to tail node. private Node<E> tail;
3. Curr reference to a specific location. private Node<E> curr;
private int size;
………….
}
2- Insert Node (in middle)
1.
30
2- Insert Node at end
Algorithm addLast(e):
newest = Node(e)
newest.next= null
If isEmpty
head=newest
else
tail.next= newest
tail = newest
size = size+1
3- Delete (Remove) Node
A. Remove beginning (at head)
Algorithm removeFirst( ):
if head == null then
Print “the list is emp
else
head = head.next
size = size−1
32
3- Delete (Remove) Node.
3- Delete (Remove) Node
B. Remove End or last (at tail)
Algorithm size():
return size
AlgorithmisEmpty()
return head==null
38
Singly Linked List ADT
Node class
Singly Linked List ADT
SLL class
Singly Linked List ADT
SLL class
Singly Linked List ADT
SLL class
Linked Lists: Performance Analysis
Using SinglyLinkedList
Example: Output