560 likes | 627 Views
Visual C++ Programming: Concepts and Projects. Chapter 12B: Linked List (Tutorial). Tutorial: Linked List. Problem description This program helps the user visualize a linked list by drawing one on the interface Buttons are provided to: Create the initial list
E N D
Visual C++ Programming: Concepts and Projects Chapter 12B: Linked List (Tutorial)
Tutorial: Linked List • Problem description • This program helps the user visualize a linked list by drawing one on the interface • Buttons are provided to: • Create the initial list • Add a Node to the front of the list • Delete a Node from the front of the list • Remove all Nodes (delete the list) Programming with Visual C++
Problem Description Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Problem Description (continued) Programming with Visual C++
Design • The Node class definition • Start with a UML class diagram • All data members and methods will have public access (+) • Class variable: nodeCount • Instance variables: nodeData, next • Default constructor: Node() Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) • Interface sketch • Equal distance between Node rectangles • Arrows drawn to link Nodes • Starting coordinates of the first Node are crucial • The positions of all other Nodes are calculated from them Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) • The head pointer • The head pointer always points to the first Node in the list • All insertions and deletions involve the head pointer Programming with Visual C++
Design (continued) • Table of constants Programming with Visual C++
Design (continued) • Table of Drawing objects Programming with Visual C++
Design (continued) • Creating the first Node in a linked list Programming with Visual C++
Design (continued) • Inserting a new Node • Create the new Node • Point the new Nodes next pointer at the first Node in the list • Assign the head pointer to point to the new Node • This always inserts the new Node at the head of the linked list Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) • Deleting a Node • Create a temporary Node pointer (temp) • Point temp at the first Node in the linked list • Assign the head pointer to point to the second Node • Delete the Node pointed to by temp • This always deletes the Node at the head of the linked list Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) Programming with Visual C++
Design (continued) • Use a loop to delete each node in the list • Delete from the front • Reassign head to point to the next Node • Delete Nodes until there are no more Programming with Visual C++
Design (continued) Programming with Visual C++
Development • Interface • Only four buttons required • Coding • Create Node.h (Node header file) • Create Node.cpp (implementation file) • Finish coding Form1.h (client code) Programming with Visual C++
Development (continued) Programming with Visual C++
Development (continued) Programming with Visual C++
Development (continued) Programming with Visual C++
Development (continued) • The Node class contains a default constructor • Change this to a prototype • Put the default constructor code in the implementation file (Node.cpp) Programming with Visual C++
Development (continued) Programming with Visual C++
Development (continued) Programming with Visual C++
Development (continued) • Enter the code for the Node class in Node.h Programming with Visual C++
Development (continued) • Write the default constructor code in the implementation file (Node.cpp) • Remember to include Node.h Programming with Visual C++
Development (continued) • Client code (Form1.h) • Include the Node class definition file (Node.h) Programming with Visual C++
Development (continued) • Client code (Form1.h) • Constants, head pointer, and Drawing objects Programming with Visual C++
Development (continued) • Client code (Form1.h) • Code Form1->Load() • Instantiate Drawing objects Programming with Visual C++
Development (continued) • Client code (Form1.h) • Code the reset()method • Properly enable all buttons • Reset class variable Node::nodeCountto 0 Programming with Visual C++
Coding • Write code for btnHead_Click() • Create new head pointer • Draw the linked list • Properly reset the buttons Programming with Visual C++
Coding (continued) • Write the code for btnInsert_Click() • No more than seven Nodes allowed • Implement three-step insertion algorithm • Draw the linked list Programming with Visual C++
Coding (continued) Programming with Visual C++
Coding (continued) • Code drawNode() • drawNode()is recursive • level is used to determine when to stop drawing Nodes • The location of each Node rectangle is calculated by multiplying nodeDistance and level Programming with Visual C++
Coding (continued) Programming with Visual C++
Coding (continued) Programming with Visual C++
Coding (continued) • Draw the current Node • If there is another Node linked to it (next is not the nullptr), then draw that Node • This is a recursive process • The base case occurs when next is the nullptr Programming with Visual C++