Linked Lists

This is a discussion on Linked Lists within the C++ Programming forums, part of the General Programming Boards category; I have to place list into an array....I think that it is working except the 0 position is repeating(only this ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Linked Lists

    I have to place list into an array....I think that it is working except the 0 position is repeating(only this one)

    Can anyone help?

    Code:
    //******************************************************************BUCKET SORT
    void bucket_sort(int digit)
    {
         node *buckets[10];
         node *temp, *temp2;
         temp = start_ptr;
         int i;
         int digit_num;
         
         for(i=0; i<10; i++)
         {
                  buckets[i] = NULL;
         }
         
    do
      {  if (temp == NULL)
         {
         }
         else
         {
             digit_num = get_digit(temp->node_struct,digit);
             //cout << "Digit = " << digit_num << endl;
             if(digit_num == 0)
             {
                  if(buckets[0] == NULL)
                  {
                  cout << " A ZERO DIGIT" << endl;
                  buckets[0] = temp;
                  }
                  else
                  {
                      buckets[0]->nxt = temp;
                  }
             }
             else if(digit_num == 1)
             {
             if(buckets[1] == NULL)
                  {
                  cout << " A ONE DIGIT" << endl;
                  buckets[1] = temp;
                  }
                  else
                  {
                      buckets[1]->nxt = temp;
                  }
             }
             else if(digit_num == 2)
             {
                  if(buckets[2] == NULL)
                  {
                  cout << " A TWO DIGIT" << endl;
                  buckets[2] = temp;
                  }
                  else
                  {
                      buckets[2]->nxt = temp;
                  }
             }
             else if(digit_num == 3)
             {
                  if(buckets[3] == NULL)
                  {
                  cout << " A THREE DIGIT" << endl;
                  buckets[3] = temp;
                  }
                  else
                  {
                      buckets[3]->nxt = temp;
                  }
             }
             else if(digit_num == 4)
             {
                  if(buckets[4] == NULL)
                  {
                  cout << " A FOUR DIGIT" << endl;
                  buckets[4] = temp;
                  }
                  else
                  {
                      buckets[4]->nxt = temp;
                  }
             }
             else if(digit_num == 5)
             {
                 if(buckets[5] == NULL)
                  {
                  cout << " A FIVE DIGIT" << endl;
                  buckets[5] = temp;
                  }
                  else
                  {
                      buckets[5]->nxt = temp;
                  }
             
             }  
             else if(digit_num == 6)
             {
                 if(buckets[6] == NULL)
                  {
                  cout << " A SIX DIGIT" << endl;
                  buckets[6] = temp;
                  }
                  else
                  {
                      buckets[6]->nxt = temp;
                  }
             
             }
             else if(digit_num == 7)
             {
                 if(buckets[7] == NULL)
                  {
                  cout << " A SEVEN DIGIT" << endl;
                  buckets[7] = temp;
                  }
                  else
                  {
                      buckets[7]->nxt = temp;
                  }
             }
             else if(digit_num == 8)
             {
                 if(buckets[8] == NULL)
                  {
                  cout << " A EIGHT DIGIT" << endl;
                  buckets[8] = temp;
                  }
                  else
                  {
                      buckets[8]->nxt = temp;
                  }
             }
             else if(digit_num == 9)
             {
                 if(buckets[9] == NULL)
                  {
                  cout << " A NINE DIGIT" << endl;
                  buckets[9] = temp;
                  }
                  else
                  {
                      buckets[9]->nxt = temp;
                  }
             }
             
         temp2 = temp->nxt;
         temp->nxt = NULL;
         temp = temp2;
         }
      }while (temp != NULL);
    
         
         
          
              
    }

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    This should do what you want:

    This function will return your digit amount in an int, but will return 0 if there are more than 'maxdigits' digits.

    Code:
    int bucket_sort(int digit, int maxdigits)
    {
      char c[maxdigits + 3];
      wsprintf(c,"%d",digit);
      int i;
      for(i = 0; i <= maxdigits; i++)
      if(c[i] == '\0')
      break;
      if(i == maxdigits)
      return 0;
      return i;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21