洗牌链表

时间:2016-11-21 18:42:00

标签: c++ linked-list shuffle

目前正在处理我的项目,而且洗牌是我必须写的最后一个功能,但我很困惑,我不知道如何

这里的任何人都可以帮助我吗?不管怎样,谢谢!

所以这是我的班级

//   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");
}

};

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

你可以创建一个包含所有节点地址的向量,称之为V,并创建一组从1到N的索引(N是节点数),称之为I.创建一个空栈S. 现在:(伪码)

for i in range 1 to V.size():
   index=I.pick_at_rand() // you can easily find these sort of functions
   I.delete(index)
   if(S.top!=NULL):
      V[i].next=S.top
      S.push(V[index])
   else:
      S.push(V[index])