28

A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head
Page 2: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

A data structure that uses only

the amount of memory needed for

the number of elements in use

at a given point in time.

Page 3: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

- 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.

Page 4: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

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.

Page 5: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

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 .

Page 6: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head
Page 7: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

class node {

friend class LList;

private:

int key;

node * next;

};

class LList {

private: node * head;

};

Page 8: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a 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

Page 9: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

(well, most anyway)

node * p = head;

while (p != NULL) {

...

p = p->next;

}

Page 10: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

#include <iostream>

node::node() {

key = 0;

next = NULL;

}

LList::LList() {

head = NULL;

}

Page 11: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

void LList::print() {

cout << p->key << " ";

}

Page 12: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

node * LList::search(int srchKey) {

if (p->key == srchKey)

return p;

return NULL;

}

Page 13: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

void LList::insert(int newkey) {

node * n = new node;

n->key = newkey;

n->next = NULL;

(n);

}

Page 14: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

- 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.

Page 15: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

Blue arrows/null show

values before insertion.

Red arrows show links

after insertion (changes)

Page 16: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

red arrows show

links after changes.

the same code will

work when

head is NULL

Page 17: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

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;

}

}

Page 18: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

- 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?

Page 19: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

- 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.

Page 20: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head
Page 21: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

Use to

find a

pointer to

the node

the one to

remove

Page 22: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

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

}

Page 23: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

- 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?

Page 24: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

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; }

Page 25: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

still points to the removed node

after remove() is finished.

The function invoking may choose

to deallocate it or not.

Page 26: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

void LList::fun() {

...

node * p;

p = ptr to node to remove;

remove(p);

cout << p->key << " was removed";

delete p;

...

}

Page 27: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

// return: count of the nodes in the list

void LList::length() {

int num = 0;

num++;

return num;

}

Page 28: A data structure that uses only the amount of memory ...kwjoiner/cs215/notes/LinkedLists.pdf · Linked List A linear, dynamically-allocated list data structure. It consists of a head

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.