A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf ·...
Transcript of A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf ·...
A data structure that uses only
the amount of memory needed for
the number of elements in use
at a given point in time.
- easily allocated/deallocated
- direct access (accessing element n
is done simply by indexing: [n])
- no maximum number of elements
- no waste of memory for unused
elements.
A is a dynamically allocated list
, implemented as a structure
that has at least two members:
a (usually) unique identifier
for an element.
a pointer to a node.
A is a linear, dynamically-
allocated data structure. It consists of a
head which is a pointer to a node.
- The points to the first element of
the list; or is when the list is empty.
- The pointer of each node points to
the next node in the list; except the last
node, where is .
class node {
friend class LList;
private:
int key;
node * next;
};
class LList {
private: node * head;
};
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
(well, most anyway)
node * p = head;
while (p != NULL) {
...
p = p->next;
}
#include <iostream>
node::node() {
key = 0;
next = NULL;
}
LList::LList() {
head = NULL;
}
void LList::print() {
cout << p->key << " ";
}
node * LList::search(int srchKey) {
if (p->key == srchKey)
return p;
return NULL;
}
void LList::insert(int newkey) {
node * n = new node;
n->key = newkey;
n->next = NULL;
(n);
}
- a pointer to a node in the List,
after which the new node will be
inserted. NULL to insert new head.
- a pointer to the new node, already
allocated and populated.
Blue arrows/null show
values before insertion.
Red arrows show links
after insertion (changes)
red arrows show
links after changes.
the same code will
work when
head is NULL
void LList::insertAfter(node* after, node* n) {
if (head == NULL || after == NULL) {
n->next = head;
head = n;
}
else {
n->next = after->next;
after->next = n;
}
}
- Does it work for Empty List? [ is NULL]
- Does it work for a List of 1 element?
- as the new head? [ is NULL]
- after the current head? [ == ]
- Does it work for a list of 2 or more elements?
- as the new head? [ is NULL]
- as the new tail? (last in list) [ points to ]
- in the middle of the list?
- a pointer to a node in the List to be removed
Removes the node from the list, but does not
deallocate it.
Removes and deallocates the node.
Use to
find a
pointer to
the node
the one to
remove
void LList::remove(node * r) {
if (r == head)
head = head->next;
else {
node * b4 = head;
while (b4->next != r)
{ b4 = b4->next; }
b4->next = r->next;
}
r->next = NULL; // detach following node
}
- 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?
void LList::remove(node * r) {
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 = b4->next; }
b4->next = r->next;
}
r->next = NULL; }
still points to the removed node
after remove() is finished.
The function invoking may choose
to deallocate it or not.
void LList::fun() {
...
node * p;
p = ptr to node to remove;
remove(p);
cout << p->key << " was removed";
delete p;
...
}
// return: count of the nodes in the list
void LList::length() {
int num = 0;
num++;
return num;
}
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.