棧的順序存儲
第一種:棧的簡單實現,代碼如下:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAXSIZE 20
4
5 //棧結構體
6 typedef struct STACK
7 {
8 int data[MAXSIZE];
9 int stackTop;//棧頂標記
10 }Stack;
11
12 //初始化
13 Stack* Init_Stack()
14 {
15 Stack* myStack = (Stack*)malloc(sizeof(Stack));
16 myStack->stackTop = -1;
17 return myStack;
18 }
19
20 //入棧
21 void push_Stack(Stack* stack, int element)
22 {
23 //判斷棧是否已滿
24 if (stack->stackTop + 1 == MAXSIZE)
25 {
26 printf("棧已滿,無法入棧!");
27 exit(-1);
28 }
29 //入棧
30 stack->data[++stack->stackTop] = element;
31 }
32
33 //出棧
34 void pop_Stack(Stack* stack, int *element)
35 {
36 if (stack->stackTop == -1)
37 {
38 printf("棧空,無法出棧!");
39 exit(-1);
40 }
41 *element = stack->data[stack->stackTop--];
42 }
43
44 //判斷棧空
45 int isEmpty_Stack(Stack* stack)
46 {
47 return stack->stackTop == -1; //返回1表示NULL 返回0表示 非NULL
48 }
49
50 //清空
51 void Clear_Stack(Stack* stack)
52 {
53 stack->stackTop = -1;
54 }
55
56 //測試
57 void test01()
58 {
59 Stack* myStack = Init_Stack();
60 for (int i = 1; i < 10; i++)
61 {
62 push_Stack(myStack, i);
63 }
64 while (!isEmpty_Stack(myStack))
65 {
66 int data = 0;
67 pop_Stack(myStack, &data);
68 printf("%d ", data);
69 }
70 printf("\n");
71 Clear_Stack(myStack);
72 }
73
74 int main()
75 {
76 test01();
77 system("pause");
78 return 0;
79 }
第二種:自定義類型的棧,代碼如下:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAXSIZE 20
4
5 //棧結構體
6 typedef struct seqStack
7 {
8 void* data[MAXSIZE];
9 int stackTop;
10 }seqStack;
11
12 //數據
13 typedef struct Person
14 {
15 char name[32];
16 int age;
17 }Person;
18
19 //初始化
20 seqStack* Init_seqStack()
21 {
22 seqStack* stack = (seqStack*)malloc(sizeof(seqStack));
23 stack->stackTop = -1;
24 return stack;
25 }
26
27 //入棧
28 void push_seqStack(seqStack* stack, void* element)
29 {
30 if (stack->stackTop + 1 == MAXSIZE)
31 {
32 printf("棧滿,無法入棧");
33 exit(-1);
34 }
35 stack->data[++stack->stackTop] = element;
36 }
37
38 //出棧
39 void* Pop_seqStack(seqStack* stack)
40 {
41 if (stack == NULL)
42 {
43 printf("棧空,無法出棧");
44 exit(-1);
45 }
46 return stack->data[stack->stackTop--];
47 }
48
49 //判斷棧空
50 int isEmpty_seqStack(seqStack* stack)
51 {
52 return stack->stackTop == -1; //返回1表示NULL 返回0表示 非NULL
53 }
54
55 //清空
56 void Clear_seqStack(seqStack* stack)
57 {
58 stack->stackTop = -1;
59 }
60
61 int main()
62 {
63 seqStack* myseqStack = Init_seqStack();
64 //數據
65 Person p1 = { "aaa",10 };
66 Person p2 = { "bbb",20 };
67 Person p3 = { "ccc",30 };
68 Person p4 = { "ddd",40 };
69 Person p5 = { "eee",50 };
70 //入棧
71 push_seqStack(myseqStack, &p1);
72 push_seqStack(myseqStack, &p2);
73 push_seqStack(myseqStack, &p3);
74 push_seqStack(myseqStack, &p4);
75 push_seqStack(myseqStack, &p5);
76 while (!isEmpty_seqStack(myseqStack))
77 {
78 Person* person = (Person*)Pop_seqStack(myseqStack);
79 printf("姓名:%s 年齡:%d\n", person->name, person->age);
80 }
81 system("pause");
82 return 0;
83 }
第三種:棧容量自動增長,代碼如下:
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10 //初識容量
#define STACK_INCREMENT_SIZE 20 //增量
//棧結構體
typedef struct St
{
int* base;//棧底指針
int* top;//棧頂指針
int size;//棧容量
}St;
//初始化
St* initSeqStack()
{
St* s = (St*)malloc(sizeof(St));
s->base = s->top = (int*)malloc(STACK_INIT_SIZE*sizeof(int));
if (!s->base)
{
printf("OVERFLOW");
exit(-1);
}
s->size = STACK_INIT_SIZE;
return s;
}
//入棧
void push(St* s, int data)
{
if (s == NULL)
return;
if (s->top - s->base == s->size)
{
int* newbase = (int*)realloc(s->base, (s->size + STACK_INCREMENT_SIZE)*sizeof(int));
if (!newbase)
{
printf("OVERFLOW!");
exit(-1);
}
s->top = s->base + s->size;
s->size += STACK_INCREMENT_SIZE;
}
*s->top++ = data;
s->size++;
}
//出棧
void pop(St* s, int* data)
{
if (s == NULL || s->top == s->base)
{
printf("棧空,無法出棧");
exit(-1);
}
*data = *--s->top;
}
//判斷棧空
int empty(St* s)
{
if (NULL == s) {
return 0;
}
if (s->top == s->base) {
return 1;
}
return 0;
}
//棧的應用--進制轉換
void covertBinary(int num)
{
int data = 0;
printf("%d的二進制是:", num);
St* stack = initSeqStack();
while (num != 0)
{
push(stack, num % 2);
num /= 2;//除數的自動取整
}
while (!empty(stack))
{
pop(stack, &data);
printf("%d", data);
}
printf("\n");
}
void testStack()
{
St* stack = initSeqStack();
for (int i = 0; i <35; i++)
{
push(stack, i);
}
int data = 0;
while (!empty(stack))
{
pop(stack, &data);
printf("%d ", data);
}
printf("\n");
}
int main()
{
covertBinary(11);
system("pause");
return 0;
}