Hello Friends..
I need suggestions to make my this program better...

Implementation of a STACK(containing names) as an array, with its basic operations PUSH,POP & SHOW..

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <process.h>
# define STACKSIZE 5
typedef struct{
   int size;
   char names[STACKSIZE][20];
  }STACK;
void push(char *);
char *pop();
void show();
int is_empty();
int is_full();
STACK *ps;
void main(){
 int choice;
 char name[20];
 STACK s;
 ps=&s;
 ps->size=0;
 do{
   clrscr();
   printf("\n\t  :1 for Push. ");
   printf("\n\t  :2 for Pop. ");
   printf("\n\t  :3 for Show.");
   printf("\n\t  :4 for Exit.");
   printf("\n\t Enter your choice:");
   scanf("%d",&choice);
   switch(choice){
    case 1:
      if(is_full())
	printf("\n\t Error: Stack Overflow..");
      else{
	printf("\n\n\t Enter a Name:");
	fflush(stdin);
	gets(name);
	push(name);
      }
      break;
    case 2:
      if(is_empty())
	printf("\n\n\t Error: Stack Underflow..");
      else{
	printf("\n\n\t Poped up Name is:");
	puts(pop());
      }
      break;
    case 3:
      if(is_empty())
	printf("\n\n\t The Stack is Empty..");
      else{
	printf("\n\n\t Current Stack is:\n\t\t");
	show();
      }
      break;
    case 4:
      exit(0);
    default:
      printf("\n\n\t Press only 1,2,3,4 keys..");
   }
   while(!kbhit());
  }while(1);
}

int is_empty(){
    return ps->size==0;
  }
int is_full(){
    return ps->size==STACKSIZE;
  }
void push(char *pc){
     strcpy(ps->names[(ps->size)++],pc);
     printf("\n\n\t  Successfully Pushed into STACK");
  }
char * pop(){
     return ps->names[--(ps->size)];
  }
void show(){
     int x=ps->size;
     while(x!=0)
       printf("\n\t\t %s",ps->names[--x]);
  }

Recommended Answers

All 3 Replies

line 2: delete conio.h because its non-standard and non-portable.

line 16: void main(){

It's always int main() void main() is non-standard and therefore non-portable.

line 2: delete conio.h because its non-standard and non-portable.

line 16: void main(){

It's always int main() void main() is non-standard and therefore non-portable.

well... thanks for your suggestions..
i am using conio.h for clrscr() function... what else should i use instead of clrscr()..

void clrscr()
{
   system("cls");
}

The above is only useful for MS-Windows. If you are using *nix then change "cls" to "clear"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.