1 / 55

Visual C++ Programming: Concepts and Projects

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

Télécharger la présentation

Visual C++ Programming: Concepts and Projects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual C++ Programming: Concepts and Projects Chapter 12B: Linked List (Tutorial)

  2. 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++

  3. Problem Description Programming with Visual C++

  4. Problem Description (continued) Programming with Visual C++

  5. Problem Description (continued) Programming with Visual C++

  6. Problem Description (continued) Programming with Visual C++

  7. Problem Description (continued) Programming with Visual C++

  8. Problem Description (continued) Programming with Visual C++

  9. Problem Description (continued) Programming with Visual C++

  10. Problem Description (continued) Programming with Visual C++

  11. Problem Description (continued) Programming with Visual C++

  12. 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++

  13. Design (continued) Programming with Visual C++

  14. 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++

  15. Design (continued) Programming with Visual C++

  16. 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++

  17. Design (continued) • Table of constants Programming with Visual C++

  18. Design (continued) • Table of Drawing objects Programming with Visual C++

  19. Design (continued) • Creating the first Node in a linked list Programming with Visual C++

  20. 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++

  21. Design (continued) Programming with Visual C++

  22. Design (continued) Programming with Visual C++

  23. Design (continued) Programming with Visual C++

  24. Design (continued) Programming with Visual C++

  25. 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++

  26. Design (continued) Programming with Visual C++

  27. Design (continued) Programming with Visual C++

  28. Design (continued) Programming with Visual C++

  29. 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++

  30. Design (continued) Programming with Visual C++

  31. 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++

  32. Development (continued) Programming with Visual C++

  33. Development (continued) Programming with Visual C++

  34. Development (continued) Programming with Visual C++

  35. 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++

  36. Development (continued) Programming with Visual C++

  37. Development (continued) Programming with Visual C++

  38. Development (continued) • Enter the code for the Node class in Node.h Programming with Visual C++

  39. Development (continued) • Write the default constructor code in the implementation file (Node.cpp) • Remember to include Node.h Programming with Visual C++

  40. Development (continued) • Client code (Form1.h) • Include the Node class definition file (Node.h) Programming with Visual C++

  41. Development (continued) • Client code (Form1.h) • Constants, head pointer, and Drawing objects Programming with Visual C++

  42. Development (continued) • Client code (Form1.h) • Code Form1->Load() • Instantiate Drawing objects Programming with Visual C++

  43. Development (continued) • Client code (Form1.h) • Code the reset()method • Properly enable all buttons • Reset class variable Node::nodeCountto 0 Programming with Visual C++

  44. Coding • Write code for btnHead_Click() • Create new head pointer • Draw the linked list • Properly reset the buttons Programming with Visual C++

  45. 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++

  46. Coding (continued) Programming with Visual C++

  47. 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++

  48. Coding (continued) Programming with Visual C++

  49. Coding (continued) Programming with Visual C++

  50. 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++

More Related