数据结构之栈(参考王道书25数据结构)

头文件

#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <stdbool.h>
#include <algorithm>
#include <math.h>
#pragma warning(disable:4996)//忽略scanf,printf的检查
#pragma warning(disable:6011)//忽略空指针的赋值警告
using namespace std;
#define MaxSize 100

结构体定义

typedef struct {
	int data[MaxSize];
	int top;


}SqStack;

//用于05题
typedef struct {
	int stack[MaxSize];
	int top[2];
}stk;
stk s;

相关函数

void InitStack(SqStack& s)
{
	s.top = -1;
}
bool push(SqStack& s, int x)
{
	if (s.top == MaxSize - 1)
		return false;
	/*s.top = s.top + 1;
	s.data[s.top] = x;*/
	s.data[++s.top] = x;
	return true;

}
bool pop(SqStack& s, int x)
{
	if (s.top ==- 1)
		return false;
	/* x=s.data[s.top];
	* s.top=s.top-1;
	*/
	 x=s.data[s.top--] ;
	return true;

}
bool Gettop(SqStack& s, int x)
{
	if (s.top == -1)
		return false;
	x = s.data[s.top];
	return true;

}
bool StackEmpty(SqStack s)
{
	if (s.top == -1)
		return true;
	else return false;



}

课后题目

03

bool p03_2(char a[])
{
	int pushnum = 0;
	int popnum = 0;
	for (int i = 0;a[i] != '\0';i++)
	{
		if (a[i] == 'I')
		{
			pushnum++; continue;
		}
		if (a[i] == 'O')
		{
			popnum++;
			if(popnum>pushnum)
			{
				cout << "ERROR1";
				exit(-1);

			}
		}
	if(popnum!=pushnum)
	{
		cout << "ERROR2";
		exit(0);

	}
	return true;



}

}

04

bool p04(LinkList l, int n)
{
	int i;
	LNode* p = l->Next;
	char *s=new char[n/2] ;
	for ( i = 0;i < n / 2;i++)
	{
		s[i] = p->data;
		p = p->Next;

	}
	i--;
	if (n % 2 == 1)
		p = p->Next;
	while (p != NULL && s[i] == p->data)
	{
		i--;
		p = p->Next;

	}
	if (i == -1)return true;
	else return false;
}

05

typedef struct {
	int stack[MaxSize];
	int top[2];
}stk;
stk s;

int stkpush(int i, int x)
{
	if (i < 0 || i>1) {
		printf("error");
		exit(0);
	}
	if(s.top[1]-s.top[0]==1)
	{
		printf("full");
		return 0;
	}
	switch (i)
	{
	case 0:s.stack[++s.top[0]] = x; return 1; break;
	case 1:s.stack[--s.top[1]] = x;return 1;


	}

}
int stkpop(int i)
{
	if (i < 0 || i>1)
	{
		printf("error");
		exit(0);
	
	}switch (i)
	{
	case 0:if (s.top[0] == -1) return -1; else return s.stack[s.top[0]--];  break;
	case 1:if (s.top[1] == MaxSize) return -1; else return s.stack[s.top[1]++];  



}

 }

全文

// WangDao_Stack_queue.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <stdbool.h>
#include <algorithm>
#include <math.h>
#pragma warning(disable:4996)//忽略scanf,printf的检查
#pragma warning(disable:6011)//忽略空指针的赋值警告
using namespace std;
#define MaxSize 100
typedef struct {
	int data[MaxSize];
	int top;


}SqStack;
void InitStack(SqStack& s)
{
	s.top = -1;
}
bool push(SqStack& s, int x)
{
	if (s.top == MaxSize - 1)
		return false;
	/*s.top = s.top + 1;
	s.data[s.top] = x;*/
	s.data[++s.top] = x;
	return true;

}
bool pop(SqStack& s, int x)
{
	if (s.top ==- 1)
		return false;
	/* x=s.data[s.top];
	* s.top=s.top-1;
	*/
	 x=s.data[s.top--] ;
	return true;

}
bool Gettop(SqStack& s, int x)
{
	if (s.top == -1)
		return false;
	x = s.data[s.top];
	return true;

}
bool p03_2(char a[])
{
	int pushnum = 0;
	int popnum = 0;
	for (int i = 0;a[i] != '\0';i++)
	{
		if (a[i] == 'I')
		{
			pushnum++; continue;
		}
		if (a[i] == 'O')
		{
			popnum++;
			if(popnum>pushnum)
			{
				cout << "ERROR1";
				exit(-1);

			}
		}
	if(popnum!=pushnum)
	{
		cout << "ERROR2";
		exit(0);

	}
	return true;



}

}
typedef struct LNode {

	char data;
	struct LNode* Next;

}LNode, * LinkList;

bool p04(LinkList l, int n)
{
	int i;
	LNode* p = l->Next;
	char *s=new char[n/2] ;
	for ( i = 0;i < n / 2;i++)
	{
		s[i] = p->data;
		p = p->Next;

	}
	i--;
	if (n % 2 == 1)
		p = p->Next;
	while (p != NULL && s[i] == p->data)
	{
		i--;
		p = p->Next;

	}
	if (i == -1)return true;
	else return false;
}

typedef struct {
	int stack[MaxSize];
	int top[2];
}stk;
stk s;

int stkpush(int i, int x)
{
	if (i < 0 || i>1) {
		printf("error");
		exit(0);
	}
	if(s.top[1]-s.top[0]==1)
	{
		printf("full");
		return 0;
	}
	switch (i)
	{
	case 0:s.stack[++s.top[0]] = x; return 1; break;
	case 1:s.stack[--s.top[1]] = x;return 1;


	}

}
int stkpop(int i)
{
	if (i < 0 || i>1)
	{
		printf("error");
		exit(0);
	
	}switch (i)
	{
	case 0:if (s.top[0] == -1) return -1; else return s.stack[s.top[0]--];  break;
	case 1:if (s.top[1] == MaxSize) return -1; else return s.stack[s.top[1]++];  



}

 }
int main()
{
   cout << "Hello World!\n";
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值