! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Similar documents
! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Ch. 17: Linked Lists. Introduction to Linked Lists

! A data type for which: ! An ADT may be implemented using various. ! Examples:

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Chapter 17: Linked Lists

Chapter 17: Linked Lists

C++ is Fun Part 14 at Turbine/Warner Bros.! Russell Hanson

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

1 Short Answer (7 Points Each)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Linked List using a Sentinel

! Given the following Structure: ! We can define a pointer to a structure. ! Now studentptr points to the s1 structure.

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CS 103 Unit 11. Linked Lists. Mark Redekopp

CMSC 341 Lecture 7 Lists

Data Structures and Algorithms

Introduction to the Linked List ADT

FORM 2 (Please put your name and form # on the scantron!!!!)

CA341 - Comparative Programming Languages

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS 103 Unit 11. Linked Lists. Mark Redekopp

Dynamic Data Structures

CPSC 427: Object-Oriented Programming

Week 3: Pointers (Part 2)

True or False (15 Points)

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

BBM 201 DATA STRUCTURES

05-01 Discussion Notes

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Name Section: M/W or T/TH. True or False (14 Points)

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Ashish Gupta, Data JUET, Guna

LECTURE 03 LINKED LIST

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CSCE 110 PROGRAMMING FUNDAMENTALS

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Data Structures (CS301) LAB

Intermediate Programming, Spring 2017*

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Assignment of Objects

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Common Misunderstandings from Exam 1 Material

Programming in C/C Lecture 2

Spring 2008 Data Structures (CS301) LAB

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Homework #3 CS2255 Fall 2012

Heap Arrays and Linked Lists. Steven R. Bagley

Data Structure Series

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CS 251 Practice Final Exam R. Brown May 1, 2015 SHOW YOUR WORK No work may mean no credit Point totals will be adjusted to a 120 point scale later

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

REFERENCES, POINTERS AND STRUCTS

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Linked List. ape hen dog cat fox. tail. head. count 5

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Cpt S 122 Data Structures. Data Structures

การทดลองท 8_2 Editor Buffer Array Implementation

Name SECTION: 12:45 2:20. True or False (12 Points)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSCE 110 PROGRAMMING FUNDAMENTALS

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

CS201- Introduction to Programming Current Quizzes

Functions, Arrays & Structs

CS32 Discussion Week 3

Constructor - example

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

2. It is possible for a structure variable to be a member of another structure variable.

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Written by John Bell for CS 342, Spring 2018

A linked list. Subtopic: Linked lists. List-node structure type. Array implementation of a linked list. Inserting into a sorted collection

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Review Questions for Final Exam

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

More C++ : Vectors, Classes, Inheritance, Templates

CS24 Week 3 Lecture 1

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

moretosearch = (location < length);

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

CPSC 427: Object-Oriented Programming

Transcription:

Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure representing a list! A series of nodes chained together in sequence CS 2308 Spring 2013 Jill Seaman - Each node points to one other node.! A separate pointer (the ) points to the first item in the list.! The last element points to nothing () list 1 2 Introduction to Linked Lists! The nodes are dynamically allocated - The list grows and shrinks as nodes are added/ removed. Linked lists can easily insert a node between other nodes Linked lists can easily delete a node from between other nodes Node Organization! The node is usually implemented as a struct! Each node contains: - a data field may be organized as a structure, an object, etc. - a pointer that can point to another node data pointer list 3 4

Linked List Organization! A linked list contains 0 or more nodes! The list is a pointer that points to the first node.! Each node points to the next node in the list.! The last node points to (address 0). Empty List! An empty list contains 0 nodes.! The list points to (address 0)! (There are no nodes, it s empty) list list 5 6 Declaring the Linked List data type Declaring the Node data type! We will be defining a class to represent a linked list data type that can store values of type double.! This data type will describe the values (the lists) and operations over those values.! In order to define the values we must: - define a (nested) data type for the nodes - define a pointer variable () that points to the first node in the list. 7! Use a struct for the node type struct ListNode { double value; ListNode *next; ;! (this is just a data type, no variables declared)! next can hold the address of a ListNode. - it can also be - self-referential data structure 8 static int getobjectcount(); static int getobjectcount();

Defining the Linked List member variable! Define a pointer for the of the list: ListNode *;! It must be initialized to to signify the end of the list.! Now we have an empty linked list: Using! Equivalent to address 0! Used to specify end of the list! is defined in the cstddef er: #include <cstddef>! to test a pointer for (these are equivalent): while (p)... <==> while (p!= )... 9 if (!p)... <==> if (p == )... 10 static int getobjectcount(); static int getobjectcount(); 17.2 Linked List operations! Basic operations: - create a new, empty list - append a node to the end of the list - insert a node within the list - delete a node - display the linked list - delete/destroy the list 11! Linked List class declaration #include <cstddef> // for using namespace std; class NumberList { private: struct ListNode // the node data type { double value; // data struct ListNode *next; // ptr to next node ; ListNode *; // the list ; public: NumberList(); ~NumberList(); void appendnode(double); void insertnode(double); void deletenode(double); void displaylist(); NumberList.h // creates an empty list 12 static int getobjectcount();

Operation: Create the empty list! Constructor: sets up empty list Operation: append node to end of list! appendnode: adds new node to end of list! Algorithm: #include "NumberList.h" NumberList::NumberList() { = ; NumberList.cpp Create a new node and store the data in it If the list has no nodes (it s empty) Make point to the new node. Else Find the last node in the list Make the last node point to the new node 13 14 appendnode: find last elem! How to find the last node in the list?! Algorithm: Make a pointer p point to the first element while (the node p points to) is not pointing to make p point to (the node p points to) is pointing to void NumberList::appendNode(double num) { ListNode *newnode; // To point to the new node // Create a new node and store the data in it newnode = new ListNode; newnode->value = num; newnode->next = ; // If empty, make point to new node if (==) = newnode; else { ListNode *p; // To move through the list p = ; // initialize to start of list! In C++: ListNode *p = ; while ((*p).next!= ) p = (*p).next; <==> ListNode *p = ; while (p->next) p = p->next; p=p->next is like i++ 15 // traverse list to find last node while (p->next) //it s not last p = p->next; //make it pt to next // now p pts to last node // make last node point to newnode p->next = newnode; 16 ListNode *p = ;

Driver to demo NumberList Traversing a Linked List! ListDriver.cpp version 1 (no output) #include "NumberList.h" int main() { // Define the list NumberList list; // Append some values to the list list.appendnode(2.5); list.appendnode(7.9); list.appendnode(); return 0; ListDriver.cpp 17! Visit each node in a linked list, to - display contents, sum data, test data, etc.! Basic process: set a pointer to point to what points to while pointer is not process data of current node go to the next node by setting the pointer to the pointer field of the current node end while 18 Operation: display the list Driver to demo NumberList void NumberList::displayList() { ListNode *p; //ptr to traverse the list // start p at the of the list p = ; // while p pts to something (not ), continue while (p) { //Display the value in the current node cout << p->value << endl; //Move to the next node p = p->next; 19! ListDriver.cpp version 2 #include "NumberList.h" int main() { // Define the list NumberList list; // Append some values to the list list.appendnode(2.5); list.appendnode(7.9); list.appendnode(); // Display the values in the list. return 0; ListDriver.cpp Output: 2.5 7.9 20

Operation: destroy a List! The destructor must delete (deallocate) all nodes used in the list! To do this, use list traversal to visit each node! ~NumberList: what s wrong with this definition? destructor! You need to save p->next before deleting p: NumberList::~NumberList() { NumberList::~NumberList() { ListNode *p; ListNode *n; // traversal ptr // saves the next node ListNode *p; // traversal ptr p = ; //start at of list while (p) { delete p; // delete current p = p->next; // advance ptr 21 p = ; //start at of list while (p) { n = p->next; // save the next delete p; // delete current p = n; // advance ptr 22 Operation: delete a node from the list! deletenode: removes node from list, and deletes (deallocates) the removed node.! Requires two extra pointers: - one to point to the node before the node to be deleted. (n) [why?] - one to point to the node to be deleted (p) [why?] n p Deleting a node! Change the pointer of the previous node to point to the node after the one to be deleted. n n->next = p->next; p 5 13 19 5 13 19! Now just delete the p node Deleting 13 from the list 23 24

Deleting a node! After the node is deleted: delete p; n p deletenode code void NumberList::deleteNode(double num) { // empty list, exit if (==) return; ListNode *p; // to traverse the list 5 19 // check if first node is num if (->value == num) { p = ; = p->next; delete p; // else is continued on next slide 25 26 deletenode code cont. Driver to demo NumberList else { ListNode *n; // trailing node pointer // initialize traversal ptr to first node p = ; // skip nodes not equal to num, stop at last while (p && p->value!= num) { n = p; // save it! p = p->next; // advance it // set up the list NumberList list; list.appendnode(2.5); list.appendnode(7.9); list.appendnode(); cout << endl << "remove 7.9:" << endl; list.deletenode(7.9); in ListDriver.cpp Output: 2.5 7.9 remove 7.9: 2.5 // p not null: num is found, set links + delete if (p!= ) { n->next = p->next; delete p; // else: end of list, num not found in list 27 cout << endl << "remove 8.9: " << endl; list.deletenode(8.9); cout << endl << "remove 2.5: " << endl; list.deletenode(2.5); remove 8.9: 2.5 remove 2.5: 28

Operation: insert a node into a linked list! Inserts a new node into the middle of a list.! Requires two extra pointers: - one to point to node before the insertion point [why?] - one to point to the node after the insertion point [why?] n p Inserting a Node into a Linked List! Insertion completed: n->next = newnode; newnode->next = p; 5 13 19 n p 5 13 19 list 17 newnode newnode 17 29 30 Insert Node Algorithm How do you determine the insertion point?! Maintain sorted list: the insertion point is immediately before the first node in the list that has a value greater than the value being inserted. We do this.! Insert by position: InsertNode takes a second argument that is the index of a node. Insert new value before that node.! Use a cursor: The list might have a pointer to a current node, insert new value before the current node. 31 insertnode code void NumberList::insertNode(double num) { ListNode *newnode; // ptr to new node ListNode *p; // ptr to traverse list ListNode *n; // node previous to p //allocate new node newnode = new ListNode; newnode->value = num; // empty list, insert at front if (!) { = newnode; newnode->next = ; //else is on the next slide... 32

insertnode code cont. Driver to demo NumberList else { // initialize the two traversal ptrs p = ; n = ; int main() { in ListDriver.cpp // skip all nodes less than num while (p && p->value < num) { n = p; // save p = p->next; // advance if (n == ) { //insert before first = newnode; newnode->next = p; else { n->next = newnode; newnode->next = p; //insert after n 33 // set up the list NumberList list; list.appendnode(2.5); list.appendnode(7.9); list.appendnode(); list.insertnode(10.5); return 0; Output: 2.5 7.9 10.5 34 Advantages of linked lists over arrays! A linked list can easily grow or shrink in size. - The programmer doesn t need to know how many nodes will be in the list. - Nodes are created in memory as they are needed.! When a node is inserted into or deleted from a linked list, none of the other nodes have to be moved. Advantages of arrays over linked lists! Arrays allow random access to elements: array[i] - linked lists allow only sequential access to elements (must traverse list to get to i th element).! Arrays do not require extra storage for links - linked lists are impractical for lists of characters or booleans (pointer value is bigger than data value). 35 36

! Exercise: find four errors int main() { struct node { int data; node * next; // create empty list node * list; // insert six nodes at front of list node *n; for (int i=0;i<=5;i++) { n = new node; n->data = i; n->next = list; // print list n = list; while (!n) { cout << n->data << " "; n = n->next; cout << endl; return 0; 37