当前位置: 首页 > article >正文

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

堆栈:

顺序存储:

#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)--];
}

链式存储:

#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;
}

队列:

顺序存储:

#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];
     
}

链式存储:

#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;

}


http://www.kler.cn/news/294770.html

相关文章:

  • 2024全国大学省数学建模竞赛A题-原创参考论文(部分+第一问代码)
  • 大数据-124 - Flink State 01篇 状态原理和原理剖析:状态类型 执行分析
  • 网页开发 HTML
  • [代码已更新]2024数学建模国赛高教社杯C题:农作物的种植策略 思路代码文章助攻手把手保姆级
  • uniapp网站和微信小程序 添加 百度统计
  • 一起学习LeetCode热题100道(71/100)
  • 大数据时代的技术hive:hive的数据类型和数据模型
  • How can I provide a RGBA png file to OpenAI PHP library
  • 前缀和 — 利用前缀信息解决子数组问题
  • 【Azure Redis】Redis-CLI连接Redis 6380端口始终遇见 I/O Error
  • 实践reflex:项目架构解析
  • 去中心化网络:Web3如何颠覆传统互联网
  • 标准IO与系统IO
  • Java架构师未来篇大模型
  • 新加坡服务器:亚洲地区的优选之选
  • 【软件工程】软件开发模型
  • k8s中的层级结构,及节点组件的作用
  • Termius for Mac/Win:高效、安全的跨平台多协议远程管理软件
  • 黑马点评2——商户查询缓存(P37店铺类型查询业务添加缓存练习题答案)redis缓存、更新、穿透、雪崩、击穿、工具封装
  • 2-85 基于matlab的FrFT下时变幅度LFM信号参数估计
  • ROADM(可重构光分插复用器)-介绍
  • LTE PSS主同步信号搜索 MATLAB实现
  • 开发台球助教小程序前景分析
  • 高效传输秘籍,揭秘Rsync和SCP的优劣,助你做出明智选择!
  • RedisStack十部曲之二:Redis的核心概念
  • (五)vForm 动态表单文件上传、下载
  • 代码随想录算法训练营Day37|完全背包问题、518.零钱兑换II、377. 组合总和 Ⅳ、70. 爬楼梯(进阶版)
  • 连续信号的matlab表示
  • nginx 新建一个 PC web 站点
  • 前端HTML基础笔记