## 栈 ##
栈(stack),是一种线性存储结构,它有以下几个特点:
(01) 栈中数据是按照”后进先出(LIFO, Last In First Out)”或者先进后出(FILO)方式进出栈的。
(02) 向栈中添加/删除数据时,只能从栈顶进行操作。
定义初始链表
struct Node;
typedef struct Node *T;
const int N = 5;
int NUM = 0;
struct Node
{
int num;
int i;
T next;
};
void Creat(T &node)
{
node = (T)malloc(sizeof(struct Node));
if (node == NULL)
exit(0);
node->next = NULL;
node->i = NUM;
}
- 进栈
void Push(T &node, int inset)
{
if (node == NULL)
Creat(node);
T temp;
temp = (T)malloc(sizeof(struct Node));
temp->num = inset;
temp->next = node->next;
node->next = temp;
node->next->i = NUM++;
}
- 出栈
void Pop(T node)
{
T pop;
if(node -> next == NULL)
return;
else
{
pop = node->next;
node->next = pop->next;
free(pop);
}
}