Question SOLVED: For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class

$30.00

Category:

Description

Question SOLVED: For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class

For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions.

Since the list is doubly linked, each node will have the following structure:

In addition, we’ll maintain both a head node and a tail node as variables within the class. Your class, therefore, should have at least two private attributes (data members): (a) a Node variable that serves as the head node; (b) a Node variable that serves as the tail node. (This list can be expanded later as we add functionality to the class.)

Description of the class

At the minimum, your class should have the following public member functions:

  1. A default constructor. This constructor will:
  2. Instantiate the head node and the tail node.
  3. Set the previous node pointer of the head node to nullptr and the nextNode pointer to point to the tail node.
  4. Set the previous node pointer of the tail node to point to the head node and the next node pointer to nullptr.
  5. Node * addNodeToEnd (int value);

This function will add a node to the end of the list, set the node’s value, and return a pointer to the new node added.

  1. Node * addNodeToBeginning (int value);

This function will add a node to the beginning of the list, set the node’s value, and return a pointer to the new node added.

  1. void printListForward ();

This member function will traverse the list from the first node to the last node and print out the values using the formatting requirements we’ve maintained throughout the semester (e.g., 10 numbers per line, 5 byte field, etc).

  1. void printListBackward ();

This member function will traverse the list from the last node to the first node and print out the values using the formatting requirements we’ve maintained throughout the semester (e.g., 10 numbers per line, etc).

  1. void sort ();

This member function will sort the numbers in the list by using one of the sorting algorithms we’ve implemented before. (I suggest using Bubble Sort, as it works with adjacent elements.) Note that it is not necessary to swap actual Nodes; the sort can be performed by simply swapping values between nodes.

  1. A destructor for the list.

This member function will traverse the list and delete each node in turn.

General Tasking

Once the LinkedList class is built, have your main() function perform the following tasks:

  1. Instantiate a LinkedList object.
  2. Read the data from the same file we used in the previous linked list problem, “Perm 50.txt”, into the list.
  3. Print the list in forward order by using the printListForward() member function.
  4. Sort the list by calling the sort() member function.
  5. Print the list in forward order again, but after it has been sorted.
  6. Print the list in reverse order using the printListBackward() member function.———————————————————————————————————————————————————————
    Perm 50:

3

17

22

48

21

10

28

8

12

14

24

30

35

27

29

0

18

4

37

2

15

25

5

39

1

32

7

11

13

16

47

19

26

43

36

33

42

49

31

44

23

46

34

38

41

45

9

20

40

6


error: Content is protected !!