![](/img/trans.png)
[英]I am trying to implement a stack using two Queues but my "push" operation is problematic although the code seems to be fine
[英]Trying to get rid of repeated indexes in my stack push function. I am using a 2d array to simulate a linked list
我的堆棧將打印出25個隨機索引,包括一些重復的索引,我正嘗試解決刪除任何重復索引的問題,這是我嘗試解決的問題。
void Push(int value)
{
//check if stack is full
if (myTop == Capacity - 1)
{
std::cout << "The stack is full" << std::endl;
}
else
{
for (int i = 0; i < Capacity; i++)
{
Array[myTop] = rand() % Capacity; //generates 25 random indexes
//if i have some indexes that repeat
if (Array[myTop]== Array[myTop])
{
Array[myTop] = rand() % Capacity;
}
std::cout << Array[myTop]<<" #"<<i+1 << std::endl;//prints those 25 random indexes
}
std::cout << myTop <<": random index"<<std::endl; //prints out the random index my top is stored in
std::cout << value <<": value user entered"<<std::endl;//prints out value user wants to push
}
}
在使用rand()之前,首先需要用種子初始化它,以便在每個會話中可以得到不同的結果。 帶時間播種是個不錯的選擇。 請包括“ #include”標頭並輸入“ srand(time(NULL));” 在代碼中的以下行之前:
Array [myTop] = rand()%容量; //產生25個隨機索引
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.