CircleMUD (amateur linked list problem)

This is a discussion on CircleMUD (amateur linked list problem) within the C Programming forums, part of the General Programming Boards category; First, here's a quick background: I've been assigned to modify this 'circleMUD' for my grade 12 summative, and my teacher ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    CircleMUD (amateur linked list problem)

    First, here's a quick background: I've been assigned to modify this 'circleMUD' for my grade 12 summative, and my teacher has asked us to do several things that he hasn't taught us. As well as him wandering off and not staying focused when I tried to get help from him on this problem, we have never been officially taught pointers, and the indirect referencing in this code was the first time I've ever tried to do such a thing. One of the tasks is to add sorting functionality to a command that displays all the characters names currently online, and as you'll notice, I've put no attention into efficiency.

    Anyways, the program compiles (and, amazingly enough, it sorts [I think] ), but I've clearly messed something up - as when I test the 'who' command (which is the code shown below, which I am trying to modify) by opening and connecting multiple users, the command appears in the next person's screen, as well as any following commands. It's sort of as if the person who uses the 'who' command hijacks the next player's game, and takes over their user. (however, output is still displayed on the original person's console, and that person, too, can hijack another person's game, as long as it's not the person who's controlling theirs)

    I wasn't sure if this needed any more explanation, however I've added comments saying what I've intended on a line doing - and hopefully that will be enough.
    Code:
                  
    ACMD(do_who)
    {
      struct descriptor_data *d;
      struct char_data *tch, *ntch;
      struct descriptor_data swap;
      struct descriptor_data *next;
    
         .... Other unimportant declairations and code, for about 50 lines ...
    
    for(;;success = 0) {                                  // if sort is completed without changes
        for (d = descriptor_list; d && d->next; d = d->next) {
          if (d->original)                                       // if d->original has content, use that
            tch = d->original;                      // tch isn't an acronym, and has no real meaning
          else if (!(tch = d->character))                                   // or else, use d->character
            continue;                                     // or else quit, because something is wrong
        
    	  if (d->next->original)                                        // this is for the next descriptor
    	    ntch = d->next->original;             // if the next ->original has content, use that
    	  else if (!(ntch = d->next->character))           // or else use next ->character
    		continue;                                 // or else quit, because something is wrong
    
    
    	  if (GET_LEVEL(tch) < GET_LEVEL(ntch)) {         //this level to next descriptors level
    	    next = d->next->next;    // marks the point of the 3rd descriptor after the current 1
    	    swap = *d;                             // swap takes everything from current descriptor
    	    *d = *d->next;                        //  everything in next descriptor copies into current
    	    d->next = swap.next;       // fixes the broken link in list, pointing current to next
    	    *d->next = swap;         //  everything in the next becomes what used to be in first
    	    d->next->next = next;     //the 'next' pointer in second link is fixed, points to 3rd		
    	    success++;                     // marks that a change has been made, to continue sort
    	  }
    	}
    	if(!success)         
    		break;
      }
    
                      .... and continues on for about 50 lines ...
    Thanks for taking the time to read this lengthy post, and I'd greatly appreciate any response.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > *d->next = swap;
    Everything you've done so far in swapping has been lost with this statement.
    You need to copy everything except the next pointer, or set *d->next afterwards.

    Though ideally, you should just swap the pointers with just 3 assignments.

    If your list is
    A-->B-->C-->D

    Then swapping B and C is
    A->next = C;
    B->next = D;
    C->next = B;

    The only tricky step is right at the start of the list, where you have to update the pointer to the whole list (the head pointer) rather than the next pointer in a node element.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Okay, so I've edited it and here I have:
    Code:
    if (GET_LEVEL(tch) < GET_LEVEL(ntch)) {  
    	current = d->next;                //Pointer points at B
    	save = current->next;          //Pointer points at C
            swap = *d;                           //Swap = A
    	*d = *d->next;                     //A = B
    	d->next = current;                //A (B clone) links to B
    	*current = swap;                 //B = A
            current->next = save;          //B (A clone) points to C
    	success++;                           //Success :)
    }
    With it done like this, I don't have to worry about the original being correct, like you said, and all that stuff and the program works - however, I've tested it where I have 5 people going, and the function sorts properly and stuff for them, but whenever I connect another player, and another member is added to descriptor_list, the function screws up, as described before. Where do you think the flaw is? What could be going wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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