)堆栈程序中的预期错误

时间:2014-02-06 16:39:23

标签: c compiler-errors stack turbo-c

这是我的代码......我在'void push(int item,STACK * S)'中得到了这个错误。

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define STACK_SIZE 5

struct stack
{
int arr[STACK_SIZE];
int top;
};


type def struct stack,STACK;
void push(int item, STACK *S)
{
if(S->top==STACK_SIZE-1)
{
    printf("stack overflow\n");
    return;
}
S->top++;
S->arr[S->top]=item;
  } 

  void pop(STACK *S)
{
int item;
if(S->top==-1)
{
    printf("stack overflow\n");
    return;
}

printf("deleted element is %d \n ",S->arr[S->top--]);
   }

 void display(STACK *S)
 {
int i;
if(S->top==-1)
{
    printf("stack underflow\n");
    return;
}

printf("the contest of the stack \n");
for(i=s->top;i>=0;i--)
{
    printf("%d\n",S->arr[i]);
}
    } 


   void main()
   {
int item,ch;
STACK S;
clrsacr();
S.to=-1;
for(;;)
{
    printf("1:push\n 2:pop\n 3:display\n 4:exit\n");
    printf("enter the choice\n");
    scanf("%d",ch);

    switch(ch)
    {
        case 1: printf("enter the item to be inserted\n");
            scanf("%d",&item);
            push(item,&S);
            break;

        case 2: pop(&s);
            break;

        case 3: display(&S);
            break;

        default: exit(0);
    }
}
getch();
    }

在这我得到另一个错误...声明在“类型def结构堆栈,STACK;'时错误地终止。我在turbo c中编译此程序时出现此错误。我对堆栈有点新。

3 个答案:

答案 0 :(得分:1)

type def struct stack,STACK;
    ^ typedef is a keyword not type def

使用: -

typedef struct stack STACK;

答案 1 :(得分:1)

在typedef中删除space,

更改为

typedef struct stack STACK;

而不是

type def struct stack,STACK;

您也可以将结构设置为

typedef struct stack
{
    int arr[STACK_SIZE];
    int top;
}STACK;

答案 2 :(得分:1)

type def struct stack,STACK;更改为typedef struct stack STACK;

同时更改

S.to = -1 ;

S.top= -1 ;

及其clrscr();代替clrsacr();