页面输出双向链表的函数存在问题

huangapple go评论61阅读模式
英文:

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...

页面输出列表项。每页有count/2个项目。由于末尾的项目数可能不等于这个数字,而更少,因此必须单独处理。我输入上限和下限,当下限达到时,尾部单独输出,这就是整个问题...

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

请注意,这只是提供给你的代码的中文翻译部分,不包括任何其他内容。如果你有任何其他问题或需要进一步的帮助,请随时提出。

英文:

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-&gt;head;
node* UpLimit = hwnd-&gt;head;
node* LwLimit = hwnd-&gt;tail;
if(!head) return;
for(int i = 0; i &lt; count/2 ; i++)
UpLimit = UpLimit-&gt;next;
CountElementstInEnd = hwnd-&gt;size % (count/2) - 1;
for(int i = 0; i &lt; CountElementstInEnd; i++)
LwLimit = LwLimit-&gt;prev;
temp = LwLimit;
char c;
do
{
system(&quot;cls&quot;);
puts      (&quot; ID      NAME          SEX        SPORT          BORN    GROWTH &quot;);
for(int i = 0; i &lt; 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 &lt; CountElementstInEnd; i++, id++){
printf(&quot; %-2.2d   %-12.12s    %-6.6s   %-16.16s  %-4.4d      %-3.3d  \n&quot;, id, temp-&gt;data-&gt;name, temp-&gt;data-&gt;sex, temp-&gt;data-&gt;sport, temp-&gt;data-&gt;born, temp-&gt;data-&gt;growth);
temp = temp-&gt;next;
}
temp = LwLimit;
id-=CountElementstInEnd;
break;
}
///normal output
printf(&quot; %-2.2d   %-12.12s    %-6.6s   %-16.16s  %-4.4d      %-3.3d  \n&quot;, id, head-&gt;data-&gt;name, head-&gt;data-&gt;sex, head-&gt;data-&gt;sport, head-&gt;data-&gt;born, head-&gt;data-&gt;growth);
head = head-&gt;next;
}
///users input 1 - next, 2 - prev
while(1){
c = getch();
if (c == 0x31 &amp;&amp; (head == LwLimit)){
for(int i = 0; i &lt; count; i++)
head = head-&gt;prev;
id -= count;
break;
}
if (c == 0x31 &amp;&amp; (head != UpLimit)){
for(int i = 0; i &lt; count; i++)
head = head-&gt;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 &amp;&amp; (head == hwnd-&gt;tail)){
head = head-&gt;prev-&gt;prev;
break;
}

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

https://pastebin.com/J2bnc151

答案1

得分: 1

这是一个可能的(有效)示例,演示如何实现您的目标:

#include <stdio.h>
#include <stdlib.h>

typedef struct node_s { // 缩写的结构声明
    char *name;
    struct node_s *prev, *next;
} node;

const int pagesz = 2; // 一个“有效”的页面大小以适应

void ShowList(node *head) {
    int id = 0;
    char c = 0;

    while (c != 27) { // 丑陋的“魔数”
        node *p = head; // 工作副本

        // 输出“页面”(无“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) // 再次,无意义的魔数
                break;

            if (c == '+') { // '+' == “前进”
                for (int i = 0; i < pagesz; i++)
                    if (head->next)
                        head = head->next, id++;
                break;
            }

            if (c == '-') { // '-' == “倒带”
                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

请注意,代码部分没有翻译,只提供了注释。

英文:

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

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

huangapple
  • 本文由 发表于 2023年2月19日 08:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定