• 数据结构PT2——堆栈/队列


    一、堆栈(FILO)

    堆栈是一种线性结构,也是一种特殊的线性表

    1:堆栈的顺序存储实现

        栈的顺序存储结构通常由一个一维数组和一个记录栈顶元素位置的变量组成

    1. #define MaxSize
    2. typeof struct SNode *Stack
    3. struct SNode{
    4. ElementType Data[MaxSize]
    5. int Top;
    6. };

    ①入栈

    1. void Push(Stack PtrS,ElementType item)
    2. {
    3. if(PtrS -> Top == MaxSize - 1){
    4. printf("堆栈满");return}
    5. else
    6. {
    7. PtrS -> Data[++(PtrS -> Top)] = item; //item到栈顶,先用再+
    8. return;
    9. }
    10. }

    ②出栈

    1. void Pop(Stack PtrS)
    2. {
    3. if(PtrS -> Top == - 1){
    4. printf("堆栈空");return ERROR;}
    5. else
    6. {
    7. return (PtrS -> Data[(PtrS -> Top)- -]); //先减再用
    8. }
    9. }

    2:堆栈的链式存储实现

    栈的链式存储结构实际上是一个单链表,叫链栈

    1. typedef struct SNode *Stack;
    2. struct SNode{
    3. ElementType Data;
    4. struct SNode *Next;
    5. };

    ①堆栈初始化/是否为空

    实际上是创建一个 堆栈的头节点,返回指针

    1. Stack CreateStack(){
    2. Stack S;
    3. S = (Stack) malloc (sizeof(struct SNode));
    4. S -> Next = NULL;
    5. return S;
    6. }

    1. int IsEmpty(Stack S)
    2. {
    3. return(S -> Next == NULL);
    4. }

    ②入栈

    1. void Push(ElementType item, Stack S) //S是指向栈的指针,单纯是一个指针,并没有元素
    2. {
    3. struct SNode *TmpCell;
    4. TmpCell = (struct SNode*) malloc(sizeof(struct SNode)); //创建新节点
    5. TmpCell -> Element = item;
    6. TmpCell -> Next = S -> Next; //S就如上定义,是一直指向栈顶的,S的Next是一直指向栈顶元素,这时候就是把有元素的Next指针指向了原栈顶元素
    7. S -> Next = TmpCell; //然后栈顶指针S继续指向栈顶
    8. }

    ③出栈

    1. ElementType Pop(Stack S)
    2. {
    3. struct SNode *FirstCell; //指向该结构的指针
    4. ElementType TopElem; //存储栈顶的值
    5. if(IsEmpty(S)){
    6. printf("堆栈空");return NULL;
    7. }
    8. else{
    9. FirstCell = S -> Next; //拿一个临时指针来指向栈顶元素
    10. S -> Next = FirstCell -> Next; //把S指向该栈顶的下一个元素
    11. TopElem = FirstCell -> Element; //元素赋值给该Element变量
    12. free(FirstCell); //释放临时指针
    13. return TopElem;
    14. }
    15. }

    二、队列(FIFO)

    队列是一种手操作约束的线性表

    1:队列的顺序存储实现

    队列的顺序存储结构通常由一个一维数组和一个记录队列头元素位置的变量front和一个记录队列尾位置的变量rear组成(加入元素,rear+1,删除元素,front+1)

    1. #define MaxSize
    2. struct QNode{
    3. ElementType Data[MaxSize];
    4. int rear;
    5. int front;
    6. };
    7. typedef struct QNode *Queue;

    ①入队列

    1. void AddQ(Queue PtrQ,ElementAType item)
    2. {
    3. if(PtrQ -> rear + 1) % MaxSize == PtrQ -> front){
    4. print("队列满");
    5. return;
    6. PtrQ -> rear = (PtrQ -> rear + 1)%MaxSize;
    7. PtrQ -> Data[PtrQ -> rear] = item;
    8. }

    ②出队列

    1. ElementType DeleteQ(Queue PtrQ)
    2. {
    3. if(PtrQ -> front = = PtrQ -> rear){
    4. print("队列空");
    5. return ERROR;
    6. }else{
    7. PtrQ -> front = (PtrQ -> front + 1)%MaxSize;
    8. return PtrQ -> Data[PtrQ -> front];
    9. }
    10. }

    2:队列的链式存储实现

    1. struct Node{
    2. ElementType Data;
    3. struct Node *Next;
    4. };
    5. struct QNode{
    6. struct Node *rear; //指向队尾
    7. struct Node *front; //指向队头
    8. };
    9. typedef struct QNode *Queue;
    10. Queue PtrQ;

    ①入队

     
    

    ②出队

    1. ElementType DeleteQ(Queue PtrQ)
    2. {
    3. struct Node *FrontCell;
    4. ElementType FrontElem;
    5. if(PtrQ -> front = = NULL){
    6. printf("队列空");return ERROR;
    7. }
    8. if(PtrQ -> front = = PtrQ -> rear);
    9. PtrQ -> front = PtrQ -> rear = NULL;
    10. else
    11. PtrQ -> front = PtrQ -> front -> Next;
    12. FrontElem = FrontCell -> Data;
    13. free(FrontCell);
    14. return FrontElem;
    15. }
  • 相关阅读:
    【Java进阶篇】第五章 集合(上)--Collection集合
    使用etcd选举sdk实践master/slave故障转移
    中国青少年棒球运动宣传·棒球7号位
    Win10下pytorch环境搭建详细教程以及示例测试(二)
    CentOS配置本地yum源
    【密码学】RSA的攻与防_2.0
    【IMX6ULL学习笔记之驱动学习02】LED字符设备驱动
    加班一周开发了报表系统,这个低代码免费IT报表神器太好用了
    【RocketMq 系列】RocketMq 消息重试机制、死信队列
    携带参数的退出功能
  • 原文地址:https://blog.csdn.net/leikang111/article/details/138069639