栈&队列

1.栈

  栈的定义:只允许在一端进行插入和删除操作的线性表

  栈的数学性质:n个不同元素进栈,出栈元素不同排列的个数为Cn2n/(n+1),这个公式称为卡特兰数

  栈及其操作的实现:

    以顺序表为存储结构的栈:

/**@数据结构:栈->顺序栈 
 **@作者:9761滴 
 **@实现方式:静态分配(即分配固定大小的数组,数组长度不可变) 
 **/ 

//一种数据结构的操作无非 创建删除,增删改查
//本文件中实现了顺序栈的
    /*1.init
     **2.push
    **3.pop
    **4.getHead 
    **5.isEmpty
    */ 
//等操作 
#include<cstdio>
#define MAX_SIZE 10
typedef int ElementType;
typedef struct{
    ElementType data[MAX_SIZE];
    int top;
}SqStack;
bool isEmpty(SqStack stack);
bool init(SqStack &stack){
    stack.top=-1;
    return true;
}
bool push(SqStack &stack,ElementType x){
    if(stack.top==MAX_SIZE-1)
        return  false;
    stack.data[++stack.top]=x;
    return true;
}
bool pop(SqStack &stack,ElementType &x){
    if(isEmpty(stack))
        return  false;
    x=stack.data[stack.top--];
    return true;
}
ElementType getTop(SqStack stack){
    if(isEmpty(stack)){
        printf("stack is already empty");
        return  -1;
    }
    return stack.data[stack.top];
}
bool isEmpty(SqStack stack){
    return stack.top==-1;
}

 

  

    带头节点的链栈:

/**@数据结构:栈->链栈 
 **@作者:9761滴 
 **@是否带头结点:是 
 **/ 

//本文件中实现了链栈的
    /*1.init
     **2.push
    **3.pop
    **4.getHead 
    **5.isEmpty
    */ 
//等操作 
#include<cstdio>
#include<stdlib.h> 
using namespace std;
typedef char ElementType; 
typedef struct LNode{
    ElementType data;
    LNode *next;
}LNode,*LStack;
bool isEmpty(LStack &LS);
bool init(LStack &LS){
    LS=(LNode*)malloc(sizeof(LNode));
    if(LS==NULL){
        return false;
    }
    LS->next=NULL;
    return true;
}
bool push(LStack &LS,ElementType x){
    LNode* LN=(LNode*)malloc(sizeof(LNode));
    if(LN==NULL)
        return false;
    LN->next=LS->next;
    LN->data=x;
    LS->next=LN;
}

bool pop(LStack &LS,ElementType &x){
    if(isEmpty(LS))
        return false;
    LNode* LN=LS->next;
    LS->next=LS->next->next;
    x=LN->data;
    free(LN);
}

ElementType getHead(LStack &LS){
    if(isEmpty(LS)){
        printf("Stack is already empty");
        return -1;
    }
    return LS->next->data;
}
bool isEmpty(LStack &LS){
    return LS->next==NULL;
}

 

   

    不带头节点的链栈:

/**@数据结构:栈->链栈 
 **@作者:9761滴 
 **@是否带头结点:否  
 **/ 

//本文件中实现了链栈的
    /*1.init
     **2.push
    **3.pop
    **4.getHead 
    **5.isEmpty
    */ 
//等操作
#include<cstdio>
#include<stdlib.h>
typedef int ElementType;
typedef struct LNode{
    ElementType data;
    LNode *next;
}LNode,*LStack;
bool isEmpty(LStack LS);
bool init(LStack &LS){
    LS==NULL;
} 
bool push(LStack &LS,ElementType x){
    if(LS==NULL){
        LS=(LNode*)malloc(sizeof(LNode));
        if(LS==NULL)
            return false;
        LS->data=x;
        LS->next=NULL;
    }
    else{
        LNode *LN=(LNode*)malloc(sizeof(LNode));
        if(LN==NULL)
            return false;
        LN->data=x;
        LN->next=LS;
        LS=LN;
    }
    return true;
}
bool pop(LStack &LS,ElementType &x){
    if(isEmpty(LS)){
        printf("Stack is already empty");
        return false;
    }
    else{
        LNode *LN=LS;
        x=LN->data;
        LS=LS->next;
        free(LN);
    }
    return true;
}
ElementType getHead(LStack LS){
    if(isEmpty(LS)){
        printf("Stack is already empty");
        return false;
    }
    else{
        ElementType x=LS->data;
        return x;
    }

}
bool isEmpty(LStack LS){
    return LS==NULL;
}

 

 

2.队列

  队列定义:只能在一端进行插入,在另一端进行删除的线性表,允许插入的一端叫做队尾,允许删除的一段叫做队头

  队列及其操作的实现:

    顺序队列:

/**@数据结构:队列->循环队列 
 **@作者:9761滴 
 **@实现方式:静态分配(即分配固定大小的数组,数组长度不可变) 
 **@rear指向最后一个元素的下一个元素 
 **/ 

//一种数据结构的操作无非 创建删除,增删改查
//本文件中实现了循环队列的
    /*1.init
     **2.enQueue 
    **3.deQueue
    **4.getHead 
    **5.isEmpty
    */ 
//等操作 
#include<cstdio>
#define MAX_SIZE 10
typedef int ElementType;
typedef struct SqQueue{
    ElementType data[MAX_SIZE];
    int front,rear;
//    int size;    方便判断队空或者队满
//    int tag;    方案二    ,入队之后置为1,出队之后置为0        
}Queue;
bool init(Queue &Q){
    Q.front=Q.rear=0;
//  Q.size=0; 
//    Q.tag=0;
    return true;
}
bool enQueue(Queue &Q,ElementType x){
    if((Q.rear+1)%MAX_SIZE==Q.front)
//        if(size==MAX_SIZE)
//        if(Q.tag==1&&Q.rear=Q.front)
        return false;
    Q.data[Q.rear]=x;
    Q.rear=(Q.rear+1)%MAX_SIZE;
//    Q.size++;
//    Q.tag=1;
    return true;
}
bool deQueue(Queue &Q,ElementType &x){
    if(Q.rear==Q.front)
//    if(Q.size==0)
//    if(Q.tag==0&&Q.rear=Q.front)
        return false;
    x=Q.data[Q.front];
    Q.front=(Q.front+1)%MAX_SIZE;
//    Q.size--;
//    Q.tag=0;
    return true;
}
bool getHead(Queue Q,ElementType &x){
    if(Q.front==Q.rear)
//    if(Q.size==0)
//    if(Q.tag==0&&Q.rear=Q.front)
        return false;
    x=Q.data[Q.front];
    return true;
}
bool isEmpty(Queue Q){
    if(Q.front==Q.rear)
//    if(Q.size==0)
        return true;
    return false;
}

 

    链队列:

/**@数据结构:队列->链队列 
 **@作者:9761滴 
 **@带头结点 
 **/ 

//本文件中实现了链队列的
    /*1.init
     **2.enQueue 
    **3.deQueue
    **4.getHead 
    **5.isEmpty
    */ 
//等操作 
#include<cstdio>
#include<stdlib.h>
typedef int ElementType;
typedef struct LNode{
    ElementType data;
    LNode* next;
}LNode;
typedef struct LQueue{
    LNode* front;
    LNode* rear;
}LQueue;
bool init(LQueue &Q){
    Q.front=Q.rear=(LNode*)malloc(sizeof(LNode));
    if(Q.front==NULL||Q.rear==NULL)
        return false;
    Q.front->next=NULL;
    return true;
}
bool enQueue(LQueue &Q,ElementType x){
    LNode* L=(LNode*)malloc(sizeof(LNode));
    if(L==NULL)
        return false;
    L->data=x;
    L->next=NULL;
    Q.rear->next=L;
    Q.rear=L;
    return true;
}
bool deQueue(LQueue &Q,ElementType &x){
    if(Q.front==Q.rear)
        return false;
    LNode* L=Q.front->next;
    x=L->data;
    Q.front->next=L->next;
    if(Q.rear==L)            //如果删除的是最后一个结点,需要修改rear,使其指向头结点 
        Q.rear=Q.front;
    free(L);
    return true;
}
bool getHead(LQueue Q,ElementType &x){
    if(Q.front==Q.rear)
        return false;
    x=Q.front->next->data;
    return true;
}
bool isEmpty(LQueue Q) {
    if(Q.front==Q.rear)
        return true;
    return false;
}
int main(){
    LQueue Q;
    init(Q);
    return 0;
}

 

 

    不带头节点的链队列:

/**@数据结构:队列->链队列 
 **@作者:9761滴 
 **@不带头结点 
 **/ 

//本文件中实现了链队列的
    /*1.init
     **2.enQueue 
    **3.deQueue
    **4.getHead 
    **5.isEmpty
    */ 
//等操作 
#include<cstdio>
#include<stdlib.h>
typedef int ElementType;
typedef struct LNode{
    ElementType data;
    LNode* next;
}LNode;
typedef struct LQueue{
    LNode* front;
    LNode* rear;
}LQueue;
bool init(LQueue &Q){
    Q.front=Q.rear=NULL;
    return true;
}
bool enQueue(LQueue &Q,ElementType x){
    LNode* L=(LNode*)malloc(sizeof(LNode));
    if(L==NULL)
        return false;
    L->data=x;
    L->next=NULL;
    if(Q.rear==NULL){
        Q.front=Q.rear=L;
    }
    else{
        Q.rear->next=L;
        Q.rear=L;
    }

    return true;
}
bool deQueue(LQueue &Q,ElementType &x){
    if(Q.front==NULL)
        return false;
    LNode* L=Q.front;
    x=L->data;
  Q.front->next=L->next; 
if(Q.rear==L) //如果删除的是最后一个结点,需要修改rear,使其指向头结点 Q.rear=Q.front==NULL; free(L); return true; } bool getHead(LQueue Q,ElementType &x){ if(Q.front==NULL) return false; x=Q.front->data; return true; } bool isEmpty(LQueue Q) { if(Q.front==NULL) return true; return false; }

 

    

posted @ 2020-11-12 19:43  9761滴  阅读(78)  评论(0编辑  收藏  举报