cdoubly-linked-list

Problem with the page output function of a double-linked list


Page output of list items. There are count/2 items on the page. Since the end may not be equal to this number, but be less, it must be handled separately. I enter boundaries upper and lower, when the lower boundary is reached the tail is output separately and this is the whole problem...

void ShowList(hwnd* hwnd){
    int id = 0, count = 10, CountElementstInEnd;
    node* temp;
    node* head = hwnd->head;
    node* UpLimit = hwnd->head;
    node* LwLimit = hwnd->tail;

    if(!head) return;
    for(int i = 0; i < count/2 ; i++)
        UpLimit = UpLimit->next;
    CountElementstInEnd = hwnd->size % (count/2) - 1;
    for(int i = 0; i < CountElementstInEnd; i++)
        LwLimit = LwLimit->prev;
    temp = LwLimit;
    char c;

    do
    {
        system("cls");
        puts      (" ID      NAME          SEX        SPORT          BORN    GROWTH ");
        for(int i = 0; i < count/2 ; i++, id++){
            ///Output the records at the end (their number may not be a multiple of count/2)
            if(head == LwLimit){
                for(int i = 0; i < CountElementstInEnd; i++, id++){
                    printf(" %-2.2d   %-12.12s    %-6.6s   %-16.16s  %-4.4d      %-3.3d  \n", id, temp->data->name, temp->data->sex, temp->data->sport, temp->data->born, temp->data->growth);
                    temp = temp->next;
                }
                temp = LwLimit;
                id-=CountElementstInEnd;
                break;
            }
            ///normal output
            printf(" %-2.2d   %-12.12s    %-6.6s   %-16.16s  %-4.4d      %-3.3d  \n", id, head->data->name, head->data->sex, head->data->sport, head->data->born, head->data->growth);
            head = head->next;
        }
        ///users input 1 - next, 2 - prev
         while(1){
            c = getch();
            if (c == 0x31 && (head == LwLimit)){
                for(int i = 0; i < count; i++)
                    head = head->prev;
                id -= count;
                break;
            }
            if (c == 0x31 && (head != UpLimit)){
                for(int i = 0; i < count; i++)
                    head = head->prev;
                id -= count;
                break;
            }
            else if(c == 0x32 || c == 27)
                break;
        }
        
    }
    while(c != 27);
}

The function works, but if we share to the end and then go back, it skips 1 page. If we go back when it reaches not by count but by count/2 entries, the output will loop to the last page and "tail".

if (c == 0x31 && (head == hwnd->tail)){
        head = head->prev->prev;
        break;
    }

then the output will loop at the end... Is there any way out of this situation? =(

https://pastebin.com/J2bnc151


Solution

  • This is a possible (working) example of how you might achieve your goals:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node_s { // abbreviated struct declaration
        char *name;
        struct node_s *prev, *next;
    } node;
    
    const int pagesz = 2; // a "working" page size to adapt
    
    void ShowList( node *head ) {
        int id = 0;
        char c = 0;
    
        while( c != 27 ) { // UGLY "magic number"
            node *p = head; // a working copy
    
            // output the "page" (without "cls")
            puts( " ID      NAME" );
            for( int i = 0; i < pagesz && p; i++, p = p->next )
                printf(" %2d   %s\n", id + i, p->name );
    
            while( 1 ) {
                c = getchar();
                if( c == 27) // again, meaningless magic number
                    break;
    
                if( c == '+' ) { // '+' == "advance"
                    for( int i = 0; i < pagesz; i++ )
                        if( head->next )
                            head = head->next, id++;
                    break;
                }
    
                if( c == '-' ) { // '-' == "rewind"
                    for( int i = 0; i < pagesz; i++ )
                        if( head->prev )
                            head = head->prev, id--;
                    break;
                }
            }
        }
    }
    
    int main() {
        node arr[] = {
            { "Grumpy 0"},
            { "Dopey 1" },
            { "Doc 2" },
            { "Sneezy 3" },
            { "Bashful 4" },
            { "Sleepy 5" },
            { "Happy 6" },
        };
    
        int i;
        for( i = 0; i < sizeof arr/sizeof arr[0]; i++ )
            arr[i].prev = &arr[i-1], arr[i].next = &arr[i+1];
    
        arr[0].prev = arr[i-1].next = NULL;
    
        ShowList( arr );
    
        return 0;
    }
    
     ID      NAME
      0   Grumpy 0
      1   Dopey 1
    +
     ID      NAME
      2   Doc 2
      3   Sneezy 3
    +
     ID      NAME
      4   Bashful 4
      5   Sleepy 5
    -
     ID      NAME
      2   Doc 2
      3   Sneezy 3
    +
     ID      NAME
      4   Bashful 4
      5   Sleepy 5
    +
     ID      NAME
      6   Happy 6
    -
     ID      NAME
      4   Bashful 4
      5   Sleepy 5