0
if(Box->head == NULL)
{
Box ->head = Box -> tail = Box;
cout<<Box->data
}
else
{
Box->tail -> next = Box;
Box -> tail = Box;
Box-> data = i;
cout<<Box->data;
}

actually this code doesn't point to the same code imo.

when I put cout<<Box->Data in there.

after I say how many boxes.

I would see
say i enter 5

5

1
2
3
4
5

that's what I'll see

0

your test is wrong. Put this after the end of the code you posted.

node* node = Box;
while( node )
{
   cout << node->data << "\n";
   node = node->next;
}
0
Box->tail -> next = Box;
Box -> tail = Box;

If you fail to see that the code above is just making all nodes point to the same Box node then you way over your head.

0

your test is wrong. Put this after the end of the code you posted.

node* node = Box;
while( node )
{
   cout << node->data << "\n";
   node = node->next;
}

that while loop will just keep spamming a number.

0
Box->tail -> next = Box;
Box -> tail = Box;

If you fail to see that the code above is just making all nodes point to the same Box node then you ware way over your head.

Ave, Ancient Dragon The Patient!
;)
Over 30 posts to point to i-th list node?..
This is too much even for DaniWeb forums.
It seems that's time for textbooks reading...

0

The output you are seeing just means that each time you go through the loop the current node has the value you put in. It doesn't mean the nodes in the list have the values you think, or that the nodes point to where you think. In order to see what is in the nodes of a list you need to write code to run the list and output the value of each node as you go. This should be done after you have completed creating the whole list, not after you have created a single node.

It seems to me that you are confusing a node with a list. The following snippet was taken from post #26.

struct Node {
int data;
Node*head;
Node*tail;
Node*next;
};

In my experience head and tail are names usually associated with nodes in a list, not with pointers in a node. True, you can write whatever you want, but this just doesn't pass the sniff test on first pass. I recommend you take head and node out of the declaration of Node and declare them in main(). That way head can be the first node in the list and tail can be the last.

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

int main()
{
    Node * head = NULL;
    Node * tail = NULL;
    int boxes = 0;

    //create list with 3 Nodes
    while(boxes < 3)
    {
       Node * box = new Node;
       box->data = boxes;
       box->next = NULL;

       //add each new Node to end of list
       if(boxes == 0)
       {
          head = box;
          tail = head;
        }
        else
        {
           tail->next = box;
           tail = tail->next;
        }
        ++boxes;
     }
      
      //display contents of the list
      Node * current = head;
      while(current != NULL)
      {
          cout << current->data << endl;
          current = current->next;
       }
    cin.get();
    return 0;
}
0

BlackStar is actually trying to create a very complicated linked list of linked lists (from another thread). These are lists within lists, and is the reason for the head and tail nodes within the Node structure. But unfortunately BlackStar has all that very confused.

Restaurant linked list
    cash register linked list
       customer linked list
          items_ordered linked list
0

The output you are seeing just means that each time you go through the loop the current node has the value you put in. It doesn't mean the nodes in the list have the values you think, or that the nodes point to where you think. In order to see what is in the nodes of a list you need to write code to run the list and output the value of each node as you go. This should be done after you have completed creating the whole list, not after you have created a single node.

It seems to me that you are confusing a node with a list. The following snippet was taken from post #26.

struct Node {
int data;
Node*head;
Node*tail;
Node*next;
};

In my experience head and tail are names usually associated with nodes in a list, not with pointers in a node. True, you can write whatever you want, but this just doesn't pass the sniff test on first pass. I recommend you take head and node out of the declaration of Node and declare them in main(). That way head can be the first node in the list and tail can be the last.

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

int main()
{
    Node * head = NULL;
    Node * tail = NULL;
    int boxes = 0;

    //create list with 3 Nodes
    while(boxes < 3)
    {
       Node * box = new Node;
       box->data = boxes;
       box->next = NULL;

       //add each new Node to end of list
       if(boxes == 0)
       {
          head = box;
          tail = head;
        }
        else
        {
           tail->next = box;
           tail = tail->next;
        }
        ++boxes;
     }
      
      //display contents of the list
      Node * current = head;
      while(current != NULL)
      {
          cout << current->data << endl;
          current = current->next;
       }
    cin.get();
    return 0;
}

Is there a way to access the node box outside of the loop?
Because it seems that when I try to add on to your code.
trying to choose one of the numbers. It says box is undeclared.

0

why is it that when I take out

Node* box = new Node;
outside of the loop.

and compile it, it becomes a spamming number?

0

box is local the while loop it is declared in. Any attempt to access it as such is fruitless. Each time through the loop the data in box is placed into the list as a new, and nameless node. If access to a given node, say the second node in the list is needed, then a loop is used to find it. A loop can also be used to find a node with a given value as well.

struct Node 
{
   int data;
   char ch;
   Node*next;
};

int main()
{
    Node * head = NULL;
    Node * tail = NULL;
    int boxes = 0;
    char ch = 'c';

    //create list with 3 Nodes
    while(boxes < 3)
    {
       //declare a new Node and initialize data members
       Node * box = new Node;
       box->data = boxes;
       box->ch = c++;
       box->next = NULL;

       //add each new Node to end of list
       if(boxes == 0)
       {
          head = box;
          tail = head;
        }
        else
        {
           tail->next = box;
           tail = tail->next;
        }
        ++boxes;
     }
      
      //display contents of the list
      Node * current = head;
      while(current != NULL)
      {
          cout << current->data << ' ' << current->ch << endl;
          current = current->next;
       }
   
    //display second node in the list
    int i = 0;
    current = head;
    while(i < 2)
    {
       current = current->next;
       ++i;
     }
     cout << current->data << ' ' << current->ch << endl;

    //what node in the list has the value 'e' in member ch?
    current = head;
    int i = 1;
    while(current != NULL && current->ch != 'e')
    {
       current = current->next;
       ++i;
    }

     if(current == NULL)
       cout << "e is not in the list as a value of ch" << endl;
     else
       cout << "node #" << i << " has the value of e" << endl;

    cin.get();
    return 0;
}

NOTE: the above code is untested----I don't have a compiler at work. There may be mistakes, but the concept is sound.

0

Are you C or C++ (see this forum title) programmer?
Make your job easer:

#include <vector>

class Item {
public:
  ...
};

class Customer {
public:
  void addItem(const Item& item) {
    items.push_back(item);
  }
  void removeItem(size_t i) {
    vector<Item>::iterator it;
    for (it = items.begin(); i-->0; it++)
      ;
    items.erase(it);
  }
  size_t size() const { return items.size(); }
  Item& operator[](size_t i) {
    return items[i];
  }
  ...
private: // (or public, it does not matter in your case;)
  std::vector<Item> items;
};

class CRM {
public: // see class Customer interface
  CRM();
   ...
  std::vector<Customer> orders;
};

class Restaurant {
public:
  void addCash(const CRM& crm);
  void addCash(size_t n = 1) {
    CRM crm;
    while (n-->0)
      cashes.push_back(crm);
  }
  ...
  std::vector<CRM> cashes;
};

What for that nightmare with four home-made lists functional interfaces? It seems you never finish this project in C ;)...

0

never used the standard libary lists before.
Besides, having trouble writing these codes.
learning more IMO

0

Well, you have never used C++ year (or month:)) ago ;)
http://www.cprogramming.com/tutorial/stl/stllist.html
(and lots of other good links, search Google)...

Have you a chance to solve the problem without ready-to-use list implementation? No, of course (you can't understand the simplest getIthNode function, for example). Don't waste a time, don't invent square wheels ;)...

0
struct Node 
{
   int data;
   Node*next;
};

int main()
{
    Node * head = NULL;
    Node * tail = NULL;
    int boxes = 0;

    //create list with 3 Nodes
    while(boxes < 3)
    {
       Node * box = new Node;
       box->data = boxes;
       box->next = NULL;

       //add each new Node to end of list
       if(boxes == 0)
       {
          head = box;
          tail = head;
        }
        else
        {
           tail->next = box;
           tail = tail->next;
        }
        ++boxes;
     }
      
      //display contents of the list
      Node * current = head;
      while(current != NULL)
      {
          cout << current->data << endl;
          current = current->next;
       }

// code above creates numbered nodes in a linked list
int n;
cout<<"which box do you want to be in?"<<endl;
cin>>n;

Node *ptr = box;
int count = 0;
for( count = 0; count < n && ptr != NULL; count++)
{
    ptr = ptr->next;
}
// now ptr points to the nth node.




    cin.get();
    return 0;
}

would this code do what I want now?

Example run

How many boxes: 5
// five boxes gets created

which one you want to get in: 2

2

0

Better present this code to your compiler. Are you sure that the best C++ compiler now is DaniWeb C++ forum?

0
struct Node 
{
   int data;
   Node*next;
};

int main()
{
    Node * head = NULL;
    Node * tail = NULL;
    int boxes = 0;

    //create list with 3 Nodes
    while(boxes < 3)
    {
       Node * box = new Node;
       box->data = boxes;
       box->next = NULL;

       //add each new Node to end of list
       if(boxes == 0)
       {
          head = box;
          tail = head;
        }
        else
        {
           tail->next = box;
           tail = tail->next;
        }
        ++boxes;
     }
      
      //display contents of the list
      Node * current = head;
      while(current != NULL)
      {
          cout << current->data << endl;
          current = current->next;
       }

// code above creates numbered nodes in a linked list
int n;
cout<<"which box do you want to be in?"<<endl;
cin>>n;

Node *ptr = current;
// now ptr points to the nth node.


while( ptr )
{
   cout << ptr->data << "\n";
   ptr = ptr->next;
}

 system("PAUSE");
    return 0;
}

It compiles, but like last time.
I thought it worked, AD pointed out that the pointers were pointing to the same one. Thus the second part of my goal wasn't working.

0

...
It compiles, but like last time.
I thought it worked, AD pointed out that the pointers were pointing to the same one. Thus the second part of my goal wasn't working.

What's the second part of your goal?

0

First goal, it creates a linked list.
second part - choose a specific node.

It's like a linked list = a train right?
Second is which cart do you want to be in.

The concepts are easy for me, but just the design on implementing it is troubling.

0

I finally give up. BlackStar has not the slightest clue what he is doing -- just tossing together random pieces of code in the hopes that it will do something.

My suggestion to BlackStar is to just drop this course because its over your head anyway, then take a course in logic thinking. If you can't think at least a little bit logically then computer programming will be impossible for you.

0

I agree with Ancient Dragon's diagnosis. There were at least two complete solutions of the "problem" #2 in this thread (three day ago) but BlackStar can't understand this fact.
Probably the patient's case is hopeless :(

0

Sigh... but I feel that I'm close to finishing this.

Sorry, but you are not even close. You still have to do all those other linked lists, unless you have decided to simplify and redesign the program.

0

If I get these things working properly.
I'll know what to do with the other ones.

Since they're really the same.

0

When programmers write complicated linked lists they do not duplicate all that code for every linked list. Instead they write the code once for general purpose lists so that the code can be re-used for any number of lists. Here is one example of how to write such a program, with test main() to exercise the functions

#include<iostream>
using namespace std;

struct node
{
    struct node* next;
    void* data;
};

struct node* InsertTail(struct node** head, void* data)
    // This function inserts a new node at the end 
    // of the linked list.
{
    // allocate memory for the new node
    struct node* item = new struct node;
    // initialize the node's variables
    item->next = NULL;
    item->data = data;
    // if this is the first time a node has been
    // added to the linked list then just set the
    // list head to the same address as the new node
    if( *head == NULL)
    {
        *head = item;
    }
    else
    {
        // There are other nodes in the list, 
        struct node* curr = *head;
        // find the last node in the list
        while( curr->next != NULL)
            curr = curr->next;
        // now insert the new node at the end
        curr->next = item;
    }
    return item;
}

struct node* InsertHead(struct node** head, void* data)
    // This function inserts a new node at the top 
    // of the linked list.
{
    // allocate memory for the new node
    struct node* item = new struct node;
    // initialize the node's variables
    item->next = NULL;
    item->data = data;
    // if this is the first time a node has been
    // added to the linked list then just set the
    // list head to the same address as the new node
    if( *head == NULL)
    {
        *head = item;
    }
    else
    {
        // There are other nodes in the list, so 
        // just set the head as the new node.
       item->next = *head;
       *head = item;
    }
    return item;
}

struct node* GetItem(struct node* head, int itemnum)
    // This function returns the ith node in the list
{
    for(int i = 0; i < itemnum && head != NULL; i++)
    {
        head = head->next;
    }
    return head;
}

void DeleteList(struct node** head)
{
    // delete all nodes in the list and set the
    // head to NULL;
    struct node* n = *head;
    while(n)
    {
        struct node* hold = n->next;
        delete n;
        n = hold;
    }
    *head = NULL;

}


int main()
{
    struct node* head = NULL;
    int a = 1, b = 2, c = 3, d = 4, e = 5;
    InsertTail(&head, &a);
    InsertTail(&head, &b);
    InsertTail(&head, &c);
    InsertTail(&head, &d);
    InsertTail(&head, &e);

    struct node* n = head;
    while( n != NULL)
    {
        cout << *(int *)n->data << "\n";
        n = n->next;
    }
    int number = 0;
    cout << "Which node to you want (1-5) ?\n";
    cin >> number;
    n = GetItem(head, number-1);
    cout << "The value is: " << *(int *)n->data << "\n";

    DeleteList(&head);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.