The STACK class For your stack class, the header file, stack.h , should look lik
ID: 3879248 • Letter: T
Question
The STACK class
For your stack class, the header file, stack.h, should look like:
The header file contains the function signatures of your public methods while the code module, stack.c, contains their implementations.
The only local includes that stack.c should have are stack.h and the header file of the underlying data structure on which the array is based.
Method behavior
Here are some of the behaviors your methods should have. This listing is not exhaustive; you are expected, as a computer scientist, to complete the implementation in the best possible and most logical manner.
newSTACK - The constructor is passed a function that knows how to display the generic value stored in the stack. That function is stored in a display field of the STACK object:
push - The push method runs in constant or amortized constant time. The value to be pushed is stored in the underlying data structure.
removeSTACK - The pop method runs in constant or amortized constant time. The value to be popped is removed in the underlying data structure.
peekSTACK - The peek method returns the value ready to come off the stack, but leaves the stack unchanged. It runs in constant time.
sizeSTACK - The size method returns the number of items stored in the stack. It runs in amortized constant time.
displaySTACK - The display method prints the items stored in the stack. If the integers 5, 6, 2, 9, and 1 are pushed in the order given, the method would generate this output:
with no preceding or following whitespace. An empty stack displays as ||.
visualizeSTACK - The visualize method simply calls the display method of the data structure upon which the stack is based.
Assertions
Include the following assertions in your methods:
newSTACK - The memory allocated shall not be zero.
pop - The size shall be greater than zero.
peekSTACK - The size shall be greater than zero.
Explanation / Answer
#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATION ");
while (option)
{
printf ("------------------------------------------ ");
printf (" 1 --> PUSH ");
printf (" 2 --> POP ");
printf (" 3 --> DISPLAY ");
printf (" 4 --> EXIT ");
printf ("------------------------------------------ ");
printf ("Enter your choice ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)? ");
scanf ("%d", &option);
}
}
/* Function to add an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full ");
return;
}
else
{
printf ("Enter the element to be pushed ");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* Function to delete an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty ");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty ");
return;
}
else
{
printf (" The status of the stack is ");
for (i = s.top; i >= 0; i--)
{
printf ("%d ", s.stk[i]);
}
}
printf (" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.