【发布时间】:2016-11-21 18:41:59
【问题描述】:
目前正在处理我的项目,洗牌是我必须编写的最后一个函数,但我很困惑,我不知道如何
这里有人可以帮助我吗?还是谢谢!
这是我的课
// Creating a NODE Structure
struct node
{
int data;
struct node *next;
};
// Creating a class STACK
class tumpukan
{
struct node *top;
public:
tumpukan() // constructure
{
top=NULL;
}
void push(int x)
{
struct node *ptr;
ptr=new node;
ptr->data=x;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
}
int pop()
{
struct node *temp;
if(top==NULL)
{
printf("Tumpukan Kosong \n");
}
temp=top;
top=top->next;
return temp->data;
delete temp;
}
void show()
{
struct node *ptr1=top;
printf("isi tumpukan : \n");
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
printf("NULL \n");
}
};
感谢任何帮助!
【问题讨论】:
-
为什么要洗牌?堆栈的目的是保持 FILO 排序,洗牌会破坏这个目的。
-
请根据实际要求更具体。此外,您为什么要从头开始重新实现,而不是使用很容易成为语言一部分的概念?最后,您的实现仍然需要工作(例如,您的 pop 方法在打印“Empty Stack”后仍然会崩溃)
-
std::shuffleen.cppreference.com/w/cpp/algorithm/random_shuffle 怎么了?
标签: c++ linked-list shuffle