栈的顺序表实现
1 #include <iostream> 2 #include <stdlib.h> 3 #include <stdio.h> 4 #define MAXSIZE 1024 5 using namespace std; 6 typedef int Elemtype; 7 typedef struct { 8 Elemtype data[MAXSIZE]; 9 int top; 10 }Seqstack; 11 void initSeqstack(Seqstack &s){ 12 s.top=-1; 13 } 14 int stackEmpty(Seqstack &s){ 15 return s.top==-1; 16 } 17 int SeqstackPush(Seqstack &s,Elemtype e){ 18 if (s.top>=MAXSIZE-1) 19 return 0; 20 else { 21 s.top++; 22 s.data[s.top]=e; 23 return 1; 24 } 25 } 26 int SeqstackPop(Seqstack &s,Elemtype &e){ 27 if (s.top==-1) 28 return 0; 29 else { 30 e=s.data[s.top]; 31 s.top--; 32 return 1; 33 } 34 } 35 void getTop(Seqstack &s,Elemtype &e){ 36 e=s.data[s.top]; 37 } 38 void displaySeqstack(Seqstack &s){ 39 for (int i=0;i<=s.top;i++) 40 printf("%d ",s.data[i]); 41 printf ("\n"); 42 } 43 int main() 44 { 45 Seqstack s;Elemtype e; 46 initSeqstack(s); 47 for (int i=1;i<6;i++) 48 SeqstackPush(s,i); 49 displaySeqstack(s); 50 getTop(s,e); 51 printf ("%d\n",e); 52 SeqstackPop(s,e); 53 displaySeqstack(s); 54 55 56 return 0; 57 }
严蔚敏版 栈的实现
1 #include <iostream> 2 #include <stdlib.h> 3 #include <stdio.h> 4 #define STACK_INIT_SIZE 100 5 #define STACKINCREMENT 10 6 #define OVERFLOW -2 7 using namespace std; 8 9 typedef int Elemtype; 10 typedef struct { 11 Elemtype *base; 12 Elemtype *top; 13 int stacksize; 14 }Seqstack; 15 int initStack(Seqstack &s){ 16 s.base=(Elemtype *)malloc(sizeof(Elemtype)*STACK_INIT_SIZE); 17 if (!s.base ) exit(OVERFLOW);//存储分配失败 18 s.top=s.base; 19 s.stacksize=STACK_INIT_SIZE; 20 return 1; 21 } 22 int stackEmpty(Seqstack &s){ 23 return s.top==s.base; 24 } 25 int stackLength(Seqstack &s){ 26 return s.top-s.base; 27 } 28 int SeqstackPush(Seqstack &s,Elemtype e){ 29 if (s.top-s.base >=s.stacksize){ 30 s.base=(Elemtype *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(Elemtype)); 31 if (!s.base) exit(OVERFLOW); 32 s.top=s.base+s.stacksize; 33 s.stacksize+=STACKINCREMENT; 34 //上溢后: 35 //1.分配存储空间给base指针 2、判断是否分配成功 3、top/stacksize重新修改正确 36 } 37 *s.top=e; 38 s.top++;//或者可以合成*s.top++=e,先赋值再++ 39 return 1; 40 } 41 int SeqstackPop(Seqstack &s,Elemtype &e){ 42 if(s.top==s.base){ 43 printf ("null"); 44 return 0; 45 } 46 s.top--; 47 e=*s.top; 48 return 1; 49 } 50 void getTop(Seqstack &s,Elemtype &e){ 51 if (s.base!=s.top){ 52 e=*--s.top; 53 } 54 } 55 void displaySeqstack(Seqstack &s){ 56 for (int i=0;i<=s.top-s.base;i++) 57 printf("%d ",s.base[i]); 58 printf ("\n"); 59 } 60 61 int main() 62 { 63 Seqstack s; 64 initStack(s); 65 for (int i=1;i<6;i++) 66 SeqstackPush(s,i); 67 displaySeqstack(s); 68 Elemtype e; 69 getTop(s,e); 70 printf ("%d\n",e); 71 SeqstackPop(s,e); 72 displaySeqstack(s); 73 74 75 return 0; 76 }
建立链栈
1 #include <iostream> 2 #include <stdlib.h> 3 #include <stdio.h> 4 #define OVERFLOW -2 5 using namespace std; 6 typedef int Elemtype; 7 typedef struct node { 8 Elemtype data; 9 struct node *next; 10 }node,*linkstack; 11 void initLinkstack(linkstack &top){ 12 top=NULL;//无头节点的链栈 13 } 14 int linkstackEmpty(linkstack &top){ 15 return top==NULL; 16 } 17 int linkstackPush(linkstack &top,Elemtype e){ 18 linkstack p=(linkstack )malloc (sizeof(node)); 19 if (!p) exit(OVERFLOW); 20 p->data=e; 21 p->next=top; 22 top=p; 23 return 1; 24 } 25 int linkstackPop(linkstack &top,Elemtype &e){ 26 e=top->data; 27 linkstack p=top; 28 top=top->next; 29 free(p); 30 return 1; 31 } 32 void getTop(linkstack &top,Elemtype &E){ 33 E=top->data; 34 } 35 void displaylinkstack(linkstack &top){ 36 linkstack p=top; 37 while (p){ 38 printf ("%d ",p->data); 39 p=p->next; 40 } 41 printf ("\n"); 42 } 43 int main() 44 { 45 linkstack top; 46 initLinkstack(top); 47 for (int i=1;i<6;i++) 48 linkstackPush(top,i); 49 displaylinkstack(top); 50 Elemtype e; 51 getTop(top,e); 52 printf ("%d\n",e); 53 linkstackPop(top,e); 54 displaylinkstack(top); 55 56 57 return 0; 58 }
栈的应用
1.数制转换:对于输入的非负十进制整数,打印输出与之等值的八进制数。
1 //算法3.1 2 void