Thread: going backward

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    going backward

    this function suppose to move cusor to one space backward, but it gives me error. it compiles, but would not run...

    definitions
    Code:
    typedef struct node{
       char chdata;
       struct node *link;
       struct node *prev;
       struct node *next;
    }  node;
    typedef struct node *position;
    
    typedef struct bufferType{
       node *head;
       node *cursor;
       node *clipboard;
    } bufferType;
    
    typedef bufferType *bufferT;
    backward function
    Code:
    void MoveCursorBackward(bufferT buffer)
    {
    position temp;
    
    if(buffer -> head)
    {
    	while(temp -> next != buffer->cursor->next)
    	{
    		temp = temp -> next ;
    	}
    	buffer->cursor = temp;
    }
    }

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You didn't initialize temp
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You realize that your function returns sets buffer->cursor to the same spot it was originally, right? The implementation would work better like this:
    Code:
    void MoveCursorBackward(bufferT buffer)
    {
      if(buffer->head)
      {
        position temp = buffer->head;
    
        while(temp->next != buffer->cursor)
        {
          temp = temp->next;
        }
    
        buffer->cursor = temp;
      }
    }
    Now it will work, but now you have to cover for problems such as when buffer->cursor is the same as buffer->head. While we're on the subject, since the list is double linked, why not just do this instead:
    Code:
    void MoveCursorBackward(bufferT buffer)
    {
      if(buffer->head)
      {
        if(buffer->cursor->prev)
        {
          buffer->cursor = buffer->cursor->prev;
        }
      }
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing backward
    By archriku in forum C Programming
    Replies: 13
    Last Post: 06-13-2009, 07:25 AM
  2. Backward fstream?
    By kryptkat in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2007, 07:34 AM
  3. help with displaying a number backward
    By nicz888 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 10:00 AM
  4. Backward substitution in gaussian elimination
    By Meander14 in forum C Programming
    Replies: 0
    Last Post: 09-30-2007, 07:02 AM
  5. backward debugging in Visual Studio??
    By George2 in forum Tech Board
    Replies: 12
    Last Post: 11-05-2006, 02:17 AM