Download presentation
Presentation is loading. Please wait.
1
Linked Lists
2
Motivation A list data structure that uses only the amount of memory needed for the number of elements in use at a given point in time.
3
Compared to Arrays Arrays: - easily allocated/deallocated - direct access (accessing element n is done simply by indexing: [n]) Linked Lists: - no maximum number of elements - no waste of memory for unused elements.
4
Node A Node is a dynamically allocated list element, implemented as a structure that has at least two members: - key: a (usually) unique identifier for an element. - next: a pointer to a node.
5
Definition A Linked List is a linear, dynamically- allocated data structure. It consists of a head which is a pointer to a node. - The head points to the first element of the list; or is NULL when the list is empty. - The next pointer of each node points to the next node in the list; except the last node, where next is NULL.
6
Examples:
7
Class Definitions class node { friend class LList; private: int key; node * next; // circular reference OK }; class LList { private: node * head;
8
Algorithms When designing a function that operates on a Linked List, consider if the solution works for: - an empty list - a list of one node - a list of two or more nodes
9
"The Loop that solves all problems!"
Algorithms "The Loop that solves all problems!" (well, most anyway) // point p to the first/head node node * p = head; while (p != NULL) { // process this node ... p = p->next; // go to next node } // or NULL
10
Constructors #include <iostream> node::node() { key = 0; next = NULL; } LList::LList() { head = NULL;
11
Print void LList::print() { node * p = head; while (p != NULL) { cout << p->key << " "; p = p-> next; }
12
Search // return ptr to found node or // NULL = not found node * LList::search(int srchKey) { node * p = head; while (p != NULL) { if (p->key == srchKey) return p; p = p->next; } return NULL; //looked at all, not found
13
Insert A new node should first be created and populated:
void LList::insert(int newkey) { node * n = new node; //allocate new node n->key = newkey; n->next = NULL; insert(n); // some insertion method } // given pointer to new node
14
Insert (After) Given: - after: a pointer to a node in the List, after which the new node will be inserted. NULL to insert new head. - n: a pointer to the new node, already allocated and populated.
15
Insert General Case: Blue arrows/null show values before insertion. Red arrows show links after insertion (changes)
16
Insert New Head Case: red arrows show links after changes. Empty List Case: the same code will work when head is NULL
17
Insert void LList::insertAfter(node* after, node* n) { if (head == NULL || after == NULL) { n->next = head; // insert as new head = n; // head of list } else { // insert middle/tail of list n->next = after->next; after->next = n; } // note: ORDER of statements is IMPORTANT!
18
Insert Checks: - Does it work for Empty List? [head is NULL]
- Does it work for a List of 1 element? - as the new head? [after is NULL] - after the current head? [after == head] - Does it work for a list of 2 or more elements? - as the new tail? (last in list) [after points to tail] - in the middle of the list?
19
Remove Given: r- a pointer to a node in the List to be removed
Removes the node from the list, but does not deallocate it. Alternative: Removes and deallocates the node.
20
Remove Remove Head case:
21
Remove General Case: Use b4 to find a pointer to the node before the one to remove
22
Remove void LList::remove(node * r) { if (r == head) head = head->next; else { // must find node BEFORE one being removed node * b4 = head; while (b4->next != r) { b4 = b4->next; } b4->next = r->next; } r->next = NULL; // detach following node
23
Remove Checks: - Assumption: never invoked on Empty List
- Assumption: r points to a node actually in the List. - Does it work for a List of 1 element? - Does it work for a list of 2 or more elements? - removing the head? - removing the tail? - removing a node in the middle of the list?
24
Remove - Defensive Version
void LList::remove(node * r) { if (head == NULL) return; // list empty, can't remove if (r == NULL) return; // nothing to remove!? if (r == x.head) x.head = x.head->next; else { // must find node BEFORE one being removed node * b4 = x.head; while (b4->next != r && b4 != NULL) { b4 = b4->next; } if (b4 == NULL) return; // node not in list!! b4->next = r->next; } r->next = NULL; }
25
Remove Note: r still points to the removed node after remove() is finished. The function invoking remove() may choose to deallocate it or not.
26
Remove - invocation void LList::fun() { ... node * p; p = ptr to node to remove; remove(p); cout << p->key << " was removed"; delete p; // deallocate removed node }
27
Length // return: count of the nodes in the list void LList::length() { int num = 0; node * p = head; while (p != NULL) { num++; p = p->next; } return num;
28
Vocabulary Term Definition Node A dynamically-allocated list element that has at least two members: - key: a (usually) unique identifier for an element. - next: a pointer to a node. Linked List A linear, dynamically-allocated list data structure. It consists of a head which is a pointer to a node, and the list of nodes. - The head points to the first element of the list; or is NULL when the list is empty. - The next pointer of each node points to the next node in the list; except the last node, where next is NULL.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.