一、顺序栈 SeqStack.h /* Name: SeqStack.h Version: 0.10 Author: dir Date: 14-06-09 20:21 Description: */ #ifndef __SEQSTACK__H__ #define __SEQSTACK__H__ #include <stdio.h> #define MaxSize 100 typedef int DataType; typedef struct { DataType data[MaxSize]; int top; }Stack; // 初始化栈 void InitStack(Stack *s) { s->top = -1; } // 判断栈是否为空 int Empty(Stack *s) { return (s->top == -1 ? 1:0); } // 取栈顶元素 int GetTop(Stack *s, DataType *x) { if(Empty(s)) { printf("Stack is empty!/n"); return 0; } *x = s->data[s->top]; return 1; } // 压栈 int Push(Stack *s, DataType x) { // 先判断位置 if(s->top == MaxSize - 1) { printf("Stack is full!/n"); return 0; } s->top++; s->data[s->top] = x; return 1; } // 出栈 int Pop(Stack *s, DataType *x) { // 判断位置 if(Empty(s)) { printf("Stack is empty!/n"); return 0; } *x = s->data[s->top]; s->top--; return 1; } #endif SeqStack.c /* Name: SeqStack.c Version: 0.10 Author: dir Date: 14-06-09 20:51 Description: */ #include "SeqStack.h" // 一个例子 // 十进制转化为其它进制 // 将非负的十进制整数n,输出为等值的b进制数,b小于17 void DecToOther(int n, int b) { Stack s; int x; char c[] = "0123456789ABCDEF"; InitStack(&s); while(n) { Push(&s, n%b); n = n/b; } while(!Empty(&s)) { Pop(&s, &x); printf("%c", c[x]); } } int main() { int n, b; printf("Input num:"); scanf("%d", &n); printf("Input into the system:"); scanf("%d", &b); DecToOther(n, b); system("pause"); return 0; } 运行截图: 二、链栈 LinkedStack.h /* Name: LinkedStack.h Version: 0.10 Author: dir Date: 14-06-09 19:34 Description: */ #ifndef __LINKEDSTACK__H__ #define __LINKEDSTACK__H__ #define NULL 0 typedef int DataType; typedef struct snode { DataType data; struct snode *next; }LinkStack; LinkStack *top; // 初始化 void InitStack(LinkStack **top) { *top = (LinkStack *)malloc(sizeof(LinkStack)); (*top)->next = NULL; } // 判断栈空 int Empty(LinkStack **top) { return ((*top)->next == NULL ? 1 : 0); } // 压栈 int Push(LinkStack **top, DataType x) { LinkStack *s; s = (LinkStack *)malloc(sizeof(LinkStack)); s->data = x; s->next = (*top)->next; (*top)->next = s; return 1; } // 出栈 int Pop(LinkStack **top, DataType *x) { LinkStack *s; // 判断位置 if(Empty(top)) { printf("Stack is empty!/n"); return 0; } s = (*top)->next; *x = s->data; (*top)->next = s->next; free(s); return 1; } // 取栈顶元素 int GetTop(LinkStack **top, DataType *x) { if(Empty(top)) { printf("Stack is empty!/n"); return 0; } *x = (*top)->next->data; return 1; } // 求栈长 int GetLen(LinkStack **top) { LinkStack *s; int num = 0; InitStack(&s); s->next = (*top)->next; while ((*top)->next != NULL) { num++; (*top)->next = (*top)->next->next; } (*top)->next = s->next; return num; } #endif LinkedStack.c /* Name: LinkedStack.c Version: 0.10 Author: dir Date: 14-06-09 20:18 Description: */ #include "LinkedStack.h" // 一个例子 // 用栈求Fibonacci数 int fib(int n) { int num, v1, v2, temp; LinkStack *s; InitStack(&s); num = n; while (num) { Push(&s, num); num--; } if (n > 2) { // 当n = 1或n = 2的情况 Pop(&s, &temp); Pop(&s, &temp); // 将两者的值入栈 Push(&s, 1); Push(&s, 1); // 按出栈两个值,把这两者的和再入栈的步骤重复 while (1) { // 将栈顶值弹出,存入v1 Pop(&s, &v1); // 下一值存入v2 Pop(&s, &v2); // 当前n出栈 Pop(&s, &temp); // 如果栈空,则结束 if (Empty(&s)) { return v1 + v2; } // 两者和的值再入栈 Push(&s, v1 + v2); // n - 1,也就是v2再入栈,下次再出栈将成为n - 2 Push(&s, v2); } } else if (n == 1 || n == 2) { return 1; } else { printf("please input a number greater than 0!/n"); return 0; } } int main() { int n, sum; DataType x; printf("input a fibonacci number:"); scanf("%d", &n); sum = fib(n); printf("%d", sum); system("pause"); return 0; } 运行截图: