c语言全栈路线(C语言基础队列详细讲解)
队列(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)
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com