C语言栈和队列-实验代码-学习笔记

#include"stdio.h"
#include<stdio.h>
#include<malloc.h>

//顺序栈
//#define true 1
//#define MAXSIZE 3
//
//
//typedef struct
//{
// int data[MAXSIZE];
// int top;
//}stu;
//
//int judge(stu s)
//{
// if (s.top == -1)
// return 1;
// else
// return 0;
//}
//
//void push(stu* s, int e)
//{
// if (s->top == MAXSIZE - 1)
// {
// printf(“栈满了\n”);
// return;
// }
// s->data[++s->top] = e;
//}
//
//int pop(stu* s)
//{
// if (s->top == -1)
// {
// printf(“栈空了\n”);
// return -1;
// }
// return s->data[s->top–];
//}
//
//void print(stu s)
//{
// while (!judge(s))
// {
// printf(“%d”, pop(&s));
// }
//}
//
//int main()
//{
// stu s;
// s.top = -1;
// push(&s, 3);
// push(&s, 6);
// push(&s, 9);
// print(s);
// return 0;
//}

//共享栈

//#define MAXSIZE 6
//
//typedef struct {
// int data[MAXSIZE];
//
// int top1;
// int top2;
//
//}list;
//
//int judge(list s, int num)
//{
// if (num == 1)
// return s.top1 == -1;
// else
// return s.top2 == MAXSIZE;
//}
//
//void push(list s, int e, int num)
//{
// if (s->top1 + 1 == s->top2)
// {
// printf(“栈满了\n”);
// return;
// }
// if (num == 1)
// {
// s->data[++s->top1] = e;
// }
// else
// {
// s->data[–s->top2] = e;
// }
//}
//
//int pop(list
s, int num)
//{
// if (num == 1)
// {
// if (s->top1 == -1)
// {
// printf(“栈空了\n”);
// return -1;
// }
// else
// {
// return s->data[s->top1–];
// }
// }
// else
// {
// if (s->top2 == MAXSIZE)
// {
// printf(“栈空了\n”);
// return -1;
// }
// else
// {
// return s->data[s->top2++];
// }
// }
//}
//
//void print(list s, int num)
//{
// if(num==1)
// {
// while (s.top1 > -1)
// {
// printf(“%d”, s.data[s.top1–]);
// }
// }
// else
// {
// while (s.top2 < MAXSIZE)
// {
// printf(“%d”, s.data[s.top2++]);
// }
// }
// printf(“\n”);
//}
//
//int main()
//{
// list s;
// s.top1 = -1;
// s.top2 = MAXSIZE;
// if (judge(s, 1) == 1)
// {
// printf(“1栈是空的\n”);
// }
// if (judge(s, 2) == 1)
// {
// printf(“2栈是空的\n”);
// }
// push(&s, 1, 1);
// push(&s, 3, 1);
// push(&s, 5, 1);
//
// push(&s, 2, 2);
// push(&s, 4, 2);
// push(&s, 6, 2);
//
// print(s, 1);
// print(s, 2);
// printf(“%d\n”, s.top1);
// printf(“%d\n”, s.top2);
// printf(“%d\n”,judge(s,1));
// printf(“%d\n”,judge(s,2));
// if (judge(s, 1)==1)
// {
// printf(“1栈是空的\n”);
// }
// if (judge(s, 2)==1)
// {
// printf(“2栈是空的\n”);
// }
//
// pop(&s, 1);
// pop(&s, 2);
//
// print(s, 1);
// print(s, 2);
//}

//链栈

//typedef struct node {
// int data;
// struct node* next;
//}node;
//
//typedef struct {
// int cnt;
// node* top;
//}list;
//
//list init()
//{
// list s;
// s.cnt = 0;
// s.top = (node*)malloc(sizeof(node));
// s.top = NULL;
// return s;
//}
//
//int judge(list s)
//{
// return s.cnt == 0;
//}
//
//void push(list* s, int e)
//{
// node* p = (node*)malloc(sizeof(node));
// p->data = e;
// p->next = s->top;
// s->top = p;
// s->cnt++;
//}
//
//void pop(list* s)
//{
// if (s->top == NULL)
// {
// printf(“栈空了\n”);
// return;
// }
// node* p;
// printf("%d ", s->top->data);
// p = s->top;
// s->top = s->top->next;
// free§;
// s->cnt–;
//}
//
//int main()
//{
//
// list s = init();
// push(&s, 1);
// push(&s, 3);
// push(&s, 5);
// pop(&s);
// pop(&s);
// pop(&s);
// pop(&s);
// return 0;
//}

//顺序队列
//
//#define MAXSIZE 10
//
//typedef struct list
//{
// int data[MAXSIZE];
// int front, rear;
//}list;
//
//void init(list* s)
//{
// s->front = s->rear = 0;
//}
//
//void judge(list s)
//{
// if (s.front == s.rear) return 1;
// return 0;
//}
//
//void push(list* s, int e)
//{
// if (s->rear + 1 <= MAXSIZE)
// {
// s->data[s->rear++] = e;
// }
//}
//
//int pop(list* s)
//{
// return s->data[s->front++];
//}
//
//void print(list s)
//{
// for (int i = s.front; i < s.rear; i++)
// printf(“%d \n”, s.data[i]);
//}
//
//int main()
//{
// list s;
// init(&s);
//
// push(&s,1);
// push(&s, 2);
// push(&s, 3);
// push(&s, 4);
//
// print(s);
//
// pop(&s);
// pop(&s);
//
// print(s);
//
// return 0;
//}

//循环队列
//
//#define MAXSIZE 5
//
//typedef struct list
//{
// int data[MAXSIZE];
// int front, rear;
//
//}list;
//
//void init(list* q)
//{
// q->front = q->rear = 0;
//}
//
//int judge(list q)
//{
// return q.front == q.rear;
//}
//
//int isfull(list q)
//{
// return (q.rear + 1) % MAXSIZE == q.front;
//}
//
//
//int length(list q)
//{
// return (q.rear - q.front + MAXSIZE) % MAXSIZE;
//}
//
//void push(list* q, int e)
//{
// if (isfull(q))
// {
// printf(“队伍满 入队失败\n”);
// return;
// }
// q->data[q->rear] = e;
// q->rear = (q->rear + 1) % MAXSIZE;
//}
//
//void pop(list
q)
//{
// if (judge(*q))
// {
// printf(“队伍空,出队失败~\n”);
// return;
// }
// printf(“%d出队\n”, q->data[q->front]);
// q->front = (q->front + 1) % MAXSIZE;
//}
//
//void print(list q)
//{
// printf(“打印队列元素:\n”);
// while (!judge(q))
// {
// printf(“%d”, q.data[q.front]);
// q.front = (q.front + 1) % MAXSIZE;
// }
// printf(“\n”);
//}
//
//int main()
//{
// list q;
// init(&q);
//
// printf(“队伍长度为:%d\n”, length(q));
// pop(&q);
//
// printf(“指针值:%d %d %d\n”, q.front, q.rear, isfull(q));
// push(&q, 1);
// printf(“指针值:%d %d %d \n”, q.front, q.rear, isfull(q));
// printf(“数组值:%d %d %d %d %d\n”, q.data[0], q.data[1], q.data[2], q.data[3], q.data[4]);
// push(&q, 2);
// printf(“指针值:%d %d %d \n”, q.front, q.rear, isfull(q));
// printf(“数组值:%d %d %d %d %d\n”, q.data[0], q.data[1], q.data[2], q.data[3], q.data[4]);
// push(&q, 3);
// printf(“指针值:%d %d %d \n”, q.front, q.rear, isfull(q));
// printf(“数组值:%d %d %d %d %d\n”, q.data[0], q.data[1], q.data[2], q.data[3], q.data[4]);
// push(&q, 4);
// printf(“指针值:%d %d %d \n”, q.front, q.rear, isfull(q));
// printf(“数组值:%d %d %d %d %d\n”, q.data[0], q.data[1], q.data[2], q.data[3], q.data[4]);
// push(&q, 5);
// printf(“指针值:%d %d %d \n”, q.front, q.rear, isfull(q));
// printf(“数组值:%d %d %d %d %d\n”, q.data[0], q.data[1], q.data[2], q.data[3], q.data[4]);
//
//
// pop(&q);
// pop(&q);
//
// printf(“打印前指针值:%d %d \n”, q.front, q.rear);
//
// print(q);
//
// printf(“打印后指针值:%d %d \n”, q.front, q.rear);
//
// return 0;
//}

//链队伍

typedef struct node
{
int data;
struct node* next;
}node;

typedef struct list {
node* front, * rear;
int length;
}list;

void init(list* q)
{
q->front = (node*)malloc(sizeof(node));
q->rear = q->front;
q->length = 0;
}

int judge(list s)
{
if (s.length == 0)return 1;
return 0;
}

void push(list* s, int e)
{
node* p = (node*)malloc(sizeof(node));
p->data = e;
p->next = NULL;
s->rear->next = p;
s->rear = p;
s->length++;
}

void pop(list* q)
{
if (judge(q))
{
printf(“队伍空 出队失败\n”);
return;
}
node
p = q->front->next;
printf(“%d出队成功\n”, p->data);
q->front->next = p->next;
free§;
q->length–;
}

void length(list s)
{
printf(“当前队伍长度为%d\n”, s.length);
}

void print(list s)
{
if (judge(s))
{
printf(“队伍空\n”);
return;
}
node* p = s.front->next;
while (p != NULL)
{
printf(“%d”, p->data);
p = p->next;
}
printf(“\n”);
}

int main()
{
list s;
init(&s);
print(s);

push(&s,1);
push(&s, 2);
push(&s, 3);
push(&s, 4);

length(s);

print(s);

pop(&s);

print(s);

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值