英文:
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->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? =(
答案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 <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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论