跟我学数据结构之栈和队列

来源:互联网 发布:联通网络id 编辑:程序博客网 时间:2024/06/11 20:43

栈的分类

  • 栈分为顺序栈和链栈
    顺序栈的定义
typedef struct{    int data[MAXSIZE];    int top;}Sqstack;

一般情况下,栈更多的是应用到具体的实现,下面我们结合几个例子来展示栈的各种操作

  • 括号匹配问题
    Input:Only ’ ( ’ , ’ ) ’ , ’ [ ’ , ’ ] ‘
#ifndef STACK_H_#define STACK_H_#endif#include <iostream>#include <cstdio>#include <cstdlib>#define MAX 32using namespace std;typedef struct{    char data[MAX];    int top;}SqStack;void InitStack(SqStack * s){    s->top = -1;}void Pop(SqStack * s){    s->top--;}void Push(SqStack * s,char ch){    s->data[++s->top] = ch;}char getTop(SqStack * s){    return s->data[s->top];}bool comp(SqStack * s, char ch){    switch(ch)    {        case ')':            if (getTop(s) == '(')                return true;            return false;        case ']':            if (getTop(s) == '[')                return true;            return false;    }}int main(){    SqStack s;    char str[MAX];    gets(str);    InitStack(&s);    for (int i = 0; str[i] != '\0'; ++i)    {        /*directly push into stack*/        if (str[i] == '(' || str[i] == '[')            Push(&s,str[i]);        else        {            /*stack is empty means not match*/            if (s.top == -1)            {                printf("Do not match!");                exit(-1);            }            /*compare if the top element match the current*/            else if (comp(&s,str[i]))            {                Pop(&s);            }            else            {                printf("Do not match!");                exit(-1);            }        }    }    if (s.top != -1)    {        printf("Do not match!\n");    }    if (s.top == -1)    {        printf("Match!\n");    }    return 0;}
  • 数值进制转换问题
/*n represent the value to be transformed,m represent the base*/void transfer(SqStack * s, int n){    InitStack(s);    while(n)    {        Push(s,n % m);        n = n / m;    }    while(!isEmpty(s))    {        putchar(s->data[s->top--]);    }}
  • 行编辑问题
#include <iostream>#include <cstdio>#include <cstdlib>#define MAX 10000using namespace std;typedef struct{    char data[MAX];    int top;}SqStack;bool isEmpty(SqStack * s){    if (s->top == -1)        return true;    return false;}void ClearStack(SqStack * s){    s->top = -1;}void InitStack(SqStack * s){    s->top = -1;}void Pop(SqStack * s){    s->top--;}void Push(SqStack * s,char ch){    s->data[++s->top] = ch;}void EditLine(SqStack * s){    InitStack(s);    char ch;    ch = getchar();    while (true) //or while(ch != EOF)     {        while(ch != EOF && ch != '\n')        {            switch(ch)            {                case '#':                    Pop(s);                    break;                case '@':                    ClearStack(s);                    break;                default:                    Push(s,ch);            }            ch = getchar();        }        /*Output the line after edited*/                for (int i = 0; i <= s->top; ++i)            printf("%c",s->data[i]);        printf("\n");        /*Clear the stack*/        ClearStack(s);        if (ch != EOF)            ch = getchar();        /*if the outer loop wrote like "while(ch != EOF)" the break needn't*/        break;      }}int main(){    SqStack s;    EditLine(&s);    return 0;}
  • 简单的四则元算
//To be continued...
  • 前缀表达式处理计算
    这里写图片描述
  • 链栈的定义
typedef struct LNode{    int data;    struct LNode * next;}LNode;
  • 链栈的初始化
void InitStack(LNode * lst){    lst = (LNode *)malloc(sizeof(LNode));    lst->next = NULL;}
  • 判空函数
bool isEmpty(LNode * lst){    if (lst->next == NULL)        return true;    return false;}
  • 进栈操作
void Push(LNode * lst, int x){    LNode * p;    p = (LNode *)malloc(sizeof(LNode));    p->data = x;    p->next = lst->next;    lst->next = p;}
  • 出栈操作
void Pop(LNode * lst){    LNode * p;    if (lst->next == NULL)        /*Empty*/        exit(-1);    p = lst->next;    lst->next = p->next;    free(p);}

队列

队列分为链式队列和循环队列

  • 链式队列
/*队列节点*/typedef struct Node{    int data;              /*数据域*/    struct Node *next;     /*指针域*/}LinkQueueNode;/*链队类型*/typedef struct {    LinkQueueNode *front;    LinkQueueNode *rear;}LinkQueue;
int InitQueue(LinkQueue *Q){     /* 将Q初始化为一个空的链队列 */    Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));    if(Q->front!=NULL)    {        Q->rear=Q->front;        Q->front->next=NULL;        return(TRUE);    }    else return(FALSE);    /* 溢出!*/}
void EnQueue(LinkQueue *Q, int e){    LinkQueueNode * p;    p = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));    if (p != NULL)    {        p->data = e;        Q->rear->next = p;        Q->rear = p;        Q->rear->next = NULL;    }}
void DeQueue(LinkQueue *Q, int &e){    LinkQueueNode *p;    p = Q->front->next;    Q->front->next = p->next;    e = p->data;    if (Q->rear == p)        Q->rear = Q->front;    free(p);}
  • 循环队列
/*define*/typedef struct{    int data[MAXSIZE]; /*队列的元素空间*/    int front; /*头指针指示器*/    int rear;  /*尾指针指示器*/}SqQueue;
void InitSqQueue(SqQueue * Q){       /* 将*Q初始化为一个空的循环队列 */    Q->front = Q->rear = NULL;}
void EnSqQueue(SqQueue * Q, int e){    if ((Q->rear + 1) % MAXSIZE == Q->front)        return QUEUE_FULL;    Q->data[Q->rear] = e;    Q->rear = (Q->rear + 1) % MAXSIZE;}
void DeSqQueue(SqQueue * Q, int & e){    if (Q->rear == Q->front)        return QUEUE_EMPTY;    e = Q->data[Q->front];    Q->front = (Q->front + 1) % MAXSIZE;}
int GetSqQueueLength(SqQueue * Q){    return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;}
1 0