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.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Pointers & Dynamic Data Structures Chapter Dynamic Data Structures t Arrays & structs are static (compile time) t Dynamic expand as program executes.
CSC2100B Tutorial 2 List and stack implementation.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 14 Pointers and Linked Lists Slides by David B. Teague, Western Carolina University,
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 Data Structures and Algorithms Stacks and Queues.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Lists, Stacks and Queues in C
Chapter 12 – Data Structures
Pointers and Linked Lists
CC 215 Data Structures Stack ADT
Exercise 1 From Lec80b-Ponter.ppt.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Homework 4 questions???.
Stack and Queue APURBO DATTA.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Pointers and Linked Lists
Review & Lab assignments
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
The List Container and Iterators
Presentation transcript:

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

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;

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;

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

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 NULL head

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)

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

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.

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

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

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 temp NULL head 35 AfterMe 0 7NULL head 35 AfterMe 6 After function call 6

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

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 NULL head before discard NULL head before discard NULL head before discard After function call NULL head

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 NULL head before discard NULL head before discard After function call NULL head NULL head before discard

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 NULL head before discard NULL head before discard After function call NULL head NULL head before discard

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

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

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

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

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

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

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