浙大数据结构:堆栈和队列的定义与操作

发布于:2024-09-18 ⋅ 阅读:(75) ⋅ 点赞:(0)

堆栈:

顺序存储:

#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
typedef int position ;
#define MAXSIZE 100
#define ERROR -1
struct SNode 
{
    ElementType  * Data ;
    position top;
    int Maxsize;
};

typedef struct SNode *Stack;

Stack CreateStack(int Maxsize)
{
    Stack s=(Stack)malloc(sizeof(struct SNode));
    s->Data=(ElementType*)malloc(Maxsize*sizeof(ElementType));
    s->top=-1;
    s->Maxsize=Maxsize;
    return s;
}

bool isfull(Stack s)
{
    return (s->top==s->Maxsize-1);
}

bool push(Stack s,ElementType x)
{
  if(isfull(s))return false;
  s->Data[++(s->top)]=x;
  return true;
}

bool isempty(Stack s)
{
    return (s->top==-1);
}

ElementType pop(Stack s)
{
    if(isempty(s))return ERROR;
    return s->Data[(s->top)--];
}

链式存储:

#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
typedef int position ;
#define MAXSIZE 100
#define ERROR -1
typedef struct SNode *Stack;
struct SNode 
{
    ElementType   Data ;
     Stack next;
};

Stack CreateStack(int Maxsize)
{
    Stack s;
    s=(Stack)malloc(sizeof(struct SNode));
    s->next =NULL;
    return s;
}


bool push(Stack s,ElementType x)
{
Stack tmp;
tmp=(Stack)malloc(sizeof(struct SNode));
tmp->Data=x;
tmp->next=s->next;
s->next=tmp;
  return true;
}

bool isempty(Stack s)
{
    return (s->next==NULL);
}

ElementType pop(Stack s)
{
    Stack head;
    ElementType topelement;
    if(isempty(s))return ERROR;
    head=s->next;
    topelement=head->Data;
    s->next=head->next;
    free(head);
    return topelement;
}

队列:

顺序存储:

#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
typedef int position ;
#define MAXSIZE 100
#define ERROR -1
typedef struct QNode *Queue;
struct QNode
{
    ElementType * data;
    position front,rear;
    int MaxSize;
};

Queue CreateQueue(int Maxsize)
{
    Queue Q=(Queue)malloc(sizeof(struct QNode));
    Q->data=(ElementType*)malloc(Maxsize*sizeof(ElementType));
    Q->front=0,Q->rear=0;
    Q->MaxSize=Maxsize;
    return Q;
}

bool isfull(Queue q)
{
    return ((q->rear+1)%q->MaxSize==q->front);
}

bool addq(Queue q,ElementType x)
{
    if(isfull(q))return false;
    q->rear=(q->rear+1)%q->MaxSize;
    q->data[q->rear]=x;
    return true;
}

bool isempty(Queue q)
{
    return  (q->front==q->rear);
}

ElementType Delete(Queue q)
{
    if(isempty(q))return ERROR;
    q->front=(q->front+1)%q->MaxSize;
    return q->data[q->front];
     
}

链式存储:

#include<stdio.h>
#include<stdlib.h>
typedef int  ElementType ;
#define MAXSIZE 100
#define ERROR -1
typedef struct Node *ptrQueue;
typedef ptrQueue position ;
struct Node
{
    ElementType  data;
    position next;
};
typedef struct QNode *Queue;
struct QNode
{
    position front ,rear;
    int MaxSize;
};

bool isempty(Queue q)
{
  return q->front==NULL;
}

ElementType Delete(Queue q)
{
    position head;
    ElementType headelem;
    if(isempty(q))return ERROR;

    head=q->front;
    if( q->front==q->rear)
    q->front=q->rear=NULL;
     else
    q->front=head->next;

     headelem=head->data;
    free(head);
    return headelem;

}