linked list

hi. can someone help me match, or mirror, these two pieces of code. they should both do the same thing in the same order. The array version should be pretty obvious. It adds a player to the player array, and increments. Im not sure how the playerArray, count, and the loop translate into the linked list version. if you take a shot at this, please continue the use of dot operator.
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++;
    }
}


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 = nextToPlay;
          prev.next=0;
     }
}
Last edited on
Topic archived. No new replies allowed.