简体   繁体   中英

singly linked chain printing c++

I am trying to pick my chain in the format {1,2,3,4,etc}. You can find the header file below which will have the layout of the nodes. I am just confused on how I should go about cycling through my list to print out Item.

Any guidance would be greatly appreciated!

set.h

using namespace std;

#include <iostream>

class Set
{
  private:

    struct Node
    {
      int Item;      // User data item
      Node * Succ;   // Link to the node's successor
    };

    unsigned Num;    // Current count of items in the set
    Node * Head;     // Link to the head of the chain

  public:

    // Return information about the set
    //
    bool is_empty() const { return Num == 0; }
    unsigned size() const { return Num; }

    // Initialize the set to empty
    //
    Set();

    // Insert a specified item into the set, if possible
    //
    bool insert( int );

    // Display the set
    //
    void display( ostream& ) const;

};

Here are two recommendations: 1) Sort the list first, then print all nodes; 2) Create another list (indices) to the data and sort those links (don't need data in those nodes).

Sorting List First

An often used technique is to order the nodes in the order you want them printed. This should involve changing the link fields.
Next, start at the head node and print each node in the list (or the data of each node in the list).

Using an Index list

Create another linked list without the data fields. The links in this list point to the data fields in the original list. Order the new list in the order you want the nodes printed.
This technique preserves the order of creation of the first list and allows different ordering schemes.

Changing Links

Since you're writing your own Linked List, the changing of the links is left as an exercise as I'm not getting paid to write your code. There are many examples on SO as well as the web for sorting and traversing linked lists.

You just want to do something like this:

void Set::display(ostream &out) const {
    for(int i=0; i<Num; i++) {
        out << Pool[i] << " ";
    }
    out << endl;
}

An ostream behaves as cout would.

It's hard to get your question. If you want to print the array to screen you should consider writing a display() like:

#include <iostream>
#include <iterator>
void Set::display() const {
   ostream_iterator<int> out_it (cout," ");
   copy(Pool,Pool+Num,out_it);   
   cout << endl;
}

or if you want to write to a ostream& (as it is pointed out in the answer by @alestanis)

#include <iostream>
#include <iterator>
void Set::display(ostream &out) const {
   ostream_iterator<int> out_it (out," ");
   copy(Pool,Pool+Num,out_it);   
   out << endl;
}

Without testing, I'd do something like this. (Assumes the last node has Succ set to NULL , as I would recommend it does.)

void LoopList(struct Node *head)
{
    for (struct Node *p = head; p != null; p = p->Succ)
    {
        // Do whatever with this node
        Print(p);
    }
}

I think I was over thinking it. Anyway here is what I ended up doing. Now I just need to add some formatting for the commas and im all set.

Node * Temp;
Temp = new (nothrow) Node;
Temp = Head;
out << "{";
while(Temp->Succ)
{
      out << Temp->Item;
      Temp = Temp->Succ;
}
out << '}' << endl;

Suppose your list is cyclical, you can use this:

struct Node *n = begin;

if (n != NULL) {
    //do something on it
    ...
    for (n = begin->Succ; n != begin; n = n->Succ) {
    }
}

or

struct Node *n = begin;
if (n != NULL) {        
    do {        
        //do something
        ...
        n = n->Succ;
    } while (n != begin)
}

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