C++堆栈和二维数组
我一直在开发一个项目,该项目应该通过使用堆栈的二维数组来模拟链接列表。我有代码,但我不知道如何使随机数起作用。我在网上查看过,但网上没有解释如何使随机函数与模拟一起工作。下面是我的代码:
`#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未初始化随机数生成器(这称为“播种”)。
将以下内容添加到您的代码中。
另一方面,我更喜欢使用
ctime
和cstdlib
因为它们是 C++ 标头(尽管这可以争论)。另外,如果您可以访问最新的编译器,请查看random
标头。You haven`t initialized the random number generator (this is called "seeding").
Add the following to your code.
And on another note, I prefer using
ctime
andcstdlib
as those are c++ headers (although this can be debated). Also, look into therandom
header if you have access to an up-to-date compiler.兰特():
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
斯兰德():
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
Rand():
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Srand():
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
下面是如何在 C++ 中使用随机数
Here is how to use random numbers in C++