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

Chapter 3 - Linked Lists

This document discusses linked lists, including different types (singly, doubly, circular), their advantages over arrays, and how to implement them. Linked lists use nodes that store data and a pointer to the next node. This allows dynamic sizes and easy insertion/removal without moving elements in memory. The document provides code examples for common linked list operations like insertion, deletion, and traversal in both directions.

Uploaded by

Ian Molo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Chapter 3 - Linked Lists

This document discusses linked lists, including different types (singly, doubly, circular), their advantages over arrays, and how to implement them. Linked lists use nodes that store data and a pointer to the next node. This allows dynamic sizes and easy insertion/removal without moving elements in memory. The document provides code examples for common linked list operations like insertion, deletion, and traversal in both directions.

Uploaded by

Ian Molo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

Chapter 3

Linked Lists
 Our First Data Structure
◦ Linked Lists
 Singly
 Doubly
 Circular
 This week we will look at linked lists.
◦ Why to use linked lists?
◦ How to use linked lists?
◦ Different Types of linked list.
 But first, lets review arrays.
◦ A very useful structure provided by programming
languages
◦ However, arrays have at least 2 limitations;
 The size has to be known at compilation.
 The data in the array are separated in the computers
memory by the same distance.
◦ These limitations make ‘inserting’ an item inside
the array difficult.
 Linked Lists can be used to get around the
limitations of arrays by linking data
independently from where it is stored in the
computers memory.
 To create a linked list, each piece of data in

the set of data, simply has to also store the


address of the next piece of data in the set.
P

Data Data Data Data Data

0
 A linked structure is a collection of nodes
storing data and links to other nodes.
 Nodes can be located anywhere in memory,

and passing from one node of the linked


structure to another is accomplished by
storing the addresses of other nodes in the
linked structure.
 a data structure that makes it easy to
rearrange data without having to move data
in memory.

 Ex1:
 picture a classroom of students who are
seated in no particular order. A unique
number identifies each seat (see next
figure). We’ve also included the relative
height of each student, which we’ll use in
the next exercise.
Let’s say that a teacher needs to place students
names in alphabetical order so she can easily find a
name on the list.
 Solutions:
 Change their seats (chaotic if many)
 leave students seated and make a list of seat

numbers that corresponds to the alphabetical


order of students (3,1,2)
Ex2: Suppose you want to rearrange students
in size order.(1,3,2)
• play a critical role in applications that help
companies and governments manage data
dynamically.
• A single link list enables a program to move
through the list in one direction, which is
usually from the front of the list moving to
the end of the list. A doubly linked list
enables the program to move through the list
in both directions.
 Programmers choose linked lists over an
array because linked lists can grow or shrink
in size during runtime. Another entry can be
placed at the end of the last entry on the
linked list simply by assigning reference to
the new entry in the last entry on the linked
list.
 the last entry can be removed from the linked
list by simply removing reference to it in the
next element of the second-to-last entry on
the linked list. This is more efficient than
using an array and resizing at runtime.
 change the size of the array, the operating
system tries to increase the array by using
memory alongside the array. If this location is
unavailable, then the operating system finds
another location large enough to hold
elements of the array and new array
elements. Elements of the array are then
copied to the new location.
 change the size of a linked list, the operating
system changes references to the previous
item and the next item on the list, which is
fewer steps than changing the size of an
array.
 Node - each entry in a linked list
• typedef struct Node
{  \\ constructor initializes elements of the node when an instance
of the \\node is created
 struct Node(int data) \\ structure name creates
instance
   {    
this->data = data;   
    previous = NULL;      \\pointers to the previous
  next = NULL;   \\and next node in the link list
}
  int data;  
struct Node* previous;  
struct Node* next;
} NODE;
Example: Create a linked list of structures

• Last member of the structure is a pointer for


storing the address of another structure of the
same type
Example: Using the linked list of structures
Steps
1. Allocate memory for the new node.
2. Point the new node to its successor.
3. Point the new node’s predecessor to the new
node.
Figure 3-10: Adding node to an empty list.
Figure 3-11: Add node at the beginning.
Figure 3-12: Insert in a middle.
Figure 3-13: Add node at end.
algorithm insertNode (
ref list <metadata>, 5 if (pPre nulll)
val pPre <node pointer> Adding before first node or to
val dataIn <dataType>)
empty list.
Inserts data into a new node in the linked list
1 pNew->link = list.head
Pre list is metadata structure to a
valid list pPre is pointer to data’s 2 list.head = pNew
logical predecessor dataIn 6 else
contains Adding in middle or at end.
data to be inserted 1 pNew->link = pPre->link
Post data have been inserted in 2 pPre->link = pNew
sequence
7 end if
Return true if successful, false if
memory overflow 8 list.count = list.count + 1
1 allocate (pNew) 9 return true
2 if (memory overflow) end insertNode
1 return false
3 end if
4 pNew->data = dataIn
nodeType *newNode;
newNode = new nodeType;
newNode->data = 123;
newNode->link = current->link;
current->link = newNode;
 
current=head;
while(current)
  {
        if((current->data < newnode->data) &&
(current->next->data > newnode->data))
    {
            temp = current->next;
            current->next = newnode;
            newnode->next = temp;
            break;
    }
        current = current->next;
  }
 The example on the previous page was an
example of a singly linked list.
◦ A pointer (P) to the first element in the list
◦ Each element stores some data, and a pointer to
the next element in the list
◦ The final element points towards NULL (0), to
indicate it is the final element.
 The data stored in a linked list can be
anything from a simple integer to a user
defined class.
node in a single linked list references the next node
and not the previous node, although nothing stops
you from creating a backward reference by using
only the previous node reference
This enables the programmer to traverse the linked
list in both directions by referencing the previous
and next nodes.
The appendNode() member function places a new
node at the end of the linked list.
void appendNode(int data)

NODE* n = new NODE(data);  
if(back == NULL)
 {   
back = n;    
front = n;
 }
 else
 {
back->next = n;
n->previous = back;  
back = n;  
}
}
 Step 1: assigns the memory address of the new
node to the next member of the back node, shown
in the second block of memory

 Step2: assigns the memory address of the back


node to the previous member of the new node.
This links both nodes.

 Step 3:replaces the memory address of the back


node on the linked list with the memory address of
the new node. This places the new node at the
beginning of the linked list.
 displayNodes() member function displays each
node of the linked list, beginning with the node at
the front of the list and ending with the node at the
back of the list.
void displayNodes()

cout << "Nodes:";   NODE* temp = front;   
while(temp != NULL)
{
     cout << " " << temp->data;
     temp = temp->next;
}
}
• displayNodesReverse() member function displays
the contents of a linked list in reverse order, beginning
with the node at the back of the linked list and
continuing until the first node is displayed.
• void displayNodesReverse()
{   
cout << "Nodes in reverse order:";  
 NODE* temp = back;   
while(temp != NULL)  
 {       cout << " " <<  temp->data;      
temp = temp->previous;
}
}
Figure 3-14: Deleting the first node in the list.
Figure 3-15: Deleting nodes in between other two nodes.

Question: How about deleting the last node?


algorithm deleteNode(
ref list <metadata>, val 1 dataOut = pLoc->data
pPre <node pointer>
2 if (pPre null)
val pLoc <node pointer>
ref dataOut <dataType>) Deleting first node
Deletes data from a linked list and returns 1 list.head = pLoc->link
it calling module 3 else
Pre list is a metadata structure to a valid Deleting other nodes
list
1 pPre->link = pLoc->link
pPre is a pointer to predecessor node
pLoc is a pointer to node to be 4 end if
deleted
dataOut is variable to receive deleted 5 list.count = list.count - 1
data
6 recycle (pLoc)
Post data have been deleted and returned
to caller 7 return
end deleteNode
• destroyList() member function removes nodes from the
linked list without removing the linked list itself.
void destroyList()
{
  NODE* temp = back;  
 while(temp != NULL)
  {     
 NODE* temp2 = temp;      
temp = temp->previous;
delete temp2;   

 back = NULL;   front = NULL;
}
removing the linked list itself
while(current!=NULL)
  {
        if(current->data!=data)
    {
            prev=current;
            current = current->next;
    }
        else
    {
            prev->next = current->next;
            delete current;
            break;
    }
  }
 Another type of Linked List is a circular linked
list
◦ Here the final node in the list points back to the
first node.
◦ This can be useful for tracking things continually in
sequence
Figure 3-21 A circularly linked list

The last node’s link points to the first node


 The delete from tail function highlighted a
difficulty inherent in singly linked lists.
◦ In order to find the last element in a list, we have to
scan through the entire list till we find the element
pointing to NULL, we then have to delete this
element, while tracking the previous element and
pointing that to NULL.
 If we are dealing with a long list, or frequent
tail_delete function calls, it may be useful to
have a Doubly Linked List.
Head Tail

Data Data Data


0
0
Figure 3-22 A doubly linked list`

A linked list structure in which each node has pointer to both its successor
and its predecessor.

Three pieces of metadata of the Each node contains two pointers:


head structure: 1. Backward pointer – points to its
1. Count predecessor
2. Position pointer for traversals 2. Forward pointer – points to its
3. Rear pointer (optional) successor
Figure 3-23 Doubly linked list insert
Figure 3-24 Doubly linked list delete

You might also like