zoukankan      html  css  js  c++  java
  • 重构之路--代码实现

    重构之路--代码实现

    git链接:
    Operation3.1.1

    代码上次就已经实现了,这次加了一些新的功能,实现分数表达式。

           栈是一种最常用的和最重要的数据结构,是一种只能在一端进行插入或删除操作的线性表。表中允许进行插入、删除操作的一端称为栈顶。栈顶当前位置是动态的,栈顶当前位置由一个称为栈顶指针的位置指示器表示。表的另一端称为栈底。当栈中没有数据元素时,称为空栈,栈的插入操作通常称为进栈或入栈,栈的删除操作通常称为退栈或出栈。
           栈的主要特点是“后进先出”,即后入栈的元素先弹出。每次进栈的数据元素都放在原当前栈顶元素之前,成为新的栈顶元素,每次出栈的数据都是原当前栈顶元素。

    栈的基本操作

    • 初始化栈, init()
    • 入栈,push()
    • 出栈,pop()
    • 取栈顶元素,gettop()
    • 判断栈是否为空 empty()
    • 显示栈元素 display()
    • 释放栈 setnull()

           当然,这些操作你可以直接用stl模板,也可以自定义栈,比如数组仿真或者指针仿真。模板的话可以自行百度,而我用的是数组仿真,下面例子就演示指针仿真的吧。

    /*堆栈演示!*/
    #include <iostream>
    using namespace std;
    typedef struct Stack *link;
    typedef struct Stack Snode;
    
    struct Stack
    {
      int data;
      struct Stack *next;
    };
    
    link init()//初始化栈
    {
      link p;
      p=NULL;
      return p;
    }
    
    link push(link Head,int x)//入栈
    {
      link p;
      p=new Snode;
      if(p==NULL)
      {
        cout<<"
    Memory Error
    ";
        return Head;
      }
      p->data=x;
      p->next=Head;
      return p;
    }
    
    link pop(link Head)//出栈
    {
      link p;p=Head;
      if(p==NULL)
        cout<<"
    Stack is Empty!
    ";
      else
      {
        p=p->next;
        delete(Head);
      }
      return p;
    }
    
    link setnull(link Head)//释放栈
    {
      link p;p=Head;
      while(p!=NULL)
      {
        p=p->next;
        delete(Head);
        Head=p;
      }
      return Head;
    }
    
    int lenth(link Head)// 获得栈内元素个数
    { 
      int len=0;
      link p;
      p=Head;
      while(p!=NULL)
      {
        len++;
        p=p->next;
      }
      return len;
    }
    
    int gettop(link Head)//获得栈顶元素
    {
      if(Head==NULL)
      {
        cout<<"
    Stack is Empty!
    ";
        return -1;
      }
      else
        return Head->data;
    }
    
    void display(link Head)//显示栈内元素
    {
      link p;p=Head;
      if(p==NULL)
        cout<<"
    Stack is empty
    ";
      else
        do
        {
           cout<<p->data<<" ";
           p=p->next;
        }while(p!=NULL);
    }
    
    int empty(link Head)//判断栈是否为空
    {
      if(Head==NULL)
        return 1;
      else
        return 0;
    }
    
    int main()
    { 
      int i,x;
      link head1;
      head1=init();
      while(i!=6)
      {
        system("cls");
        cout<<"
     1.Input a stack data";
        cout<<"
     2.Output a stack data";
        cout<<"
     3.Empty or Not";
        cout<<"
     4.Display a top of stack";
        cout<<"
     5.Display the lenth of stack";
        cout<<"
     6.Exit and Free Stack
    ";
        cout<<"
      Stack is:  ";
        display(head1);
        cout<<"
    ";
    
        cin>>i;
        switch(i)
        {
          case 1: while(1)
    	          { 	
                    system("cls");
    		        cout<<"
     -.Input a stack data";
    		        cout<<"
     -.Output a stack data";
    		        cout<<"
     -.Empty or Not";
    		        cout<<"
     -.Display a top of stack";
    		        cout<<"
     -.Display the lenth of stack";
    		        cout<<"
     -.Exit and Free Stack
    ";
    		        cout<<"
      Stack is:  ";
    	            display(head1);
    		        cout<<"
    ";
    		        cout<<"
    input a number: until enter -1:
    ";
    		        cin>>x;
    		        if(x==-1)
    		          break;
    		        head1=push(head1,x);
    	          }
    	          break;
         case 2: head1=pop(head1);
    	         break;
         case 3: if(empty(head1))
    		       cout<<"
    Stack is empty
    ";
    	         else
    		       cout<<"
    Stack is not empty
    ";
                 system("pause");
    	         break;
         case 4: cout<<"
     The top is  "<<gettop(head1)<<endl;
    	         system("pause");
    	         break;
         case 5: cout<<"
     The length of stack is "<<lenth(head1)<<endl;
    	         system("pause");
    	         break;
        }
      }
      system("cls");;
      head1=setnull(head1);
      display(head1);
      getchar();
      return 0;
    }
    
    
  • 相关阅读:
    IBinder介绍
    Android组成部分
    Android中handler,looper与messageQueue的代码解析
    JS
    设计模式
    冒泡排序
    战斗逻辑
    mongo数据库基础
    JS闭包
    c/c++
  • 原文地址:https://www.cnblogs.com/yyf031602438/p/6892170.html
Copyright © 2011-2022 走看看