pop | 弹出链表头 |
push | 把一个元素压入链表 |
CreateStack | 创建一个链表 |
DeleteStack | 删除链表 |
IsEmpty | 检测链表是否为空 |
PrintStack | 打印链表所有元素 |
#include<cstdio>
typedef struct StackNode
{
int data;
StackNode* next;
}StackNode;
typedef struct StackLink
{
StackNode* top;
int length;
}StackLink;
void CreateStack(StackLink* S)
{
S->top = nullptr;
S->length = 0;
}
bool IsEmpty(StackLink* S)
{
return S->top == nullptr;
}
void push(StackLink* S, int num)
{
StackNode* newnode = new StackNode();
newnode->data = num;
newnode->next = S->top;
S->top = newnode;
S->length++;
}
int pop(StackLink* S)
{
if (IsEmpty(S))
{
printf("The stack is empty...\n");
return -1;
}
StackNode* popnode = S->top;
int popdata = popnode->data;
S->top = popnode->next;
delete(popnode);
S->length--;
return popdata;
}
void DeleteStack(StackLink* S)
{
while (!IsEmpty(S))
{
pop(S);
}
}
bool PrintStack(StackLink* S)
{
if (IsEmpty(S))
{
printf("The stack is empty...\n");
return 0;
}
StackNode* cur = S->top;
while (cur != nullptr)
{
printf("%d\n", cur->data);
cur = cur->next;
}
return 1;
}
int main()
{
return 0;
}