赞
踩
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:以下是本篇文章正文内容,下面案例可供参考
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 getfrontQueue(LinkQueue* ps) { assert(ps != NULL); elemstyle item; if (ps->frontNode != NULL) { item = ps->frontNode->data; printf("%5d\n", item); } //获取队头元素是不用删除结点的; }
该处使用的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); }
提示:这里对文章进行总结:
队列和链表结构相似,作为一种数据存储结构,要熟练掌握他的性质,先进先出的特点在很多的方面会有应用。下一期我会更新顺序队列,有需要的可以留意一下,c语言上有问题的可以留言也可加V,虽然我才大一,但是在我能力范围内的我都会回答的。我们共同进步;
V:16655560651;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。