say you have

struct Node{
int item;
Node *next;
}

int main()
{

Node *p;
p= new Node;
p-> item = 100;
p-> next = NULL;

}

is there a way to make it that, the

p-> item

becomes whatever the user inputs?

cout << "Enter item\n";
cin >> node->item;

Is there a way that base on the user input, you create how many nodes?

Say

How many nodes?
4

That creates four nodes, and they are link listed.

>>Is there a way that base on the user input, you create how many nodes?

Yes. Use a loop. I am vague here on purpose -- think about it for awhile and you will come up with the answer.

I been thinking about it for the past hour.
And I'm coming up with blanks.

for(int i = 0; i < NumWanted; i++)
{
    // allocate a new node
    // attach new node to next pointer in previous node
}

So, ???

cout<<"Num wanted";
cin>>numwanted;

for(int i=0; i<numwanted; i++);
{
   node *a = new node;
   B-> next = A; 

}

That might work if you want to add the new node at the head of the linked list.

node *a = new node;
   a-> next = B;
   B = a;

If you want to add it to the tail then you have to search for the end of the list and add it there

node* n = B; 
while( n->next != NULL)
    n = n->next;
// now add new node
node* a = new node;
n->next = a;
a->next = NULL;

Is there a way to order your nodes?
Like base on the data that the pointer possess? Numerically?

cout<<"Num wanted";
cin>>numwanted;

for(int i=0; i<numwanted; i++);
{
   node* n = B; 
while( n->next != NULL)
    n = n->next;
// now add new node
node* a = new node;
n->next = a;
a->next = NULL;

}

would this be the entire code? I don't see how the input from the user creates the amount of nodes.

Say instead of a link list, it's a Queue.
Say for example the program was this.

int bosses;
cout<<"How many Bosses?";
cin>>bosses

// depending on the input.
// this creates how many type of queues burger and soda has. 
// so if someone say he wanted 5 bosses, there will be 5 bosses queues. 


for(int i=0; i<=bosses; i++);
{
   node* n = B; 
while( n->next != NULL)
    n = n->next;
// now add new node
node* a = new node;
n->next = a;
a->next = NULL;

}

Would this work?
Also, how would I check if there is actually 5 queues created?

I'm stumped, could someone give me a hint as to that code is right or not?

Yes, that's all there is to it. The very first two lines is there the user enters the number of nodes to be created. And the for loop is what creates them all.

cout<<"Num wanted";
cin>>numwanted;

for(int i=0; i<numwanted; i++);
{
   node* n = B; 
while( n->next != NULL)
    n = n->next;
// now add new node
node* a = new node;
n->next = a;
a->next = NULL;

}

would this be the entire code? I don't see how the input from the user creates the amount of nodes.

>> depending on the input.
>> this creates how many type of queues burger and soda has.
>> so if someone say he wanted 5 bosses, there will be 5 bosses queues.

The code I posted is only one queue. If you want more queues, such as what you have be Burger King or McDonalds then you need multiple queues -- one for each cash register. And in that case you will need to create a linked list of linked lists. Something like below would work.

struct customer
{
    struct customer* next;
    int n;
};

struct register
{
    struct register* next;
    // linked list for people in line at the cash register
    struct customer* head;
};

How come when you declare the pointers, you also put a struct on the name?

struct customer *next;

That's just convention from C language. In C++ you can omit the struct keyword.

that's what I figured ><
right now I'm trying to form the code by myself ><

Would it be something like this?

struct brand
{
    struct brand *next;
    int n;
};

struct register
{
    struct register* next;
    // linked list for people in line at the cash register
    struct brand *head;
};

brand BurgerKing;
register ATBK;

// enter amount of burger king queues.

cout<<"How many Burger Kings are in your area?";
cint>>BurgerKing-> next->n;




for(int i=0; i<=BurgerKing->next->n; i++);
{  
	brand* n = B; 
	while( n->next != NULL)
	    n = n->next;
	// now add new node
	node* a = new node;
	n->next = a;
	a->next = NULL;    

  for (int j=0; j<BurgerKing->next->n; j++)

 {
        ATBK-> next= new node;
	
   }
}


// chooses which queue you would like to use
cout<<"Which one would you like to go to?"
cin>> // what do I put here?

I have a feeling this is wrong

I probably don't need two loops right?

>>brand BurgerKing;
>>register ATBK;

That is not correct.

// this structure is for each of the items that the customer orders
struct item
{
    struct item* next;
     std::string item_name;
     float price;
     int quantity;
};

// one of these structures for each customer.  It contains a linked list 
// of items structures from above.
struct customer
{
    struct customer* next;
    struct item* items_ordered;

    std::string customer_name;
};


// One of these structures for each cash register in the store.  Don't 
// forget the registers used at the drive-through windows.  When the 
// customer orders something a customer structure is added to the register
// linked list.  Then when the customer pays for it the customer list is deleted
// from the register's customer queue.

struct register
{
    struct register* next;
    struct customer* customer_queue;  
};

// One of these brand structures for each band, such as BurgerKing, McDonalds
// Wendys, etc.
struct brand
{
    struct brand *next;
    struct register* cash_registers;
};

Now what you want is a linked list of brands that holds all the other linked lists brand* head = NULL; Now add a node to head above for each brand.

So basically what you're saying is two structs, and one of the structs, is the datatype of that other struct.

And that loop code that you gave a page back?

That would create serveral queues?

I understand why there's four structs, with. They serve a purpose.
But I dun see how that serves in helping me create serveral queues.
Say someone enters 5 burgerKing stores in their town.
That would mean five different queues of burger King, each with their own separate cash register, and customers.

Unless this would be the code for it?

cout<<"How many Stores in your town?"
cin>>number;

for(int i=0; i<=number; i++)
{

brand* n = B; 
    while( n->next != NULL)
        n = n->next;
    // now add new node
    node* a = new node;
    n->next = a;
    a->next = NULL;  
}

wait I think I understand

the for loop creates a linked list.
but since the data type of the struct is from another struct.
that's basically two linked lists?

I think you have it -- the brand link list contains one node for each store. You can think of the stores any way you wish. And you might want to add a string to the brand structure that is the name or type of store to make it easy to keep track of which store is which.

struct brand
{
    struct brand *next;
    struct register* cash_registers;
    std::string name;
};

I was just thinking of like.

First BurgerKing
Second BurgerKing
Third BurgerKing.

but with that code though.
if someone enters like i want five burgerking (queues)
It'll juts be five burger king queues.
it wouldn't be numbered though right?

I understand the method.
But How would we access the second linked list?

struct brand
{
    brand *next;
    brand *previous;
    int n;
};

struct register
{
    register* next;
    // linked list for people in line at the cash register
    brand *head;
};

// enter amount of burger king queues.

cout<<"How many Burger Kings are in your area?";
cint>>Brand-> next->n;

for(int i=0; i<=Brand->next->n; i++);
{  
	brand *A = new brand;
	while( A->next != NULL)
	    A->next->previous = A;                    
	// now add new node
	node* D = new node;
	A->next = D;
	D->next = A   

// does this code work?
}

// if it does
//how does I access the register linked list?
//unless it's already created?

I'm afraid you don't understand the method.

brand* head = NULL; // top of linked list
brand* tail = NULL; // linked list tail

int NumberOfBurgerKings = 0;
cout << "Enter the number of BurgerKings\n";
cin >> NumberOfBurgerKings;
for(int i = 0; i < NumberOfBurgerKings; i++)
{
    brand* restaurant = new brand;
    restaurant->next = NULL;
    // now add to linked list
    if( head == NULL)  
    {
          head = tail = restaurant;
    }
    else
    {
         // add new node to end of linked list
         tail->next = restaurant;
         tail = restaurant;
     }
}

but then how does register link list gets created?

Maybe something like this?

brand* head = NULL; // top of linked list
brand* tail = NULL; // linked list tail

int NumberOfBurgerKings = 0;
cout << "Enter the number of BurgerKings\n";
cin >> NumberOfBurgerKings;
for(int i = 0; i < NumberOfBurgerKings; i++)
{
    brand* restaurant = new brand;
    restaurant->next = NULL;
    cout << "How many registers for this store?\n";
    int nRegisters;
    cin >> nRegisters;
    for(int j = 0; j < nRegisters; j++)
    {
            // create linked list for this store
    }
    // now add to linked list
    if( head == NULL)  
    {
          head = tail = restaurant;
    }
    else
    {
         // add new node to end of linked list
         tail->next = restaurant;
         tail = restaurant;
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.