capacity

Pages: 12
[code]void CircleList::add(string s)
{
Player p;
p.name=s;
if(count==0) // no players at the table
{
// special case: adds a player to an empty table
playerArray[0]=p;
count=1; // adds the first player
whoWasLastToPlay=0; // whoWasLastToPlay at pos 0
}
else
{
for(int i=count-1; i>=whoWasLastToPlay+1; i--)
{
playerArray[i+1]=playerArray[i];
}
playerArray[whoWasLastToPlay+1]=p;
count++;
}
}[/ code]
get rid of the space after the '/' in the [/ code]
Last edited on
EddieV223 wrote:
An unsigned int is 4 bytes in 32 or 64 bit OS.

It doesn't have to be. It depends on the compiler.

EddieV223 wrote:
Because string uses a unsigned int it will have the limit as such.

size_t doesn't have to be a typedef for unsigned int. On 64 bit compilers it's often a 64 bit integer. Note that even if sizeof(size_t) == sizeof(unsigned int), it doesn't mean they are necessary the same type.

Zephilinox wrote:
You're right, ints in C++ are either 16-bit or 32-bit, they can't be 64-bit.

Sarcasm?

Thank you, you are getting closer to my point. What ever data type is used will be the limit. Run time limitations are different.

Its really hard to have a conversion about type sizes in c++ since they are not defined. My point about 32 or 64 was ment that it is not defined that the size of an unsigned int will be 8 bytes just cause its a 64 bit build.

For example I can guarantee that on VS2012's implementations, an int is 4 bytes on 32 and 64bit builds.


std::numeric_limits<std::string::size_type>::max() - 1; // this is your code's limit on string size.

or
std::string str;
str.max_size();
Last edited on
It wasn't sarcasm, but I also don't understand your quotes.

yeah, I thought it was possible for an int to be 64-bits, but apparently an int in c++ is guaranteed to be either 16 bit or 32 bit (quick google)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void CircleList::add(string s)
{
    Player p;
    p.name=s;
    if(count==0) // no players at the table
    {
        // special case: adds a player to an empty table
        playerArray[0]=p; 
        count=1; // adds the first player
        whoWasLastToPlay=0; // whoWasLastToPlay at pos 0
    }
    else
    {
        for(int i=count-1; i>=whoWasLastToPlay+1; i--)
        {
             playerArray[i+1]=playerArray[i];
        }
        playerArray[whoWasLastToPlay+1]=p;
        count++;
    }
}


what I'm trying to do is implement the above array based code as a dynamic, pointer based singly linked list code. Therefore my earlier question as to weather in the following:
1
2
3
4
5
 
Node  *loc = new Node;
(*loc).data = 10;
Tail = loc;
Tail->next = Tail;

where 10 is indeed the data content of "that one node", and since in a linked list each node contains data and a pointer to the next node, can I let the string "whoWasLastToPlay" in the above code be the content of one node. I will continue after your response. Thanks for your help!!
whoWasLastToPlay is an int, not a string.

if Node.data is an int, then data can be whoWasLastToPlay.

if Node.data is a string, then data can be any string, it will fit in a node.
Last edited on
you are correct, it is an int, and it resides(at this point)implicitly in position(or index) 0(zero) in the array. How can I make the following "add" code resemble(ideally mirror)the above "add"code? They are (very) different now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void CircleList::Add(string s)
{
     result= "Add has been called\n"; // the constructor
     if(front.next=0)
     {
         front.next = new node;
         front.next = data=whoWasLastToPlay;  // = item;
         front.next.next=0;
     }
     else
     {
          node *prev = front.next;
 
          while(prev.next!=0)
          {
              prev = prev.next;
          }
          prev.next = new node;
          prev = prev.next;
          prev.data = item;
          prev.next=0;
     }
}
what's wrong with the way you've done it? I've changed it a bit to make it a bit clearer for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void CircleList::Add(string s)
{
    //assume you have a node called firstNode
    
    //linked list is empty
    if (firstNode->nextNode == nullptr)
    {
        firstNode->nextNode = new node;
        firstNode->nextNode->data = whoWasLastToPlay;
        firstNode->nextNode->nextNode = nullptr; //really, the constructor of the node should initialize it to nullptr
    }
    else
    {
        node *endNode = firstNode;
        while (endNode->nextNode != nullptr) //while it points to something
        {
            //keep moving down the linked list
            endNode->nextNode = endNode->nextNode->nextNode;
        }
        endNode->nextNode = new node;
        //add item as the data of the last node
        endNode->nextNode->data = item;
        endNode->nextNode->nextNode = nullptr;
    }
}
considering your code, or mine: how can I make the linked list implementation do what the array implementation is doing, ie, add a player to the group, and increment the count. How does the incrementing loop in the else statement and the playerArray container, and count translate into the linked list version. Is not the arrow operator associated more with structs than classes?
the arrow operator dereferences a pointer, otherwise you are trying to access the member variables of a memory address.

wow, it's 1 AM, I've got to get to sleep, I wish I could help but I just don't understand what it is you are trying to do, sorry.
good nite. thanks for your help. I am in the Eastern Standard time zone. It is 8PM, Saturday here. If it is 1AM where you are, then you are either in Europe or in the middle of the Pacific ocean.
Topic archived. No new replies allowed.
Pages: 12