今天360行~~~
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<Windows.h>
#define MAXSIZE 30
#define ElemType char
//栈结点说明
typedef struct SqStack {
ElemType date[MAXSIZE];
int top;
};
//共享栈结点说明
typedef struct DStack {
ElemType date[MAXSIZE];
int top1, top2;
};
//栈的创建(进栈)+栈的初始化(注:输入的内容仅为int型)
int CreatStack01(SqStack*& s,int a[],int n) {
s = (SqStack*)malloc(sizeof(SqStack));
s->top = -1;
SqStack* q;
for (int i = 0; i < n; ++i) {
if (s->top >= MAXSIZE) {
printf("栈中已经满员了\n");
return 0;
}
//q = (SqStack*)malloc(sizeof(SqStack));
s->date[i] = a[i];
++s->top;
}
return 0;
}
int CreatStack(SqStack*& s) {
s = (SqStack*)malloc(sizeof(SqStack));
s->top = -1;
SqStack* q;
printf("请输入先要入栈元素的数量:\n");
int n;
scanf("%d", &n);
getchar();
printf("开始输入\n");
for (int i = 0; i < n; ++i) {
if (s->top >= MAXSIZE) {
printf("栈中已经满员了\n");
return 0;
}
//q = (SqStack*)malloc(sizeof(SqStack));
scanf("%d",&s->date[i]);
++s->top;
}
return 0;
}
//栈的初始化
void InitStack(SqStack*& s) {
s = (SqStack*)malloc(sizeof(SqStack));
s->top = -1;
}
void InitStack2(DStack*& s) {
s = (DStack*)malloc(sizeof(DStack));
s->top1 = -1;
s->top2 = MAXSIZE;
}
//判断栈空
bool StackEmpty(SqStack*& s) {
if (s->top == -1) {
return true;
}
else {
return false;
}
return true;
}
bool StackEmpty2(DStack*& s) {
if (s->top1 == -1) {
return true;
}
else {
return false;
}
return true;
}
//进栈
int PushStack(SqStack*& s,ElemType e) {
//元素'e'进栈之前先判断栈是不是满的
if (s->top == MAXSIZE - 1) {
printf("栈是满的,无法进栈\n");
return 0;
}
s->top++;
s->date[s->top] = e;
return 0;;
}
int PushStack01(SqStack*& s, ElemType e[],int k) {
//元素'e'进栈之前先判断栈是不是满的
if (s->top == MAXSIZE - 1) {
printf("栈是满的,无法进栈\n");
return 0;
}
for (int i = 0; i <k ; i++) {
s->top++;
s->date[s->top] = e[i];
}
return 0;;
}
int PushStack2(DStack*& s, ElemType e) {
//元素'e'进栈之前先判断栈是不是满的
if (s->top2-1 == s->top1) {
printf("栈是满的,无法进栈\n");
return 0;
}
if (s->top1<= MAXSIZE - 1) {
s->top1++;
s->date[s->top1] = e;
}
else {
s->top2--;
s->date[s->top2] = e;
}
//s->top1++;
//s->date[s->top] = e;
return 0;;
}
int PushStack21(DStack*& s, ElemType e[], int k) {
//元素'e'进栈之前先判断栈是不是满的
if (s->top2 - 1 == s->top1) {
printf("栈是满的,无法进栈\n");
return 0;
}
for (int i = 0; i < k; i++) {
if (s->top1 <= MAXSIZE - 1) {
s->top1++;
s->date[s->top1] = e[i];
}
else {
s->top2--;
s->date[s->top2] = e[i];
}
//s->top++;
//s->date[s->top] = e[i];
}
return 0;;
}
//输出栈
bool PopStack(SqStack*& s,ElemType &e) {
//元素'e'出栈时,先考虑栈是不是空的
if (s->top == -1) {
printf("栈是空的,无法输出\n");
return false;
}
e = s->date[s->top];
s->top--;
return true;
}
bool PopStack2(DStack*& s, ElemType& e) {
//元素'e'出栈时,先考虑栈是不是空的
if (s->top1 == -1) {
printf("栈是空的,无法输出\n");
return false;
}
if (s->top2 < MAXSIZE-1) {
e = s->date[s->top1];
s->top1--;
}
else {
e = s->date[s->top1];
s->top1++;
}
return true;
}
//取栈顶元素
bool GetTop(SqStack*& s,ElemType &e) {
//取元素之前先判空
if (StackEmpty(s) == -1) {
return false;
}
e = s->date[s->top];
return true;
}
//销毁栈
void DestroyStack(SqStack*& s) {
free(s);
}
//练习----设计一个算法利用顺序串判断一个字符串是否为对称串(注意是对称相等)
bool symmertry(ElemType s[],int k) {
SqStack* q;
//q = w = s;
//先初始化栈---
InitStack(q);
//进栈
PushStack01(q, s, k);
/*
for (int a=0; s[a] != '\n';a++)
{
//printf("请输入第%d个元素:",a);
//ElemType e;
//scanf("%c", &i);
}
*/
//判断相等
for (int a=-1; q->top-1 != -1;++a, q->top--) {
if (q->date[a + 1] != q->date[q->top-1]) {
DestroyStack(q);
return false;
}
}
DestroyStack(q);
return true;
}
int main() {
SqStack* s;
DStack* d;
int choose;
ElemType e,q;
/*
InitStack2(d);
StackEmpty2(d);
printf("请输入你想要进栈的元素:\n");
scanf("%d", &e);
PushStack2(d,e);
PopStack2(d,q);
return 0;
*/
while (1) {
printf("\n");
printf("\t\t\t\t\t******************菜单********************\n");
printf("\t\t\t\t\t1.初 始 化 栈 2.销 毁 栈 \n");
printf("\t\t\t\t\t3.进 栈 4.出 栈 \n");
printf("\t\t\t\t\t5.取 栈 顶 元 素 6.三 合 一 创 建 \n");
printf("\t\t\t\t\t**************** 7-练习 ******************\n");
printf("\t\t\t\t\t**************** 0-退出 ******************\n");
printf("\n"); printf("\n"); printf("\n");
printf("\n");
printf("请进行功能选择:");
scanf("%d", &choose);
switch (choose){
case 1: {
InitStack(s);
printf("初始化完成");
printf("\n");
system("pause");
break;
}
case 2: {
DestroyStack(s);
printf("销毁完成"); printf("\n");
system("pause");
break;
}
case 3: {
ElemType e;
printf("请输入你想要进栈的元素:\n");
scanf("%d", &e);
PushStack(s, e);
printf("\n");
system("pause");
break;
}
case 4: {
ElemType i;
PopStack(s,i);
printf("\n");
system("pause");
break;
}
case 5: {
ElemType q;
GetTop(s, q);
printf("栈顶元素为:%d\n", q);
printf("\n");
system("pause");
break;
}
case 6: {
CreatStack(s);
printf("\n");
system("pause");
break;
}
case 7: {
ElemType str[] = "sasdasdada";
ElemType st[] = "sasas";
int k1 = sizeof(str) / sizeof(str[0]);
int k2 = sizeof(st) / sizeof(st[0]);
bool a=symmertry(str,k1);
bool b=symmertry(st,k2);
//int k = sizeof(str) ;
//int k1= sizeof(ElemType);
//printf("%d\t%d", k, k1);
if (a == true) {
printf("str中对称\n");
}
else {
printf("str不对称\n");
}
if (b == true) {
printf("st中对称\n");
}
else {
printf("st不对称\n");
}
printf("\n");
system("pause");
break;
}
case 0: {
printf("程序结束\n");
return 0;
}
default: {
printf("请输入正确的选项\n");
printf("\n");
break;
}
}
}
return 0;
}
开始的计划很完美!
果然一通百通,花费大量时间弄通了线性表 ,在学习栈的过程中节奏不进快了很多,而且更加顺畅和易懂!!!
加油 !!!
继续努力!!!!!!
代码如下: