1. 程式人生 > >0x00資料結構——C語言實現(棧+字尾表示式計算)

0x00資料結構——C語言實現(棧+字尾表示式計算)

0x00資料結構——C語言實現(棧)

棧的實現

/*
 棧(tack)是限制插入和刪除只能在一個位置上進行的表,該位置是表的末端,叫做棧的頂(top)。
 對棧的基本操作有Push(進棧)和Pop(出棧)。

 Functions:
 (在連結串列中增加附加頭結點的版本)
    建立一個空棧
    將棧置為空
    計算棧長度
    返回棧的地址
    棧push操作函式
    棧pop函式
    取棧頂元素Top函式
    判斷棧空,空返回真,否則返回假
    輸出

*/

#ifndef STACK_H
#define STACK_H


#define MAXLEN 100
typedef enum { false = 0, true } BOOL; //資料結構。 struct node; typedef struct node node; typedef struct node *to_node; typedef to_node pos; struct stack_node; typedef struct stack_node stack_node; typedef stack_node *stack; //建立一個空棧 stack create_stack(void); //將棧置為空 BOOL set_empty(stack s); //計算棧長度
int calc_length(stack s); //棧push操作函式 int push(stack s, int x); //棧pop函式 int pop(stack s); //取棧頂元素Top函式 int get_val(stack s); //判斷棧空,空返回真,否則返回假 BOOL is_empty(stack s); //輸出 void output(stack s); #endif
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>


/*
struct node;
typedef struct node node;
typedef struct node *to_node;
typedef to_node pos;

struct stack_node;
typedef struct stack_node stack_node;
typedef stack_node *stack;
*/
struct node { int val; struct node *next; }; struct stack_node { int capacity; struct node *top; }; /* 用連結串列來實現棧,這裡設計一個額外的棧頭節點,該節點指向棧頂(便於進棧和出棧) */ //建立一個空棧 stack create_stack(void) { stack tmp = (stack)malloc(sizeof(stack_node)); if(tmp != NULL) { tmp->top = NULL; tmp->capacity = 0; } return tmp; } //將棧置為空 BOOL set_empty(stack s) { pos tmp; while(s->capacity != 0) { tmp = s->top; s->top = tmp->next; free(tmp); (s->capacity)--; } return true; } //計算棧長度 int calc_length(stack s) { return s->capacity; } //棧push操作函式 int push(stack s, int x) { node *tmp = (node *)malloc(sizeof(node)); tmp->val = x; tmp->next = s->top; s->top = tmp; (s->capacity)++; return x; } //棧pop函式 int pop(stack s) { node *tmp; int r = 0; tmp = s->top; s->top = tmp->next; (s->capacity)--; r = tmp->val; free(tmp); return r; } //取棧頂元素Top函式 int get_val(stack s) { return s->top->val; } //判斷棧空,空返回真,否則返回假 BOOL is_empty(stack s) { return (s->capacity == 0); } //輸出 void output(stack s) { pos tmp = s->top; while(tmp != NULL) { printf("%d->", tmp->val); tmp = tmp->next; } printf("|\n"); }

利用棧進行字尾表示式的計算

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"


int main()
{
//  字尾表示式的計算

    stack s;
    int i = 0, len = 0;
    int tmp1, tmp2;
    int op;
//  char in[100] = {0};
    char in[] = {'6','5','2','3','+','8','*','+','3','+','*'};
    s = create_stack();
//  while((in[i] = getchar())!=EOF) {
//      i++;
//  }

    len = strlen(in);

    for(i = 0; i<len; i++) {
        tmp1 = in[i];
        if(tmp1=='+' || tmp1=='-' || tmp1 == '*' || tmp1 == '/') {
            op = tmp1;
            tmp1 = pop(s);
            tmp2 = pop(s);
            switch(op) {
                case '+': push(s, tmp1+tmp2);break;
                case '-': push(s, tmp1-tmp2);break;
                case '*': push(s, tmp1*tmp2);break;
                case '/': push(s, tmp1/tmp2);break;
                default: break;
            }
        } else if(isdigit(tmp1)) {
            push(s,tmp1-48);
        }
        output(s);
    }

    output(s);
    return 0;
}

實驗結果

6->|
5->6->|
2->5->6->|
3->2->5->6->|
5->5->6->|
8->5->5->6->|
40->5->6->|
45->6->|
3->45->6->|
48->6->|
288->|
288->|
Time elapsed: 000:00:047
Press any key to continue

相關推薦

0x00資料結構——C語言實現+字尾表示式計算

0x00資料結構——C語言實現(棧) 棧的實現 /* 棧(tack)是限制插入和刪除只能在一個位置上進行的表,該位置是表的末端,叫做棧的頂(top)。 對棧的基本操作有Push(進棧)和Pop(出棧)。 Functions: (在連結串列中增加

資料結構-C語言實現線性

#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100//儲存空間初始化分配量 #define STACKINCREAMENT 10//儲存空間分配增量 #defi

[資料結構]c語言實現的入,出,清空,銷燬等操作

最近在學習資料結構中的棧,於是在此記錄一下棧鏈式結構的抽象資料型別 /* 棧的抽象資料型別 ADT 棧(stack) Data 同線性表。元素具有相同的型別,相鄰元素具有前驅和後繼關係 Operation InitStack(*S):初始化

資料結構 c語言實現順序佇列輸數字入隊,字元出隊

一.標頭檔案seqqueue.h實現 #ifndef __SEQQUEUE_H__ #define __SEQQUEUE_H__ #include<stdio.h> #include<stdlib.h> #include<stdbool.h&g

嚴蔚敏資料結構C語言實現的基本操作

int main(){   SqStack S;   SElemType *e;   int n,i;   InitStack(&S);   printf("請輸入需要入棧的資料個數\n");   scanf("%d",&n);   for(i=0;i<n;i++)   {    sca

資料結構C語言第二章迷宮

轉自未空blog   //我剛開始對STACK的記憶體分配那有點問題,後來用這個程式碼除錯了下,感覺有點明白了,   地址由高到低分配,然後程式碼中的base和top剛開始指向地址最低的地方,記憶體不夠時重新在原有基礎上新增記憶體,top指向原有的棧頂,然後繼續

資料結構-C語言實現順序表

#include<stdio.h> #include<stdlib.h> #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREAMENT 10 #define OK 1

資料結構-C語言實現迴圈佇列

#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 #define OK 1 #define OVERFLOW -1 typedef struct{ int *base;

資料結構C語言實現-2—線性表

Table of Contents 靜態連結串列 線性表順序儲存結構 #include <stdio.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #de

聰明的學生C語言實現

不寫初中高階這種實驗啦 直接上STL吧 /如果有不懂的話可以看看STL容器,我直接給個百度百科的連結吧https://baike.baidu.com/item/STL/70103?fr=aladdin/ 聰明的學生(實驗名稱) 一、實驗目的 掌握遞迴思想,將“聰明的學生”問題抽象出遞

資料結構-c語言實現連結串列的建立,增刪,翻轉

很經典的課題了,這裡直接給出源程式: #include <stdio.h> #include <stdlib.h> #define LIST_MAX_LEN 10 typedef int ElementType; typedef int BOOL; #define TR

資料結構C++語言實現——圖

圖 圖的基本概念 圖是資料結構G=(V,E) V(G)是G中結點的有限非空集合 E(G)是G中邊的有限集合 若圖中代表一條邊的偶對是有序的,則稱為有向圖,<u,v>u稱為該邊的始點(尾) ,v稱為邊的終點(頭)。有向邊也稱為弧。

資料結構java語言實現及其應用

 棧的結構比較簡單,跟連結串列差不多底層還是一個數組,程式碼實現也比較容易,主要利用的就是他的先進後出的特點,java程式碼如下: <span style="font-size:14px;">package Stack; /* * 棧的實現 * 基本思路還是

資料結構 C語言實現直接插入排序

一、直接插入排序簡介  每次從無序表中取出第一個元素,把它插入到有序表的合適位置,使有序表仍然有序。  第一趟比較前兩個數,然後把第二個數按大小插入到有序表中; 第二趟把第三個資料與前兩個數從前向後掃描,把第三個數按大小插入到有序表中;依次進行下去,進行了(

資料結構C語言實現——順序線性表SqList

delcaration.h #ifndef DECLARATION_H_INCLUDED #define DECLARATION_H_INCLUDED #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0

資料結構C語言實現之鏈式佇列的6種演算法程式碼

#include <stdio.h>#include <stdlib.h>typedef int elemType;/************************************************************************//* 以下是關於佇列連

處理機排程演算法C語言實現註釋得當!!

/* created by herbert on 10 Nov */ #include <iostream> #include <queue> #include <algorithm> #include <c

佇列的C語言實現通過核心連結串列

0. 環境說明 本文的實驗環境是: win7作業系統+Keil 5 IDE. 非常適合嵌入式軟體開發 1. 打造自己的“list.h” 在微控制器程式開發中,有時候會用到佇列。能否設計一個通用的佇列呢?我想,可以把核心連結串列用起來。 以下程式碼是我

順序的九種基本操作和實現資料結構C語言版清華大學嚴蔚敏

棧是僅限定在表尾進行插入和刪除操作的線性表,在嚴蔚敏版的C語言版的資料結構中共定義了九種棧的基本操作;分別是構造 銷燬 清空 棧長 棧頂 插入 刪除 遍歷。下面就是程式碼實現: 標頭檔案和巨集定義(

資料結構——c語言描述 第三章 2的練習四則運算的實現

棧的基本概念和實現我在上一篇文章中實現了,現在做一下練習,一個簡單的四則運算的實現,還是比較簡單的,我並沒有再往下實現括號的四則運算,這個都是次要的,主要是掌握棧的操作方法,和一些基本的注意事項,其實這個程式碼我之前刪除了又重寫了一邊,第一遍在實現的過程中對自己的程式碼並沒