浙大数据结构:堆栈和队列的定义与操作

堆栈:

顺序存储:

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
typedef int position ;
#define MAXSIZE 100
#define ERROR -1
struct SNode 
{
    ElementType  * Data ;
    position top;
    int Maxsize;
};

typedef struct SNode *Stack;

Stack CreateStack(int Maxsize)
{
    Stack s=(Stack)malloc(sizeof(struct SNode));
    s->Data=(ElementType*)malloc(Maxsize*sizeof(ElementType));
    s->top=-1;
    s->Maxsize=Maxsize;
    return s;
}

bool isfull(Stack s)
{
    return (s->top==s->Maxsize-1);
}

bool push(Stack s,ElementType x)
{
  if(isfull(s))return false;
  s->Data[++(s->top)]=x;
  return true;
}

bool isempty(Stack s)
{
    return (s->top==-1);
}

ElementType pop(Stack s)
{
    if(isempty(s))return ERROR;
    return s->Data[(s->top)--];
}

链式存储:

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
typedef int position ;
#define MAXSIZE 100
#define ERROR -1
typedef struct SNode *Stack;
struct SNode 
{
    ElementType   Data ;
     Stack next;
};

Stack CreateStack(int Maxsize)
{
    Stack s;
    s=(Stack)malloc(sizeof(struct SNode));
    s->next =NULL;
    return s;
}


bool push(Stack s,ElementType x)
{
Stack tmp;
tmp=(Stack)malloc(sizeof(struct SNode));
tmp->Data=x;
tmp->next=s->next;
s->next=tmp;
  return true;
}

bool isempty(Stack s)
{
    return (s->next==NULL);
}

ElementType pop(Stack s)
{
    Stack head;
    ElementType topelement;
    if(isempty(s))return ERROR;
    head=s->next;
    topelement=head->Data;
    s->next=head->next;
    free(head);
    return topelement;
}

队列:

顺序存储:

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
typedef int position ;
#define MAXSIZE 100
#define ERROR -1
typedef struct QNode *Queue;
struct QNode
{
    ElementType * data;
    position front,rear;
    int MaxSize;
};

Queue CreateQueue(int Maxsize)
{
    Queue Q=(Queue)malloc(sizeof(struct QNode));
    Q->data=(ElementType*)malloc(Maxsize*sizeof(ElementType));
    Q->front=0,Q->rear=0;
    Q->MaxSize=Maxsize;
    return Q;
}

bool isfull(Queue q)
{
    return ((q->rear+1)%q->MaxSize==q->front);
}

bool addq(Queue q,ElementType x)
{
    if(isfull(q))return false;
    q->rear=(q->rear+1)%q->MaxSize;
    q->data[q->rear]=x;
    return true;
}

bool isempty(Queue q)
{
    return  (q->front==q->rear);
}

ElementType Delete(Queue q)
{
    if(isempty(q))return ERROR;
    q->front=(q->front+1)%q->MaxSize;
    return q->data[q->front];
     
}

链式存储:

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
#define MAXSIZE 100
#define ERROR -1
typedef struct Node *ptrQueue;
typedef ptrQueue position ;
struct Node
{
    ElementType  data;
    position next;
};
typedef struct QNode *Queue;
struct QNode
{
    position front ,rear;
    int MaxSize;
};

bool isempty(Queue q)
{
  return q->front==NULL;
}

ElementType Delete(Queue q)
{
    position head;
    ElementType headelem;
    if(isempty(q))return ERROR;

    head=q->front;
    if( q->front==q->rear)
    q->front=q->rear=NULL;
     else
    q->front=head->next;

     headelem=head->data;
    free(head);
    return headelem;

}
相关推荐
积极向上的向日葵2 分钟前
有效的括号题解
数据结构·算法·
GIS小天8 分钟前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__29 分钟前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃29 分钟前
计算矩阵A和B的乘积
线性代数·算法·矩阵
不爱写代码的玉子33 分钟前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
Java 技术轻分享39 分钟前
《树数据结构解析:核心概念、类型特性、应用场景及选择策略》
数据结构·算法·二叉树··都差速
芜湖xin1 小时前
【题解-洛谷】P1706 全排列问题
算法·dfs
chao_7892 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
曦月逸霜2 小时前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法