不定长链表队列C语言实现

#ifndef _CONST_H_
#define _CONST_H_

#include <stdio.h>
#include <stdlib.h>

typedef enum
{
False = 0,
True,
}Bool;

typedef int ElemType;

#define QUEUE_MAX_SIZE 10

#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 2

#define Null ((void *)0)

typedef enum
{
NORMAL = 0,
ERROR,
UNDERFLOW,
OVERFLOW,
STATUSCOUNT,
}Status;

#endif

#ifndef _LINKED_QUEUE_H_
#define _LINKED_QUEUE_H_

#include "Const.h"

typedef struct node
{
ElemType data;
struct node *pNext;
}Node, *pNode;

typedef struct linkedqueue
{
pNode pfront;
pNode prear;
}LinkedQueue, *pLinkedQueue;

Status InitLinkedQueue(pLinkedQueue pQ);

Bool IsLinkedQueueEmpty(pLinkedQueue pQ);

Bool EnLinkedQueue(pLinkedQueue pQ, ElemType elem);

Bool DeLinkedQueue(pLinkedQueue pQ, ElemType *e);

void DestoryLinkedQueue(pLinkedQueue pQ);

void ClearLinkedQueue(pLinkedQueue pQ);

Status GetLinkedQueueHead(pLinkedQueue pQ, ElemType *e);

int GetLinkedQueueLength(pLinkedQueue pQ);

Bool InvertLinkedQueue_Static(pLinkedQueue pQ);

Bool InvertLinkedQueue_Dynamic(pLinkedQueue pQ);

#endif

#include "LinkedQueue.h"
#include "Const.h"
#include "StaticStack.h"
#include "DynamicStack.h"

Status InitLinkedQueue(pLinkedQueue pQ)
{
pNode pN = (pNode)malloc(sizeof(Node));
if (pN == Null)
{
printf("No accessable free memory.\n");
return ERROR;
}
pN->pNext = Null;
pQ->pfront = pN;
pQ->prear = pN;
}

Bool IsLinkedQueueEmpty(pLinkedQueue pQ)
{
if (pQ->pfront == pQ->prear)
{
return True;
}
else
{
return False;
}
}

Bool EnLinkedQueue(pLinkedQueue pQ, ElemType elem)
{
pNode pTempNode = (pNode)malloc(sizeof(Node));
if (pTempNode == Null)
{
printf("No accessable free memory.\n");
return False;
}

pTempNode->data = elem;
pTempNode->pNext = Null;

pQ->prear->pNext = pTempNode;
pQ->prear = pTempNode;
}

Bool DeLinkedQueue(pLinkedQueue pQ, ElemType *e)
{
if (IsLinkedQueueEmpty(pQ))
{
printf("The Queue Is Empty.\n");
return False;
}
else
{
pNode pTempNode = pQ->pfront->pNext;
*e = pTempNode->data;
pQ->pfront->pNext = pTempNode->pNext;
//Only has one node in this linked queue.
if (pQ->prear == pTempNode)
{
pQ->prear = pQ->pfront;
}
free(pTempNode);
return True;
}
}

void DestoryLinkedQueue(pLinkedQueue pQ)
{
pNode p = pQ->pfront;
pNode q = Null;
while(p != Null)
{
q = p;
p = p->pNext;
free(q);
}
pQ->pfront = Null;
pQ->prear = Null;
}

void ClearLinkedQueue(pLinkedQueue pQ)
{
pNode p = pQ->pfront->pNext;
pNode q = Null;
while(p != Null)
{
q = p;
p = p->pNext;
free(q);
}
pQ->pfront->pNext = Null;
pQ->prear = pQ->pfront;
}

Status GetLinkedQueueHead(pLinkedQueue pQ, ElemType *e)
{
if (IsLinkedQueueEmpty(pQ))
{
printf("The linked queue is empty.\n");
return ERROR;
}
*e = pQ->pfront->pNext->data;
return NORMAL;
}

int GetLinkedQueueLength(pLinkedQueue pQ)
{
if (IsLinkedQueueEmpty(pQ))
{
return 0;
}
int Count = 0;
pNode pTemp = pQ->pfront;

while(pTemp->pNext != Null)
{
Count++;
pTemp = pTemp->pNext;
}
return Count;
}

Status QueueTraverse(pLinkedQueue pQ, void(*func)(ElemType*))
{
pNode pTemp = pQ->pfront->pNext;
while(pTemp != Null)
{
func(&pTemp->data);
pTemp = pTemp->pNext;
}
return NORMAL;
}

Bool InvertLinkedQueue_Static(pLinkedQueue pQ)
{
pStaticStack pSS = (pStaticStack)malloc(sizeof(StaticStack));
if (NORMAL != InitStaticStack(pSS))
{
return False;
}

ElemType V;
while(!IsLinkedQueueEmpty(pQ))
{
DeLinkedQueue(pQ, &V);
PushStaticStack(pSS, V);
}

while(!IsStaticStackEmpty(pSS))
{
PopStaticStack(pSS, &V);
EnLinkedQueue(pQ, V);
}
DestoryStaticStack(pSS);
return True;
}

Bool InvertLinkedQueue_Dynamic(pLinkedQueue pQ)
{
pDynamicStack pDS = (pDynamicStack)malloc(sizeof(DynamicStack));
if (NORMAL != InitDynamicStack(pDS))
{
return False;
}

ElemType V;
while(!IsLinkedQueueEmpty(pQ))
{
DeLinkedQueue(pQ, &V);
PushDynamicStack(pDS, V);
}

while(!IsDynamicStackEmpty(pDS))
{
PopDynamicStack(pDS, &V);
EnLinkedQueue(pQ, V);
}
DestoryDynamicStack(pDS);
return True;
}

时间: 2024-08-06 06:04:57

不定长链表队列C语言实现的相关文章

C语言格式化输入不定长数组

先随便写写,有空再整理. 直接贴代码 #include <stdio.h> #include <stdlib.h> //从一行标准输入中格式化输入一个不定长数组 void inputVec (); //读入给定行数的不定长数组 void inputVecs1 (); //读入不确定行数不定长数组 void inputVecs2 (); void main () { inputVecs1 (); return; } void inputVecs1 () { while (!feof(

C语言实现链表队列(基本操作及图示)

-------------------------------------------- 基本概念: 和栈相反,队列是一种先进先出(FIFO)的线性表.只允许在一端插入,在另一端删除. 允许插入的叫"队尾"(rear),允许删除的叫"队头"(front). 使用场景:操作系统的作业排队.在允许多道程序运行的计算机系统中,同时有几个作业运行.如果运行结果都需要通道输出,则按照请求输出的先后次序排队.每当通道传输完毕可以接受新的输出任务时,队头的作业先从队列中退出作输出

数据结构线性表链表的C语言实现

                                                                                      数据结构线性表链表的C语言实现      说明:线性表是一种最简单的线性结构,也是最基本的一种线性结构,所以它不仅是学习中的重点,也是应用开发非常常用的一种数据结构.它可以分为顺序表和链表.它的主要操作是数据元素的插入,删除,以及排序等.接下来,本篇文章将对线性表链表的基本操作和运用进行详细的说明(包含在源代码的注释中),并给

带头节点控制单链表之C语言实现

//  1.头.尾节点位置 //  2.链表 //  元素数量 #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h> //链表节点信息: // //   1.数据域 //   2.指针域 #define TRUE     (1) #define FALSE    (0) #define ZERO     (0) #define ONLY_ONE (1)

不定长内存池之apr_pool

内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.              不定长内存池.典型的实现有apr_pool.obstack.优点是不需要为不同的数据类型创建不同的内存池,缺点是造成分配出的内存不能回收到池中.这是由于这种方案以session为粒度,以业务处理的层次性为设计基础. 2.             定长内存池.典型的实现有LOKI.B

STL map vector(不定长数组)

啊啊啊!记住啊   多看几遍应该就好. 1 #include<cstdio> 2 #include<iostream> 3 #include<string> 4 #include<cctype>//1.测试字符ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符.控制字符等等.既支持单字节字符,也支持宽字符. 5 #in

30、不定长参数

def sum( a, b=10):b是缺省参数  ,向后排 result = a+b  sum( 12, 24 )  sum(a=12 ,  b)  a 是命名参数 def sum( a, b,*args,  * *kwargs ):是不定长参数,args是变量名,*是一个标识,告诉解释器要特殊对待 result = a+b sum(12, 23, 24, 15, m=3,n=5) print(agrs)   ( 24,15) print(kwargs) {'m':3, 'n': 5 } fo

链表队列

接下来把链表队列的代码分享给大家.因为还是链表操作,不做其他介绍. lqueue.h #ifndef _QUEUE_H #define _QUEUE_H #define MAXSIZE 10 typedef struct node { int data; struct node * next; } Node; typedef struct queue { Node * front; Node * rear; int size; } Queue; void q_init(Queue * queue

【C/C++学院】0802-链式栈/链表队列以及优先队列/封装链表库

链式栈 // stacklinknode.h #define datatype int struct stacknode { int num;//编号 datatype data;//数据 struct stacknode *pNext;//指针域 }; typedef struct stacknode StackNode;//简化 StackNode * init(StackNode * phead);//初始化 StackNode * push(StackNode * phead, int