Linked Lists. Please Read These slides are provided for the use of students enrolled in James...

90
Linked Lists

Transcript of Linked Lists. Please Read These slides are provided for the use of students enrolled in James...

Page 1: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked Lists

Page 2: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Please ReadThese slides are provided for the use of students enrolled in James Durbano’s Data Structures class (CISC 220). They are the creation of James Durbano and he reserves all rights as to the slides and their content. These slides are not to be modified or redistributed in any way. All of the slides, including all of their content, may only be used by CISC 220 students for the purpose of reviewing the material covered in lecture. Any other use, including but not limited to, the modification of any slides or the sale of any slides, in whole or in part, is expressly prohibited. Possession of this file implies understanding and agreement to the preceding policy.

Further information, including references to outside authors, can be found on the class website.

Page 3: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Objectives Understand the basic building

blocks of linked lists. Understand the advantages (and

disadvantages) of using linked lists.

Understand the basic insertion and deletion operations associated with linked lists.

Be able to code up a linked list and its basic operations.

Page 4: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts A linked list is a collection of data

in which each element contains the location of the next element.

Each element in the list contains 2 parts: Data (Contains the stored data) Link (Contains the address of the next

element in the list)

Page 5: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts

Here we see a basic linked list. There are 4 elements in the list, each one

with a data portion and a link portion. pHead is a pointer to the head of the list.

Typically, the name given to this pointer is the name of the list.

Note the last element of the list. The X in the link portion denotes a NULL pointer (i.e., the end of the list).

Page 6: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Nodes The elements in a linked list are

traditionally called nodes. A node in a linked list is a structure

that has at least 2 fields: one contains the data, the other contains the address of the next element in the list.

A node can contain data of any type, including objects of other classes.

Page 7: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

NodesHere we see 3 examples ofnodes:1) A node with 1 data field

(number) and 1 link field.

2) A node with 3 data fields (name, id, grdPts) and 1 link field.

3) A node with 1 data field and 1 link field. However, this data field contains a structure, which itself contains multiple fields.

Page 8: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Nodes The nodes that make up a linked

list are self-referential structures.

A self-referential structure is one in which each instance of the structure contains a pointer to another instance of the same structural type.

Page 9: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts Data is stored in a linked list

dynamically – each node is created as necessary.

Nodes of linked lists are not necessarily stored contiguously in memory (as in an array).

Although lists of data can be stored in arrays, linked lists provide several advantages.

Page 10: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts Advantage 1: Dynamic A linked list is appropriate when

the number of data elements to be stored in the list is unknown.

Because linked lists are dynamic, their size can grow or shrink to accommodate the actual number of elements in the list.

Page 11: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts The size of a “conventional” C++ array,

however, cannot be altered, because the array size is fixed at compile time.

Also, arrays can become full (i.e., all elements of the array are occupied).

A linked list is full only when the computer runs out of memory in which to store nodes.

Note: it is possible to create a dynamic array.

Page 12: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts Advantage 2: Easy Insertions and

Deletions Although arrays are easy to implement and

use, they can be quite inefficient when sequenced data needs to be inserted or deleted.

With arrays, it is more difficult to rearrange data (copying to temporary variables, etc).

However, the linked list structure allows us to easily insert and delete items from a list.

Page 13: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Concepts Unfortunately, linked lists are not

without their drawbacks. For example, we can perform

efficient searches on arrays (e.g., binary search), but this is not practical with a linked list.

Page 14: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Data Structure One of the attributes of a linked list

is that there is not a physical relationship between the nodes; that is, they are not stored contiguously in memory (as array elements are).

To determine the beginning of the list, and each additional element in the list, we need to use pointers.

Page 15: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Data Structure The pointer to the first node in the list is

referred to as the head pointer, because it points to the head node in the list.

In addition to the head pointer, there are usually other pointers associated with the list: Pointer to the last element in the list (tail

pointer) Pointer that traverses the list to find data

(navigator or traversal pointer)

Page 16: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Data Structure Oftentimes it is convenient to create

a structure that contains the head pointer of a list and also information about the list itself (e.g., number of elements currently in the list, maximum number of elements to be allowed in the list, etc).

This extra data is referred to as metadata.

Page 17: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Data Structure A linked list is usually implemented with

2 classes: List class Node class

The Node class contains the data and link fields.

The List class contains the head pointer and the metadata, along with the list insert and delete functions.

Page 18: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Algorithms With a linked list, there are a

standard set of functions that operate on the list: Creating the list Inserting nodes Deleting nodes Traversing the list Destroying the list

Page 19: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Linked List Algorithms The algorithms we discuss will be

implemented using pseudocode. Note that these algorithms are merely

used to introduce the concepts involved when working with linked lists … they do not necessarily represent the best way to implement a linked list in practice.

The end of the slides contains an actual C++ linked list implementation.

Page 20: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Create List This function is used to initialize a

linked list. Typically, this is implemented in

the constructor for the List class.

Page 21: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Create ListPseudocodehead = null

? ?

count headlist

\0 count = 0 0

Page 22: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert Node This function inserts a node into the

linked list. To insert a node into the list, we need a

pointer to the node’s logical predecessor.

Given the predecessor, there are 3 steps to the insertion:

1) Allocate memory for the new node2) Point the new node to its successor3) Point the new node’s predecessor to the

new node.

Page 23: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert Node The inserted node can come at the

beginning, middle, or end of the list.

We will look at each of these possibilities …

Page 24: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert into an Empty List When the head pointer of the list is null,

the list is empty. To add a node to an empty list, we

simply assign the head pointer the address of the new node and set the link field of the new node to NULL to indicate the end of the list.

Note: it’s usually a good idea to initialize the link field of a node to NULL in the node’s constructor.

Page 25: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert into an Empty ListPseudocodehead = pNew

? ?

count headlist

\0 0 \0 75

pNew

1

Here, we want to insert our newnode in the list.pNew is a pointer to the new node.

Page 26: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert at Beginning We insert a node at the beginning of

the list anytime we need to insert a node before the first element of the list.

To insert at the beginning of the list, we set the link portion of our new node to the head pointer of the list and then set the head pointer to the new node.

Page 27: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert at BeginningPseudocodepNew->link = head

?

count headlist

0 \0 75

pNew

1

39

head = pNew

2

\0

Here, we want to insert our newnode before 75 to make it the first element of the list.

Page 28: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert in Middle To insert a node between two

nodes, we point the new node to its successor and then point its predecessor to the new node.

Page 29: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert in MiddlePseudocodepNew->link = pPre->link

?

count headlist

2 \0 75

pPre->link = pNew

pNew

52 \0

39

pPre

Here, we want to insert our newnode between 39 and 75.pPre is a pointer to the predecessor node.

3

Page 30: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert at End To insert a node at the end of the

list, we only need to point the last node of the list to the new node and set the new node’s link field to NULL.

Page 31: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert at EndPseudocodepPre->link = pNew

Here, we want to insert our newnode after 75 at the end of the list.pPre is a pointer to the predecessor node. 3

count headlist

75

pNew

134 \0

39

pPre

52

\0 4

Page 32: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert Node Algorithmif( pPre null )

//adding before 1st node or to empty listpNew->link = headhead = pNew

else//adding in middle or at endpNew->link = pPre->linkpPre->link = pNew

end ifcount++

Page 33: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Insert Node Algorithm Note that although we presented 4

different cases (insert into an empty list, insert at front, insert at back, and insert in middle) we were able to reduce the code to 2 cases: Adding before 1st node or to an empty

list. Adding in the middle or at the end of

the list.

Page 34: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Delete Node The delete node algorithm logically

removes a node from the linked list by changing various link pointers and then reclaiming the memory used by the node.

The delete situations parallel those for insert: Delete the only node Delete the first node Delete the last node Delete a node in the middle

Page 35: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Delete First Node When we delete the first node, we

must reset the head pointer to point to the first node’s successor and then recycle the memory for the deleted node.

The pseudocode to delete the first node in the list also applies to the case where we are deleting the only node in the list.

Page 36: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Delete First NodePseudocodehead = pLoc->link

3

count headlist 134 39

\0pPre

Here, we want to delete the first node in the list (39).pPre is NULL as there is no predecessor to 39 and pLoc points to the node we want to delete.

75

\0

pLoc

delete pLoc

(available) 2

Page 37: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

General Delete Case We call deleting any node other

than the first node a general case because the same logic applies to deleting a node in either the middle or at the end of the list.

For both of these cases, we simply point the predecessor node to the successor node (of the node being deleted).

Page 38: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

General Delete CasePseudocodepPre->link = pLoc->link

count headlist 134 39

pPre

Here, we want to delete 75.pPre is a pointer to the predecessor node and pLoc points to the node we want to delete.

75

\0

pLoc

delete pLoc

(available)

3 2

Page 39: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Delete Node Algorithmif (pPre null)

//Deleting first nodehead = pLoc->link

else//Deleting other nodespPre->link = pLoc->link

end ifdelete pLoccount--

Page 40: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Search List A search list function is used by

several algorithms to locate data in a list: To insert data, we need to know the

logical predecessor to the new data. To delete data, we need to find the

node to be deleted and identify its logical predecessor.

To retrieve data from a list, we need to search the list and find the data.

Page 41: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Ordered List Search First let us consider searching a list

in which the elements are sorted based on a field value.

Given a target key, the ordered list search attempts to locate the requested node in the linked list.

If a node in the list matches the target, the search returns true, else we return false.

Page 42: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Ordered List Search We start at the beginning and

search the list sequentially until the target value is no longer greater than the current node’s key.

When the target value becomes less than the current node’s key, we know that the item being searched for cannot be in the list.

Page 43: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Ordered List SearchpLoc = pHeadloop (pLoc not null AND target > pLoc-

>data.key)pLoc = pLoc->link

end loop//Set return valueif( pLoc null )

found = false

elseif(target equal pLoc->data.key)

found = trueelse

found = falseend if

end ifreturn found

1 2 4 5

See what happens iftarget = 3, 4, and 6

Page 44: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Unordered List Search In our previous slides, we assumed

that the list was ordered on a key. Oftentimes we need to search an

unordered list looking for a specific data match.

To do this, we use a simple sequential search (i.e., just search every node in order).

Page 45: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Empty List

The code we write often assumes that the list is not empty.

Therefore, we will develop a function to check whether or the not the list is empty before we begin searching, inserting, deleting, etc.

This is simply done with the following:

return (count equal to zero)

Page 46: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Full List Similarly, we must also make sure

that our list is not full before we begin.

To do this, we need to make sure that our system has more memory and that the number of nodes we have created is less than the maximum number of nodes we have designated for our list.

Page 47: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Full Listif (count equal to maxCount)

return trueend ifallocate pNewif (allocation successful)

delete pNewreturn false

end ifreturn true

Page 48: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

List Count This is a simple member function

to return the number of elements in the list:

return count

Page 49: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Traverse List Algorithms that traverse a list start at

the first node and examine each node in succession until the last node has been processed.

Traversal logic is used by several different types of algorithms, such as changing a value in each node, printing the list, summing a field in the list, or calculating the average of a field.

Any application that requires that the entire list be processed uses a traversal.

Page 50: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Traverse List To traverse the list we need a

walking (navigator) pointer. This pointer is used to move from

node to node as each element is processed.

Page 51: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Traverse ListpWalker = headloop (pWalker not null)

process (pWalker->data)pWalker = pWalker->link

end loop

We begin by setting the walking pointer to the first node in the list.

We then process the first node and continue until all of the data has been processed.

When the last node has been processed, the walking pointer becomes null and the loop terminates.

Page 52: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Destroy List When a list is no longer needed but the

application is not finished, the list should be destroyed.

This function will delete any nodes in the list, reclaim their memory, and set any metadata to an empty list condition.

This type of functionality is commonly found in the destructor.

Page 53: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Destroy List

loop (head not null)pNodeToDelete = headhead = head->nextdelete pNodeToDelete

end loop

Page 54: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Testing As with all software development

applications, it is vital to test your implementation.

Here are some tests to run on your toolbox of linked list functions to verify correct operation.

Page 55: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Testing Insert Logic

1) Insert a node into an empty list.2) Insert a node before the 1st node.3) Insert a node between two nodes.4) Insert a node after the last node.

Page 56: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Testing Delete Logic1) Delete the entire list.2) Delete the first node.3) Delete a node between two nodes.4) Delete the last node.5) Try to delete a node that doesn’t exist.6) Try to delete a node whose key is less

than the first data node’s key.7) Try to delete a node whose key is greater

than the last data node’s key.8) Try to delete from an empty list.

Page 57: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Complex Linked List Structures In this section, we introduce 3

useful linked list variations: Circular linked list Doubly linked list Circular doubly linked list Multilinked list

Page 58: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Circular Linked Lists In a circular linked list, the last node’s

link points to the first node of the list.

Page 59: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Circular Linked Lists Insertions and deletions into a circular

linked list follow the same logic patterns used in a singly linked list except that the last node points to the first node.

Therefore, when inserting or deleting the last node, in addition to updating the tail pointer, we must also point the link field of the new last node to the first node.

Page 60: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Circular Linked Lists Note that the last element in the list does

not have a NULL valued link field … it points back to the first node.

In this case, how do we know when we have finished searching the list?

loop (target not equal to pLoc->data.key AND pLoc->link not equal to head)

Note: you must still account for the last node if you implement this logic.

Page 61: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Circular Linked Lists One application: a queue. When we get to queues next week, we will

see that they are essentially a linked list with pointers to the head and tail nodes.

However, if the queue is implemented using a circular linked list, only one pointer is needed -- tail.

Why?

because head = tail->next …

Page 62: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Doubly Linked Lists One of the most powerful

variations of a linked list is the doubly linked list.

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

Page 63: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Doubly Linked Lists The doubly linked list eliminates

the need for the “pPre” pointer since each node has pointers to both the previous and next nodes in the list.

Page 64: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Doubly Linked Lists A variation on the doubly linked list is the

circular doubly linked list. In this variation, the forward pointer of the

last node points to the first node and the backward pointer of the first node points to the last node.

Advantage: if you have a “dummy” node at the beginning of the list, there are no special cases to worry about when inserting/deleting.

Page 65: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Multilinked Lists A multilinked list is a list with two or more

logical key sequences. For example, consider the list of the first 10

presidents of the United States. We could have a list that is sorted by:

Date the president assumed office Alphabetical order based on the president’s last

name Alphabetical order by the president’s wife’s maiden

name etc.

Page 66: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Multilinked Lists We may wish to have the list sorted in all these formats. The beauty of the multilinked list is that the data exists

only once, but there are different pointers for the different lists.

Page 67: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Actual Implementations Up to now, we have introduced

everything in very abstract terms and have used pseudocode.

Now we will develop a more concrete view of the linked list.

In the coming slides, we will develop the C++ code to implement a basic linked list and some of the functions associated with it.

Page 68: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Introduction to the Code We will have 2 main classes:

List Node

List is the class that all the functions of the list will be included in (e.g., add, delete, print, etc).

Node is the class that will contain the structure and operations for a specific node in the linked list.

Page 69: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Node Classclass Node {

friend class List;public:

Node();Node(int);

private:int data;Node *next;

};

We declare List as a friend of Node. This way, List will be able to access Node’s private data members directly.Here are our constructors: the first one is the default constructor.

data is the value stored in the node while next is the link pointer – the pointer to the next node in the list.

Page 70: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Node Class ConstructorsNode::Node() {

data = 0;next = NULL;

}

Node::Node(int value) {data = value;next = NULL;

}

The constructors initialize the data value and also set the link field of the node to NULL.

This constructor assigns a given data value upon instantiation.

Page 71: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

List Classclass List {public:

List();void printList();void deleteList();bool isEmpty();Node* findNode(int);void addToFront(Node *);void addToBack(Node *);

void deleteFromFront();void deleteFromBack();

private:int numNodes;Node *head; };

Functions to print and delete the list.

Functions to add/delete nodes to/from the list.

Metadata (number of nodes in the list) and a pointer to the first node in the list.

A function to find a node with a given value.

A helper function.

Page 72: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

List Class Constructor

List::List() {numNodes = 0;head = NULL;

}

Here we initialize the number of nodes in the linked list to 0 and set the head pointer to NULL.

Remember: the constructor is called upon instantiation. Since the list was just instantiated, it has no nodes yet.

Page 73: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

deleteListvoid List::deleteList() {

Node *toDelete;while( head != NULL ) {toDelete = head;head = head->next;delete toDelete;}numNodes = 0;

}

This function is used to delete the entire list.

Since the list is now empty, we set the number of elements in the list to 0.

We go through the list, node by node, deleting each node along the way.

Page 74: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

isEmptybool List::isEmpty() {

if (numNodes == 0) {

return true;}else {

return false;}

}

If the list is empty, this function will return TRUE. If there are nodes in the list, it will return FALSE.

Here we check to see if the list is empty … what’s another way to check to see if the list is empty?if (head == NULL) …

Page 75: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

addToFront

void List::addToFront(Node *nodeToAdd) {nodeToAdd->next = head;head = nodeToAdd;numNodes++;

}

We increment the number ofnodes in the list when we’re done.

We have to make sure to update the head pointer.

This function is used to insert a node at the front of the list.

Page 76: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

addToBackvoid List::addToBack(Node *nodeToAdd) {

if( isEmpty() ) {head = nodeToAdd;

}else {

Node *navigator = head;while( navigator->next != NULL ) {

navigator = navigator->next;}navigator->next = nodeToAdd;

}numNodes++;}

This function is used to insert a node at the back of the list.

If the list is empty, we need to handle the insert differently.

We increment the number ofnodes in the list when we’re done.

We are traversing the list to find the last node.

1 2 4 5

Page 77: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

deleteFromFrontvoid List::deleteFromFront() {

if( !isEmpty() ) {Node *newHead = head-

>next;delete head;head = newHead;numNodes--;

}else {}

}We decrement the number ofnodes in the list when we’re done.

This function is used to delete a node from the front of the list.

We have to make sure to update the head pointer.

Page 78: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

deleteFromBackvoid List::deleteFromBack() {

if( !isEmpty() ) { Node *follower = NULL; Node *leader = head; if( numNodes == 1 ) {

delete head;head = NULL;

} else {

while( leader->next != NULL ) { follower = leader; leader = leader->next;}delete leader;follower->next = NULL;

} numNodes--;}}

This function is used to delete a node from the back of the list.

We decrement the number of nodes in the list when we’re done.

We need to handle the special case of a list with 1 node.

Note that we need 2 temppointers for this function.

Page 79: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

findNodeNode* List::findNode(int valueToFind) {

Node *navigator = head;while(navigator != NULL && navigator->data !=valueToFind){

navigator = navigator->next;}return navigator;

}

This function returns a pointer to the node with a given value.

If this is not the 1st condition in the loop, your program might seg fault.

Keep looping until we find the node with the data.

Page 80: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

printListvoid List::printList() {

cout << "Number of elements in list: " << numNodes << endl;Node *navigator = head;while( navigator != NULL ) {

cout << (navigator->data) << endl;navigator = navigator->next;

}}

This function prints the entire list.

Initially we print the number of elements in the list.

Then we traverse the list, printing out the data values as we go.

Page 81: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

mainint main(int argc, char* argv[]){

List list;list.printList();Node *firstNode = new Node(5);list.addToFront(firstNode);list.printList();Node *lastNode = new Node;list.addToBack(lastNode);list.printList();

Node *newFirstNode = new Node(2);

list.addToFront(newFirstNode);list.printList();list.deleteFromBack();list.printList();list.deleteFromFront();list.printList();list.deleteList();list.printList();return 0; }Note that when we make a new node, it is declared as a pointer to a node.

Page 82: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Output of Program

Page 83: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Another Example In this example, we will develop

the C++ code that reads a list of integers from the keyboard, creates a linked list out of them, and prints the results.

Page 84: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Node Class

class Node {friend class List;

public:Node(int);

private:int data;Node *next;

};

We declare List as a friend of Node. This way, List will be able to access Node’s private data members directly.We make sure to create a constructor.

data is the value stored in the node while next is the link pointer – the pointer to the next node in the list.

Page 85: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

List Class

class List {public:

List();void addToList(Node *);void printList();

private:Node *head;

};

The List class is what contains all the functions we use to operate on the list.

head is a pointer to the first node of the list.

Page 86: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Constructors

List::List() {head = NULL;

}

Node::Node(int value) {data = value;next = NULL;

}

The List constructor initializes the empty list.

The Node constructor initializes the data to the given value and the link pointer to NULL.

Page 87: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

addToListvoid List::addToList(Node *nodeToAdd) {

if(head == NULL) {head = nodeToAdd;

}else {

Node *navigator = head;while( navigator->next != NULL ) {

navigator = navigator->next;}navigator->next = nodeToAdd;

}}

We handle the special case of an empty list.

Here, we traverse the list until we get to the last node.

Once we get to the end of the list, we insert the node.

Page 88: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

printListvoid List::printList() {

Node *navigator = head;cout << "List: ";while( navigator != NULL ) {

cout << navigator->data << " ";navigator = navigator->next;

}cout << endl;

}

Here, we traverse the list and print out each node’s data along the way.

Page 89: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Mainint main(int argc, char* argv[]){

List list;int input, done = 0;while( !done ) {

cout << "Enter an integer (-9999 to quit): ";cin >> input;if( input != -9999 ) {

Node *newNode = new Node(input);list.addToList(newNode);

}else { done = 1; }

}list.printList();

}

Here we read a list of integers from the user until we receive a –9999.

After each integer, we add the node to the list.

Page 90: Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.

Final Suggestion To code linked lists, there is only

one thing to remember …

DRAW A PICTUREDRAW A PICTUREDRAW A PICTURE