隊列(Queue)
一般的順序隊列:
由于這種結構會有假溢出的情況,所以一般不選擇這種隊列,而更多的使用循環隊列。
循環隊列:
判斷隊列滿的情況:
1、count來計數;通常使用count
Count等于隊列的MAXSIZE
2、Flag标志 int
入隊列 flag=1 出隊列flag=0
Front=rear&&flag==0
3、把一個存儲單元空出來,不存放數據
//#include <stdio.h>
//#include <stdlib.h>
//
//typedef struct node //一個節點的信息
//{
// int data;
// struct node *pnext;
//}Qnode;
//
//typedef struct //隊列 隊頭 對位
//{
// Qnode *front; //隊頭的節點
// Qnode *rear; //隊尾節點
//}Queue;
//
////初始化
//void LQueueInit(Queue*);
////入隊
//void LQueueIn(Queue*, int);
////出隊列
//int LQueueOut(Queue*, int*);
//
//int main() //計算機有時候有些事情先做
//{
// int data;
// Queue myQueue;
//
// LQueueInit(&myQueue);
//
// for (int i = 0; i < 10; i )
// {
// LQueueIn(&myQueue, i 1); //1 2 3 4 5 6 7 8 9 10
// }
//
// for (int i = 0; i < 10; i )
// {
// LQueueOut(&myQueue, &data);
// printf("%d\t", data);
// }
//
// printf("front=%d\nrear=%d\n", myQueue.front, myQueue.rear);
//
// LQueueIn(&myQueue, 11); //11
//
// printf("front=%d\nrear=%d\n", myQueue.front, myQueue.rear); //一個節點 指向同一個位置
//
// LQueueIn(&myQueue, 12);
// printf("front=%d\nrear=%d\n", myQueue.front, myQueue.rear);
//
//
// return 0; //賺錢,不義之财。解決大部分人的問題最賺錢。
//}
//
////初始化
//void LQueueInit(Queue *Q) //帶空頭節點的棧
//{
// Q->front = NULL;
// Q->rear = NULL;
//}
//
////入隊
//void LQueueIn(Queue *Q, int data)
//{
// Qnode *pnew; //新節點
// pnew = (Qnode *)malloc(sizeof(Qnode)); //開辟空間
// pnew->data = data; //給新節點賦值
// pnew->pnext = NULL; //把新節點的pnext指向空
//
// //隊尾 1、隊尾節點有内容 2、原來的時候隊尾啥也沒有
// if (Q->rear != NULL)
// {
// Q->rear->pnext = pnew; //1、隊尾節點有内容
// }
//
// Q->rear = pnew; //2、原來的隊尾啥也沒有
//
// if (Q->front == NULL) //front 有内容的時候 什麼都不做 沒有有内容的時候
// {
// Q->front = pnew;
// }
//}
//
////出隊列
//int LQueueOut(Queue *Q, int *data)
//{
// //malloc開辟的空間 釋放 front改變掉 能不能找到我出去的節點
// Qnode *pOut;
// if (Q->front == NULL)
// {
// printf("隊列空\n");
// return 0;
// }
// else
// {
// *data = Q->front->data; //把隊頭的數據賦值給data
// pOut = Q->front; //先用臨時指針pOut保存隊頭的數據
// Q->front = Q->front->pnext; //把隊頭往後移動 //這裡玩去能夠知道front是不是空
//
// if (Q->front == NULL)
// {
// Q->rear = NULL;
// }
// free(pOut);
// return 1;
// }
//}
//動态存儲
/*
#include <stdlib.h>
malloc:動态開辟一塊内存 (void *)malloc(size)
free();釋放内存
calloc(): (void *)calloc(unsigned n,unsigned size);
在内存中動态開辟n個 大小為size的内存。存儲空間是連續的。
realloc(): (void *)realloc(void *ptr,size_t size)
改變ptr這個指針所指向的内存空間的大小。
double *d=(double *)malloc(sizeof(double));
int *i;
i=realloc(d,sizeof(int));
總結:Void * 強制轉換為任意的指針類型。
*/
//#include <stido.h>
//
//struct stu
//{
// int data;
// struct stu *pnext;
//};
//
//int main()
//{
// struct stu *p;
// p = (struct stu *)malloc(sizeof(struct stu));
//
// return 0;
//}
今天的講解就到這裡,希望對大家有所幫助!後期會持續講解C基礎的! (C語言C 交流學習群560655063)
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!