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

堆栈:

顺序存储:

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;

}
相关推荐
艾莉丝努力练剑4 分钟前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法
ZZZS051613 分钟前
stack栈练习
c++·笔记·学习·算法·动态规划
hans汉斯39 分钟前
【人工智能与机器人研究】基于力传感器坐标系预标定的重力补偿算法
人工智能·算法·机器人·信号处理·深度神经网络
vortex52 小时前
算法设计与分析:分治、动态规划与贪心算法的异同与选择
算法·贪心算法·动态规划
前端拿破轮3 小时前
🤡🤡🤡面试官:就你这还每天刷leetcode?连四数相加和四数之和都分不清!
算法·leetcode·面试
地平线开发者3 小时前
征程 6|工具链量化简介与代码实操
算法·自动驾驶
DoraBigHead3 小时前
🧠 小哆啦解题记——谁偷改了狗狗的台词?
算法
Kaltistss3 小时前
240.搜索二维矩阵Ⅱ
线性代数·算法·矩阵
轻语呢喃3 小时前
每日LeetCode:合并两个有序数组
javascript·算法
大熊猫侯佩4 小时前
Swift 数学计算:用 Accelerate 框架让性能“加速吃鸡”
算法·swift