栈
顺序栈
单栈
//单栈
typedef struct {
int* data;
int* len;
int max_s;
}lnode;
void chu(lnode* S);
void yazhan(lnode* S);
void chuzhan(lnode* S);
void dayin(lnode* S);
int main() {
lnode S;
chu(&S);
do {
printf("1:压栈\n2:出栈\n3:打印\n0:退出\n");
int a = 0;
scanf_s("%d", &a);
switch (a) {
case 0:{
printf("退出\n");
return 0;
}
case 1: {
yazhan(&S);
break;
}
case 2: {
chuzhan(&S);
break;
}
case 3: {
dayin(&S);
break;
}
default: {
printf("输入错误,请重新输入\n");
break;
}
}
} while (1);
return 0;
}
//初始化栈
void chu(lnode* S) {
S->data = (int*) malloc (sizeof(int)* (max+1));
S->len = S->data;
S->max_s = max;
if (S->data != NULL) {
printf("初始化成功\n");
}
else{
printf("初始化失败\n");
}
}
//压栈
void yazhan(lnode* S) {
if (S->len-S->data == S->max_s) {
printf("栈已满\n");
return;
}
printf("请输入要压栈的元素\n");
int a;
scanf_s("%d", &a);
*(S->len) = a;
S->len++;
printf("压栈成功\n");
}
//出栈
void chuzhan(lnode* S) {
if (S->len == S->data) {
printf("栈为空\n");
return;
}
S->len--;
int m = *(S->len);
printf("%d出栈成功\n",m);
}
//打印
void dayin(lnode* S) {
lnode x;
x.len = S->len;
while (S->len!=S->data) {
S->len--;
printf("%d ", *(S->len));
}
S->len = x.len;
printf("\n");
}
双栈
//双栈
typedef struct {
char* data;
int len;
int max_s;
}lnode2;
void chushi(lnode2* S);
void yazhan(lnode2* S);
void chuzhan(lnode2* S);
void dayin(lnode2* S);
int main() {
lnode2 S;
chushi(&S);
do {
printf("1:压栈\n2:出栈\n3:打印\n0:退出\n");
int a = 0;
scanf_s("%d", &a);
switch (a) {
case 0:{
printf("退出\n");
return 0;
}
case 1: {
yazhan(&S);
break;
}
case 2: {
chuzhan(&S);
break;
}
case 3: {
dayin(&S);
break;
}
default: {
printf("输入错误,请重新输入\n");
break;
}
}
} while (1);
return 0;
}
void chushi(lnode2* S) {
S->data = (char*)malloc(sizeof(char) * (max + 1));
S->len = 0;
S->max_s = max;
if (S->data != NULL) {
printf("初始化成功\n");
}
else {
printf("初始化失败\n");
}
}
//压栈
void yazhan(lnode2 *S) {
if (S->len - 1 == S->max_s) {
printf("栈已满\n");
return;
}
/*把数据压入几号堆栈*/
int a = 0;
printf("请输入要压入的数据\n");
char m;
//getchar();//接收回车,防止%c被屏蔽
scanf_s("\n%c", &m);
printf("请输入要压入的堆栈号\n");
scanf_s("%d", &a);
switch (a) {
case 1: {
S->data[S->len] = m;
S->len++;
printf("压栈成功\n");
break;
}
case 2: {
S->data[S->max_s] = m;
S->max_s--;
printf("压栈成功\n");
break;
}
default: {
printf("参数错误\n");
break;
}
}
}
//出栈
void chuzhan(lnode2* S) {
printf("要对几号栈进行出栈\n");
int a = 0;
scanf_s("%d", &a);
switch (a) {
case 1: {
if (S->len == 0) {
printf("栈为空\n");
return;
}
S->data[S->len - 1] = NULL;
S->len--;
}
case 2: {
if (S->max_s == max) {
printf("栈为空\n");
return;
}
S->data[S->max_s + 1] = NULL;
S->max_s++;
break;
}
default: {
printf("参数错误\n");
break;
}
}
}
//打印
void dayin(lnode2* S) {
int a = 0;
printf("一号栈:\n");
while (a < S->len) {
printf("%c ", S->data[a]);
a++;
}
printf("\n");
int b = max;
printf("二号栈\n");
while (b > S->max_s) {
printf("%c ", S->data[b]);
b--;
}
printf("\n");
}
链栈
//链栈
typedef struct {
char name[8];
int age;
}mmm;
typedef struct sur{
mmm x;
struct sur* data;
}lnode;
void chushi(lnode** S);
void ruzhan(lnode** S);
void chuzhan(lnode** S);
void dayin(lnode* S);
int main() {
lnode* S;
chushi(&S);
do {
printf("1:压栈\n2:出栈\n3:打印\n0:退出\n");
int a = 0;
scanf_s("%d", &a);
switch (a) {
case 0:{
printf("退出\n");
return 0;
}
case 1: {
ruzhan(&S);
break;
}
case 2: {
chuzhan(&S);
break;
}
case 3: {
dayin(S);
break;
}
default: {
printf("输入错误,请重新输入\n");
break;
}
}
} while (1);
return 0;
}
void chushi(lnode** S) {
(*S) = NULL;
}
void ruzhan(lnode** S) {
lnode*q = (lnode*)malloc(sizeof(lnode));
if (q == NULL) {
printf("开辟空间失败\n");
return;
}
mmm k;
printf("请输入要入栈的元素:姓名 年龄\n");
scanf("%s %d", k.name, &k.age);
q->x.age = k.age;
strcpy_s(q->x.name, k.name);
q->data = (*S);
(*S) = q;
printf("入栈成功\n");
}
void chuzhan(lnode** S) {
lnode* q = (*S);
if (q == NULL) {
printf("栈为空\n");
return;
}
lnode* m = q;
q = (*S);
*S = (*S)->data;
printf("%s %d 出栈成功\n", m->x.name, m->x.age);
}
void dayin(lnode* S) {
lnode* q = S;
if (q == NULL) {
printf("栈为空\n");
return;
}
while (q) {
printf("%s %d\n", q->x.name, q->x.age);
q = q->data;
}
}