C++实现栈和队列

using namespace std;
#include<iostream>
#define Maxsize 100  //顺序栈初始分配量
#define OK 1
#define ERROR -1;
typedef struct {
    int* base;//本题要求整数,可以更换为其他类型
    int* top;
    int stacksize;
}Sqtack;
//顺序栈的初始化
int InitStack(Sqtack& S)
{  
    S.base = new int[Maxsize];
    if (!S.base)
    {
        cout << "分配失败!";
        return ERROR;
    }
    S.top = S.base;  //置空栈
    S.stacksize = Maxsize; //设置最大空间
    cout << "顺序栈初始化成功!" << endl;
    return OK; 
}
//进栈操作
int push(Sqtack& S, int e)
{
    if (S.top == S.base + S.stacksize)
    {
        cout << "栈满!";
        return  ERROR;
    }
    *S.top++ = e;
    return OK;
}
//出栈操作
int pop(Sqtack& S)
{
    if (S.top == S.base)
    {
        cout << "栈空!";
        return ERROR;
    }

    cout << "出栈元素为" << *--S.top << endl;
    return OK;
}
int main()
{
    int n1, n2, n3, n4 = 0;
    cout << "请按任意键初始化栈!"<<endl;
    system("pause");
    Sqtack a;
    InitStack(a);
    cout << "请输入进栈元素的个数!" << endl;
    cin >> n1;
    for (int e1 = 0;e1 < n1;e1++)
    {
        cout << "请输入第" << e1 + 1 << "个进栈元素!" << endl;
        cin >> n2;
        push(a, n2);
    }
    cout << "请输入出栈元素的个数!" << endl;
    cin >> n3;
    for (int e2 = 0;e2 < n3;e2++)
    {
        pop(a);
    }
    return 0;
 }

C++实现栈

using namespace std;
#include<iostream>
#define Maxsize 100
#define error -1
#define ok 1
typedef struct {
	int* base;
	int head;
	int tail;
}Sqque;
//初始化队列 
  void Initsq(Sqque& s)
{
	s.base = new int[Maxsize];
	s.head = s.tail = 0;
	cout << "队列初始化成功!" << endl;
}
//实现入队操作
int push(Sqque& s, int e)
{
	if ((s.tail + 1) % Maxsize == s.head)//队尾指针在循环意义上加一等于队头指针
	{
		cout << "队满!" << endl;
		return error;
	}
	s.base[s.tail] = e;
	s.tail = (s.tail + 1) % Maxsize;
	return ok;
}
//实现出队操作
int pop(Sqque& s)
{
	if (s.head == s.tail)
	{
		cout << "队空!" << endl;
		return error;
	}
	cout << "出队元素为" << s.base[s.head];
	s.head = (s.head + 1) % Maxsize;//循环意义
	return ok;
}
//获取队头元素
void getelem(Sqque s, int& e)//这里应该不修改队列,并且用e来接受队头元素
{
	if (s.head != s.tail) {
		e = s.base[s.head];
	}
	else
		cout << "队列为空!" << endl;
	system("pause");
	system("cls");
}
int isexit(Sqque s)
{
	if (s.head == s.tail)
	{
		cout << "队列为空!" << endl;
		system("pause");
		system("cls");
		return ok;
	}
	else
	{
		cout << "队列非空!" << endl;
		system("pause");
		system("cls");
		return error;
	}

}
//清空队列
void clearsq(Sqque& s)
{
	s.head = s.tail;
	s.base = NULL;
	system("pause");
	system("cls");
}
int main()
{
	int e1, e2;
	int s1, s2;
	Sqque a;
	Initsq(a);
	cout << "请输入进队元素的个数!" << endl;
	cin >> e1;
	for (int n1 = 0;n1 < e1;n1++)
	{
		cout << "请输入第" << n1 + 1 << "个进队元素" << endl;
		cin >> s1;
		push(a,s1);
	}
	cout << "请输入出队元素的个数!" << endl;
	cin >> e2;
	for (int n2 = 0;n2 < e2;n2++)
	{
		pop(a);
		cout << endl;
	}

	while (true)
	{
		cout << "请选择功能!" << endl;
		cout << "1、取得队头元素" << endl;
		cout << "2、判断队列是否为空" << endl;
		cout << "3、清空队列" << endl;
		cout << "4、退出菜单" << endl;

		int select = 0;
		cin >> select;
		switch (select)
		{
		case 1:
			getelem(a, s2);
			break;
		case 2:
			isexit(a);
			break;
		case 3:
			clearsq(a);
			break;
		case 4:
			cout << "欢迎您的下次使用!" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	system("pause");
	return 0;
}

C++实现队列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值