Title: COT 3002 Foundations of Computer Science
1COT 3002 - Foundations of Computer Science
Linked Lists
- Professor Georgiana Hamza-Lup
- ghamzal_at_fau.edu
2Linked Lists
- A linked list is a sequence of items, with each
item connected to the next by a link (a pointer) - A linked list can grow and shrink as necessary
while the program is running - An item and its link form a node (the
link/pointer inside each node points to the next
node) - Nodes can be implemented as structs/classes
struct Node int data Node link
3The Head Pointer
- A node can contain more than one data item
struct Node string data1 int data2
Node link
- The box labeled head is not a node, but a pointer
to the first node - Pointer variable head is declared as
- Node head
4Accessing Items in a Node
- Using the dot operator, just like in any other
structure - (head).data2 12
- head is a pointer variable, so
- head is the node the head points to
- The parentheses are necessary
- because the dot operator . has
- higher precedence than the dereference operator
- Or using the arrow operator
- head-gtdata2 12
- The arrow operator -gt combines the actions of the
dereferencing operator and the dot operator to
specify a member of a struct pointed to by a
pointer
12
5NULL
- The defined constant NULL is used as
- The value of a pointer that has nothing to point
to - An end marker for a linked list
- A program can step through a list of nodes by
following the pointers, but when it finds a node
containing NULL, it knows it has come to the end
of the list - The value of NULL is 0
- Any pointer can be assigned the value NULL
double there NULL - A definition of NULL is found in several
libraries, including ltiostreamgt and ltcstddefgt
(the using directive is not needed)
6Building a Linked List
- The node definition
- Declaring pointer variable head
- head will point to the head node when it is
created - Creating the first node
- the operator new is used to create a node in the
list - 4. Initializing the node
- Since this node is the last
- node the link is set to NULL
struct Node int data Node link
Node head
head new Node
head-gtdata 12 head-gtlink NULL
7Adding a New Node (at the head of the list)
- Create a new node (a dynamic var. pointed to by
temp_ptr) - Node temp_ptr new Node
- Place the data in the new node
- temp_ptr-gtdata number
- Insert the new node in the list
- 3.1. Make temp_ptrs link variable point to the
head node - temp_ptr-gtlink head
- 3.2. Make the head pointer point to temp_ptr
- head temp_ptr
Order of steps is important if 3.2 is performed
before 3.1 the original list is lost (nodes will
still be in memory but will be un-accessible)
8Memory Leaks
- Nodes that are lost by assigning their pointers a
new address are not accessible any longer - The program has no way to refer to the nodesand
cannot delete them to return their memoryto the
freestore - Programs that lose nodes have a memory leak
- Significant memory leaks can cause system crashes
9Function head_insert
void head_insert(Node head, int
entry) //Precondition head is a pointer to the
head of a linked list //Postcondition A new node
containing the given entry //has been added at
the head of the list head now points //to the
new head of the list Node temp_ptr new
Node temp_ptr-gtdata entry temp_ptr-gtlink
head head temp_ptr
- Any functions written to manipulate a linked
listshould be check to see if it works on the
empty list - (an empty list has no nodes head NULL)
10(No Transcript)
11Pointers as Iterators
- An iterator is a construct that allows you to
cycle through the data items in a data structure
to perform an action on each item - An iterator can be an array index or simply a
pointer - Example of using a pointer as an iterator
-
- Node iter for (iterhead iter!NULL
iteriter-gtlink) cout ltlt (iter-gtdata) - head is a pointer to the head node of the list
12Searching a Linked List
- Locates a particular node in a linked list and
returns a pointer to it, so we can use the data
if we find it otherwise, it returns NULL
Node search (Node head, int target)
//Precondition head is a pointer to the head of
a linked list //Postcondition the return value
is a pointer to the first node //containing the
specified target in its data field if there is
no //such node the null pointer is returned
Node here for (here head here ! NULL
herehere-gtlink) if (here-gtdata target)
return here return NULL
13Searching a Linked List (cont.)
- Another algorithm for searching
Node search (Node head, int target)
//Precondition head is a pointer to the head of
a linked list //Postcondition the return value
is a pointer to the first node //containing the
specified target in its data field if there is
no //such node the null pointer is returned
Node here head if (here NULL) return
NULL while ((here-gtdata!target)(here-gtlink!
NULL)) here here-gtlink if
(here-gtdata target) return here else
return NULL
14Inserting a Node in a Linked List
- To insert a node after a specified node in the
linked list - obtain a pointer to the node after which the new
node will be inserted (call that pointer
after_me) - call function insert, defined below, to insert
the node
void insert(Node after_me, int
entry) //Precondition after_me points to a node
in a linked list //Postcondition a new node
containing the given entry has //been added after
the node that after_me points to. Node
temp_ptr new Node temp_ptr-gtdata
entry temp_ptr-gtlink after_me-gtlink
after_me-gtlink temp_ptr
15Inserting the New Node
- This code will accomplish the insertion of the
new node, pointed to by temp_ptr, after the node
pointed to by after_me - temp_ptr-gtlink after_me-gtlink
after_me-gtlink temp_ptr - The order of the above two pointer assignments is
critical if we changed it we loose the last
nodes of the list!
- Notice that inserting into a linked list requires
that you only change two pointers, regardless of
the length of the list - Inserting into an array would require
copying/shifting array elements to the right to
make room for the new item
16Removing a Node from a Linked List
- To remove a node from a linked list
- Position a pointer (before) to point at the node
prior to the node to remove - Position a pointer (discard) to point at the
node to remove - Perform before-gtlink discard-gtlink
- The node is removed from the list, but is still
in memory - Return discard to the freestore
- delete discard
17(No Transcript)
18Removing a Node
void remove(Node after_me) //Precondition
after_me pointes to a node in a linked
list. //Postcondition the node after after_me
has been removed //from the linked list. Node
temp after_me-gtlink after_me-gtlink
temp-gtlink delete temp
Removing the Head of the List
void head_remove(Node head) //Precondition
head pointes to the head of a linked
list. //Postcondition the head node has been
removed from the list. if (head NULL)
return Node temp head head
head-gtlink delete temp
19Assignment with Linked Lists
- If head1 and head2 are pointer variables and
head1 points to the head node of a list then
- head2 head1
- causes head2 and head1 to point to the same list
- There is only one list!
- If you want head2 to point to a separate
copy,you must copy the list, node by node
20Variations on Linked Lists
NULL
struct Node Node prev int count
Node next
NULL
21Variations on Linked Lists (cont.)
struct Node int value Node left
Node right