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

堆栈:

顺序存储:

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;

}
相关推荐
碧海银沙音频科技研究院11 分钟前
i2s的LRCK时钟有毛刺以及BCLK数据在高采样率有变形数据解析错误问题原因以及解决方法
人工智能·深度学习·算法·分类·音视频
kida_yuan1 小时前
【从零开始】17. 中文摘要提取工具
python·算法·数据分析
水蓝烟雨1 小时前
0430. 扁平化多级双向链表
数据结构·链表
未到结局,焉知生死1 小时前
PAT每日三题
算法
阿巴~阿巴~2 小时前
Linux线程与进程的栈管理、页表机制及线程封装
数据结构·线程·进程·线程封装·页表机制·栈管理
进击的炸酱面2 小时前
第三章 线性模型
人工智能·算法·机器学习
立志成为大牛的小牛2 小时前
数据结构——三十一、最小生成树(王道408)
数据结构·学习·程序人生·考研·算法
CoovallyAIHub2 小时前
一致性模型:单步生成高质量图像,破解扩散模型速度瓶颈
深度学习·算法·计算机视觉
JMzz2 小时前
Rust 中的数据结构选择与性能影响:从算法复杂度到硬件特性 [特殊字符]
开发语言·数据结构·后端·算法·性能优化·rust
CoovallyAIHub3 小时前
搞定边缘AI部署:开源神器RamaLama,让视觉语言模型无处不在
深度学习·算法·计算机视觉