前言
链式队列----用链表实现,链式队列就是一个操作受限的单向链表;
提示:以下是本篇文章正文内容,下面案例可供参考
一、队列初始化;
LinkQueue* Init_LinkQueue() { LinkQueue* ps = (LinkQueue*)malloc(sizeof(LinkQueue)); ps->frontNode = ps->tailNode = NULL; ps->cursize = 0; return ps; }
二、代码实现;
1.入队
代码如下(示例):
void pushQueue(LinkQueue* ps, elemstyle data) { assert(ps != NULL); Node* newnode = creatNode(data); if (ps->cursize == 0) { ps->frontNode = ps->tailNode=newnode; } else { ps->tailNode->next = newnode; ps->tailNode == newnode; } ps->cursize++; }
2.出队
代码如下(示例):
int popLinkQueue(LinkQueue* ps) { assert(ps != NULL); if (ps->cursize == 0) { printf("队列为空,出队失败;\n"); } else { Node* nextNode = ps->frontNode->next; free(ps->frontNode); ps->frontNode = nextNode; ps->cursize--; //return *item; } } //获取队头元素; int getfrontQueue(LinkQueue* ps) { assert(ps != NULL); elemstyle item; if (ps->frontNode != NULL) { item = ps->frontNode->data; printf("%5d\n", item); } //获取队头元素是不用删除结点的; }
3.源代码实现:
该处使用的url网络请求的数据。
//链式队列; #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<Windows.h> #include<assert.h> #define maxsize 5 #define true 2 #define false -2 #define ok 1 #define erro -1 typedef int elemstyle; typedef struct node { elemstyle data; struct node* next; }Node; typedef struct queue { Node* frontNode; Node* tailNode; int cursize; }LinkQueue; //创建结点; Node* creatNode(elemstyle data) { Node* newnode = (Node*)malloc(sizeof(Node)); newnode->data = data; newnode->next = NULL; return newnode; } //链表的初始化; LinkQueue* Init_LinkQueue() { LinkQueue* ps = (LinkQueue*)malloc(sizeof(LinkQueue)); ps->frontNode = ps->tailNode = NULL; ps->cursize = 0; return ps; } //入队; void pushQueue(LinkQueue* ps, elemstyle data) { assert(ps != NULL); Node* newnode = creatNode(data); if (ps->cursize == 0) { ps->frontNode = ps->tailNode=newnode; } else { ps->tailNode->next = newnode; ps->tailNode == newnode; } ps->cursize++; } //出队; int popLinkQueue(LinkQueue* ps) { assert(ps != NULL); if (ps->cursize == 0) { printf("队列为空,出队失败;\n"); } else { Node* nextNode = ps->frontNode->next; free(ps->frontNode); ps->frontNode = nextNode; ps->cursize--; //return *item; } } //判空; int emptyLinkQueue(LinkQueue* ps) { assert(ps != NULL); if (ps->cursize == 0) { return true; } else { return false; } } //获取队头元素; int getfrontQueue(LinkQueue* ps) { assert(ps != NULL); elemstyle item; if (ps->frontNode != NULL) { item = ps->frontNode->data; printf("%5d\n", item); } //获取队头元素是不用删除结点的; } //获取队列长度; void lengthQueue(LinkQueue* ps) { assert(ps != NULL); printf("队列的长度为:%5d\n", ps->cursize); } //队列打印函数; void printQueue(LinkQueue* ps) { assert(ps != NULL); //elemstyle item; if (ps->cursize = 0) { printf("链表为空,无法打印;\n"); return; } while (ps->cursize != 0) { //获取队头元素; getfrontQueue(ps); //出队; popLinkQueue(ps); } } //调试函数; int main(void) { LinkQueue *ps; elemstyle item; //队列初始化; ps=Init_LinkQueue(); int data; for (int i = 0; i < maxsize; i++) { scanf("%d", &data); pushQueue(ps, data); } //获取队头元素; printf("队头元素为:"); getfrontQueue(ps); //获取队列长度; lengthQueue(ps); //队列打印函数; printQueue(ps); }