chapter 13

23
Chapter 13 Pointers and Linked Lists

Upload: parley

Post on 05-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Chapter 13. Pointers and Linked Lists. Nodes and Linked Lists. Linked list: A sequence of nodes in which each node is linked or connected to the node preceding it. Node: a composite type (struct/class) that has member(s) as pointer(s) of its type. 5. 10. 4. 7. 2. NULL. A linked list. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 13

Chapter 13

Pointers and Linked Lists

Page 2: Chapter 13

Nodes and Linked Lists

• Linked list: A sequence of nodes in which each node is linked or connected to the node preceding it.

• Node: a composite type (struct/class) that has member(s) as pointer(s) of its type.

5 210 4 7 NULL

A linked list A node

Page 3: Chapter 13

Node

• Is a composite type (struct/class) with at least 2 members:– The first one is the data– The second one is the pointer point to the next

element in the list.

• Ex: struct Node {

int data;Node *link;

}typedef Node* NodePtr;

Page 4: Chapter 13

Arrow Operator (->)

• Used only with pointer variable.• Simplifies the notation specifying the members of a

struct (or a class).• Combining the actions of dereferencing operator (*)

and a dot operator (.) to specify a member of a dynamic struct or object that is pointed to by a pointer.– Those statements are equivalent:

head-> data = 5; (*head).data = 5;

Page 5: Chapter 13

Accessing members of a Node

• Using dot operator:(*head).data = 5;

• Using arrow operator (more convenient):head-> data = 5;

Page 6: Chapter 13

Linked List• Each linked list starts with a pointer head which points to the first element

in the list.• Ex:

Node *head;NodePtr head;

• Since the last element in the list doesn’t point to anything, its pointer is assigned NULL to signify the end.

• If head is a pointer to the biginning of list and head == NULL, then the list is empty.

head NULL

5 210 4 7 NULL

head

Page 7: Chapter 13

Operations on a Linked list

• Pass in the head pointer but always assign to a local variable or you can lose the head of the list.

• The head pointer must be preserved or the whole list is lost.

• Only pass by reference if the head needs to be changed (to insert or delete at the head of the list)

Page 8: Chapter 13

Initialize a list// create a new node pointer NodePtr head;

// point it to a new node head = new Node;

// initialize a value for data head->data = 5;

// end the list with NULLhead->link = NULL;

5 NULL

head

?head

?head

5head

Page 9: Chapter 13

Inserting a Node at the Head of a List

• Precondition: The pointer argument head points to the beginning of the linked list.

• Postcondition: A new node containing num added at the beginning of the linked list.

• Pseudocode:– Create a new dynamic variable pointed to by temp.– Place the data in this new node.– Make the link member of this new node point to the first

node of the original linked list.– Make the pointer variable named head point to the new

node.

Page 10: Chapter 13

Inserting a Node at the Head of a List (code)

void HeadInsert (NodePtr& head, int num){

NodePtr temp;temp = new Node;temp->data = num;

temp->link = head;head = temp;

}

Before function call HeadInsert(head, 3)

5 NULL

head 0

5 NULLhead

?temp 1

5 NULLhead

temp 32

5 NULLhead

temp3 3

5 NULLhead

temp3 4

5 NULLhead

3

Finish function call

5

Page 11: Chapter 13

Losing Nodesvoid HeadInsert (NodePtr& head, int num){

NodePtr temp;temp = new Node;temp->data = num;

//temp->link = head;head = temp;

}

Memory leak: caused by dynamic variables not returning their memory when they are out of scope.

Before function call HeadInsert(head, 3)

5 NULLhead

?temp 1

5 NULLhead

temp 32

5 NULLhead

temp3 3

5 NULLhead

3

After function call

4

5 NULLhead

0

Page 12: Chapter 13

Insert a Node in the middle of the list

// Precondition: afterMe points to a node// in the linked list// Postcondition: A new node containing num // is added after the node pointed by afterMevoid insert (NodePtr afterMe, int num){

NodePtr temp;temp = new Node;temp->data = num;

temp->link = afterMe->link;afterMe->link = temp;

}

Before function call insert(AfterMe, 6)

7 NULL

head

3 5

AfterMe temp 6 4

7 NULL

head

3 5

AfterMe temp 6 5

7 NULL

head

3 5

AfterMe temp 6

123

7 NULL

head

3 5

AfterMe 0

7 NULL

head

3 5

AfterMe 6

After function call

6

Page 13: Chapter 13

Remove a Node from the list// Precondition: head is a linked list// Postcondition: A new linked list NOT// containing num void remove (NodePtr head, int num){

if (head == NULL){ cout << "The list is empty."; return;}if (head->data == num) head = head->link;else{ NodePtr before = head; NodePtr discard = head->link; while (discard->data < num && discard->link != NULL) {

before = discard; discard = discard->link;

} if (discard->link == NULL) // the last node

cout << num << " is not in the list\n"; else

if (discard->data == num) { before->link = discard->link; discard->link = NULL; // optional}

}}

Before function call remove(head, 6) fromempty list

NULL

head 0

NULL

head

3 5 6 7

0

Before function call remove(head, 3)

After function call

NULL

head

5 6 7

2

After function call

NULL

head 1

NULL

head

3 5 6 7

1

Page 14: Chapter 13

Remove a Node from the list// Precondition: head is a linked list// Postcondition: A new linked list NOT// containing num void remove (NodePtr head, int num){

if (head == NULL){ cout << "The list is empty."; return;}if (head->data == num) head = head->link;else{ NodePtr before = head; NodePtr discard = head->link; while (discard->data < num && discard->link != NULL) {

before = discard; discard = discard->link;

} if (discard->link == NULL) // the last node

cout << num << " is not in the list\n"; else

if (discard->data == num) { before->link = discard->link; discard->link = NULL;}

}}

Before function call remove(head, 9)

NULL

head

3 5 6 7

0

NULL

head

3 5 6 7

1before discard

NULL

head

3 5 6 7

2before discard

NULL

head

3 5 6 7

3before discard

After function call

NULL

head

3 5 6 7

4

Page 15: Chapter 13

Remove a Node from the list// Precondition: head is a linked list// Postcondition: A new linked list NOT// containing num void remove (NodePtr head, int num){

if (head == NULL){ cout << "The list is empty."; return;}if (head->data == num) head = head->link;else{ NodePtr before = head; NodePtr discard = head->link; while (discard->data < num && discard->link != NULL) {

before = discard; discard = discard->link;

} if (discard->link == NULL) // the last node

cout << num << " is not in the list\n"; else

if (discard->data == num) { before->link = discard->link; discard->link = NULL;}

}}

Before function call remove(head, 6)

NULL

head

3 5 6 7

0

NULL

head

3 5 6 7

1before discard

NULL

head

3 5 6 7

2before discard

After function call

NULL

head

3 5 7

5

NULL

head

3 5 6 7

34before discard

Page 16: Chapter 13

Remove a Node from the list// Precondition: head is a linked list// Postcondition: A new linked list NOT// containing num void remove (NodePtr head, int num){

if (head == NULL){ cout << "The list is empty."; return;}if (head->data == num) head = head->link;else{ NodePtr before = head; NodePtr discard = head->link; while (discard->data < num && discard->link != NULL) {

before = discard; discard = discard->link;

} if (discard->link == NULL) // the last node

cout << num << " is not in the list\n"; else

if (discard->data == num) { before->link = discard->link; discard->link = NULL;}

}}

Before function call remove(head, 6)

NULL

head

3 5 6 7

0

NULL

head

3 5 6 7

1before discard

NULL

head

3 5 6 7

2before discard

After function call

NULL

head

3 5 7

5

NULL

head

3 5 6 7

34before discard

Page 17: Chapter 13

Doubly linked list

Each node has 2 pointers forward and back.struct Node{

int data;Node* forward;Node* back;

};

5 73 NULLNULL

front back

Page 18: Chapter 13

Binary Tree

Each node has 2 pointers left and right.struct TreeNode{

int data;Node* left;Node* right;

};

40

5020

NULL

NULL

root

10 30 60

NULL NULLNULLNULL NULL

Page 19: Chapter 13

Stacks

• A data structure that retrieves data in the reverse of the order in which the data is stored.

• Last In First Out (LIFO)A B

A

C

CBA

BA

BA

C

A

B A

STORE

RETRIEVE

Page 20: Chapter 13

Stack Class#ifndef STACK_H#define STACK_H

struct StackFrame{ char data; StackFrame *link;};

typedef StackFrame* StackFramePtr;

class Stack {public: Stack (); // Initializes the object to an empty stack Stack (const Stack& aStack); // Copy constructor ~Stack (); // Destroys the stack and returns free all memory

void push (char symbol); // Post condition: symbol has been added to the stack char pop (); // Precondition: The stack is NOT empty // Returns the top symbol on the stack and // remove that top symbol from the stack bool empty () const; // Returns true if the stack is empty. Returns false otherwise.

private: StackFramePtr top;};#endif

Page 21: Chapter 13

Queues

• A data structure that retrieves data in the order in which the data is stored.

• First In First Out (FIFO)

Page 22: Chapter 13

Queues

• A data structure that retrieves data in the order in which the data is stored.

• First In First Out (FIFO)

A B

A

C

CBA

BA

CB

A

C

B C

STORE

RETRIEVE

Page 23: Chapter 13

Queue Class#ifndef QUEUE_H#define QUEUE_H

struct QueueNote{ char data; QueueNote *link;};

typedef QueueNote * QueueNotePtr;

class Queue {public: Queue (); // Initializes the object to an empty queue Queue (const Queue& aQueue); // Copy constructor ~Queue (); // Destroys the queue and returns free all memory

void add (char item); // Post condition: item has been added to the back of the queue char remove (); // Precondition: The queue is NOT empty // Returns the item at the front of the queue and // remove that top item from the queue bool empty () const; // Returns true if the queue is empty. Returns false otherwise.private: QueueNotePtr top; // points to the head of the linked list QueueNotePtr back // points to the node at the other end of the // linked list. Items are added at this end.};#endif