栈的定义:栈(stack)是限定仅在表尾进行插入或者删除的线性表。对于栈来说,表尾端称为栈顶(top),表头端称为栈低(bottom)。不含元素的空表称为空栈。因为栈限定在表尾进行插入或者删除,所以栈又被称为后进先出
的线性表(简称LIFO:Last in, First out.
结构)。
数据结构——栈
先看老师代码
#include <stdio.h>
#include <malloc.h>
#define STACK_MAX_SIZE 10
/**
* Linear stack of integers. The key is data.
*/
typedef struct CharStack {
int top;
int data[STACK_MAX_SIZE]; //The maximum length is fixed.
} *CharStackPtr;
/**
* Output the stack.
*/
void outputStack(CharStackPtr paraStack) {
for (int i = 0; i <= paraStack->top; i ++) {
printf("%c ", paraStack->data[i]);
}// Of for i
printf("\r\n");
}// Of outputStack
/**
* Initialize an empty char stack. No error checking for this function.
* @param paraStackPtr The pointer to the stack. It must be a pointer to change the stack.
* @param paraValues An int array storing all elements.
*/
CharStackPtr charStackInit() {
CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
resultPtr->top = -1;
return resultPtr;
}//Of charStackInit
/**
* Push an element to the stack.
* @param paraValue The value to be pushed.
*/
void push(CharStackPtr paraStackPtr, int paraValue) {
// Step 1. Space check.
if (paraStackPtr->top >= STACK_MAX_SIZE - 1) {
printf("Cannot push element: stack full.\r\n");
return;
}//Of if
// Step 2. Update the top.
paraStackPtr->top ++;
// Step 3. Push element.
paraStackPtr->data[paraStackPtr->top] = paraValue;
}// Of push
/**
* Pop an element from the stack.
* @return The poped value.
*/
char pop(CharStackPtr paraStackPtr) {
// Step 1. Space check.
if (paraStackPtr->top < 0) {
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}//Of if
// Step 2. Update the top.
paraStackPtr->top --;
// Step 3. Push element.
return paraStackPtr->data[paraStackPtr->top + 1];
}// Of pop
/**
* Test the push function.
*/
void pushPopTest() {
printf("---- pushPopTest begins. ----\r\n");
// Initialize.
CharStackPtr tempStack = charStackInit();
printf("After initialization, the stack is: ");
outputStack(tempStack);
// Pop.
for (char ch = 'a'; ch < 'm'; ch ++) {
printf("Pushing %c.\r\n", ch);
push(tempStack, ch);
outputStack(tempStack);
}//Of for i
// Pop.
for (int i = 0; i < 3; i ++) {
ch = pop(tempStack);
printf("Pop %c.\r\n", ch);
outputStack(tempStack);
}//Of for i
printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest
/**
The entrance.
*/
void main() {
pushPopTest();
}// Of main
运行结果为
---- pushPopTest begins. ----
After initialization, the stack is:
Pushing a.
a
Pushing b.
a b
Pushing c.
a b c
Pushing d.
a b c d
Pushing e.
a b c d e
Pushing f.
a b c d e f
Pushing g.
a b c d e f g
Pushing h.
a b c d e f g h
Pushing i.
a b c d e f g h i
Pushing j.
a b c d e f g h i j
Pushing k.
Cannot push element: stack full.
a b c d e f g h i j
Pushing l.
Cannot push element: stack full.
a b c d e f g h i j
Pop j.
a b c d e f g h i
Pop i.
a b c d e f g h
Pop h.
a b c d e f g
---- pushPopTest ends. ----
Press any key to continue
自己写的代码:
1.定义结构体
typedef struct stack {
int top;
int data[STAKE_MAX];
}*StackPtr;
2.输出函数
void printStake(StackPtr parastack)
{
int i;
for (i = 0; i <= parastack->top; i++)
{
printf("%c ", parastack->data[i]);
}
printf("\r\n");
}
3.初始化栈
StackPtr initStake()
{
StackPtr resultPtr = (StackPtr)malloc(sizeof(struct stack));
resultPtr->top = -1;
return resultPtr;
}
4.入栈函数
void push(StackPtr parastackPtr, int e)
{
if (parastackPtr->top >= STAKE_MAX - 1)
{
printf("Cannot push element: stack full.\r\n");
return;
}
parastackPtr->top++;
parastackPtr->data[parastackPtr->top] = e;
}
5.出栈函数
char pop(StackPtr parastackPtr)
{
if (parastackPtr->top < 0)
{
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}
parastackPtr->top--;
return parastackPtr->data[parastackPtr->top + 1];
}
6.测试函数
void pushAndpopText()
{
StackPtr tempStack = initStake();
printf("The stake is:");
printStake(tempStack);
//PUSH
int i;
char ch='a';
for (i = 0; i < 7; i++)
{
printf("Push %c\n", ch + i);
push(tempStack, ch+i);
}
printStake(tempStack);
//POP
for (i = 0; i < 3; i++)
{
ch = pop(tempStack);
printf("POP %c\r\n", ch);
printStake(tempStack);
}
printf("END");
}
完整代码
#include<stdio.h>
#include<malloc.h>
#define STAKE_MAX 10
typedef struct stack {
int top;
int data[STAKE_MAX];
}*StackPtr;
StackPtr initStake()
{
StackPtr resultPtr = (StackPtr)malloc(sizeof(struct stack));
resultPtr->top = -1;
return resultPtr;
}
void printStake(StackPtr parastack)
{
int i;
for (i = 0; i <= parastack->top; i++)
{
printf("%c ", parastack->data[i]);
}
printf("\r\n");
}
void push(StackPtr parastackPtr, int e)
{
if (parastackPtr->top >= STAKE_MAX - 1)
{
printf("Cannot push element: stack full.\r\n");
return;
}
parastackPtr->top++;
parastackPtr->data[parastackPtr->top] = e;
}
char pop(StackPtr parastackPtr)
{
if (parastackPtr->top < 0)
{
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}
parastackPtr->top--;
return parastackPtr->data[parastackPtr->top + 1];
}
void pushAndpopText()
{
StackPtr tempStack = initStake();
printf("The stake is:");
printStake(tempStack);
//PUSH
int i;
char ch='a';
for (i = 0; i < 7; i++)
{
printf("Push %c\n", ch + i);
push(tempStack, ch+i);
}
printStake(tempStack);
//POP
for (i = 0; i < 3; i++)
{
ch = pop(tempStack);
printf("POP %c\r\n", ch);
printStake(tempStack);
}
printf("END");
}
void main()
{
pushAndpopText();
}
运行结果
The stake is:
Push a
Push b
Push c
Push d
Push e
Push f
Push g
a b c d e f g
POP g
a b c d e f
POP f
a b c d e
POP e
a b c d
END
C:\Users\86183\source\repos\栈\x64\Debug\栈.exe (进程 23504)已退出,代码为 0。
按任意键关闭此窗口. . .
总结:栈的特点是先进后出,后进先出,也叫作LIFO表,只能在栈顶添加和删除元素。栈的结构和功能都很简单,可以用数组或链表来实现。虽然看似栈是以前落后的东西,但现在学懂之后对顺序表又有了不一样的理解,将栈运用好对我们学习数据结构也会有不小的帮助