数据结构栈实验


```c
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
#include"stacklist.h"
int main()
{
    int n;
    int i;
	int x;
    char test[100];
   do
    {
		printf("请选择你想要实现的功能\n");
        printf("------1.利用顺序栈将十进制转换为十六进制---------------\n");
		printf("------2.利用顺序栈设计算法对以字符串形式读入的表达式S,判断其中的各括号是否是匹配的。---\n");
		printf("------3.利用链栈将十进制转换为十六进制----\n");
		printf("------4.利用链栈将设计算法对以字符串形式读入的表达式S,判断其中的各括号是否是匹配的----\n");
		scanf("%d",&n);
        switch(n)
		{
        case 1:
        system("CLS");
        printf("请输入你想要转换为十六进制的十进制数字");
        scanf("%d",&x);
        change(x);
        system("Pause");
		break;
        case 2:
        system("CLS");
        printf("请输入一串字符");
        scanf("%s",test);
        Match_SeqStack(test);
        system("Pause");
        break;
        case 3:
        system("CLS");
        printf("请输入你想要转换为十六进制的十进制数字");
        scanf("%d",&x);
        _Decimal_to_Hexadecimal(x);
        system("Pause");
		break;
        case 4:
        system("CLS");
        printf("请输入一串字符");
        scanf("%s",test);
        _CheckMatch(test);在这里插入代码片
        system("Pause");
        default:
			printf("Selection is error!\n");
			system("Pause");
        }
	} while (1);
  return 0;
}

```c
#include <stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100 /* 存储空间初始分配量 */
typedef int Status; 
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */

typedef struct
{
      int data[MAXSIZE];
        int top; /* 用于栈顶指针 */
}SqStack;

typedef struct
{
      char data[MAXSIZE];
       int top; /* 用于栈顶指针 */
}SqStack1;



Status visit(SElemType c)
{
        printf("%d ",c);
        return OK;
}

/*  构造一个空栈S */
Status InitStack(SqStack *S)
{ 
        /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
        S->top=-1;
        return OK;
}
Status InitStack1(SqStack1 *S)
{ 
        /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
        S->top=-1;
        return OK;
}

/* 把S置为空栈 */
Status ClearStack(SqStack *S)
{ 
        S->top=-1;
        return OK;
}

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
Status StackEmpty(SqStack *S)
{ 
        if (S->top==-1)
                return TRUE;
        else
                return FALSE;
}

/* 返回S的元素个数,即栈的长度 */
int StackLength(SqStack *S)
{ 
        return S->top+1;
}

/* 若栈不空,则用x返回S的栈顶元素,并返回OK;否则返回ERROR */
Status GetTop(SqStack *S)
{
    int x;
        if (S->top==-1)
                return ERROR;
        else
        x=S->data[S->top];
        return x;
}

/* 插入元素e为新的栈顶元素 */
Status Push(SqStack *S,SElemType e)
{
        if(S->top == MAXSIZE -1) /* 栈满 */
        {
                return ERROR;
        }
        S->top++;				/* 栈顶指针增加一 */
        S->data[S->top]=e;  /* 将新插入元素赋值给栈顶空间 */
        return OK;
}
Status Push1(SqStack1 *S,SElemType e)
{
        if(S->top == MAXSIZE -1) /* 栈满 */
        {
                return ERROR;
        }
        S->top++;				/* 栈顶指针增加一 */
        S->data[S->top]=e;  /* 将新插入元素赋值给栈顶空间 */
        return OK;
}

// 出栈  返回栈顶
int pop(SqStack  *s)
{
	if(s->top < 0)
        {
		printf("栈为空!\n");
		exit(-1);
	}
	s->top--;
	return s->top;
}

int pop1(SqStack1  *s)
{
	if(s->top < 0)
        {
		printf("栈为空!\n");
		exit(-1);
	}
	s->top--;
	return s->top;
}


/* 从栈底到栈顶依次对栈中每个元素显示 */
Status StackTraverse(SqStack *S)
{
        int i;
        i=0;
        while(i<=S->top)
        {
                visit(S->data[i++]);
        }
        printf("\n");
        return OK;
}


// 进制转换函数
void change(int x)
{
    SqStack *s;
    int mod;//余数
    int top;
    InitStack(s);//清空栈
    while(x>0)
    {
        mod=x%16;
        Push(s,mod);
        x=x/16;
    }
    top=s->top;
    printf("转换为十六进制为");
    while (top!=-1)
    {
        if(GetTop(s)<10)
        printf("%d",GetTop(s));
        else
        printf("%c",GetTop(s)-10+'A');
        top=pop(s);
    }
    putchar('\n');
}

void Match_SeqStack(char test[])
{
        SqStack1 *s;
        InitStack1(s);
        int i=0;
        int k=0,m=0;
        while(test[i]!='\0')
    {
    //遇见左括号入栈
        if(test[i]=='('||test[i]=='['||test[i]=='{')
            {
                Push1(s,test[i]);
                i++;
                continue;
                //每入栈一个元素就进行下一次循环,扫描下一个字符
            }
            //当遇见匹配的右括号时,则将栈顶左括号出栈
                if(test[i]==')'&&s->data[s->top]=='(')
               {
                           pop1(s);
                           i++;
                           continue;
               }
                else if(test[i]==']'&&s->data[s->top]=='[')
               {
                           pop1(s);
                           i++;
                           continue;
               }
               else if(test[i]=='}'&&s->data[s->top]=='{')
               {
                           pop1(s);
                           i++;
                           continue;
               }
               i++;
    }
    //防止不是因为扫描完而退出循环
    if(s->top==-1&&test[i]=='\0'&&k==m) 
     printf("match!\n");
    else
     printf("don't match!\n");
     }
 

```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct lsNode
{
    int data;
    struct lsNode *next;
}sNode;

/*****链栈基本运算*****/

//初始化栈
void InitialStack(sNode *top)
{
    top->next = NULL;
}

//判定栈空
bool StackEmpty1(sNode *top)
{
    if(top->next == NULL)
        return true;
    else
        return false;
}

//取栈顶元素
bool StackTop(sNode *top, int *x)
{
    if(StackEmpty1(top))
        return false;
    else
    {
        *x = top->data;
        return true;
    }
}

//入栈
sNode *PushStack(sNode *top, int x)
{
    sNode *s;
    s = (sNode *)malloc(sizeof(sNode));
    s->data = x;
    s->next = top;
    top = s;
    return top;
}

//出栈
sNode *PopStack(sNode *top, int *x)
{
    sNode *u;
    if(StackEmpty1(top))
        return top; //栈空
    else
    {
        *x = top->data;
        u = top;
        top = top->next;
        free(u);
        return top;
    }
}

/*****链栈实验内容*****/

//十进制转十六进制
void _Decimal_to_Hexadecimal(int n)
{
    sNode *S;
    int mod, x;
    S = (sNode *)malloc(sizeof(sNode));
    InitialStack(S);
    printf("十进制数为:%d\n", n);
    while(n != 0)
    {
        mod = n % 16;
        S = PushStack(S, mod);
        n = n/16;
    }
    printf("十六进制数为:");
    while(!StackEmpty1(S))
    {
        S = PopStack(S, &x);
        if(x >= 0 && x < 10)
            printf("%d", x);
        else
            printf("%c", x%10+'a');
    }
}

//括号匹配
/* '('记作1,'['记作2,'{'记作3 */
void _CheckMatch(char *str)
{
    sNode *S;
    int i, x, iFlag;
    S = (sNode *)malloc(sizeof(sNode));
    InitialStack(S);
    i = 0, iFlag = 1;

    while(str[i] != '\0' && iFlag == 1)
    {
        switch(str[i])
        {
        case '(':
            S = PushStack(S, 1);
            break;

        case '[':
            S = PushStack(S, 2);
            break;

        case '{':
            S = PushStack(S, 3);
            break;

        case ')':
            if(StackEmpty1(S))
            {
                printf("左右括号不匹配。\n");
                iFlag = 0;
                break;
            }
            else
            {
                S = PopStack(S, &x);
                if(x != 1)
                {
                    printf("左右括号不匹配。\n");
                    iFlag = 0;
                    break;
                }
                break;
            }

        case ']':
            if(StackEmpty1(S))
            {
                printf("左右括号不匹配。\n");
                iFlag = 0;
                break;
            }
            else
            {
                S = PopStack(S, &x);
                if(x != 2)
                {
                    printf("左右括号不匹配。\n");
                    iFlag = 0;
                    break;
                }
                break;
            }

        case '}':
            if(StackEmpty1(S))
            {
                printf("左右括号不匹配。\n");
                iFlag = 0;
                break;
            }
            else
            {
                S = PopStack(S, &x);
                if(x != 3)
                {
                    printf("左右括号不匹配。\n");
                    iFlag = 0;
                    break;
                }
                break;
            }

        default :
            break;
        }
        i++;
    }

    if(!(!StackEmpty1(S)||iFlag == 0))
        printf("左右括号匹配。\n");
        else
        {
            printf("左右括号不匹配\n");
        }
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值