Question

this is my code...i am getting that error at '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();
    }

in this i'm getting another error...declaration terminated incorrectly at "type def struct stack,STACK;'. i got this error while compiling this program in turbo c. i am a bit new to stacks.

Was it helpful?

Solution

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

Use:-

typedef struct stack STACK;

OTHER TIPS

Remove space and , in typedef

change to

typedef struct stack STACK;

instead of

type def struct stack,STACK;

Also you can typedef your structure as

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

Change type def struct stack,STACK; to typedef struct stack STACK;

Also change

S.to = -1 ;

to

S.top= -1 ;

and its clrscr(); instead of clrsacr();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top