C 如何使用单循环打印链表的最后三个元素下面是我的代码,我使用了两个循环?

C 如何使用单循环打印链表的最后三个元素下面是我的代码,我使用了两个循环?,c,singly-linked-list,C,Singly Linked List,如何使用下面的单循环打印链表的所有最后三个元素是我使用了两个循环的代码?我需要使用单循环打印最后3个元素。请不要将其标记为重复,因为这个问题已经提出,但答案是使用双循环。谢谢 例子 如果输入为1234567,则o/p应为321 #include <stdio.h> #include <string.h> #include<stdlib.h> struct node { int data; struct node

如何使用下面的单循环打印链表的所有最后三个元素是我使用了两个循环的代码?我需要使用单循环打印最后3个元素。请不要将其标记为重复,因为这个问题已经提出,但答案是使用双循环。谢谢 例子 如果输入为1234567,则o/p应为321

   #include <stdio.h> 
   #include <string.h>
   #include<stdlib.h>
   struct node
   {
    int data;
    struct node *next;
   };

   void add_begin(struct node **ptr,int n)
{
    struct node *temp;
    temp=(struct node* )malloc(sizeof(struct node));
    temp->data=n;
    temp->next=*ptr;
    *ptr=temp;
}

 void display(struct node *ptr)
{
   struct node *prevprev,*prev;
   prevprev=prev=NULL;
   while(ptr->next!=NULL) // here i am getting 3rd last element which is store prevprev
  {
     prevprev=prev;
     prev=ptr;
     ptr=ptr->next;
  }

   while(prevprev)    // from prevprev (i.e last 3rd element ) i m printing till null 
                     //i need to remove this loop and develop a logic in above loop?
  {                  // i am supposed to use single loop?
    printf("%d\n",prevprev->data);
    prevprev=prevprev->next;
  } 
}

main()
{
 struct node *headptr=NULL;
 add_begin(&headptr,1);
 add_begin(&headptr,2);
 add_begin(&headptr,3);
 add_begin(&headptr,4);
 add_begin(&headptr,5);
 add_begin(&headptr,6);
 add_begin(&headptr,7);
 display(headptr);

}
#包括
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
void add_begin(结构节点**ptr,int n)
{
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=n;
温度->下一步=*ptr;
*ptr=温度;
}
无效显示(结构节点*ptr)
{
结构节点*prev,*prev;
prev=prev=NULL;
while(ptr->next!=NULL)//这里我得到最后三个元素,即store prev
{
prev=prev;
prev=ptr;
ptr=ptr->next;
}
while(prev)//从prev(即最后的第三个元素)开始,我一直打印到null
//我需要删除这个循环并在上面的循环中开发一个逻辑?
{//我应该使用单循环吗?
printf(“%d\n”,prev->data);
prev=prev->next;
} 
}
main()
{
结构节点*headptr=NULL;
添加开始(&headptr,1);
添加_begin(&headptr,2);
添加_begin(&headptr,3);
添加_begin(&headptr,4);
添加_begin(&headptr,5);
添加开始(&headptr,6);
添加开始(&headptr,7);
显示器(headptr);
}

删除第二个循环,因为您可以一次性打印三个值。因此,改变这一点:

   while(prevprev)    // from prevprev (i.e last 3rd element ) i m printing till null 
                     //i need to remove this loop and develop a logic in above loop?
  {                  // i am supposed to use single loop?
    printf("%d\n",prevprev->data);
    prevprev=prevprev->next;
  } 


在列表中运行两个由三个节点偏移的指针。第一个指针到达终点后,开始打印第二个指针指向的节点。

void display(struct node*ptr){
void display(struct node *ptr){
    struct node x1, x2, x3, *curr;
    curr = &x1;
    x1.next = &x2;
    x2.next = &x3;
    x3.next = &x1;
    while(ptr!=NULL){
        curr->data = ptr->data;
        curr = curr->next;
        ptr  = ptr->next;
    }
    for(int i=0; i<3 ;++i, curr=curr->next )
        printf("%d\n", curr->data);
}
结构节点x1,x2,x3,*curr; curr=&x1; x1.next=&x2; x2.next=&x3; x3.next=&x1; while(ptr!=NULL){ 当前->数据=ptr->数据; 当前=当前->下一步; ptr=ptr->next; } 用于(整数i=0;不精确) printf(“%d\n”,当前->数据); }
示例如果输入是
1 2 3 4 5 6 7
o/p应该是
3 2 1
。很抱歉,我应该只使用单循环,上面的代码用于循环。@user3784175我们过去通过在下一个连接指定的方式。例如
printf(“%d\n%d\n%d\n”,当前->数据,当前->下一步->数据,当前->下一步->下一步->数据)
void display(struct node *ptr){
    struct node x1, x2, x3, *curr;
    curr = &x1;
    x1.next = &x2;
    x2.next = &x3;
    x3.next = &x1;
    while(ptr!=NULL){
        curr->data = ptr->data;
        curr = curr->next;
        ptr  = ptr->next;
    }
    for(int i=0; i<3 ;++i, curr=curr->next )
        printf("%d\n", curr->data);
}