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

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

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

问题

以下是你提供的代码的翻译部分:

  1. 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...
  2. 页面输出列表项。每页有count/2个项目。由于末尾的项目数可能不等于这个数字,而更少,因此必须单独处理。我输入上限和下限,当下限达到时,尾部单独输出,这就是整个问题...
  3. void ShowList(hwnd* hwnd){
  4. int id = 0, count = 10, CountElementstInEnd;
  5. node* temp;
  6. node* head = hwnd->head;
  7. node* UpLimit = hwnd->head;
  8. node* LwLimit = hwnd->tail;
  9. if(!head) return;
  10. for(int i = 0; i < count/2 ; i++)
  11. UpLimit = UpLimit->next;
  12. CountElementstInEnd = hwnd->size % (count/2) - 1;
  13. for(int i = 0; i < CountElementstInEnd; i++)
  14. LwLimit = LwLimit->prev;
  15. temp = LwLimit;
  16. char c;
  17. do
  18. {
  19. system("cls");
  20. puts (" ID NAME SEX SPORT BORN GROWTH ");
  21. for(int i = 0; i < count/2 ; i++, id++){
  22. ///Output the records at the end (their number may not be a multiple of count/2)
  23. if(head == LwLimit){
  24. for(int i = 0; i < CountElementstInEnd; i++, id++){
  25. 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);
  26. temp = temp->next;
  27. }
  28. temp = LwLimit;
  29. id-=CountElementstInEnd;
  30. break;
  31. }
  32. ///normal output
  33. 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);
  34. head = head->next;
  35. }
  36. ///users input 1 - next, 2 - prev
  37. while(1){
  38. c = getch();
  39. if (c == 0x31 && (head == LwLimit)){
  40. for(int i = 0; i < count; i++)
  41. head = head->prev;
  42. id -= count;
  43. break;
  44. }
  45. if (c == 0x31 && (head != UpLimit)){
  46. for(int i = 0; i < count; i++)
  47. head = head->prev;
  48. id -= count;
  49. break;
  50. }
  51. else if(c == 0x32 || c == 27)
  52. break;
  53. }
  54. }
  55. while(c != 27);
  56. }

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

英文:

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

  1. void ShowList(hwnd* hwnd){
  2. int id = 0, count = 10, CountElementstInEnd;
  3. node* temp;
  4. node* head = hwnd-&gt;head;
  5. node* UpLimit = hwnd-&gt;head;
  6. node* LwLimit = hwnd-&gt;tail;
  7. if(!head) return;
  8. for(int i = 0; i &lt; count/2 ; i++)
  9. UpLimit = UpLimit-&gt;next;
  10. CountElementstInEnd = hwnd-&gt;size % (count/2) - 1;
  11. for(int i = 0; i &lt; CountElementstInEnd; i++)
  12. LwLimit = LwLimit-&gt;prev;
  13. temp = LwLimit;
  14. char c;
  15. do
  16. {
  17. system(&quot;cls&quot;);
  18. puts (&quot; ID NAME SEX SPORT BORN GROWTH &quot;);
  19. for(int i = 0; i &lt; count/2 ; i++, id++){
  20. ///Output the records at the end (their number may not be a multiple of count/2)
  21. if(head == LwLimit){
  22. for(int i = 0; i &lt; CountElementstInEnd; i++, id++){
  23. 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);
  24. temp = temp-&gt;next;
  25. }
  26. temp = LwLimit;
  27. id-=CountElementstInEnd;
  28. break;
  29. }
  30. ///normal output
  31. 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);
  32. head = head-&gt;next;
  33. }
  34. ///users input 1 - next, 2 - prev
  35. while(1){
  36. c = getch();
  37. if (c == 0x31 &amp;&amp; (head == LwLimit)){
  38. for(int i = 0; i &lt; count; i++)
  39. head = head-&gt;prev;
  40. id -= count;
  41. break;
  42. }
  43. if (c == 0x31 &amp;&amp; (head != UpLimit)){
  44. for(int i = 0; i &lt; count; i++)
  45. head = head-&gt;prev;
  46. id -= count;
  47. break;
  48. }
  49. else if(c == 0x32 || c == 27)
  50. break;
  51. }
  52. }
  53. while(c != 27);
  54. }

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

  1. if (c == 0x31 &amp;&amp; (head == hwnd-&gt;tail)){
  2. head = head-&gt;prev-&gt;prev;
  3. break;
  4. }

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

https://pastebin.com/J2bnc151

答案1

得分: 1

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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node_s { // 缩写的结构声明
  4. char *name;
  5. struct node_s *prev, *next;
  6. } node;
  7. const int pagesz = 2; // 一个“有效”的页面大小以适应
  8. void ShowList(node *head) {
  9. int id = 0;
  10. char c = 0;
  11. while (c != 27) { // 丑陋的“魔数”
  12. node *p = head; // 工作副本
  13. // 输出“页面”(无“cls”)
  14. puts(" ID NAME");
  15. for (int i = 0; i < pagesz && p; i++, p = p->next)
  16. printf(" %2d %s\n", id + i, p->name);
  17. while (1) {
  18. c = getchar();
  19. if (c == 27) // 再次,无意义的魔数
  20. break;
  21. if (c == '+') { // '+' == “前进”
  22. for (int i = 0; i < pagesz; i++)
  23. if (head->next)
  24. head = head->next, id++;
  25. break;
  26. }
  27. if (c == '-') { // '-' == “倒带”
  28. for (int i = 0; i < pagesz; i++)
  29. if (head->prev)
  30. head = head->prev, id--;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. int main() {
  37. node arr[] = {
  38. { "Grumpy 0"},
  39. { "Dopey 1" },
  40. { "Doc 2" },
  41. { "Sneezy 3" },
  42. { "Bashful 4" },
  43. { "Sleepy 5" },
  44. { "Happy 6" },
  45. };
  46. int i;
  47. for (i = 0; i < sizeof arr / sizeof arr[0]; i++)
  48. arr[i].prev = &arr[i - 1], arr[i].next = &arr[i + 1];
  49. arr[0].prev = arr[i - 1].next = NULL;
  50. ShowList(arr);
  51. return 0;
  52. }
  1. ID NAME
  2. 0 Grumpy 0
  3. 1 Dopey 1
  4. +
  5. ID NAME
  6. 2 Doc 2
  7. 3 Sneezy 3
  8. +
  9. ID NAME
  10. 4 Bashful 4
  11. 5 Sleepy 5
  12. -
  13. ID NAME
  14. 2 Doc 2
  15. 3 Sneezy 3
  16. +
  17. ID NAME
  18. 4 Bashful 4
  19. 5 Sleepy 5
  20. +
  21. ID NAME
  22. 6 Happy 6
  23. -
  24. ID NAME
  25. 4 Bashful 4
  26. 5 Sleepy 5

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

英文:

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

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. typedef struct node_s { // abbreviated struct declaration
  4. char *name;
  5. struct node_s *prev, *next;
  6. } node;
  7. const int pagesz = 2; // a &quot;working&quot; page size to adapt
  8. void ShowList( node *head ) {
  9. int id = 0;
  10. char c = 0;
  11. while( c != 27 ) { // UGLY &quot;magic number&quot;
  12. node *p = head; // a working copy
  13. // output the &quot;page&quot; (without &quot;cls&quot;)
  14. puts( &quot; ID NAME&quot; );
  15. for( int i = 0; i &lt; pagesz &amp;&amp; p; i++, p = p-&gt;next )
  16. printf(&quot; %2d %s\n&quot;, id + i, p-&gt;name );
  17. while( 1 ) {
  18. c = getchar();
  19. if( c == 27) // again, meaningless magic number
  20. break;
  21. if( c == &#39;+&#39; ) { // &#39;+&#39; == &quot;advance&quot;
  22. for( int i = 0; i &lt; pagesz; i++ )
  23. if( head-&gt;next )
  24. head = head-&gt;next, id++;
  25. break;
  26. }
  27. if( c == &#39;-&#39; ) { // &#39;-&#39; == &quot;rewind&quot;
  28. for( int i = 0; i &lt; pagesz; i++ )
  29. if( head-&gt;prev )
  30. head = head-&gt;prev, id--;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. int main() {
  37. node arr[] = {
  38. { &quot;Grumpy 0&quot;},
  39. { &quot;Dopey 1&quot; },
  40. { &quot;Doc 2&quot; },
  41. { &quot;Sneezy 3&quot; },
  42. { &quot;Bashful 4&quot; },
  43. { &quot;Sleepy 5&quot; },
  44. { &quot;Happy 6&quot; },
  45. };
  46. int i;
  47. for( i = 0; i &lt; sizeof arr/sizeof arr[0]; i++ )
  48. arr[i].prev = &amp;arr[i-1], arr[i].next = &amp;arr[i+1];
  49. arr[0].prev = arr[i-1].next = NULL;
  50. ShowList( arr );
  51. return 0;
  52. }
  1. ID NAME
  2. 0 Grumpy 0
  3. 1 Dopey 1
  4. +
  5. ID NAME
  6. 2 Doc 2
  7. 3 Sneezy 3
  8. +
  9. ID NAME
  10. 4 Bashful 4
  11. 5 Sleepy 5
  12. -
  13. ID NAME
  14. 2 Doc 2
  15. 3 Sneezy 3
  16. +
  17. ID NAME
  18. 4 Bashful 4
  19. 5 Sleepy 5
  20. +
  21. ID NAME
  22. 6 Happy 6
  23. -
  24. ID NAME
  25. 4 Bashful 4
  26. 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:

确定