顺序栈的一种实现

xiaoxiao2021-02-28  111

弹指间,自己已经到了而立之年,感觉自己的记忆力也在逐渐衰退,俗话说“好记性不如一个赖笔头”,故写下此博客,便于日后查阅。

声明:本文只用于自学使用,若不慎造成侵权,请及时联系,若情况属实,会立即修正

今天用c语言写了一个顺序栈,原本认为半小时完事,但实际花了一个小时,中间也走了点弯路,所以在此警告自己:要多敲代码,不要眼高手低!!

本想着用指针的引用来实现堆栈的分配,但是使用gcc编译时总是报错,后来才发现原来c语言不支持指针的引用(c++才支持指针的引用),但是在c语言中,可以使用返回该类型的指针或者使用指针的指针来代替。

#ifndef SQSTACK_H #define SQSTACK_H #include <stdio.h> #define MAX_SIZE 10 typedef char Elemtype; typedef struct { Elemtype data[MAX_SIZE]; int top; } sqstack; int initsqstack(sqstack *&s); void pushsqstack(sqstack *s,Elemtype e); void popsqstack(sqstack *s,Elemtype *e); void destorysqstack(sqstack *s); #endif

#include <stdio.h> #include <stdlib.h> #include "sqstack.h" int initsqstack(sqstack *&s) { s = (sqstack *)malloc(sizeof(sqstack)); if(s == NULL) { printf("Error: alloc sqstack failed!!\n"); return 0; } s->top = -1; return 1; } void pushsqstack(sqstack *s, Elemtype e) { if(s->top >= MAX_SIZE - 1) { printf("Error: the sqstack is already full!!\n"); return; } s->top++; printf("yxw test s->top = %d\n",s->top); printf("yxw test e = %c\n",e); s->data[s->top] = e; return; } void popsqstack(sqstack *s, Elemtype *e) { if(s->top < 0) { printf("Error: the sqstack is already empty!!\n"); return; } *e = s->data[s->top]; printf("yxw test pop data = %c\n",*e); printf("yxw test pop s->top = %d\n",s->top); s->top--; return; } void destorysqstack(sqstack *s) { if(s != NULL) free(s); return; }

#include <stdio.h> #include <stdlib.h> #include "sqstack.h" int main(void) { sqstack *s; initsqstack(s); char *str="abcdefg"; Elemtype c; while(*str != '\0') { pushsqstack(s,*str); str++; } while(s->top >= 0) { popsqstack(s,&c); } destorysqstack(s); return 0; } yxw@ubuntu:~/study/data_struct_c/3/sqstack$ gcc -o sqstack main.c sqstack.c In file included from main.c:3:0: sqstack.h:15:26: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token In file included from sqstack.c:3:0: sqstack.h:15:26: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token sqstack.c:5:26: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token yxw@ubuntu:~/study/data_struct_c/3/sqstack$ 

解决方案一:

使用c++编译命令:yxw@ubuntu:~/study/data_struct_c/3/sqstack$ g++ -o sqstack main.c sqstack.c

解决方案二:

使用指针的指针代替如下:

#ifndef SQSTACK_H #define SQSTACK_H #include <stdio.h> #define MAX_SIZE 10 typedef char Elemtype; typedef struct { Elemtype data[MAX_SIZE]; int top; } sqstack; int initsqstack(sqstack **s); void pushsqstack(sqstack *s,Elemtype e); void popsqstack(sqstack *s,Elemtype *e); void destorysqstack(sqstack *s); #endif #include <stdio.h> #include <stdlib.h> #include "sqstack.h" int initsqstack(sqstack **s) { *s = (sqstack *)malloc(sizeof(sqstack)); if(*s == NULL) { printf("Error: alloc sqstack failed!!\n"); return 0; } (*s)->top = -1; return 1; } void pushsqstack(sqstack *s, Elemtype e) { if(s->top >= MAX_SIZE - 1) { printf("Error: the sqstack is already full!!\n"); return; } s->top++; printf("yxw test s->top = %d\n",s->top); printf("yxw test e = %c\n",e); s->data[s->top] = e; return; } void popsqstack(sqstack *s, Elemtype *e) { if(s->top < 0) { printf("Error: the sqstack is already empty!!\n"); return; } *e = s->data[s->top]; printf("yxw test pop data = %c\n",*e); printf("yxw test pop s->top = %d\n",s->top); s->top--; return; } void destorysqstack(sqstack *s) { if(s != NULL) free(s); return; } #include <stdio.h> #include <stdlib.h> #include "sqstack.h" int main(void) { sqstack *s; initsqstack(&s); char *str="abcdefg"; Elemtype c; while(*str != '\0') { pushsqstack(s,*str); str++; } while(s->top >= 0) { popsqstack(s,&c); } destorysqstack(s); return 0; }

程序运行结果如下:

yxw@ubuntu:~/study/data_struct_c/3/sqstack$  yxw@ubuntu:~/study/data_struct_c/3/sqstack$ ./sqstack yxw test s->top = 0 yxw test e = a yxw test s->top = 1 yxw test e = b yxw test s->top = 2 yxw test e = c yxw test s->top = 3 yxw test e = d yxw test s->top = 4 yxw test e = e yxw test s->top = 5 yxw test e = f yxw test s->top = 6 yxw test e = g yxw test pop data = g yxw test pop s->top = 6 yxw test pop data = f yxw test pop s->top = 5 yxw test pop data = e yxw test pop s->top = 4 yxw test pop data = d yxw test pop s->top = 3 yxw test pop data = c yxw test pop s->top = 2 yxw test pop data = b yxw test pop s->top = 1 yxw test pop data = a yxw test pop s->top = 0 yxw@ubuntu:~/study/data_struct_c/3/sqstack$  yxw@ubuntu:~/study/data_struct_c/3/sqstack$

运行结果符合预期,结束^_^

转载请注明原文地址: https://www.6miu.com/read-50601.html

最新回复(0)