Linked Lists
Abstract Data Type (ADT)
•
a set of objects
• a set of operations
Same set of objects ,
different sets of operations =>
different ADTs
ADTs are implemented using classes,
hiding implementation details:
LIST ABSTRACTION
Definition:
A linear configuration of
elements, called nodes.
Basic operations
•
To create/destroy a list
•
To expand/shrink the list
•
Read/Write operations
•
Changing the current node (moving along the
list)
•
To report current position in the list
Specifications for the ADT List
•
Operations on lists
▫ Add new entry – at end, or anywhere
▫ Remove an item
▫ Remove all items
▫ Replace an entry
▫ Look at any entry
▫ Look for an entry of a specific value
▫ Count how many entries
▫ Check if list is empty, full
▫ Display all the entries
Limitations of Arrays
Limitations of an array are:
•Size of array is fixed; even dynamically
allocated arrays' size cannot be changed.
•Adding element to front or middle of
Potential Problem Operations
•
add
, remove, replace work OK when
valid position given
•
Remove
not meaningful on empty lists
•
A list could become full, what happens to
•
A collection of connected nodes
with a set of operations.
•
Nodes are divided into to bit of
information:
1.
Data
2.
Pointer
Characteristics
•
Insert and delete nodes in any order
•
The nodes are connected
•
Each node has two components
▫
Information (data)
▫
Link to the next node
•
The nodes are accessed through the links
between them
Understanding the linked list
• Linked list is not a continuous memory location as array so each node in linked list is stored at different memory location , in order to access the whole list or to weave them in one list each node should have the address of the next respective node.
• And to access the list we should always keep safe the address of first node so that we can access the whole list with the help of the first node, and if in case we lose the address of the first node then the whole list will become a garbage as we cannot find or access this list again .
• So before we create any node we first create a pointer which will hold the address of the first node in the list
Linked list consists of
1) A sequence of nodes used to hold individual data Elements
2) Each node will have two Sections a) The value to hold
b) The Address of next node in the list
Head Predecessor
of X Node X
Success-o
r of X
tail
For each node the node that is in front
of it is called predecessor.
The node that is after it is called
Terminology
•
Head (front, first node):
▫
The node without predecessor, the node that
starts the lists.
•
Tail (end, last node):
▫
The node that has no successor, the last node
in the list.
•
Current node:
The node being processed.
▫
From the current node we can access the next node.
ADT List Notation
L
- list
e
- item of the same type as the
information part of an element
(a node) in the list
Operations in ADT Notation
Insert(L,e)
Inserts a node with information e before the
current position
Delete(L)
Deletes the current node in L , the current
position indicates the next node.
RetrieveInfo(L)
e
A. Insertion
To insert a node X between the
nodes A and B:
.Create a link from X to B.
.Create a link from A to X,
Insertion
X
B. Deletion
To delete a node X between A and B:
• Create a link from A to B,
Deletion
Keep It Simple
• Let us understand the Linked Structure with the help of this example Following picture depicts linked list holding sequence of numbers (87, 42, 53, 4):
Each node include: • The data element
• The link or address to the next node
87
.
42
.
53
.
4
/
/ means Null ,this is used to specify that this is the last node in the list
Defining the structure of the linked list
Structure of node in linked list here is implemented as class in C++ Here the Structure of a node will have 2 parts
1) The Data
2) The Pointer to next node ( i.e the address of the next node) Let us understand with an example:
class Node {
public:
int data; // the value to be stored Node* next; // pointer to nest node };
Here the Data is defined as integer and named as data
and the pointer to the next node is declared as a pointer and Named as next
Starting with the linked Structure
• Linked list is a collection of many individual nodes• But before we actually create the nodes we should first create the
pointer which will hold the address of the first node in the list so that with this pointer we can actually track the whole list
With the following line of code we will create a pointer variable to point towards the first node in the list
Example: Node* List;
At this point our list is empty- it has no elements
and it is represented by a NULL pointer, i.e. a pointer whose value is 0 (signified by / OR NULL)
/
Lis tStarting with the linked Structure
Example: List = new Node;Here with this one line of code we did two things 1) we created a new first node with the new operator,
2) and we also saved the address of this new node in the pointer variable knows as List ( one can give any name )
Lis t
.
Starting with the linked Structure
Now we will insert data into this new node with the following codeList->data = 87; Lis t
.
87 This part is List data This part is List nextWe have till now successfully created the structure for the node and added the first node and a pointer which will hold the address of the first node
Implementing a Linked Stack
• If we are implementing a linked stack then the stack does all the operations from the front side so the deletion and insertion is from the front of the list.
• Appending the value 42 into the linked stack :
Initially we have a pointer named List and our first node with 87 value
Lis
t
.
87/
• We will be adding a node to the linked Stack
Node * temp = new Node ;
Adding a new node at the front of
the linked list
Lis t
.
87With the above code we create a new node, and assign its address to the temporary pointer variable temp
.
temp
Adding a node to the front
Assign value to the newly added nodetemp data=43; Lis t
.
87 temp.
43/
Adding a node to the front
Set next data member of new node to point to first node in original list
temp -> next = List;
Lis t
.
87 temp.
43/
.
Adding a node to the front
Now we can Update original List pointer to point to the new first node as the List pointer always points towards first node in the list List = temp; Lis t
.
87 temp.
43/
.
Adding a node to the front
Summarized, as a function:void add2front( int val, Node* &List) {
Node *temp = new Node; temp -> data = val;
temp -> next = List; List = temp;
Adding a node at end
• To add a new node at the back we need to reach at the last node and to go to the last node we will assign a new temp pointer which will hop through each node till it reaches the node which is having a null value. It will check each node if it is == Null, until Null is found.
87
.
42.
53/
Lis t.
temp.
Adding a node at end
while(temp->next!=NULL){
ptr = ptr -> next;
// with this line the temp pointer checks each node for Null value and when it reached the last node it will find the Null value ,
so by the time the control comes out of this while loop the pointer temp will have the address of the last node
}
temp->next= new Node;
// now we are at the last node here insert a node at the last
temp = temp->next; // pointer Points to the inserted node temp->next =NULL; // sets the next of this new node to null temp->data = 90; // put value in the new inserted node