What’s New? Six homework programming assignments. New: five

Slides:



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

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
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.
Chapter 17 Linked Data Structures. Learning Objectives Nodes and Linked Lists – Creating, searching Linked List Applications – Stacks, queues, sets, hash.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
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.
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.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
CSCE 210 Data Structures and Algorithms
Structures and Classes
Programming Circular Linked List.
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Exercise 1 From Lec80b-Ponter.ppt.
UNIT-3 LINKED LIST.
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
COMP 2710 Software Construction Pointers
COMP 2710 Software Construction File I/O
Introduction to Data Structures
Chapter 4 Linked Lists
Sequences 9/18/ :20 PM C201: Linked List.
Linked Data Structures
The Bag and Sequence Classes with Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Learning Objectives Structures Structure types
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Pointers and Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
List Iterator Implementation
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

What’s New? Six homework programming assignments. New: five Homework 5: 1 week. New: 2 weeks. Midterm 1. New: No review lecture and no feedback lecture Linked List List Exercise 4. C vs. C++ Linked List Today’s lecture Next Monday and Friday: Use Cases, Overloading, Dynamic Array Next Wednesday: Project 1 – Sample Use Case and DFD

COMP 2710 Software Construction Linked List – Exercises: Part 4 Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu See also Lec05a.ppt in Fall’09

Review: Linked List struct node { int data; node *next; }; typedef node* nodePtr; void insertNode(nodePtr& root, int info); void appendNode(nodePtr& root, int info); void deleteHead(nodePtr& root); void deleteTail(nodePtr& root); void deleteNode(nodePtr& root, int info); void insertNode(nodePtr& root, int info) { nodePtr cur; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur->data = info; cur->next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Insert cur as the root of the list cur->next = root; }

Exercise 8: Insert a node: Maintain a sorted list (non-decreasing) struct node { int data; node *next; }; typedef node* nodePtr; void insert_node_sorted_List(nodePtr& root, int info); How many cases should we consider? What are these cases? Implement the function Write a test driver

Exercise 8 - Insert a node into a sorted list void deleteNode(nodePtr& root, int info); new_ptr 1 24 3 root 5 16 32 47 cur_ptr 2 NULL void appendNode(nodePtr& root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur->data = info; cur->next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre->next != NULL) pre = pre->next; pre->next = cur; } pre_ptr What are the conditions to keep traversing the list?

Exercise 8: Insert a node 5 Minutes struct node { int data; node *next; }; typedef node* nodePtr; void insert_node_sorted_List(nodePtr& root, int info);

void insert_node_sorted_list(nodePtr& root, int info) { nodePtr cur_ptr, pre_ptr, new_ptr new_ptr = new node; //Do not use this: new nodePtr assert(new_ptr != NULL); //Ensure that memory is allocatd new_ptr->data = info; new_ptr->next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = new_ptr; else { //Insert the new node into a sorted list pre_ptr = NULL; cur_ptr = root; while ((cur_ptr != NULL) && (cur_ptr->data < info)) { pre_ptr = cur_ptr; cur_ptr = cur_ptr->next; } if (pre_ptr == NULL) { //insert new node to the head of the list new_ptr->next = root; else { //insert new node in the list, non-head node new_ptr->next = cur_ptr; pre_ptr->next = new_ptr;

Demo: link_list.cpp

Linked List Class Definition class IntNode { public: IntNode() { } IntNode(int theData, IntNode* theLink) : data(theData), link(theLink) { } IntNode* getLink() const {return link;} int getData() const {return data;} void setData(int theData) {data = theData;} void setLink(IntNode* pointer) {link=pointer;} private: int data; IntNode *link; }; typedef IntNode* IntNodePtr; Notice all member function definitions are inline Small and simple enough Notice two-parameter constructor Allows creation of nodes with specific data value and specified link member Example: IntNodePtr p2 = new IntNode(42, p1);

Create 1st Node IntNodePtr head; head = new IntNode; Declares pointer variable head head = new IntNode; Dynamically allocates new node Our 1st node in list, so assigned to head head->setData(3); head->setLink(NULL); Sets head node data Link set to NULL since it’s the only node!

void headInsert(IntNodePtr& head, int theData) { head = new IntNode(theData, head); }

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Lost Nodes Pitfall: Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Searching a Linked List Function with two arguments: IntNodePtr search(IntNodePtr head, int target); //Precondition: pointer head points to head of //linked list. Pointer in last node is NULL. //If list is empty, head is NULL //Returns pointer to 1st node containing target //If not found, returns NULL Simple "traversal" of list Similar to array traversal

IntNodePtr search(IntNodePtr head, int target) { IntNodePtr here = head; if (here == NULL) //if empty list return NULL; } else while (here->getData( ) != target && here->getLink( ) != NULL) here = here->getLink( ); if (here->getData( ) == target) return here;

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Doubly Linked Lists class DoublyLinkedIntNode { public: DoublyLinkedIntNode ( ){} DoublyLinkedIntNode (int theData, DoublyLinkedIntNode* previous, DoublyLinkedIntNode* next) : data(theData), nextLink(next), previousLink(previous) {} DoublyLinkedIntNode* getNextLink( ) const { return nextLink; } DoublyLinkedIntNode* getPreviousLink( ) const { return previousLink; } int getData( ) const { return data; } void setData(int theData) { data = theData; } void setNextLink(DoublyLinkedIntNode* pointer) { nextLink = pointer; } void setPreviousLink(DoublyLinkedIntNode* pointer) { previousLink = pointer; } private: int data; DoublyLinkedIntNode *nextLink; DoublyLinkedIntNode *previousLink; }; typedef DoublyLinkedIntNode* DoublyLinkedIntNodePtr; Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

A Stack—Graphic: Display 17.12 A Stack Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

References and Backup Slides

Hierarchical Structures A member of a structure is a smaller structure struct Date { int month; int day; int year; }; //Improved structure for a bank certificate of deposit: struct CDAccount double initialBalance; double interestRate; int term;//months until maturity Date maturity; //date when CD matures double balanceAtMaturity;

Access Hierarchical Structures Two doc operators cout << "When the CD matured on " << account.maturity.month << "-" << account.maturity.day << "-" << account.maturity.year << endl See accountv2.cpp

Initializing Structures Can initialize at declaration Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; Declaration provides initial data to all three member variables Define Declare <- Initialize Use Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 6-22 22

Exercise 5

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Pointer Assignments Pointer variables can be "assigned": int *p1, *p2; p2 = p1; Assigns one pointer to another "Make p2 point to where p1 points" Do not confuse with: *p1 = *p2; Assigns "value pointed to" by p1, to "value pointed to" by p2 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 10-24 24

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables Try this! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 10-25 25