国内大一html网站简单设计,桂林建设银行招聘网站,遵义会议在线,jsp小型网站开发代码学习就像一段长跑#xff0c;比的不是谁跑得快#xff0c;而是谁更能坚持#xff01;#xff01; 1 队列的概念及结构 队列#xff1a;只允许在一端进行插入数据操作#xff0c;在另一端进行删除数据操作的特殊线性表#xff0c;队列具有先进先出 FIFO(First In First O…学习就像一段长跑比的不是谁跑得快而是谁更能坚持 1 队列的概念及结构 队列只允许在一端进行插入数据操作在另一端进行删除数据操作的特殊线性表队列具有先进先出 FIFO(First In First Out) 入队列进行插入操作的一端称为队尾。 出队列进行删除操作的一端称为队头。 和栈不同的是队列的出队顺序是唯一的
2 队列的实现
分析
有两种实现队列的方式数组和链表。链表可以用单链表也可以用双链表。 使用链表的结构实现更优一些因为如果使用数组的结构出队列在数组头上出数据效率很低 数组实现效率低 链表实现单链表更合适 思考一个问题需要带哨兵位的头节点吗 其实都可以不带也可以可以不用判断直接尾插但是如果带了哨兵位的头节点要malloc最后也要free释放空间。 因为队列我们只需要入队尾插和出队头删单链表都可以实现不需要使用双链表。但是我们要想我们要怎么分清队头和队尾呢所以我们在尾插头删的时候
需要ptail指针维护队列最后一个元素需要phead指针维护队列第一个元素
那么这个时候实现起来就需要用到二级指针了。很不方便。 那么我们怎么解决这个问题呢不用二级指针的等效替换方法 ①带哨兵位的头节点。②返回值。③可以考虑用一个结构体封装起来。 这里我们用结构体。 代码实现
Test.c
#define _CRT_SECURE_NO_WARNINGS 1#includeQueue.hint main()
{Queue q;QueueInit(q);QueuePush(q, 1);QueuePush(q, 2);QueuePush(q, 3);printf(%d , QueueFront(q));QueuePop(q);printf(%d , QueueFront(q));QueuePop(q);QueuePush(q, 4);QueuePush(q, 5);while (!QueueEmpty(q)){printf(%d , QueueFront(q));QueuePop(q);}QueueDestroy(q);return 0;
}
函数声明Queue.h
#includestdio.h
#includeassert.h
#includestdlib.h
#includestdbool.htypedef int QDataType;//创建队列节点
typedef struct QueueNode
{QDataType val;struct QueueNode* next;
}QNode;//创建维护队列的指针
typedef struct Queue
{QNode* phead;QNode* ptail;int size;//原本是需要遍历的写在结构体里可以很好的是时间复杂度由O(N)变为O(1)
}Queue;//初始化
void QueueInit(Queue* pq);//空间释放
void QueueDestroy(Queue* pq);//尾插
void QueuePush(Queue* pq, QDataType x);//头删
void QueuePop(Queue* pq);//取队头的数据
QDataType QueueFront(Queue* pq);//取队尾的数据
QDataType QueueBack(Queue* pq);//判断是否为空
bool QueueEmpty(Queue* pq);//队列元素个数
int QueueSize(Queue* pq);
函数实现Queue.c
初始化QueueInit
void QueueInit(Queue* pq)
{assert(pq);pq-phead pq-ptail NULL;pq-size 0;
}
空间释放QueueDestroy
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur pq-phead;while (cur){QNode* next cur-next;free(cur);cur next;}pq-phead pq-ptail NULL;pq-size 0;
}
入队列QueuePush
这里需要创建一个节点
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode (QNode*)malloc(sizeof(QNode));if (newnode NULL){perror(malloc fail);return;}newnode-val x;newnode-next NULL;if (pq-ptail NULL){pq-ptail pq-phead newnode;}else{pq-ptail-next newnode;pq-ptail newnode;}pq-size;
}
出队列QueuePop
要注意两种情况
空链表只有一个元素ptail野指针的情况要进行判断置空
void QueuePop(Queue* pq)
{assert(pq);//链表不为空assert(pq-phead);QNode* del pq-phead;pq-phead pq-phead-next;free(del);del NULL;//链表中只有一个元素删完以后为空if (pq-phead NULL)pq-ptail NULL;pq-size--;
}
队头元素QueueFront
QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq-phead);return pq-phead-val;
}
队尾元素QueueBack
QDataType QueueBack(Queue* pq)
{assert(pq); assert(pq-ptail);return pq-ptail-val;
}
判断队列是否为空QueueEmpty
bool QueueEmpty(Queue* pq)
{assert(pq);return pq-phead NULL;
}
队列元素个数QueueSize
int QueueSize(Queue* pq)
{assert(pq);return pq-size;
}
Queue.c总代码
#define _CRT_SECURE_NO_WARNINGS 1#includeQueue.hvoid QueueInit(Queue* pq)
{assert(pq);pq-phead pq-ptail NULL;pq-size 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur pq-phead;while (cur){QNode* next cur-next;free(cur);cur next;}pq-phead pq-ptail NULL;pq-size 0;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode (QNode*)malloc(sizeof(QNode));if (newnode NULL){perror(malloc fail);return;}newnode-val x;newnode-next NULL;if (pq-ptail NULL){pq-ptail pq-phead newnode;}else{pq-ptail-next newnode;pq-ptail newnode;}pq-size;
}void QueuePop(Queue* pq)
{assert(pq);// assert(pq-phead);QNode* del pq-phead;pq-phead pq-phead-next;free(del);del NULL;if (pq-phead NULL)pq-ptail NULL;pq-size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);// assert(pq-phead);return pq-phead-val;
}QDataType QueueBack(Queue* pq)
{assert(pq); assert(pq-ptail);return pq-ptail-val;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq-phead NULL;
}int QueueSize(Queue* pq)
{assert(pq);return pq-size;
}
以上就是用单链表实现队列的代码实现。