Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序,用于移动使用链表构建的堆栈中的项_C_Linked List_Stack - Fatal编程技术网

C程序,用于移动使用链表构建的堆栈中的项

C程序,用于移动使用链表构建的堆栈中的项,c,linked-list,stack,C,Linked List,Stack,我试图滚动实现为链表的堆栈的前3个元素。例如,如果堆栈在滚动之前为:1 2 3 4 5。完成滚动功能后,应为:3 1 2 4 5。第三项进入堆栈顶部,其他所有项在堆栈中向下移动一个位置。以下是我一直在玩的滚动功能: // roll function rolls the top 3 numbers on the stack struct item *roll(struct item *top){ struct item *cur = top; struct item

我试图滚动实现为链表的堆栈的前3个元素。例如,如果堆栈在滚动之前为:1 2 3 4 5。完成滚动功能后,应为:3 1 2 4 5。第三项进入堆栈顶部,其他所有项在堆栈中向下移动一个位置。以下是我一直在玩的滚动功能:

// roll function rolls the top 3 numbers on the stack
struct item *roll(struct item *top){
        struct item *cur = top;
        struct item *prev = NULL;
        int i = 0;
        // get cur and prev to point to proper positions in linked list
        for(i = 0; i < 3; i++){
                prev = cur;
                cur = cur->next;
        }
        // roll the linked list
        prev->next = cur->next;
        cur->next = top;
        top = cur;

        return top;
}
//roll函数滚动堆栈上的前3个数字
结构项目*滚动(结构项目*顶部){
结构项*cur=顶部;
结构项*prev=NULL;
int i=0;
//让cur和prev指向链接列表中的正确位置
对于(i=0;i<3;i++){
prev=cur;
cur=cur->next;
}
//滚动链接列表
上一步->下一步=当前->下一步;
cur->next=顶部;
top=cur;
返回顶部;
}

您的循环应该运行n-1次

struct item *roll(struct item *top){
    struct item *cur = top;
    struct item *prev = NULL;
    int i = 0;
    // get cur and prev to point to proper positions in linked list
    for(i = 0; i < 2; i++){
            prev = cur;
            cur = cur->next;
    }
    // roll the linked list
    prev->next = cur->next;
    cur->next = top;
    top = cur;

    return top;
}
void print()
{
struct item *temp = roll(top);
  while(temp!=NULL)
  {
    cout<<temp->value;
    temp=temp->next;
  }
}

你的代码有什么问题?你忘了问问题。当我运行它时,它不能正常工作。结果是链表的顺序相同,但缺少最后一项。我想要一些我可能出错的地方的指针。弹出前三个元素。然后按第二个、第一个和第三个。您的数据结构是一个循环链表还是一个底部元素具有
的实际堆栈。next==NULL
?不确定为什么,但通过此更改,“我的代码”仍然会出现与以前相同的问题,您在创建或打印时犯了错误。上传你的全部代码。
31245