Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

COSC 2336 Homework Assignment 4 Linked List Write a program to read the numbers from the text file numbers.txt into an unordered linked list. Then

COSC 2336 Homework Assignment 4 Linked List

Write a program to read the numbers from the text file numbers.txt into an unordered linked list. Then do the following task:

  1. Display all the numbers.
  2. Delete all the negative numbers in the list.
  3. Display the new list after deletion.
  4. Ask the user to specify an index of the list, and delete the node by index. I.e. If the user types 2, delete the second node of the list. If the user types 3, delete the third node of the list.
  5. Display the new list after deletion.

Bonus:

  1. Ask the user to enter an index and a real number from the keyboard, and insert (not replace) the real number to the list at the position of the index. I.e. If the user types 3 and 99.99, insert 99.99 at the third place of the list, and the original third node should become the fourth. (There is no partial credit for the bonus. You will get either 0 or 1.)
  2. Display the new list after insertion.

Requirements:

  1. You should write a function to delete the negative numbers in the list.
  2. You should write a function to delete the node by index. If the index is less than 1, or if the index is greater than the length of the list, your program should display an error message instead of deleting anything.
  3. (Bonus) You should write a function to insert a node by index. If the index is less than 1, your program should display an error message instead of inserting anything. If the index is greater than the length of the list, insert the node at the tail of the list.
  4. After you finish your program, compress all your files, including both CPP and HPP, into a single ZIP file and upload it to D2L.

Here is an example of how your program should look like:

The list has the following numbers:

1.2 2.5 3 -4.7 5.1 -6.9 7.4 8.6 9.3 -10 -11.2 12 -13.8 -14.2 15.9

The list after deleting negative numbers is:

1.2 2.5 3 5.1 7.4 8.6 9.3 12 15.9

Enter the index of the number you want to delete: (starting from 1)

2

The list after the deletion is:

1.2 3 5.1 7.4 8.6 9.3 12 15.9

Bonus part:

Enter the index of the number you want to insert: (starting from 1)

6

Enter the number you want to insert:

99

The list after the insertion is:

1.2 3 5.1 7.4 8.6 99 9.3 12 15.9

Hints:

  1. You can reuse the code in the examples that I uploaded.
  2. You do not have to implement linked list as a class.
    1. If you choose to implement linked list as a class, you may reuse the code in the folder \"Linked List implemented as class\" on D2L.
    2. If you choose to implement linked list without a class, you may reuse the code in the folder \"Week 6 Linked Lists (Part 1)\".
    3. However, it is recommended that you study the code in both examples.
  3. The code on the textbook does not work for every C++ compiler. In Dev C++, you should putthis->in front of every member variable that unorderedLinkedList has inherited from linkedListType, just like the example I uploaded.
  4. Dont forget to use delete command to free the memory of a node after you delete it from the list.
  5. In the bonus part, dont forget to increase count by 1 after insertion.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions