简体   繁体   中英

Printing a linked list in ascending and descending order

I have an assignment that involves using a menu to modify a linked list and be able to print it in ascending and descending order. It is an extension of a previous assignment, in which we had to load a .dat file into the program and it would print it. Our new instructions are to add a new pointer called before, which points up. I'm at a loss for how to print it in descending order. Our professor said something about using a loop, but I'm confused as to how all this would work. The code is a little sloppy right now, as I haven't had a chance to clean it up yet.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct Part
{
  int number;
  float price;
  Part *next;
  Part *before;
};

class Inventory
{
  protected:
    Part *start;
  public:
    Inventory(void);
    void link(Part);
    string getFileName(void);
    bool checkFileExistence(const string& filename);
    void getFile(string filename, ifstream& file);
    void PrintInventory (void);
    void PrintDescending (void);
    void AddPart(void);
    void loadFile(void);
    void DeleteItem(int);
    void DeletePart(void);
};

Inventory inven;

Inventory::Inventory(void)
{
  start = NULL;
}

void Inventory::link(Part item)
{
  Part *p, *last, *here;
  p = new Part;

  p->number = item.number;
  p->price  = item.price;

  if (start == NULL)
  {
    start = p;
    start -> next = NULL;
  }
  else
  {
    here = start;
    if(p->number < here->number)
    {
      p->next = here;
      start = p;
    }
    else
    {
      while(p->number > here->number && here->next != NULL)
      {
        last = here;
        here = here->next;
      }

      if (p->number < here->number)
      {
        last->next = p;
        p->next = here;
      }
      else
      {
        here->next = p;
        p->next = NULL;
      }
    }
  }
}

void Inventory::PrintInventory()
{
    Part *travel;
    travel = start;
    cout.setf(ios::fixed);
    cout.precision(2);

    if (travel != NULL)
    {
        cout << "\nPart #" << setw(13) << "Price" << endl;
    }

    while (travel != NULL)
    {
        cout << setw(5) << travel->number;
        cout << setw(8) << '$' << setw(6) << travel->price << endl;
        travel = travel->next;
    }
    cout << endl;
}

void Inventory::loadFile()
{
    string filename;
    filename = getFileName();
    Part thing;
    cout << endl;

    if (!checkFileExistence(filename))
    {
        cout << "File '" << filename << "' not found." << endl;
        return;
    }

    ifstream infile;
    infile.open(filename.c_str());

    while(!infile.eof())
{
    infile >> thing.number;
    infile >> thing.price;
    inven.link(thing);
}

    cout << "\n Inventory File Loaded. \n\n";

}

void Inventory::PrintDescending()
{

}

int main()
{

char key;
int res;


    do{
        cout << "Menu:" << endl;
        cout << "1) Load Inventory File" << endl;
        cout << "2) Add Item to Inventory" << endl;
        cout << "3) Remove Item from Inventory" << endl;
        cout << "4) Print Inventory in Ascending Order" << endl;
        cout << "5) Print Inventory in Descending Order" << endl;
        cout << "6) Quit" << endl << endl;
        cout << "Option Key: ";
        cin >> key;

        switch (key){
            case '2':
                inven.AddPart();
                res = 1;
                break;
            case '3':
                inven.DeletePart();
                res = 1;
                break;
            case '1':
                inven.loadFile();
                res = 1;
                break;
            case '4':
                inven.PrintInventory();
                res = 1;
                break;
            case '5':
                inven.PrintDescending();
                res = 1;
                break;
            case '6':
                res = 0;
                break;
            default:
                res = 1;
                break;
        }

    }while(res == 1);
}

I left out the functions for adding and deleting items as they aren't necessary for this part. The .dat file we are using contains:

123  19.95
 46   7.63
271  29.99
 17    .85
 65   2.45
 32  49.50
128   8.25

This is a classical data structure algorithm. See Double linked list .

However :

Before trying to print, you need to update your code with the new pointer before . You can also simplify this one, see the comments. So your link function:

  if (start == NULL)
  {
    start = p;
    start -> next = NULL;
    // Here :
    start->before = NULL;
  }
  else
  {
    here = start;
    // You can remove this if...
    if(p->number < here->number)
    {
      p->next = here;
      start = p;
    }
    else
    {
      // ... Because your condition in the next while is enough.
      while(p->number > here->number && here->next != NULL)
      {
        last = here;
        here = here->next;
      }

      if (p->number < here->number)
      {
        // Here : TODO link with the previous one
        last->next = p;
        p->next = here;
      }
      else
      {
        // Here : TODO link with the previous one
        here->next = p;
        p->next = NULL;
      }
    }
  }
}

And then to print, just take your PrintInventory function, but parse using before .

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM