这里给大家分享一道栈的练习和一道队列的练习!
利用栈的基本操作实现将任何一个十进制整数转化为R进制整数。
Sqstack::Sqstack()
{
base = top = new SElemType[InitStacksize];
stacksize = InitStacksize;
}
void Sqstack::push(SElemType e)
{
if (top - base == stacksize)
{
SElemType* base1 = new SElemType[stacksize + increment];
int i = 0;
for (i = 0; i < InitStacksize; i++)
{
base1[i] = base[i];
}
delete[]base;
base = base1;
top = base + stacksize;
stacksize += increment;
}
*top++ = e;
}
SElemType Sqstack::pop()
{
if (base == top)
throw runtime_error("栈为空!");
return *(--top);
}
bool Sqstack::isEmpty()
{
if (base == top)
return 1;
else
return 0;
}
~Sqstack()
{
delete[]base;
stacksize = 0;
}
void Sqstack::conversion()
{
long int n = 0;
cout << "请输入十进制数:";
cin >> n;
SElemType b = 0;
cout << "请输入要转换的进制:";
cin >> b;
Sqstack s;
SElemType e;
char a = 'A';
while (n)
{
e = n % b;
s.push(e);
n /= b;
}
while (!s.isEmpty())
{
e = s.pop();
if (e >= 10)
{
a = 'A';
a = a + e - 10;
cout << a;
}
else
{
cout << e;
}
}
cout << endl;
}
bool Sqstack::isEmpty()
{
if (base == top)
return 1;
else
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
using namespace std;
#define InitStacksize 100
#define increment 10
//利用栈的基本操作实现将任何一个十进制整数转化为R进制整数
typedef int SElemType;
class Sqstack
{
private:
SElemType* base;//栈底指针
SElemType* top;//栈顶指针
int stacksize;//栈容量
public:
Sqstack();
~Sqstack()
{
delete[]base;
stacksize = 0;
}
void push(SElemType e);
SElemType pop();
void conversion();
bool isEmpty();
};
Sqstack::Sqstack()
{
base = top = new SElemType[InitStacksize];
stacksize = InitStacksize;
}
void Sqstack::push(SElemType e)
{
if (top - base == stacksize)
{
SElemType* base1 = new SElemType[stacksize + increment];
int i = 0;
for (i = 0; i < InitStacksize; i++)
{
base1[i] = base[i];
}
delete[]base;
base = base1;
top = base + stacksize;
stacksize += increment;
}
*top++ = e;
}
SElemType Sqstack::pop()
{
if (base == top)
throw runtime_error("栈为空!");
return *(--top);
}
bool Sqstack::isEmpty()
{
if (base == top)
return 1;
else
return 0;
}
void Sqstack::conversion()
{
long int n = 0;
cout << "请输入十进制数:";
cin >> n;
SElemType b = 0;
cout << "请输入要转换的进制:";
cin >> b;
Sqstack s;
SElemType e;
char a = 'A';
while (n)
{
e = n % b;
s.push(e);
n /= b;
}
while (!s.isEmpty())
{
e = s.pop();
if (e >= 10)
{
a = 'A';
a = a + e - 10;
cout << a;
}
else
{
cout << e;
}
}
cout << endl;
}
int main()
{
Sqstack a;
a.conversion();
return 0;
}
利用循环队列实现.约瑟夫环问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。
从编号为k的人开始报数,数到k的那个人出圈;他的下一个人又从1开始报数,数到k的那个人出圈;
依此规律重复下去,直到圆桌周围的人只剩最后一个。模拟该游戏,并输出出圈顺序。
SqQueue::SqQueue()
{
base = new QElemType[QueueSize];
front = rear = 0;
}
~SqQueue()
{
delete[]base;
front = rear = 0;
}
void SqQueue::EnQueueu(QElemType e)
{
if (front == (rear + 1) % QueueSize)
return;
else
{
base[rear] = e;
rear = (rear + 1) % QueueSize;
}
}
void SqQueue::DeQueue()
{
QElemType e;
if (rear == front)
return;
else
{
e = base[front];
front = (front + 1) % QueueSize;
}
}
QElemType SqQueue::IsEmpty()
{
if (front == rear)
return 1;
else
return 0;
}
QElemType SqQueue::length()
{
return (rear - front + QueueSize) % QueueSize;
}
QElemType SqQueue::GetHead()
{
if (front == rear)
return 0;
else
return base[front];
}
void SqQueue::yuesefu()
{
int n = 0, k = 0;
cout << "输入人数: ";
cin >> n;
cout << "输入第几个人出局:";
cin >> k;
SqQueue S;
int i = 0;
for (i = 1; i <= n; i++)
{
S.EnQueueu(i);
}
for (i = 1; i < k; i++)
{
QElemType temp = S.base[S.front];
S.DeQueue();
S.EnQueueu(temp);
}
int count = 1;
while (!S.IsEmpty())
{
if (count < k)
{
QElemType temp = S.base[S.front];
S.DeQueue();
S.EnQueueu(temp);
count++;
}
if (count == k)
{
cout << S.GetHead() << " ";
S.DeQueue();
count = 1;
}
}
cout << endl;
}
/*利用循环队列实现.约瑟夫环问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。
从编号为k的人开始报数,数到k的那个人出圈;他的下一个人又从1开始报数,数到k的那个人出圈;
依此规律重复下去,直到圆桌周围的人只剩最后一个。模拟该游戏,并输出出圈顺序。*/
#define _CRT_SECURE_NO_WARNINGS 1
#define QueueSize 100
#include
using namespace std;
typedef int QElemType;
class SqQueue
{
private:
QElemType* base;
int front;
int rear;
public:
SqQueue();
~SqQueue()
{
delete[]base;
front = rear = 0;
}
void EnQueueu(QElemType e);
void DeQueue();
QElemType IsEmpty();
void yuesefu();
QElemType GetHead();
};
SqQueue::SqQueue()
{
base = new QElemType[QueueSize];
front = rear = 0;
}
void SqQueue::EnQueueu(QElemType e)
{
if (front == (rear + 1) % QueueSize)
return;
else
{
base[rear] = e;
rear = (rear + 1) % QueueSize;
}
}
void SqQueue::DeQueue()
{
QElemType e;
if (rear == front)
return;
else
{
e = base[front];
front = (front + 1) % QueueSize;
}
}
QElemType SqQueue::IsEmpty()
{
if (front == rear)
return 1;
else
return 0;
}
QElemType SqQueue::GetHead()
{
if (front == rear)
return 0;
else
return base[front];
}
void SqQueue::yuesefu()
{
int n = 0, k = 0;
cout << "输入人数: ";
cin >> n;
cout << "输入第几个人出局:";
cin >> k;
SqQueue S;
int i = 0;
for (i = 1; i <= n; i++)
{
S.EnQueueu(i);
}
for (i = 1; i < k; i++)
{
QElemType temp = S.base[S.front];
S.DeQueue();
S.EnQueueu(temp);
}
int count = 1;
while (!S.IsEmpty())
{
if (count < k)
{
QElemType temp = S.base[S.front];
S.DeQueue();
S.EnQueueu(temp);
count++;
}
if (count == k)
{
cout << S.GetHead() << " ";
S.DeQueue();
count = 1;
}
}
cout << endl;
}
int main()
{
SqQueue S;
S.yuesefu();
return 0;
}
好啦,关于栈和队列的小练习到这里就结束啦,后期会继续更新数据结构与算法的相关知识,欢迎大家持续关注、点赞和评论!❤️❤️❤️