Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 13 Pointers and Linked Lists

2 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. 521047NULL A linked list A node

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

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

5 Accessing members of a Node Using dot operator: (*head).data = 5; Using arrow operator (more convenient): head-> data = 5;

6 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. headNULL 521047NULL head

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

8 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 NULL head->link = NULL; 5NULL head ? ? 5

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

10 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) 5NULL head 0 5NULLhead ?temp 1 5NULLhead temp3 2 5NULLhead temp 3 3 5NULLhead temp 3 4 5NULLhead 3 Finish function call 5

11 Losing Nodes void 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) 5NULLhead ?temp 1 5NULLhead temp3 2 5NULLhead temp 3 3 5NULLhead 3 After function call 4 5NULL head 0

12 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 afterMe void 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) 7NULL head 35 AfterMe temp6 4 7NULL head 35 AfterMe temp6 5 7NULL head 35 AfterMe temp6 123123 7NULL head 35 AfterMe 0 7NULL head 35 AfterMe 6 After function call 6

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 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 3567 0 Before function call remove(head, 3) After function call NULL head 567 2 After function call NULL head 1 NULL head 3567 1

14 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 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 3567 0 NULL head 3567 1 before discard NULL head 3567 2 before discard NULL head 3567 3 before discard After function call NULL head 3567 4

15 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 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 3567 0 NULL head 3567 1 before discard NULL head 3567 2 before discard After function call NULL head 357 5 NULL head 3567 3434 before discard

16 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 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 3567 0 NULL head 3567 1 before discard NULL head 3567 2 before discard After function call NULL head 357 5 NULL head 3567 3434 before discard

17 Doubly linked list Each node has 2 pointers forward and back. struct Node { int data; Node* forward; Node* back; }; 5 73 NULL front back

18 Binary Tree Each node has 2 pointers left and right. struct TreeNode { int data; Node* left; Node* right; }; 40 5020 NULL root 103060 NULL

19 Stacks A data structure that retrieves data in the reverse of the order in which the data is stored. Last In First Out (LIFO) AB A C CBACBA BABA BABA C A B A STORE RETRIEVE

20 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

21 Queues A data structure that retrieves data in the order in which the data is stored. First In First Out (FIFO)

22 Queues A data structure that retrieves data in the order in which the data is stored. First In First Out (FIFO) AB A C CBACBA BABA CBCB A C B C STORE RETRIEVE

23 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


Download ppt "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."

Similar presentations


Ads by Google