1.栈的定义
栈(Stack)是限定仅在表尾进行插入和删除操作的线性表。
顺序存储中用int data[STACKSIZE] 来存放所有的入栈元素,栈底的位置可以设置固定在数组的任意一端,栈顶指示实际的栈顶元素位置,它是随着插入和删除是动态变化的,用int top变量来指示栈顶的位置
2.参考代码(顺序)
1.sqstack.h
<span style="font-family:Comic Sans MS;"><span style="font-family:Comic Sans MS;">#ifndef _SEQ_STACK_H_
#define _SEQ_STACK_H_
typedef int data_t;
typedef struct
{
data_t *data; /* storage of the stack,
* in array, dynamic allocated
*/
int top; /* current top of the stack,
* the value of top is the index of
* the array
*/
int maxlen; /* max depth of stack
* initialized when the stack is
* created, used to limit top
* not exceed the max depth
*/
} sqstack_t;
/*
* create a stack and init it as empty
* Input:
* len: max depth of the stack
* Output: void
* Return: new list, NULL when failed
*/
sqstack_t *CreateEmptySqstack(int len);
/*
* destroy a stack
* Input:
* stack: the stack to be destroied.
* Output: void
* Return: void
*/
void DestroySqstack(sqstack_t *stack);
/*
* clear the stack, reset it as empty
* Input:
* stack: the list to be cleared.
* Output: void
* Return: void
*/
void ClearSqstack(sqstack_t *stack);
/*
* judge if the stack is empty
* Input:
* stack: the stack to be tested.
* Output: void
* Return:
* 1: empty
* 0: not
* -1: error
*/
int EmptySqstack(sqstack_t *stack);
/*
* judge if the stack is full
* Input:
* stack: the stack to be tested.
* Output: void
* Return:
* 1: full
* 0: not
* -1: error
*/
int FullSqstack(sqstack_t *stack);
/*
* Push
* Input:
* stack: the list to be operated.
* x: the data to be pushed
* Output: void
* Return:
* 0: success
* !0: error or the stack is already full.
*/
int PushStack(sqstack_t *stack, data_t x);
/*
* Pop
* Input :
* stack: the list to be operated.
* Output:
* x: caller should allocate the data and provide
* the address. PopStack will copy the data
* to that address as output. If the caller don't want
* the data, just provide NULL.
* Return:
* 0: success, pop one
* -1: failed, or the stack is already empty
*/
int PopStack(sqstack_t *stack, data_t *x);
/*
* get the data on the top of the stack
* Input :
* stack: the list to be operated.
* Output:
* x: caller should allocate the data and provide
* the address. GetTop will copy the data
* to that address as output. If the caller don't want
* the data, just provide NULL.
* Return:
* 0: success, get the top one
* -1: failed, or the stack is already empty
*/
int GetTop(sqstack_t *stack, data_t *x);
/*
* iterate through the stack and print out info of each element
* Input :
* stack: the list to be operated.
* Output: void
* Return: void
*/
void VisitStack(sqstack_t *stack);
#endif /* _SEQ_STACK_H_ */</span><span style="font-family:Microsoft YaHei;">
</span></span>
2.sqstack.c
<span style="font-family:Comic Sans MS;"><span style="font-family:Comic Sans MS;">#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
sqstack_t *CreateEmptySqstack(int len)
{
sqstack_t *stack;
stack = (sqstack_t *)malloc(sizeof(sqstack_t));
stack->data = (data_t *)malloc(sizeof(data_t) * len);
stack->maxlen = len;
stack->top = -1;
return stack;
}
void DestroySqstack(sqstack_t *stack)
{
if (stack) {
if (stack->data) free(stack->data);
free(stack);
}
}
int EmptySqstack(sqstack_t *stack)
{
if (!stack) return -1;
return (-1 == stack->top ? 1 : 0);
}
int FullSqstack(sqstack_t *stack)
{
if (!stack) return -1;
return (stack->maxlen - 1 == stack->top ? 1 : 0);
}
void ClearSqstack(sqstack_t *stack)
{
stack->top = -1;
return;
}
int PushStack(sqstack_t *stack, data_t x)
{
if (FullSqstack(stack)) return -1;
stack->top++;
stack->data[stack->top] = x;
return 0;
}
int PopStack(sqstack_t *stack, data_t *x)
{
if (EmptySqstack(stack)) return -1;
if (x)
*x = stack->data[stack->top];
stack->top--;
return 0;
}
int GetTop(sqstack_t *stack, data_t *x)
{
if (EmptySqstack(stack)) return -1;
if (!x)
*x = stack->data[stack->top];
return 0;
}
void VisitStack(sqstack_t *stack)
{
int i;
if (!stack) return;
/* print from the base to the top */
printf("stack = {");
if (stack->data) {
for (i = -1; i < stack->top;) {
printf("%d,", stack->data[++i]);
}
if (i > -1)
printf("\b}\n");
else
printf("}\n");
} else {
printf("}\n");
}
}
</span></span>
3.main.c
<span style="font-family:Comic Sans MS;"><span style="font-family:Comic Sans MS;">#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
int Push_Pop(sqstack_t *stack, data_t x);
int main(int argc, char *argv[])
{
sqstack_t *stack;
int len;
if (argc < 2) {
printf("Usage: %s <len>\n", argv[0]);
return -1;
}
len = atoi(argv[1]);
stack = CreateEmptySqstack(len);
if (!stack) {
printf("CreateEmptySqstack error\n");
return -1;
}
Push_Pop(stack, 1);
DestroySqstack(stack);
return 0;
}
int Push_Pop(sqstack_t *stack, data_t x)
{
data_t data_pop;
if (FullSqstack(stack)) {
printf("----- reach the max depth of the stack!\n");
return 0;
} else {
printf("Push %d\n", x);
PushStack(stack, x++);
VisitStack(stack);
Push_Pop(stack, x);
PopStack(stack, &data_pop);
printf("Pop %d\n", data_pop);
VisitStack(stack);
return -1;
}
}
</span></span>
2.参考代码(链式)
1.linkstack.h
<span style="font-family:Comic Sans MS;">#ifndef _SEQ_STACK_H_
#define _SEQ_STACK_H_
typedef int data_t;
typedef struct node_t{
data_t data;
struct node_t *next;
} linkstack_t;
/*
* create a stack and init it as empty
* Input: void
* Output: void
* Return: new list, NULL when failed
*/
linkstack_t *CreateEmptyLinkstack();
/*
* destroy a stack
* Input:
* stack: the stack to be destroied.
* Output: void
* Return: void
*/
void DestroyLinkstack(linkstack_t *stack);
/*
* clear the stack, reset it as empty
* Input:
* stack: the list to be cleared.
* Output: void
* Return: void
*/
void ClearLinkstack(linkstack_t *stack);
/*
* judge if the stack is empty
* Input:
* stack: the stack to be tested.
* Output: void
* Return:
* 1: stack is empty
* 0: not
* -1: error
*/
int EmptyLinkstack(linkstack_t *stack);
/*
* Push
* Input:
* stack: the list to be operated.
* x: the data to be pushed
* Output: void
* Return:
* 0: success
* !0: error or the stack is already full.
*/
int PushStack(linkstack_t *stack, data_t x);
/*
* Pop
* Input :
* stack: the list to be operated.
* Output:
* data: caller should allocate the data and provide
* the address. PopStack will copy the data
* to that address as output. If the caller don't want
* the data, just provide NULL.
* Return:
* 0: success, pop one
* -1: failed, or the stack is already empty
*/
int PopStack(linkstack_t *stack, data_t *data);
/*
* get the data on the top of the stack
* Input :
* stack: the list to be operated.
* Output:
* data: caller should allocate the data and provide
* the address. GetTop will copy the data
* to that address as output. If the caller don't want
* the data, just provide NULL.
* Return:
* 0: success, get the top one
* -1: failed, or the stack is already empty
*/
int GetTop(linkstack_t *stack, data_t *data);
/*
* iterate through the stack and print out info of each element
* Input :
* stack: the list to be operated.
* Output: void
* Return: void
*/
void VisitStack(linkstack_t *stack);
#endif /* _SEQ_STACK_H_ */
</span>
2.linkstack.c
<span style="font-family:Comic Sans MS;">#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
linkstack_t *CreateEmptyLinkstack()
{
linkstack_t *s;
s = (linkstack_t *)malloc(sizeof(linkstack_t));
s->next = NULL;
return s;
}
void DestroyLinkstack(linkstack_t *stack)
{
if (stack) {
/* clear the stack linked list */
ClearLinkstack(stack);
/* free the stack header */
free(stack);
}
}
int EmptyLinkstack(linkstack_t *stack)
{
if (stack) {
return ((NULL == stack->next) ? 1 : 0);
} else {
return -1;
}
}
void ClearLinkstack(linkstack_t *stack)
{
linkstack_t *node; /* node to be removed */
if (!stack) return;
while ( NULL != stack->next ) {
/* disconnect the one to be removed */
node = stack->next;
stack->next = node->next;
free(node);
}
return;
}
int PushStack(linkstack_t *stack, data_t x)
{
linkstack_t *node; /* node to be inserted */
if (!stack) return -1;
node = (linkstack_t *)malloc(sizeof(linkstack_t));
if (NULL == node) {
return -1;
}
node->data = x;
/* insert from the head of the link list
* it's cheaper to push from the head of the single-linked
* list.
*/
node->next = stack->next;
stack->next = node;
return 0;
}
int PopStack(linkstack_t *stack, data_t *data)
{
linkstack_t *node; /* node to be removed */
if (!stack) return -1;
if (!(stack->next)) return -1; /* stack is empty */
/* pop from the head of the list */
node = stack->next;
stack->next = node->next;
if (data) {
*data = node->data;
}
/* now we can free the node safely */
free(node);
return 0;
}
int GetTop(linkstack_t *stack, data_t *data)
{
if (!stack) return -1;
if (!(stack->next)) return -1; /* stack is empty */
if (data) {
*data = stack->next->data;
}
return 0;
}
void VisitStack(linkstack_t *stack)
{
linkstack_t *node; /* node to be iterated */
if (!stack) return;
/* print from the base to the top */
printf("stack = {");
if (stack->next) { /* list is not empty */
node = stack->next;
while (NULL != node) {
printf("%d,", node->data);
node = node->next;
}
printf("\b}\n");
} else {
printf("}\n");
}
}
</span>
3.main.c
<span style="font-family:Comic Sans MS;">#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
int max_depth;
int Push_Pop(linkstack_t *stack, data_t x);
int main(int argc, char *argv[])
{
linkstack_t *stack;
if (argc < 2) {
printf("Usage: %s <len>\n", argv[0]);
return -1;
}
max_depth = atoi(argv[1]);
stack = CreateEmptyLinkstack();
if (!stack) {
printf("CreateEmptyLinkstack error\n");
return -1;
}
Push_Pop(stack, 1);
DestroyLinkstack(stack);
return 0;
}
int Push_Pop(linkstack_t *stack, data_t x)
{
data_t data_pop;
static int depth = 0;
if (depth == max_depth) {
printf("----- reach the max depth of the stack!\n");
return 0;
} else {
depth++;
printf("Push %d\n", x);
PushStack(stack, x++);
VisitStack(stack);
Push_Pop(stack, x);
PopStack(stack, &data_pop);
printf("Pop %d\n", data_pop);
VisitStack(stack);
return -1;
}
}</span><span style="font-family:Microsoft YaHei;">
</span>