栈,队列的C语言实现

栈的C语言实现
队列的C语言实现

栈:后进先出  允许插入和删除的一端叫栈顶top  不允许的一端叫栈底bottom
主要操作:进栈、出栈、判断栈满和栈空
有两个现成的函数 int push(int s[],int x,int *ptop), int pop(int s[],int *py,int *ptop)  直接拿过来用就行   要压入的栈空间 要进栈的数 栈顶指针

队列:先进先出  允许插入的叫队尾rear  允许数据离开的叫队头front
主要操作:入队、出队、判断队满和队空
有两个现成的函数  int EnQueue(int *Q,int x,int *pf,int *pr)    pf:队头指针
直接拿过来用就行  int DeQueue(int *Q,int *py,int *pf,int *pr)  pr:队尾指针
                  Q:要进入的队空间  x:要进入队的数 py:要出队的数


(a) 线性队列
(b) 循环队列
队头:
    front
队尾:
    rear
队满条件:
    (rear+1)%MAX=front
队空条件:
    rear=front
入队:rear = (rear+1)%MAX
出队:front = (front+1)%MAX

习题答案
1、
//进栈算法
#include "stdio.h"
#define stacksize 100            /*定义stacksize为常数100 */

int push(int s[],int x,int *ptop)
{    
    int top;
     top=*ptop;                    //ptop是指针变量;*ptop获得实际栈顶指针
    if ( top==stacksize )        //栈满
     {
        printf("overflow\n");
        return 0;
    }
    else
    {
        s[top]=x;
        top++;
        *ptop=top;                //实际栈顶指针加1,返回到调用函数处
        return 1;   
    }
}

int pop(int s[],int *py,int *ptop)
{
    int top;
    top=*ptop;                    //ptop是指针变量;*ptop获得实际栈顶指针
    if (top==0)                    //栈空    
    {
       printf("stack empty\n");
       return 0;
    }
   else
   {    
       --top;
       *py=s[top];                //实际栈顶指针减1,返回到调用函数处
       *ptop=top;    
        return 1;
   }
}


int main()
{
    static int s[stacksize];
    int top=0,result,y;
    int i;

    result=push(s,11,&top);        //将11压进栈中
    result=push(s,22,&top);        //将22压进栈中
    result=push(s,33,&top);        //将33压进栈中
    printf("top=%d\n",top);

    for (i=0;i<3;i++)
    {
        result=pop(s,&y,&top);    //从栈中弹出
        printf("top=%d,y=%d\n",top,y);
    }

}

2、
//循环队列中加入一个元素的算法:
//设Q[MAX]表示循环队列

#define MAX 7
#include "stdio.h"

int EnQueue(int *Q,int x,int *pf,int *pr)
{
    int front,rear;
    front = *pf;
    rear=*pr;
    if ( (rear+1) % MAX == front )     //判断队满
        return 0;                
    else
    {
        Q[rear]=x;                    //将数据存入队列
        rear = ( rear+1 ) % MAX;    //队尾指针移位
         *pr=rear;                    //保存队尾指针
         return 1;
    }
}


int DeQueue(int *Q,int *py,int *pf,int *pr)
{
    int front,rear;
    front=*pf;
    rear=*pr;
    if (front==rear)                //判断队空
        return 0;                        
    else
    {
        *py=Q[front];                //将数据读出
        front=(front+1) % MAX;        //队头指针移动
        *pf=front;                    //保存队头指针
        return 1;  
    }
}

int main()
{
    static int s[MAX];
    
    int rear = 0,front = 0;
    int b=0;
    EnQueue(s,11,&front,&rear);        //入队操作
    printf("front=%d,rear=%d\n",front,rear);
    EnQueue(s,12,&front,&rear);        //入队操作
    printf("front=%d,rear=%d\n",front,rear);
    EnQueue(s,13,&front,&rear);        //入队操作
    printf("front=%d,rear=%d\n",front,rear);
    printf("***********\n");
    DeQueue(s,&b,&front,&rear);        //出队操作
    printf("front=%d,rear=%d\n",front,rear);
    printf("**%d**\n",b);
    DeQueue(s,&b,&front,&rear);        //出队操作
    printf("front=%d,rear=%d\n",front,rear);
    printf("**%d**\n",b);
    DeQueue(s,&b,&front,&rear);        //出队操作
    printf("front=%d,rear=%d\n",front,rear);
    printf("**%d**\n",b);
}

转载于:https://www.cnblogs.com/qinkai/archive/2012/04/02/2429600.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用实现队列C语言代码如下: ```c #include <stdio.h> #include <stdlib.h> // 定义结构 typedef struct { int* data; // 存储数据的数组 int top; // 顶指针 int maxSize; // 的最大容量 } Stack; // 初始化 void initStack(Stack* stack, int maxSize) { stack->data = (int*)malloc(sizeof(int) * maxSize); stack->top = -1; stack->maxSize = maxSize; } // 判断是否为空 int isEmpty(Stack* stack) { return stack->top == -1; } // 判断是否已满 int isFull(Stack* stack) { return stack->top == stack->maxSize - 1; } // 入操作 void push(Stack* stack, int value) { if (isFull(stack)) { printf("Stack is full!\n"); return; } stack->data[++stack->top] = value; } // 出操作 int pop(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty!\n"); return -1; } return stack->data[stack->top--]; } // 使用实现队列 typedef struct { Stack* inStack; // 入队 Stack* outStack; // 出队 } MyQueue; // 初始化队列 void initQueue(MyQueue* queue, int maxSize) { queue->inStack = (Stack*)malloc(sizeof(Stack)); queue->outStack = (Stack*)malloc(sizeof(Stack)); initStack(queue->inStack, maxSize); initStack(queue->outStack, maxSize); } // 入队操作 void enqueue(MyQueue* queue, int value) { if (isFull(queue->inStack)) { printf("Queue is full!\n"); return; } // 将元素压入入队 push(queue->inStack, value); } // 出队操作 int dequeue(MyQueue* queue) { if (isEmpty(queue->outStack)) { // 如果出队为空,则将入队的元素依次弹出并压入出队 while (!isEmpty(queue->inStack)) { push(queue->outStack, pop(queue->inStack)); } } return pop(queue->outStack); } int main() { MyQueue queue; initQueue(&queue, 5); enqueue(&queue, 1); enqueue(&queue, 2); enqueue(&queue, 3); printf("%d\n", dequeue(&queue)); printf("%d\n", dequeue(&queue)); enqueue(&queue, 4); enqueue(&queue, 5); printf("%d\n", dequeue(&queue)); printf("%d\n", dequeue(&queue)); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值