栈与队列--创建栈/队列
2016-06-28 10:06
459 查看
数组栈
完成Stack CreateStack(int MaxElements)函数,该函数创建一个栈,MaxElements为与分配的栈空间大小(栈可用空间为Array[0,…MaxElements-1])。
链栈
完成Stack CreateStack(void)函数,该函数创建一个栈(空栈,带头结点),并返回栈指针。
数组队列
完成Queue CreateQueue(int MaxElements)函数,该函数创建一个基于数组的队列,并返回队列指针,其中MaxElements为预分配的数组单元数。
链队列
完成Queue CreateQueue(void)函数,该函数创建一个基于链表的队列,并返回队列指针,创建的链表队列不带头节点。
完成Stack CreateStack(int MaxElements)函数,该函数创建一个栈,MaxElements为与分配的栈空间大小(栈可用空间为Array[0,…MaxElements-1])。
typedef int ElemType; struct StackRecord; typedef struct StackRecord *Stack; struct StackRecord { int Capacity; //栈容量 int Top; //栈顶,初始为1 ElemType *Array; }; Stack CreateStack(int MaxElements) { Stack s=(StackRecord *)malloc(sizeof(StackRecord)); s‐>Top=‐1; s‐>Capacity=MaxElements; s‐>Array=(ElemType *)malloc(MaxElements*sizeof(ElemType)); return s; }
链栈
完成Stack CreateStack(void)函数,该函数创建一个栈(空栈,带头结点),并返回栈指针。
typedef int ElemType; struct Node; typedef struct Node * PtrToNode; typedef PtrToNode Stack; struct Node { ElemType data; PtrToNode next; }; Stack CreateStack(void) { Stack top; Node *s; top=NULL; s=(Node *)malloc(sizeof(Node)); s‐>next=top; top=s; return top; }
数组队列
完成Queue CreateQueue(int MaxElements)函数,该函数创建一个基于数组的队列,并返回队列指针,其中MaxElements为预分配的数组单元数。
typedef int ElemType; struct QueueRecord; typedef struct QueueRecord * Queue; struct QueueRecord { int Capacity; //队列总容量 int Front; //队首 初始值为0 int Rear; //队尾,初始值为1 int Size; //队列中数据数,初始值为0 ElemType *Array; }; Queue CreateQueue(int MaxElements) { Queue s=(QueueRecord *)malloc(sizeof(QueueRecord)); s‐>Front=0; s‐>Rear=‐1; s‐>Capacity=MaxElements; s‐>Array=(ElemType *)malloc(MaxElements*sizeof(ElemType)); return s; }
链队列
完成Queue CreateQueue(void)函数,该函数创建一个基于链表的队列,并返回队列指针,创建的链表队列不带头节点。
typedef int ElemType; struct node; typedef struct node Node; struct queue; typedef struct queue * Queue; struct node { ElemType data; Node * next; }; struct queue { Node * front; //队首 Node * rear; //队尾 int size; //队列中数据数 }; Queue CreateQueue(void) { Queue q; q=(queue *)malloc(sizeof(queue)); q‐>front=NULL; q‐>rear=NULL; q‐>size=0; return q; }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- C#线程队列用法实例分析
- 算法系列15天速成 第九天 队列
- C++基于栈实现铁轨问题
- 数据结构之Treap详解
- C语言单链队列的表示与实现实例详解
- 浅析C语言中堆和栈的区别
- C语言栈的表示与实现实例详解
- C语言实现颠倒栈的方法
- 算法系列15天速成 第十天 栈
- mysql 队列 实现并发读
- C#队列Queue用法实例分析
- C#多线程处理多个队列数据的方法
- C语言循环队列的表示与实现实例详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表