C++堆栈和二维数组

发布于 2025-01-07 17:12:26 字数 1566 浏览 5 评论 0原文

我一直在开发一个项目,该项目应该通过使用堆栈的二维数组来模拟链接列表。我有代码,但我不知道如何使随机数起作用。我在网上查看过,但网上没有解释如何使随机函数与模拟一起工作。下面是我的代码:

`#include <iostream>

#include <stdlib.h>
using namespace std;

int myTop = -1;
int index = -1;
int next = -1;
int tt[25][2]; 
void construct()
{
    tt[25][2];
}

void empty()
{
    if (myTop == -1)
        cout << "Empty Stack";
    else
        cout << "Full Stack";
}

void push(int x)
{
    if (myTop < 24)
    {   
        myTop++;
        tt[myTop] = x;  
    }
    else
        cout << "The stack is full.";       
}

void top()
{
    if (myTop != -1)
        cout << "Top Value is: " << tt[myTop];
    else
        cout << "Empty Stack";

}

int pop()
{
    int x;
         if(myTop<=0)
         {
                cout<<"stack is empty"<<endl;
                return 0;
         }
         else
         {
                x=tt[myTop];
                myTop--;
          }
          return(x);

}

void display()
{
    for (int row=0; row<25; row++)
    {
        for (int column=0; column<3; column++)
        {
            cout << tt[row][column] << "\t";
            if (column == 2)
                cout << endl;
        }
    }
    cout << endl;
}

int main()
{
    push(rand() % 25);
    display();
    push(rand() % 25);
    display();
    push(rand() % 25);
    display();
    push(rand() % 25);
    display();
    top();
    pop();
    display();
    top();
    pop();
    display();
    top();
    pop();
    display();
    }

I have been working on a Project that is suppose to simulate link list by using 2D array for stacks. I have the code but I can not figure out how to make the Random numbers work. I have looked online but online doesn't explain how to make the random function work with the simulation. Here is my code below:

`#include <iostream>

#include <stdlib.h>
using namespace std;

int myTop = -1;
int index = -1;
int next = -1;
int tt[25][2]; 
void construct()
{
    tt[25][2];
}

void empty()
{
    if (myTop == -1)
        cout << "Empty Stack";
    else
        cout << "Full Stack";
}

void push(int x)
{
    if (myTop < 24)
    {   
        myTop++;
        tt[myTop] = x;  
    }
    else
        cout << "The stack is full.";       
}

void top()
{
    if (myTop != -1)
        cout << "Top Value is: " << tt[myTop];
    else
        cout << "Empty Stack";

}

int pop()
{
    int x;
         if(myTop<=0)
         {
                cout<<"stack is empty"<<endl;
                return 0;
         }
         else
         {
                x=tt[myTop];
                myTop--;
          }
          return(x);

}

void display()
{
    for (int row=0; row<25; row++)
    {
        for (int column=0; column<3; column++)
        {
            cout << tt[row][column] << "\t";
            if (column == 2)
                cout << endl;
        }
    }
    cout << endl;
}

int main()
{
    push(rand() % 25);
    display();
    push(rand() % 25);
    display();
    push(rand() % 25);
    display();
    push(rand() % 25);
    display();
    top();
    pop();
    display();
    top();
    pop();
    display();
    top();
    pop();
    display();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅忆流年 2025-01-14 17:12:26

您尚未初始化随机数生成器(这称为“播种”)。

将以下内容添加到您的代码中。

#include <time.h>

srand (time(0));

另一方面,我更喜欢使用 ctimecstdlib 因为它们是 C++ 标头(尽管这可以争论)。另外,如果您可以访问最新的编译器,请查看 random 标头。

You haven`t initialized the random number generator (this is called "seeding").

Add the following to your code.

#include <time.h>

srand (time(0));

And on another note, I prefer using ctime and cstdlib as those are c++ headers (although this can be debated). Also, look into the random header if you have access to an up-to-date compiler.

_畞蕅 2025-01-14 17:12:26

下面是如何在 C++ 中使用随机数

#include <cstdlib>
#include <ctime>

int main ()
{
  srand(time(NULL));
  number = rand() % 10 + 1; # Random number between 1 and 10
}

Here is how to use random numbers in C++

#include <cstdlib>
#include <ctime>

int main ()
{
  srand(time(NULL));
  number = rand() % 10 + 1; # Random number between 1 and 10
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文