Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "What’s New? Six homework programming assignments. New: five"— Presentation transcript:

1 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

2 COMP 2710 Software Construction Linked List – Exercises: Part 4
Dr. Xiao Qin Auburn University See also Lec05a.ppt in Fall’09

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

4 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

5 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?

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

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

8 Demo: link_list.cpp

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

10 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!

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

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

13 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

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

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

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

17

18

19 References and Backup Slides

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

21 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

22 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

23 Exercise 5

24 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

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


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

Similar presentations


Ads by Google