队列queue 栈stack入门

版权声明:谢谢 https://blog.csdn.net/Waybyway/article/details/83152872

1 队 queue 这是啊哈算法的小方法  入门在后面

#include <stdio.h> //队列 queue
struct queue{//利用结构体 建立队 = 数组*1 + 头head + 尾tail
    int data[100];//创造 queue队 的数据类型
    int head;
    int tail;
};
int main(){
    struct queue qu;
    /*产生一个 名为qu队,

    */
    int i,n;
    scanf("%d",&n);
    qu.head=0;
    qu.tail=0;
    for(i=0;i<n;i++){
        scanf("%d",&qu.data[i]);
        qu.tail++;
    }
    while(qu.head<qu.tail){
        printf("%d ",qu.data[qu.head]);
        qu.head++;
        qu.data[qu.tail]=qu.data[qu.head];
        qu.tail++;
        qu.head++;
    }
    return 0;
}

2 栈入门,这一段是C语言形式

#include <stdio.h>// 基本创立栈  初始化 入栈 出栈
#include <stdlib.h>
#define M 10
#define OK 1
#define ERROR 0
#define TRUE 1      //  用来TRUE FALSE在入栈出栈方法中返回
#define FALSE 0     //  入出栈方法可以用bool类型代替
typedef struct{
    int data[M];
    int top;
}Sqstack;
/*
    借助结构体 typedey struct
    建立结构体数据类型  Sqstack
    包括  一个数组data【M】 一个栈顶top
*/
void InitSqstack(Sqstack *s){
    s=(Sqstack *)malloc(sizeof(Sqstack));//  malloc分配空间函数需要 stdlib.h头文件
    //初始化 分配一个顺序站空间,首地址存在s中,栈顶top=-1为空
    s->top=-1;
}
/*
    初始化栈  分配空间,将栈顶置为-1 地下室层
    初始化 Initialization
*/
/*
    【】 (4层)          【 】                【】
    【】 (3)            【d】   top=3        【】  top--
    【】 (2)    —>      【c】      ->        【c】 top=3
    【】 (1)            【b】                【b】
    【】 (0层,底层)    【a】   top++        【a】
    初始  top=-1  地下层
    栈可以看成一个木桶  FILO先进后出原则
    abcd输入入栈  先输入的位于底层 随着top++ 变化 后输入的在顶层
    出栈  top--   先把顶层先输出 后输出底层  即 first in last out
*/
int SqstackPush(Sqstack *s,int x){
    if(s->top==M-1){// 入栈先检查栈 满了吗?
        return FALSE;
    }/*
        top++;栈顶向上移动读入元素
        入栈
    */
    s->top++;
    s->data[s->top]=x;
    return TRUE;
}
int SqstackPop(Sqstack *s,int *x){
    if(s->top==-1){// 出栈检查  空栈无可输出内容
        return FALSE;
    }else{
        /*
            赋x的值为栈顶元素,栈顶元素出栈
            top--
        */
        *x=s->data[s->top];
        s->top--;
        return TRUE;
    }
}

int main(){
    Sqstack s1;
    int i=0,n,m;
    InitSqstack(&s1);
    printf("请输入栈元素:\n");
    while(i<M){
        scanf("%d",&n);
        SqstackPush(&s1,n);
        i++;
    }
    /*while((scanf("%d",&n)!=EOF)){   //此处错误,输入栈元素会卡住
        SqstackPush(&s1,n);
    }*/
    printf("输出栈内元素:\n");
    for(i=0;i<M;i++){
        if(SqstackPop(&s1,&m))
            printf("%d ",m);
    }//printf("\n");
    return 0;
}
//部分代码参看属猪结构  和 csdn博主_muauma_
//https://blog.csdn.net/qq1032796097/article/details/78666768

3这段代码用c一只没有写出来,后面用c++ 结合两个文件完成 

#include <stdio.h>
#include <stdlib.h>
#define M 10
typedef struct{
    int data[M];
    int head;
    int tail;
}SqQueuee;
void InitQueue(SqQueuee *q){
    q=(SqQueuee * )malloc(sizeof(SqQueuee));
    q->head=q->tail=-1;
}
int QueueEnter(SqQueuee *q,int x){
    if(q->tail==M-1){
        return 0;
    }
    q->tail++;
    q->data[q->tail]=x;
    return 1;
}
int QueueExit(SqQueuee *q,int *x){
    if(q->head==q->tail){
        return 0;
    }
    q->head++;
    *x=q->data[q->head];
    return 1;
}

int main(){
    SqQueuee q1;
    int i=0,j=0,n=0,m=0;
    InitQueue(&q1);
    printf("请输入队列元素:\n");
    while(i<M){
        scanf("%d",&n);
        if(QueueEnter(&q1,n))
            i++;
    }
    printf("请输出队列元素:\n");
    /*while(j<M){
        if(QueueExit(&q1,&m))//有错误输出不了
            printf("%d ",m);
        j++;
    }*/
    return 0;
}

4下面是利用c++写的 两个文件(1++.cpp 和 11++.cpp位于同一文件夹下)

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define M 50
typedef struct{
    int data[M];
    int head;
    int tail;
}SqQueue;/*
    顺序存储队列  包括  一个数组+头head和+尾tail
    满足先入先出FIFO原则,读入数据,向尾巴后面加 tail++
    读出数据 位于头上的先出去 head++
    队列如同一个队伍
    见下行:
    头|先输入先输出【0】->【1】 ->【2】-> 【3】-> 【4】-> 【5】|尾
    head++向右移动负责输出        tail++向右移动将 排队 把数据输入
*/

void InitQueue(SqQueue *&q){
    //初始化这个队,分配一个队空间
    //首地址赋给q,将 head和tail都置为-1
    q=(SqQueue *)malloc(sizeof(SqQueue));
    q->head=q->tail=-1;
}
bool EnterQueue(SqQueue *&q,int e){
    if(q->tail == M-1){
        return false;
    }//如果队满,达到M-1,不可输入Entet
    q->tail++;
    q->data[q->tail]=e;
    return true;
}
bool ExitQueue(SqQueue *&q,int &e){
    if(q->head == q->tail){
        return false;
    }
    //如果队的头尾在一起 说明队中没有数据,不能读出
    q->head++;
    e=q->data[q->head];
    return true;
}
#include "1++.cpp"//1++.cpp 和 11++两个文件必须在同一文件夹下
//1++。cpp写了队的基本算法,11++.cpp负责使用
int main(){
    SqQueue *q;//创建 SqQueue类型的指针
    int x,n,i;
    InitQueue(q);//初始化队列
    /*
    读入十个元素,读入到队
    读出十个队中元素,读出到N中
    */
    for(i=0;i<10;i++){
        scanf("%d",&x);
        EnterQueue(q,x);
    }
    for(i=0;i<10;i++){
        ExitQueue(q,n);
        printf("%d ",n);
    }
    return 0;
}

5栈的C++写法

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define M 50
using namespace std;
typedef char ElemType;
typedef struct{
    ElemType data[M];
    int top;
}SqStack;

void InitStack(SqStack *&s){
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=-1;
}

void DestroyStack(SqStack *&s){
    free(s);
}

bool StackEmpty(SqStack *s){
        return (s->top == -1);
}

bool Push(SqStack *&s,ElemType e){
    if(s->top == M-1){
        return false;
    }
    s->top++;
    s->data[s->top]=e;
    return true;
}

bool Pop(SqStack *&s,ElemType &e){
    if(s->top == -1){
        return false;
    }
    e=s->data[s->top];
    s->top--;
    return true;
}

bool GetTop(SqStack *&s,ElemType &e){
    if(s->top == -1){
        return false;
    }
    e=s->data[s->top];
    return true;
}

猜你喜欢

转载自blog.csdn.net/Waybyway/article/details/83152872