[C] 纯文本查看 复制代码 #include<stdio.h>
#include<stdlib.h>
//链式队列 链式+队列
//定义结构体
//定义链式
struct Node {
int data;
struct Node* next;
};
//定义队列结构
struct queue {
struct Node* front;
struct Node* rear;
int dlsize;
};
//创建结点
struct Node* creatnode(int data) {
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
//创建队列-》初始化队列
struct queue* createQueue() {
struct queue* DL = (struct queue*)malloc(sizeof(struct queue));
DL->front = DL->rear = NULL;
DL->dlsize = 0;
return DL;
}
//入队
void push(struct queue **DL, int data)
{
//创建结点
struct Node* newnode = creatnode(data);
Node *TempFront = (*DL)->front;
Node *TempRear = (*DL)->rear;
//创建第一个值
if ((*DL)->dlsize == 0)
{
(*DL)->front = newnode;
(*DL)->front->next = NULL;
(*DL)->dlsize++;
return;
}
//创建n值
while (TempFront != NULL)
{
if (TempFront->next == NULL)
{
TempRear = (Node *)malloc(sizeof(Node));
TempRear = newnode;
TempFront->next = TempRear;
TempRear->next = NULL;
(*DL)->dlsize++;
return;
}
TempFront = TempFront->next;
}
}
//获取队头元素
int front(struct queue* DL)
{
if (DL->dlsize == 0)
{
printf("DL IS NULL"); return -1;
}
else
{
return DL->front->data;
}
}
//判断队列是否为空
int eledl(struct queue* DL)
{
return DL->dlsize == 0;
}
//出队
void dele(struct queue * DL)
{
if (DL->dlsize == 0)
{
printf("DL is null");
exit(0);
}
else
{
//struct Node* next = DL->front->next;
Node *temp = DL->front->next;
free(DL->front);
DL->front = temp;
DL->dlsize--;
}
}
int main()
{
//创建队列
struct queue* u = createQueue();
//压入值
push(&u, 1);
push(&u, 2);
push(&u, 3);
while (!eledl(u))
{
printf("%d\n", front(u));
dele(u);
}
system("pause");
return 0;
} |