栈,2月19日(补)

在这里插入图片描述

链式栈的实现

#include<stdio.h>
#include<stdlib.h>


struct Node
{
    int value;
    struct Node *next;
};

struct stack
{
    int size;			//栈的容量大小
    struct Node *bottom;	
    struct Node *top;
};

struct Node *createNode(int value)
{
    struct Node *newnode;
    newnode = (struct Node *)malloc(sizeof(struct Node));
    if(newnode == NULL)
    {
	return NULL;
    }
    newnode->next = NULL;
    newnode->value = value;
    return newnode;
}

int push(struct stack *sp, int data)
{
    struct Node *ptr = createNode(data);
    if(ptr == NULL)
    {
	printf("create error");
	return -1;
    }
    if(sp->size == 0)
    {
	sp->top = ptr;
	sp->bottom = ptr;
    }else{
	ptr->next = sp->top;
	sp->top = ptr;
    }
	sp->size++;
}

void print(struct stack *sp)
{
    int i = sp->size;

    struct Node *ptr = sp->top;
    while(i != 0)
    {
	printf("%d",ptr->value);
	i--;
	ptr = ptr->next;
    }
    printf("\n");
}

int main()
{
    struct stack Stack = {0,NULL,NULL};
    //push(&Stack);
    for(int i = 1; i < 6; i++)
    {
	push(&Stack,i);
    }
    print(&Stack);
    return 0;
}


基于栈的计算器

#include <stdio.h>    		//链式栈
#include <stdlib.h>

struct Node			//节点结构体
{
    int  value;
    struct Node *next;
};

struct stack			//栈的管理结构体
{
    int size;             	//栈的容量大小
    struct Node * bottom;	//栈底指针
    struct Node * top;		//栈顶指针
};

struct Node * createNode(int value)
{
    struct Node * newnode;
    newnode = (struct Node *)malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
	return NULL;
    }
    newnode->next = NULL;
    newnode->value = value;
    return newnode;
}


int gettop(struct stack *sp)
{
    if(sp->top == NULL)
    {
	return 0;
    }
    return sp->top->value;
}


int push(struct stack *sp,int data)
{
    struct Node *ptr = createNode(data);

    if(ptr == NULL)
    {
	printf("createNode error!\n");
	return -1;
    }
    if(sp->size == 0)
    {
	sp->top = ptr;
	sp->bottom = ptr;
    }else{
	ptr->next = sp->top;
	sp->top = ptr;
    }
    sp->size++;
}

int pop(struct stack *sp)
{
    if(sp->size == 0)
    {
	printf("stack is empty!\n");
	return -1;
    }
    struct Node *ptr = sp->top;
    sp->top = ptr->next;
    sp->size--;
    int data = ptr->value;
    free(ptr);
    return data;
}

void print(struct stack *sp)
{
    int i = sp->size;
    struct Node * ptr = sp->top;
    while(i != 0)
    {
	printf("%d ",ptr->value);
	i--;
	ptr = ptr->next;
    }
    printf("\n");
}

char precede(char ch1, char ch2)
{
    int i,j;
    char pre[7][7] = {
	{'>','>','<','<','<','>','>'},
	{'>','>','<','<','<','>','>'},
	{'>','>','>','>','<','>','>'},
	{'>','>','>','>','<','>','>'},
	{'<','<','<','<','<','=','0'},
	{'>','>','>','>','0','>','>'},
	{'<','<','<','<','<','0','='},
    };
    switch(ch1)
    {
	case '+':i = 0;break;
	case '-':i = 1;break;
	case '*':i = 2;break;
	case '/':i = 3;break;
	case '(':i = 4;break;
	case ')':i = 5;break;
	case '=':i = 6;break;
    }

    switch(ch2)
    {
	case '+':j = 0;break;
	case '-':j = 1;break;
	case '*':j = 2;break;
	case '/':j = 3;break;
	case '(':j = 4;break;
	case ')':j = 5;break;
	case '=':j = 6;break;
    }
    return pre[i][j];

}

int operate(int a,int b,char ch)
{
    int num;
    switch(ch)
    {
	case '+':return a + b;
	case '-':return a - b;
	case '*':return a * b;
	case '/':
		 if(b == 0)
		 {
		     printf("invalid devide!\n");
		     exit(1);
		 }
		 return a / b;
    }
}

int chop(char c)
{
    switch(c)
    {
	case '+':
	case '-':
	case '*':
	case '/':
	case '(':
	case ')':
	case '=':
	    return 1;
	default:
	    return 0;
    }
}

int numop(char c)
{
    if(c <= '9' && c >= '0')
    {
	return 1;
    }else{
	return 0;
    }
}


int calculate(char *str)
{
    struct stack Num = {0, NULL, NULL};
    struct stack oper = {0, NULL, NULL};
    
    int i;
    char ch, ch2; 
    push(&oper, '=');

    while(ch != '=' || gettop(&oper) != '=')
    {
	ch = str[i];
	if(chop(ch))
	{
	    switch(precede(gettop(&oper),ch))
	    case '<':
		push(ch,oper);
		i++;
		ch = str[i];

}


int main()
{
    char str[30];
    int result;
    scanf("%s",str);
   
    result = calculate(str);
    printf("result = %d\n",result);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值