• 栈的的后缀计算实现


    1.#include <stdio.h>
    #include <malloc.h>
    #include "LinkList.h"

    typedef struct _tag_LinkList
    {
        LinkListNode header;
        int length;
    } TLinkList;

    LinkList* LinkList_Create() // O(1)
    {
        TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
        
        if( ret != NULL )
        {
            ret->length = 0;
            ret->header.next = NULL;
        }
        
        return ret;
    }

    void LinkList_Destroy(LinkList* list) // O(1)
    {
        free(list);
    }

    void LinkList_Clear(LinkList* list) // O(1)
    {
        TLinkList* sList = (TLinkList*)list;
        
        if( sList != NULL )
        {
            sList->length = 0;
            sList->header.next = NULL;
        }
    }

    int LinkList_Length(LinkList* list) // O(1)
    {
        TLinkList* sList = (TLinkList*)list;
        int ret = -1;
        
        if( sList != NULL )
        {
            ret = sList->length;
        }
        
        return ret;
    }

    int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)
    {
        TLinkList* sList = (TLinkList*)list;
        int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
        int i = 0;
        
        if( ret )
        {
            LinkListNode* current = (LinkListNode*)sList;
            
            for(i=0; (i<pos) && (current->next != NULL); i++)
            {
                current = current->next;
            }
            
            node->next = current->next;
            current->next = node;
            
            sList->length++;
        }
        
        return ret;
    }

    LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
    {
        TLinkList* sList = (TLinkList*)list;
        LinkListNode* ret = NULL;
        int i = 0;
        
        if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
        {
            LinkListNode* current = (LinkListNode*)sList;
            
            for(i=0; i<pos; i++)
            {
                current = current->next;
            }
            
            ret = current->next;
        }
        
        return ret;
    }

    LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
    {
        TLinkList* sList = (TLinkList*)list;
        LinkListNode* ret = NULL;
        int i = 0;
        
        if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
        {
            LinkListNode* current = (LinkListNode*)sList;
            
            for(i=0; i<pos; i++)
            {
                current = current->next;
            }
            
            ret = current->next;
            current->next = ret->next;
            
            sList->length--;
        }
        
        return ret;
    }

    2.#ifndef _LINKLIST_H_
    #define _LINKLIST_H_

    typedef void LinkList;
    typedef struct _tag_LinkListNode LinkListNode;
    struct _tag_LinkListNode
    {
        LinkListNode* next;
    };

    LinkList* LinkList_Create();

    void LinkList_Destroy(LinkList* list);

    void LinkList_Clear(LinkList* list);

    int LinkList_Length(LinkList* list);

    int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);

    LinkListNode* LinkList_Get(LinkList* list, int pos);

    LinkListNode* LinkList_Delete(LinkList* list, int pos);

    #endif

    3.#include <malloc.h>
    #include "LinkStack.h"
    #include "LinkList.h"

    typedef struct _tag_LinkStackNode
    {
        LinkListNode header;
        void* item;
    } TLinkStackNode;

    LinkStack* LinkStack_Create()
    {
        return LinkList_Create();
    }

    void LinkStack_Destroy(LinkStack* stack)
    {
        LinkStack_Clear(stack);
        LinkList_Destroy(stack);
    }

    void LinkStack_Clear(LinkStack* stack)
    {
        while( LinkStack_Size(stack) > 0 )
        {
            LinkStack_Pop(stack);
        }
    }

    int LinkStack_Push(LinkStack* stack, void* item)
    {
        TLinkStackNode* node = (TLinkStackNode*)malloc(sizeof(TLinkStackNode));
        int ret = (node != NULL) && (item != NULL);
        
        if( ret )
        {
            node->item = item;
            
            ret  = LinkList_Insert(stack, (LinkListNode*)node, 0);
        }
        
        if( !ret )
        {
            free(node);
        }
        
        return ret;
    }

    void* LinkStack_Pop(LinkStack* stack)
    {
        TLinkStackNode* node = (TLinkStackNode*)LinkList_Delete(stack, 0);
        void* ret = NULL;
        
        if( node != NULL )
        {
            ret = node->item;
            
            free(node);
        }
        
        return ret;
    }

    void* LinkStack_Top(LinkStack* stack)
    {
        TLinkStackNode* node = (TLinkStackNode*)LinkList_Get(stack, 0);
        void* ret = NULL;
        
        if( node != NULL )
        {
            ret = node->item;
        }
        
        return ret;
    }

    int LinkStack_Size(LinkStack* stack)
    {
        return LinkList_Length(stack);
    }

    4.#ifndef _LINKSTACK_H_
    #define _LINKSTACK_H_

    typedef void LinkStack;

    LinkStack* LinkStack_Create();

    void LinkStack_Destroy(LinkStack* stack);

    void LinkStack_Clear(LinkStack* stack);

    int LinkStack_Push(LinkStack* stack, void* item);

    void* LinkStack_Pop(LinkStack* stack);

    void* LinkStack_Top(LinkStack* stack);

    int LinkStack_Size(LinkStack* stack);

    #endif

    5.#include <stdio.h>
    #include "LinkStack.h"

    int isNumber(char c)
    {
        return ('0' <= c) && (c <= '9');
    }

    int isOperator(char c)
    {
        return (c == '+') || (c == '-') || (c == '*') || (c == '/');
    }
    //将0转换成数字
    int value(char c)
    {
        return (c - '0');
    }

    int express(int left, int right, char op)
    {
        int ret = 0;
        
        switch(op)
        {
            case '+':
                ret = left + right;
                break;
            case '-':
                ret = left - right;
                break;
            case '*':
                ret = left * right;
                break;
            case '/':
                ret = left / right;
                break;
            default:
                break;
        }
        return ret;
    }

    int compute(const char* exp)
    {
        LinkStack* stack = LinkStack_Create();
        int ret = 0;
        int i = 0;
        
        while( exp[i] != '' )
        {
            if( isNumber(exp[i]) )
            {
                LinkStack_Push(stack, (void*)value(exp[i]));
            }
            else if( isOperator(exp[i]) )
            {
            //左操作
                int right = (int)LinkStack_Pop(stack);
                //右操作
            int left = (int)LinkStack_Pop(stack);
                    //计算
            int result = express(left, right, exp[i]);
                //压栈
                LinkStack_Push(stack, (void*)result);
            }
            else
            {
                printf("Invalid expression!");
                break;
            }
            
            i++;
        }
        
        if( (LinkStack_Size(stack) == 1) && (exp[i] == '') )
        {
            ret = (int)LinkStack_Pop(stack);
        }
        else
        {
            printf("Invalid expression!");
        }
        
        LinkStack_Destroy(stack);
        
        return ret;
    }

    int main()
    {
        printf("9 + (3 - 1) * 5 + 8 / 2 = %d ", compute("931-5*+82/+"));
        
        return 0;
    }

  • 相关阅读:
    my eye
    html与HTML5的区别
    h5css样式
    h5css3弹性盒子
    简单js的介绍
    2020.8.16(周报6)
    2020.8.18
    2020.8.20
    2020.8.17
    2020.8.15
  • 原文地址:https://www.cnblogs.com/wxb20/p/6140883.html
Copyright © 2020-2023  润新知