数据结构——栈练习之常见的栈操作、括号匹配、数制转换等问题

链式栈操作

main.c

#include <stdio.h>
#include <stdlib.h>
#include "StackTest.h"
extern int MAX;
int main()
{
    char date[10];
    char del[10];
    StackList *exp=init();
    while(count<MAX){
        printf("请输入数据:");
        scanf("%s",date);
        exp=push(date,exp);
    }
    display(exp);
    exp=pop(del,exp);
    printf("删除的节点的值为:%s\n",del);
    return 0;
}

StackTest.h

#ifndef STACKTEST_H_INCLUDED
#define STACKTEST_H_INCLUDED
extern int count;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct tag{
    char date[10];
    struct tag* next;
}StackList;
StackList* init();
int empty(StackList* top);
char* read(StackList* top);
void display(StackList* top);
StackList* push(char date[10],StackList* top);
StackList* pop(char date[10],StackList* top);
#endif // STACKTEST_H_INCLUDED

StackTest.c

#include "StackTest.h"
const int MAX=5;
int count=0;
//栈的初始化
StackList* init()
{
   return NULL;       //设置指向为空,初始化栈
}
int empty(StackList* top)
{
    return top==NULL?0:1;       //判断栈是否为空,是返回0,否则返回1
}
char* read(StackList* top)      //栈顶节点的节点值
{
    if(top!=NULL)
    {
        return top->date;
    }
    else
    {
        printf("当前栈为空\n");
        return 0;
    }
}
void display(StackList* top)    //展示栈顶节点
{
    if(top==NULL){
        printf("当前栈为空\n");
    }
    else{
        StackList* temp=top;
        printf("%s\n",temp->date);
        while(temp->next!=NULL)
        {
            temp=temp->next;
            printf("%s\n",temp->date);
        }
    }
}
StackList* push(char date[10],StackList* top)   //压栈操作
{
    if(count<MAX){
        StackList* temp=(StackList*)malloc(sizeof(StackList));
        strcpy(temp->date,date);
        temp->next=top;
        top=temp;
        count++;
        return top;
    }
    else{
        printf("栈已满\n");
        return top;
    }
}
StackList* pop(char date[10],StackList* top)
{
    if(top==NULL){
        printf("栈为空\n");
        return top;
    }
    else
    {
        strcpy(date,top->date);
        StackList* temp=top;
        top=temp->next;
        free(temp);
        return top;
    }
}

括号匹配

main.c

#include <stdio.h>
#include <stdlib.h>
#include "KuoHaoMatch.h"
/*
 数据结构练习——栈的应用之括号匹配
 描述
 1、后遇到的开括号比先遇到的开括号优先级更高,从而确定了采用栈的先进后出的存取方式
 2、将遇到的所有开括号存入栈中,当遇到和栈顶相匹配的闭括号时,将栈顶元素出栈
 3、根据最后栈是否为空,确定括号是否全部合法匹配
 4、算法实现采用链式栈来进行存储

 */


int main()
{
    char c1[]="{[()]}#";        //初始化字符串
    char c2[]="{[}{]()#";
    if(match_kuohao(c1))
        printf("c1括号匹配成功。\n");
    else
        printf("c1括号匹配失败。\n");
    if(match_kuohao(c2))
        printf("c2括号匹配成功。\n");
    else
        printf("c2括号匹配失败。\n");
    return 0;
}

KuoHaoMatch.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "KuoHaoMatch.h"

int match_kuohao(char c[])
{
    int i=0;
    KuoHao* top=init();
    while(c[i]!='#')
    {
        switch(c[i]){
        case '(':
        case '[':
        case '{':top=push(c[i],top);break;
        case ')':
            if(top->a=='(')
                    top=pop(top);
            break;
        case ']':
            if(top->a=='[')
                    top=pop(top);
            break;
        case '}':
            if(top->a=='{')
                    top=pop(top);
            break;
        default:
            printf("Error.\n");
        }
        i++;
    }
    if(top==NULL)
        return 1;
    else
        return 0;
}
KuoHao* init()
{
    return NULL;
}
KuoHao* push(char data,KuoHao * top)
{
    KuoHao* temp=(KuoHao*)malloc(sizeof(KuoHao));
    temp->a=data;
    temp->next=top;
    top=temp;
    return top;
}
KuoHao* pop(KuoHao* top)
{
    KuoHao* temp=top;
    top=temp->next;
    free(temp);
    return top;
}

KuoHaoMatch.h

#ifndef KUOHAOMATCH_H_INCLUDED
#define KUOHAOMATCH_H_INCLUDED
typedef struct KuoHao{
    char a;
    struct KuoHao * next;
}KuoHao;

 int match_kuohao(char c[]);
KuoHao* init();
KuoHao* push(char data,KuoHao * top);
KuoHao* pop(KuoHao* top);
#endif // KUOHAOMATCH_H_INCLUDED

Numerical.c

#include "NumericalChange.h"

void NumChang(NumStack *numResult,int numStack[],int num,int start)
{
    int yu;
    yu=start%num;
    start=start/num;
    push(numResult,yu);
    while(start!=0){
        yu=start%num;
        start=start/num;
        push(numResult,yu);
    }
}
void push(NumStack* numptr,int in)
{
    if(numptr->top==Stack_Size)
        printf("栈已满无法再存放任何数据!\n");
    else
    {
        numptr->top++;
        numptr->result[numptr->top]=in;
    }
}
void pop(NumStack* numptr)
{
    if(numptr->top==-1)
        printf("栈已经为空,无法再进行出栈操作!\n");
    else
    {
        numptr->result[numptr->top]=-1;
        numptr->top--;
    }
}

void init(NumStack* numptr)
{
    numptr->top=-1;  //设置栈顶指针指向-1,表示栈为空
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "NumericalChange.h"
/*
算法描述:要将十进制数N转换为d进制数
重复如下:
X=N mod d   (其中mod为求余运算)
N=N div d   (其中div为整除运算)
方法是: 它是分两部分进行的即整数部分和小数部分。
具体方法描述:
整数部分:(基数除法)
把我们要转换的数除以新的进制的基数,把余数作为新进制的最低位;
把上一次得的商在除以新的进制基数,把余数作为新进制的次低位;
继续上一步,直到最后的商为零,这时的余数就是新进制的最高位.
小数部分: (基数乘法)
把要转换数的小数部分乘以新进制的基数,把得到的整数部分作为新进制小数部分的最高位
把上一步得的小数部分再乘以新进制的基数,把整数部分作为新进制小数部分的次高位;
继续上一步,直到小数部分变成零为止。或者达到预定的要求也可以。
*/
int main()
{
    int start;
    int num;
    int numStack[255];
    printf("请输入要转换成的进制数:");
    scanf("%d",&num);
    printf("请输入要转换的10进制整数:");
    scanf("%d",&start);
    NumStack numResult;
    init(&numResult);
    NumChang(&numResult,numStack,num,start);
    while(numResult.top!=-1)
    {
        printf("%d",numResult.result[numResult.top]);
        pop(&numResult);
    }
    return 0;
}
#ifndef NUMERICALCHANGE_H_INCLUDED
#define NUMERICALCHANGE_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#define Stack_Size 255

typedef struct {
    int result[Stack_Size];
    int top;
}NumStack;
void NumChang(NumStack *numResult,int numStack[],int num,int start);
void push(NumStack* numptr,int in);
void pop(NumStack* numptr);
void init(NumStack* numptr);


#endif // NUMERICALCHANGE_H_INCLUDED

猜你喜欢

转载自blog.csdn.net/dypnlw/article/details/80811663
今日推荐