栈和队列都属于线性表
属于"一对一"逻辑关系
栈:“先进后出”
队列:“先进先出”
看图理解(概念只是辅助理解_理解了才算学会)
入栈:
// **元素入栈
// 参数: 存储结构, 栈顶指针, 数据
// 返回值: 栈顶指针(需要知道结束入栈之后栈顶的位置)
int pushElem(int* arr, int top, int val)
{
arr[++top] = val; // 初始为-1 数组越界,先移动top位置,后添加数值
printf("元素%d入栈\n", val);
return top;
}
出栈:
// **元素出栈
// 参数: 存储结构, 栈顶指针
// 返回值: 栈顶指针
int popElem(int* arr, int top)
{
// 判断
if (top <= -1)
{
printf("空栈!\n");
return -1;
}
printf("元素%d出栈\n",arr[top]);
top--; // 向前移动,但原来的数据还在,无需清除,在添加新元素的时候将它替换
return top;
}
完整代码+测试:
#include
// **元素入栈
// 参数: 存储结构, 栈顶指针, 数据
// 返回值: 栈顶指针(需要知道结束入栈之后栈顶的位置)
int pushElem(int* arr, int top, int val)
{
arr[++top] = val; // 初始为-1 数组越界,先移动top位置,后添加数值
printf("元素%d入栈\n", val);
return top;
}
// **元素出栈
// 参数: 存储结构, 栈顶指针
// 返回值: 栈顶指针
int popElem(int* arr, int top)
{
// 判断
if (top <= -1)
{
printf("空栈!\n");
return -1;
}
printf("元素%d出栈\n",arr[top]);
top--; // 向前移动,但原来的数据还在,无需清除,在添加新元素的时候将它替换
return top;
}
int main()
{
// 数组
int arr[100];
// top指针(下标)
int top = -1;
//入栈
top = pushElem(arr, top, 1); //函数返回新的top值赋给top
top = pushElem(arr, top, 2);
top = pushElem(arr, top, 3);
top = pushElem(arr, top, 4);
top = pushElem(arr, top, 5);
//出栈
top = popElem(arr, top);
top = popElem(arr, top);
top = popElem(arr, top);
top = popElem(arr, top);
top = popElem(arr, top);
top = popElem(arr, top);
}
// **元素入栈
// 参数: 存储结构, 栈顶指针, 数据
// 返回值: 栈顶指针(需要知道结束入栈之后栈顶的位置)
int pushElem(int* arr, int top, int val);
// **元素出栈
// 参数: 存储结构, 栈顶指针
// 返回值: 栈顶指针
int popElem(int* arr, int top);
一般将链表的头部作为栈顶
入栈:
// **添加元素
// 参数: 头指针,数据
// 返回值: 头指针
Node* pushElement(Node* stack, int Data) {
// 1 申请内存
Node* NewNode = (Node*)malloc(sizeof(Node));
// 2 初始化节点
NewNode->data = Data;
// 3 新的节点作为新的头节点
NewNode->pnext = stack;
// 4 头指针指向新的头节点
stack = NewNode;
// 5 返回头指针
return stack;
}
出栈:
// **删除元素
// 参数: 头指针
// 返回值: 头指针
Node* popElement(Node* stack) {
// 1 判断是否为空栈
if (stack)
{
// 临时指针 保存栈顶(头指针)
Node* p = stack;
// 头指针后移
stack = stack->pnext; // 与顺序栈一样,无需清除,在添加新元素的时候将它替换
// 打印数据
printf("原来栈顶元素:%d ",p->data);
// 再次判断是否到了栈顶底部
if (stack)
{
printf("当前栈顶元素:%d\n",stack->data);
}
else
{
printf("栈顶已经空了!\n");
}
// 释放节点
free(p);
}
else
{
printf("是一个空栈!\n");
}
return stack;
}
完整代码+效果展示
#include
#include
// 节点
typedef struct Node {
int data;
struct Node* pnext;
}Node;
// **添加元素
// 参数: 头指针,数据
// 返回值: 头指针
Node* pushElement(Node* stack, int Data) {
// 1 申请内存
Node* NewNode = (Node*)malloc(sizeof(Node));
// 2 初始化节点
NewNode->data = Data;
// 3 新的节点作为新的头节点
NewNode->pnext = stack;
// 4 头指针指向新的头节点
stack = NewNode;
// 5 返回头指针
return stack;
}
// **删除元素
// 参数: 头指针
// 返回值: 头指针
Node* popElement(Node* stack) {
// 1 判断是否为空栈
if (stack)
{
// 临时指针 保存栈顶(头指针)
Node* p = stack;
// 头指针后移
stack = stack->pnext; // 与顺序栈一样,无需清除,在添加新元素的时候将它替换
// 打印数据
printf("原来栈顶元素:%d ",p->data);
// 再次判断是否到了栈顶底部
if (stack)
{
printf("当前栈顶元素:%d\n",stack->data);
}
else
{
printf("栈顶已经空了!\n");
}
// 释放节点
free(p);
}
else
{
printf("是一个空栈!\n");
}
return stack;
}
int main()
{
Node* p = NULL;
// 入栈
for (int i = 0; i < 10; i++)
{
p = pushElement(p, i);
}
// 出栈
for (int i = 0; i < 11; i++)
{
p = popElement(p);
}
}
Node* pushElement(Node* stack, int data);
Node* popElement(Node* stack);
栈!\n");
}
return stack;
}
int main()
{
Node* p = NULL;
// 入栈
for (int i = 0; i < 10; i++)
{
p = pushElement(p, i);
}
// 出栈
for (int i = 0; i < 11; i++)
{
p = popElement(p);
}
}
Node* pushElement(Node* stack, int data);
Node* popElement(Node* stack);