COMP 2710 Software Construction Linked List Exercises Part

  • Slides: 19
Download presentation
COMP 2710 Software Construction Linked List – Exercises: Part 1 Dr. Xiao Qin Auburn

COMP 2710 Software Construction Linked List – Exercises: Part 1 Dr. Xiao Qin Auburn University http: //www. eng. auburn. edu/~xqin@auburn. edu

Review: Structure Types • Define struct globally (typically) • No memory is allocated –

Review: Structure Types • Define struct globally (typically) • No memory is allocated – Just a "placeholder" for what our struct will "look like" • Definition: struct CDAccount. V 1 Name of new struct "type" { double balance; member names double interest. Rate; int term; }; 6 -2

Review: Define Pointer Types • Pointers are "typed" – double *p; • Can "name"

Review: Define Pointer Types • Pointers are "typed" – double *p; • Can "name" pointer types • typedef int* Int. Ptr; – Defines a "new type" alias – Consider these declarations: Int. Ptr p; int *p; • The two are equivalent 10 -3

Review: The new Operator p 1 = new int; • Creates new "nameless" variable,

Review: The new Operator p 1 = new int; • Creates new "nameless" variable, and assigns p 1 to "point to" it • Can access with *p 1 – Use just like ordinary variable 10 -4

Avoid Dangling Pointers delete p; • Destroys dynamic memory • But p still points

Avoid Dangling Pointers delete p; • Destroys dynamic memory • But p still points there: Called "dangling pointer" • If p is then dereferenced ( *p ) – Unpredicatable results! – Often disastrous! • Avoid dangling pointers: Assign pointer to NULL after delete: delete p; p = NULL; 5

Review: Nodes and Pointers struct list_node_t { string item; int count; List. Node *link;

Review: Nodes and Pointers struct list_node_t { string item; int count; List. Node *link; }; typedef list_node_t* list_node_ptr_t; 17 -6

Exercise 1. Can you design a head node? struct list_head_t { string listname; int

Exercise 1. Can you design a head node? struct list_head_t { string listname; int max; int min; list_node_ptr_t head; }; • "head" is not a node: struct list_node_t List. Node. Ptr head; { string item; pointer to a node – A simple int –count; Set to point to 1 st node in list_node_t *link; • Head used to "maintain" start of list }; • Also used as argument to functions typedef list_node_t* list_node_ptr_t; How to maintain metadata of the list? (What is metadata? ) 7

Exercise 2: Print a list of nodes struct node_t { int data; node_t *next;

Exercise 2: Print a list of nodes struct node_t { int data; node_t *next; }; typedef node_t* node_ptr_t; void print_list(node_ptr_t root); Hint: You should consider two cases

Answer: Print a list of nodes void print_list(node_ptr_t root) { node_ptr_t cur; if (root

Answer: Print a list of nodes void print_list(node_ptr_t root) { node_ptr_t cur; if (root == NULL) /* case 1 */ cout << "This is an empty listn"; cur = root; /* case 2 */ while (cur != NULL) { cout << cur->data << endl; cur = cur->next; } }

Exercise 3. Insert a node to the head of the list Tip: Apply this

Exercise 3. Insert a node to the head of the list Tip: Apply this solution to homework 5. struct node { int data; node *next; }; typedef node* node_ptr_t; What is the difference between these two prototypes? void insert_node(node_ptr_t& root, int info); //Another possible prototype void insert_node(node_ptr_t& root, node_ptr_t new_node_ptr); How many cases should we consider? What are these cases?

Exercise 3 - Insert a node to the head of the list struct node

Exercise 3 - Insert a node to the head of the list struct node { int data; node *next; }; typedef node* node. Ptr; void insert. Node(node. Ptr& root, int info); root 6 5 1 cur_ptr info 2 3 4 1 -11

Exercise 3: Insert a node to the head of the list void insert_node(node_ptr_t& root,

Exercise 3: Insert a node to the head of the list void insert_node(node_ptr_t& root, int info) { node_ptr_t cur_prt; cur_ptr = new node; //Do not use this: new node. Ptr assert(cur_ptr != NULL); //Ensure that memory is allocatd cur_ptr->data = info; cur_ptr->next = NULL; if (root == NULL) //For empty list, cur_ptr becomes the root = cur_prt; else { //Insert cur as the root of the list cur_ptr->next = root; root = cur_ptr; } }

Exercise 4. Insert a node to the end of the list struct node_t {

Exercise 4. Insert a node to the end of the list struct node_t { int data; node_t *next; }; typedef node_t* node_ptr_t; void append_node(node_ptr_t& root, int info); How many cases should we consider? What are these cases? 1 -13

Exercise 4 - Insert a node to the end of the list struct node_t

Exercise 4 - Insert a node to the end of the list struct node_t { int data; node_t *next; }; typedef node_t* node_ptr_t; void append_node(node_ptr_t& root, int info); root 6. 1 5 cur_ptr 1 6. 2 new_ptr info 2 3 4 14

void append. Node(node_ptr_t& root, int info) { node_ptr_t new_ptr; node_ptr_t cur_ptr; new_ptr = new

void append. Node(node_ptr_t& root, int info) { node_ptr_t new_ptr; node_ptr_t cur_ptr; new_ptr = new node; //Do not use this: new node. Ptr assert(new_Ptr != NULL); //Ensure that memory is allocated new_ptr->data = info; new_ptr->next = NULL; if (root == NULL) //For empty list, new_ptr becomes the root = new_ptr; else { //Append the new node at the end of the list cur_ptr = root; while (cur_ptr->next != NULL) cur_ptr = cur_ptr->next; cur_ptr->next = new_ptr; } }

Exercise 5. Delete the head node of the list struct node_t { int data;

Exercise 5. Delete the head node of the list struct node_t { int data; node_t *next; }; typedef node_t* node_ptr_t; void delete_head(node_ptr_t& root); How many cases should we consider? What are these cases? 1 -16

Exercise 5 - Delete the head node of the list struct node { int

Exercise 5 - Delete the head node of the list struct node { int data; node *next; }; typedef node* node. Ptr; void delete. Head(node. Ptr& root); root 2 3 1 cur. Ptr 17

Exercise 5. Delete the head node of the list void delete_head(node_ptr_t& root) { node_ptr_t

Exercise 5. Delete the head node of the list void delete_head(node_ptr_t& root) { node_ptr_t cur_ptr; if (root != NULL) { cur_ptr = root; root = root->next; /* Deleted node must be returned to OS */ delete cur_ptr; } else cout << "This is an empty list. No deletion!n"; }

Summary • Node is struct or class object • Insert a node to the

Summary • Node is struct or class object • Insert a node to the head of the list • Insert a node to the end of the list • Delete the head node of the list