双链表的页面输出功能问题

问题描述 投票:0回答:1

列表项的页面输出。页面上有 count/2 个项目。由于end可能不等于这个数,而是更少,所以要单独处理。我输入上下边界,当到达下边界时,尾巴分别输出,这就是整个问题......

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);
}

该功能有效,但如果我们分享到最后再返回,它会跳过一页。如果我们在它到达时不是按计数而是按 count/2 个条目返回,输出将循环到最后一页和“尾巴”。

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

然后输出会在最后循环...有什么办法可以摆脱这种情况吗? =(

https://pastebin.com/J2bnc151

c doubly-linked-list
1个回答
0
投票

这是一个可能的(可行的)示例,说明您如何实现目标:

#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
© www.soinside.com 2019 - 2024. All rights reserved.