链栈
main.cpp:
#include <stdio.h>
#include <stdlib.h>
typedef int TYPE;
struct node
{
TYPE data;
node* next;
};
node* node_create()
{
node* temp = (node*)malloc(sizeof(node));
temp->data = 0;
temp->next = NULL;
return temp;
}
bool isEmpty(node* head)
{
return head== NULL ? true:false;
}
TYPE front(node* nn)
{
return nn->data;
}
void push(node* &head,TYPE val) {
node* temp = node_create();
temp->data = val;
temp->next = head;
head = temp;
}
TYPE pop(node* &head)
{
if (isEmpty(head)) return 0;
node* temp = head;
head = head->next;
TYPE tt = temp->data;
free(temp);
return tt;
}
void show(node* head)
{
node* temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
node* head = node_create();
push(head, 1);
push(head, 2);
push(head, 3);
show(head);
printf("pop出来的是%d\n", pop(head));
push(head, 4);
push(head, 5);
printf("pop出来的是%d\n", pop(head));
show(head);
free(head);
return 0;
}
结果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/662f547353d4015dc8c3066750af0057.png)
数组队列
#include <stdio.h>
int arr[4] = {0};
int size = 0;
bool full()
{
return size == 4 ? true : false;
}
bool empty()
{
return size == 0 ? true : false;
}
void push(int val) {
if (full()) return;
arr[size] = val;
size++;
}
int pop()
{
if (empty()) return 0;
int temp = arr[0];
for (int i = 0; i < 3; i++)
{
arr[i] = arr[i + 1];
}
size--;
return temp;
}
int main()
{
push(1);
push(2);
push(3);
printf("是否满%d\n", full()); //判断是否满
printf("返回的是%d\n", pop()); //返回1
printf("返回的是%d\n", pop()); //返回2
printf("返回的是%d\n", pop());//返回3
printf("是否空%d\n", empty()); //判断是否空,返回true
push(10);
push(20);
printf("返回的是%d\n", pop()); //返回10
printf("返回的是%d\n", pop()); //返回20
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/290a9a0187d68eb5cc27ed58ea6c3b90.png)