头文件
#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";
}