- #include
- #include "./03_linkQueue.h"
- int main(int argc, const char *argv[])
- {
- linkpos* pos = create_linkQueue();
-
- pushbyTail_linkQueue(pos,10);
- pushbyTail_linkQueue(pos,20);
- pushbyTail_linkQueue(pos,30);
- pushbyTail_linkQueue(pos,40);
- show_linkQueue(pos);
-
-
- return 0;
- }
- #include
- #include
- #include "./03_linkQueue.h"
-
- /*
- * function: 创建一个空队列
- * @param [ in]
- * @param [out]
- * @return
- */
- linkpos* create_linkQueue()
- {
- linkpos* pos=(linkpos*)malloc(sizeof(linkpos));
-
- pos->front=(linkQueue*)malloc(sizeof(linkQueue));
-
- if(pos->front==NULL)
- {
- printf("创建链队列失败\n");
- }
-
- pos->front->text.len=0;
- pos->front->next=NULL;
-
- pos->rear=pos->front;
-
- return pos;
- }
-
- /*
- * function: 尾插
- * @param [ in]
- * @param [out]
- * @return
- */
- void pushbyTail_linkQueue(linkpos* pos,dataType num)
- {
- linkQueue* temp =(linkQueue*)malloc(sizeof(linkQueue));
-
- if(temp==NULL)
- {
- printf("创建结点失败\n");
- }
-
- temp->text.data = num;
- temp->next=NULL;
-
- temp->next=NULL;
- pos->rear->next=temp;
-
-
- //更新rear的位置
- pos->rear=pos->rear->next;
-
- pos->front->text.len++;
-
- return;
- }
-
- /*
- * function: 头删
- * @param [ in]
- * @param [out]
- * @return
- */
- //判空
- int isEmpty(linkpos* pos)
- {
- return pos->rear==NULL ? 1:0;
- }
- dataType deletebyHead_linkQueue(linkpos* pos)
- {
- if(isEmpty(pos)==1)
- {
- printf("链队列为空,不可删除\n");
- }
-
- linkQueue* q=pos->front->next;
-
- pos->front->next=q->next;
-
- dataType num = q->text.data;
- free(q);
- q=NULL;
-
- return num;
- }
-
- /*
- * function: 遍历
- * @param [ in]
- * @param [out]
- * @return
- */
- void show_linkQueue(linkpos* pos)
- {
- while(pos->front->next!=NULL)
- {
- pos->front=pos->front->next;
- printf("%d ",pos->front->text.data);
- }
- printf("\n");
-
- return;
- }
-
- #ifndef QUEUE_H_
- #define QUEUE_H_
-
- typedef int dataType;
-
- union msg
- {
- dataType data;
- int len;
- };
- typedef struct node
- {
- union msg text;
- struct node* next;
- }linkQueue;
-
- typedef struct
- {
- linkQueue* front;
- linkQueue* rear;
- }linkpos;
-
- linkpos* create_linkQueue();
- void pushbyTail_linkQueue(linkpos* pos,dataType num);
- dataType deletebyHead_linkQueue(linkpos* pos);
- void show_linkQueue(linkpos* pos);
-
- #endif