#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node,*Linklist;
void push(Linklist* rear) {
int value;
printf("请输入压入数 : ");
scanf_s("%d", &value, 4);
getchar();
Linklist node = (Linklist)malloc(sizeof(Node));
node->data = value;
node->next = NULL;
(*rear)->next = node;
*rear = node;
}
void pop(Linklist front) {
Linklist p = front->next;
if (p != NULL) {
front->next = p->next;
free(p);
}
else {
printf("队列为空无法删除!");
}
}
void show(Linklist front) {
Linklist p = front->next;
printf("\n");
if (p != NULL) {
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
}
else {
printf("队列无数据打印!");
}
}
void free_data(Linklist front) {
Linklist p = front,q = NULL;
while (q!=NULL) {
q = p->next;
free(p);
p = q;
}
}
int main() {
//初始化队列
Linklist rear = (Linklist)malloc(sizeof(Node));
Linklist front = rear;
int i = 0;
do{
printf("压入请输入1,弹出请输入0,退出请输入-1 : ");
scanf_s("%d",&i,4);
getchar();
if (i == 1) {
push(&rear);
}
else if (i == 0) {
pop(front);
}
} while (i != -1);
show(front);
free_data(front);
return 0;
}