QuestionQuestion

You are going to implement a program that creates an unsorted list by using a linked list.

The “data.txt” file has three lines of data
100, 110, 120, 130, 140, 150, 160
100, 130, 160
1@0, 2@3, 3@END

You need to
1. create an empty unsorted list
2. add the numbers from the first line to list using append() function. Then print all the current keys to command line in one line using printAll().
3. delete the numbers given by the second line in the list by using delete() function.
Then print all the current keys to command line in one line using printAll()..
4. Add() the numbers in the third line to the corresponding location in the list. For example,1@0 means adding number 1 at position 0 of the list. Then print all the current keys to command line in one line using printAll()..You must implement the following functions by yourself and use them
1. append(parameter one is the object to be added): append the object at the end of the list.
2. add(parameter one is the object to be added, parameter two is the position starting from 0)
3. delete(parameter is the key)
4. get(parameter is index value which starts from 0) returns the item object reference
5. printAll(parameter is the beginning of the list), print all keys in order in the list. Must use get() function to access data

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;

struct Node {
    int val;
    Node * next;
};

class List {
private:
    Node * head;
    Node * tail;
    int size;
public:

    List() {
       head = 0;
       tail = 0;
       size = 0;
    };

    ~List() {
       Node * n = head;
       Node *t;
       while (n != 0) {
            t = n;
            n = n->next;
            delete t;
       }
    }
   
    /**
    * append(parameter one is the object to be added):
    * append the object at the end of the list
    * @param val
    */
    void append(int val) {
       Node * n = new Node;
       n->next = 0;
       n->val = val;
       if (head == 0) { // basic case
            head = n;
            tail = n;
       } else { // normal case
            tail->next = n;
            tail = n;
       }
       size++;
    }

    /**
    * add(parameter one is the object to be added,
    * parameter two is the position starting from 0)
    * @param val
    * @param position
    */
    void add(int val, int position) {
       if (position < 0) {
            return;
       }
       if (head == 0) { // basic case
            append(val);
       } else { // normal case
            int index = 0;
            Node * curr = head;
            Node * pre;
            Node * n = new Node;
            n->next = 0;
            n->val = val;
            if (position == 0) { // add to head
                n->next = head;...

By purchasing this solution you'll be able to access the following files:
Solution.cpp and SolutionData.txt.

$30.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats