[CODE]../* Program function: Simulate the stack using a stack limit of 10. Display
a menu for the the following.
[C] Create a stack
[I] Insert an item in the stack
[P] Pop an item from the stack
[E] Peep/view the item at the top of the stack
[V] Current value of top
[W] View all items stored in the stack
[Q] Quit
*the stack accepts lowercase & uppercase characters only from A-Z
*any other symbol (e.g. punctuation marks, numbers, etc. should not be accepted by the program
*the stack should not accept DUPLICATE ENTRIES
Note:
*[C]create a stack
-deletes all items in the stack after a confirmation if
there are existing items stored in it.
*Display appropriate messages esp when
* trying to insert an item in a stack that is already full
* trying to pop items from the stack that is empty
* duplicate items in the stack ( note: lowercase and
uppercase letters are accepted as two different entries)
*insertion of invalid values
*/
#include<stdio. h>
#include<conio. h>
#include<ctype. h>
#include<string .h>
#define stacklimit 10
int top,i;
typedef enum boolean{FALSE, TRUE} boolean;
wrong(int error)
{
printf("\n");
switch(error)
{
case 0: printf("Incorre ct input\n"); break;
case 1: printf("Please enter only Y or N\n"); break;
case 2: printf("Please enter a letter from A-Z\n"); break;
case 3: printf("Input is already in stack\n"); break;
}
}
createstack(cha r stack[])
{
for(i=0; i<stacklimit; i++)
stack[i]=NULL;
return stack[];
}
char initialize_stac k(char stack[])
{
int error=0;
char choice= 'Y';
clrscr();
if(top!=-1)
{
do{
printf("Do you want to reintialize the stack? [Y/N]");
choice=toupper( getch());
if((choice!='Y' )||(choice!='N' ))
{ error=1;
wrong(error);
}
else;
}while(error!=0 );
if(choice=='Y')
{ top=-1;
createstack(sta ck);
return stack[];
}
else;
}
else
{ top=-1;
createstack(sta ck);
return stack[];
}
}
int FULLSTACK()
{ int x;
if(top==stackli mit-1)
{ x=1;
return x;
}
else
{ x=0;
return x;
}
}
int checker(char input, char stack[])
{ int flag=0;
for(i=0;i<stack limit;i++)
{ if(stack[i]==input)
flag=1;
}
return flag;
}
push(char input, char stack[])
{
char temp_stack[stacklimit];
int x=1;
top+=1;
for(i=0;i<stack limit;i++)
temp_stack[i]=stack[i];
stack[0]=input;
for(i=0;i<stack limit;i++)
{ stack[x]=temp_stack[i];
x++;
}
return stack[];
}
input_a(char stack[])
{
int error=0, full;
char input,check;
full=FULLSTACK( );
if(full==1)
{ printf("Stack is already full");
return;
}
else
{
do{
printf("Please enter a letter from A-Z: ");
scanf("%c", &input);
check=toupper(i nput);
if(check!='A'|| check!='B'||che ck!='C'||check! ='D'||
check!='E'||che ck!='F'||check! ='G'||check!='H '||check
!='I'||check!=' J'||check!='K'| |check!='L'||ch eck!='M'
||check!='N'||c heck!='O'||chec k!='P'||check!= 'Q'||
check!='R'||che ck!='S'||check! ='T'||check!='U '||check
!='V'||check!=' W'||check!='X'| |check!='Y'||
check!='Z')
{ error=2;
wrong(error);
}
else if(checker(inpu t, stack))
{
error=3;
wrong(error);
}
else;
}while(error!=0 );
push(input, stack);
}
}
boolean EMPTYSTACK()
{
if(top==-1)
return TRUE;
else
return FALSE;
}
char pop(char stack[])
{
char x=0;
boolean empty;
empty=EMPTYSTAC K();
if(empty==TRUE)
{
printf("Stack is empty");
getch();
return x;
}
else
{
x=stack[top];
top-=1;
return x;
}
}
view(char stack[])
{
for(i=0;i<stack limit;i++)
printf("\n %d", stack[i]);
}
main()
{
int quit=0,error;
char choice , stack[stacklimit];
createstack(sta ck);
do{
clrscr();
printf("Main Menu");
printf("\n[C] Create a stack");
printf("\n[I] Insert an item in the stack");
printf("\n[P] Pop an item from the stack");
printf("\n[E] Peep/View the item at the top of the stack");
printf("\n[V] Current value of top");
printf("\n[W] View all items stored in the stack");
printf("\n[Q] Quit\n");
choice=toupper( getch());
switch(choice)
{
case 'C': initialize_stac k(stack); break;
case 'I': input_a(stack); break;
case 'P': pop(stack); break;
case 'E': printf("\n%d", stack[0]); break;
case 'V': printf("\n%i", top); break;
case 'W': view(stack); break;
case 'Q': quit=1; break;
default: error=0; wrong(error); getch(); break;
}
}while(quit==0) ;
}..[\CODE]
a menu for the the following.
[C] Create a stack
[I] Insert an item in the stack
[P] Pop an item from the stack
[E] Peep/view the item at the top of the stack
[V] Current value of top
[W] View all items stored in the stack
[Q] Quit
*the stack accepts lowercase & uppercase characters only from A-Z
*any other symbol (e.g. punctuation marks, numbers, etc. should not be accepted by the program
*the stack should not accept DUPLICATE ENTRIES
Note:
*[C]create a stack
-deletes all items in the stack after a confirmation if
there are existing items stored in it.
*Display appropriate messages esp when
* trying to insert an item in a stack that is already full
* trying to pop items from the stack that is empty
* duplicate items in the stack ( note: lowercase and
uppercase letters are accepted as two different entries)
*insertion of invalid values
*/
#include<stdio. h>
#include<conio. h>
#include<ctype. h>
#include<string .h>
#define stacklimit 10
int top,i;
typedef enum boolean{FALSE, TRUE} boolean;
wrong(int error)
{
printf("\n");
switch(error)
{
case 0: printf("Incorre ct input\n"); break;
case 1: printf("Please enter only Y or N\n"); break;
case 2: printf("Please enter a letter from A-Z\n"); break;
case 3: printf("Input is already in stack\n"); break;
}
}
createstack(cha r stack[])
{
for(i=0; i<stacklimit; i++)
stack[i]=NULL;
return stack[];
}
char initialize_stac k(char stack[])
{
int error=0;
char choice= 'Y';
clrscr();
if(top!=-1)
{
do{
printf("Do you want to reintialize the stack? [Y/N]");
choice=toupper( getch());
if((choice!='Y' )||(choice!='N' ))
{ error=1;
wrong(error);
}
else;
}while(error!=0 );
if(choice=='Y')
{ top=-1;
createstack(sta ck);
return stack[];
}
else;
}
else
{ top=-1;
createstack(sta ck);
return stack[];
}
}
int FULLSTACK()
{ int x;
if(top==stackli mit-1)
{ x=1;
return x;
}
else
{ x=0;
return x;
}
}
int checker(char input, char stack[])
{ int flag=0;
for(i=0;i<stack limit;i++)
{ if(stack[i]==input)
flag=1;
}
return flag;
}
push(char input, char stack[])
{
char temp_stack[stacklimit];
int x=1;
top+=1;
for(i=0;i<stack limit;i++)
temp_stack[i]=stack[i];
stack[0]=input;
for(i=0;i<stack limit;i++)
{ stack[x]=temp_stack[i];
x++;
}
return stack[];
}
input_a(char stack[])
{
int error=0, full;
char input,check;
full=FULLSTACK( );
if(full==1)
{ printf("Stack is already full");
return;
}
else
{
do{
printf("Please enter a letter from A-Z: ");
scanf("%c", &input);
check=toupper(i nput);
if(check!='A'|| check!='B'||che ck!='C'||check! ='D'||
check!='E'||che ck!='F'||check! ='G'||check!='H '||check
!='I'||check!=' J'||check!='K'| |check!='L'||ch eck!='M'
||check!='N'||c heck!='O'||chec k!='P'||check!= 'Q'||
check!='R'||che ck!='S'||check! ='T'||check!='U '||check
!='V'||check!=' W'||check!='X'| |check!='Y'||
check!='Z')
{ error=2;
wrong(error);
}
else if(checker(inpu t, stack))
{
error=3;
wrong(error);
}
else;
}while(error!=0 );
push(input, stack);
}
}
boolean EMPTYSTACK()
{
if(top==-1)
return TRUE;
else
return FALSE;
}
char pop(char stack[])
{
char x=0;
boolean empty;
empty=EMPTYSTAC K();
if(empty==TRUE)
{
printf("Stack is empty");
getch();
return x;
}
else
{
x=stack[top];
top-=1;
return x;
}
}
view(char stack[])
{
for(i=0;i<stack limit;i++)
printf("\n %d", stack[i]);
}
main()
{
int quit=0,error;
char choice , stack[stacklimit];
createstack(sta ck);
do{
clrscr();
printf("Main Menu");
printf("\n[C] Create a stack");
printf("\n[I] Insert an item in the stack");
printf("\n[P] Pop an item from the stack");
printf("\n[E] Peep/View the item at the top of the stack");
printf("\n[V] Current value of top");
printf("\n[W] View all items stored in the stack");
printf("\n[Q] Quit\n");
choice=toupper( getch());
switch(choice)
{
case 'C': initialize_stac k(stack); break;
case 'I': input_a(stack); break;
case 'P': pop(stack); break;
case 'E': printf("\n%d", stack[0]); break;
case 'V': printf("\n%i", top); break;
case 'W': view(stack); break;
case 'Q': quit=1; break;
default: error=0; wrong(error); getch(); break;
}
}while(quit==0) ;
}..[\CODE]
Comment