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

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

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

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)

2Programming with Visual C++

Problem Description

3Programming with Visual C++

Problem Description (continued)

4Programming with Visual C++

Problem Description (continued)

5Programming with Visual C++

Problem Description (continued)

6Programming with Visual C++

Problem Description (continued)

7Programming with Visual C++

Problem Description (continued)

8Programming with Visual C++

Problem Description (continued)

9Programming with Visual C++

Problem Description (continued)

10Programming with Visual C++

Problem Description (continued)

11Programming 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()

12Programming with Visual C++

Design (continued)

13Programming 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

14Programming with Visual C++

Design (continued)

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

16Programming with Visual C++

Design (continued)

• Table of constants

17Programming with Visual C++

Design (continued)

• Table of Drawing objects

18Programming with Visual C++

Design (continued)

• Creating the first Node in a linked list

19Programming 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

20Programming with Visual C++

Design (continued)

21Programming with Visual C++

Design (continued)

22Programming with Visual C++

Design (continued)

23Programming with Visual C++

Design (continued)

24Programming 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

25Programming with Visual C++

Design (continued)

26Programming with Visual C++

Design (continued)

27Programming with Visual C++

Design (continued)

28Programming 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

29Programming with Visual C++

Design (continued)

30Programming 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)

31Programming with Visual C++

Development (continued)

32Programming with Visual C++

Development (continued)

33Programming with Visual C++

Development (continued)

34Programming 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)

35Programming with Visual C++

Development (continued)

36Programming with Visual C++

Development (continued)

37Programming with Visual C++

Development (continued)

• Enter the code for the Node class in Node.h

38Programming with Visual C++

Development (continued)

• Write the default constructor code in the implementation file (Node.cpp)

• Remember to include Node.h

39Programming with Visual C++

Development (continued)

• Client code (Form1.h)– Include the Node class definition file (Node.h)

40Programming with Visual C++

Development (continued)

• Client code (Form1.h)– Constants, head pointer, and Drawing objects

41Programming with Visual C++

Development (continued)

• Client code (Form1.h)– Code Form1->Load()

• Instantiate Drawing objects

42Programming with Visual C++

Development (continued)

• Client code (Form1.h)– Code the reset() method

• Properly enable all buttons• Reset class variable Node::nodeCount to 0

43Programming with Visual C++

Coding

• Write code for btnHead_Click()– Create new head pointer– Draw the linked list– Properly reset the buttons

44Programming 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

45Programming with Visual C++

Coding (continued)

46Programming 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

47Programming with Visual C++

Coding (continued)

48Programming with Visual C++

Coding (continued)

49Programming 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

50Programming with Visual C++

Coding (continued)

51Programming with Visual C++

Coding (continued)

52Programming with Visual C++

Testing

• Check each button– Create a list– Insert nodes (make sure the nodeData is correct)– Try to insert more than seven (not allowed)

53Programming with Visual C++

On Your Own

• Task 1. Coding btnDelete_Click()• Task 2. Coding btnClear_Click() and deleteList()

• Task 3. Final Testing to make sure all buttons work correctly

Programming with Visual C++ 54

On Your Own (continued)

55Programming with Visual C++