Linked List

Hi everyone

I am making a linked list with class.
I have to make the node fix at 10. I just do not know how?

Anyone can help me point me in the right direction.

Here is my header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

class List
{
public:
	List();
	~List();
	void insert(int number);
	int isEmpty();
	
private:
	struct Node
	{
		int data;
		Node* next;
	};
	Node* head;
};
Last edited on
You mean you don't want to allow any more than 10 elements at one time? Then just stop the insert if count == 10.
Thanks for the reply.

I understand controlling the insert function, but is there another way or that would be the best approach?

thanks
There are no other ways except to keep a counter of nodes.
Last edited on
There are no other ways except to keep a counter of nodes.


Oh really? :)

Keeping the counter is probably the way to go unless you are very limited on ram, but here's another way:
1
2
3
4
5
6
7
8
int count =0;
Node *nodePtr = head;
while (nodePtr)
{
  count++;
  nodePtr = nodePtr->next;
}
return count;


I suppose you could even put that in the push_back Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool push_back(int i)
{
  int count = 0;
  Node* previous = NULL; 
  Node* nodePtr = head;
  while (nodePtr)
  {
    count++;
    if (count == 10) return false;  // Max Size
    previous = nodePtr;
    nodePtr = nodePtr->next;
  }
  if (previous)
    previous->next = new Node(i);
  else
    head = new Node(i);
  return true;
}

//  Later
if (!myList.push_back(myFavoriteInteger))
  std::cout << "List Full\n";
Last edited on
@LowestOne Yes. vlad is correct. The fact that you're doing a count on the fly is hugely inefficient and definitely not something that should be recommended. It's a complicated solution to a simple problem.

The fact that you're doing a count on the fly is hugely inefficient and definitely not something that should be recommended.


Not in the general case. For a list limited to 10 nodes.. maybe it's not so bad.

What if we kept a queue of unused nodes as part of the implementation and just depended on knowing if the queue was empty to stop insertions? Is keeping a count still the only way?
Is keeping a count still the only way?

If you initialise a counter to 10 and subtract 1 when adding a node - that's a counter.
If you initialise a queue with 10 items and remove 1 when adding a node, that's also a counter.
If you initialise a counter to 10 and subtract 1 when adding a node - that's a counter.

Yes.

If you initialise a queue with 10 items and remove 1 when adding a node, that's also a counter.

No. The list can be completely oblivious of any count. There's not necessarily a hidden count either as queue implementations don't require one.

edit:
By way of illustration:

My nephew has this transparent tubing construct that he likes to run marbles through. He also has a jar of marbles that attaches to the end of the tube to catch the marbles when they've completed their journey.. The typical use case: He'll take marbles from the jar and insert them into the tube and watch them go down until they return to the jar.

Would you say he's keeping a count of marbles?
Last edited on
Thanks vlad
Topic archived. No new replies allowed.