栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
临摹老师的代码:
#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)
{
int i;
for (i = 0;i <= paraStack->top; i++)
{
printf("%c",paraStack->data[i]);
}//of for i
printf("\r\n");
} // of outputStack
// Initalize an empty char stack.No error checing for this function.
// @param paraStacPtr The pointer to the stack.It must be a pointer to change the stack.
// @param paraValus An int array storing all element.
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. Uptade 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("Connot 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.
char ch;
for( ch == 'a';ch <'m';ch ++)
{
printf("Pushing %c.\r\n",ch);
push(tempStack,ch);
outputStack(tempStack);
}//of for i
printf("----pushPopTest ends.-----\r\n");
} // of pushPopTest
// The entrance
void main()
{
pushPopTest();
} // of main
运行结果
abcd
Pushing e.
abcde
Pushing f.
abcdef
Pushing g.
abcdefg
Pushing h.
abcdefgh
Pushing i.
abcdefghi
Pushing j.
abcdefghij
Pushing k.
Cannot push element: stack full.
abcdefghij
Pushing l.
Cannot push element: stack full.
abcdefghij
Pop j.
abcdefghi
Pop i.
abcdefgh
Pop h.
abcdefg
---- pushPopTest ends. ----
打印
void outputStack(CharStackPtr paraStack)
{
int i;
for( i = 0; i <= paraStack->top; i ++ )
{
printf("%c",paraStack->data[i]);
} // of for i
printf("\r\n");
}// of outPutStack
入栈
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;
}
// Step 2. Update the top.
paraStackPtr->top ++;
// Step 3. Push element
paraStackPtr->data[paraStackPtr->top] = paraValue;
}
出栈
char pop(CharStackPtr paraStackPtr)
{
// Step 1. Space check.
if(paraStackPtr->top < 0)
{
printf("Cannot pop element: stack empty.\r\n");
return '\0';
}
// Step 2. Update the top.
paraStackPtr->top --;
// Step 3. Push element.
return paraStackPtr->data[paraStackPtr->top + 1];
}