回 帖 发 新 帖 刷新版面

主题:求救啊  24个错误~~

typedef char SElemType;
#include<string.h>
#include<ctype.h>
#include<malloc.h> 
#include<limits.h> 
#include<stdio.h>
#include<stdlib.h>
typedef struct
    { 
   char top;    
   int length;   
  }Stack;

void InitStack (Stack &S)
 { 
    S.top = NULL;   
  S.length = 0;  
 }
void push(Stack &S,char e)      
{   
     p=new LNode;
    if(!p) exit(1);  
      p -> data = e;
  p -> next = S.top; 
  S.top = p;     

  ++S.length;     /* 栈的长度增1*/
 } 

  

char pop(char &e,Stack &S)     /*弹出栈顶的字符*/
{
    if(!S.top)
    return false;
    else
    {
        e=S.top->data;
        q=S.top;
        S.top=S.top->next;
        delete q;
          return e;
}

               
main()
 { int result;
   char str="(((2*(1+(8-4)))-8)/2)";
   result=process(str);
   printf("%d\n",result);
 }
 int process(char *str)
 {  
   InitStack(&S1);  /*放右括号子( */
   InitStack(&S2);  /*放整数*/
   InitStack(&S3); /*放计算符*/
 
 int i=0;
   char temp;
  for(i=0;str[i]!='\0';i++)
 {
    if (str[i]!=')')
    { 
        if (str[i]=='(') push(S1,str[i]);
        
        if (str[i]=='1'&&'2'&&'3'&&'4'&&'5'
                       &&'6'&&'7'&&'8'&&'9'&&'0') 
         push(&S2,(int)str[i]); 
            
         if (str[i]=='+' && '-' && '*' && '/') push(S3,str[i]); 
        else
          {pop(right,S2);pop(left,S2);pop(operator,S3);

         switch(operator) 
          { char temp
            case'+': temp = left + right;break;
           case'-': temp = left - right;break;                       
           case'*': temp = left * right;break;                    
            case'/': temp = left / right;                     
           }  
Push(S2,temp);
                                       
   }                
                         
           return temp;
              
      }
   }

回复列表 (共3个回复)

沙发

二、链栈 结构定义:
  typedef struct { 
   SLink top;    // 栈顶指针 
   int length;   // 栈中元素个数
  } Stack;

  此时只需要对顺序栈的基本操作接口作两处改动,便可作为链栈的基本操作接口。
两处是哪两处啊??

彻底被栈搞疯了。。。 一塌糊涂

板凳

栈的定义好象不行哦!下面是顺序的
include "stdio.h"
#define StackSize 100
typedef int ElemType;
typedef struct {
  ElemType elem[StackSize];
  int top;
}SqStack;

InitStack(SqStack *pS)
{
  pS->top=0; /* top指向栈顶的上一个元素 */
}

int Push(SqStack *pS,ElemType e)
{
  if (pS->top==StackSize-1)   /* 栈满 */
    return 0;

  pS->elem[pS->top]=e;
  pS->top=pS->top+1;
  return 1;
}

int Pop(SqStack *pS,ElemType* pe)
{
  if (pS->top==0)  /* 栈空 */
    return 0;

  pS->top = pS->top - 1;
  *pe = pS->elem[pS->top];
  return 1;
}
下面是链式的
#include "stdio.h"
/* 数据元素的类型 */
typedef char ElemType;

/* 节点的类型(包括头节点) */
typedef struct Node{
  ElemType elem;
  struct Node *next;
}SNode;

/* 初始化,头节点 */
InitStack(SNode* pS)
{
  pS->next=NULL;
}

/* 入栈:在头节点之后插入一个新节点 */
Push(SNode* pS,ElemType e)
{
  SNode* node;
  node = (SNode*)malloc(sizeof(SNode));
  node->elem = e;
  node->next = pS->next;
  pS->next = node;
}

int Pop(SNode* pS,ElemType* pe)
{
  SNode* node;

  if (pS->next==NULL){
    return 0;
  }
  *pe = pS->next->elem;

  node=pS->next;
  pS->next=node->next;
  free(node);
  return 1;
  
}
希望对你有帮助!

3 楼

恩,我看了书了,链栈实现可以通过链表直接来实现,写下头文件就可以了

我来回复

您尚未登录,请登录后再回复。点此登录或注册