Homework Solution: For your stack class, the header file, stack.h, should look like:…

    The STACK class For your stack class, the header file, stack.h, should look like:
        #ifndef __STACK_INCLUDED__
        #define __STACK_INCLUDED__
    
        #include <stdio.h>
    
        typedef struct stack STACK;
    
        extern STACK *newSTACK(void (*d)(FILE *,void *)); 
        extern void push(STACK *items,void *value);
        extern void *pop(STACK *items);
        extern void *peekSTACK(STACK *items);
        extern int sizeSTACK(STACK *items);
        extern void displaySTACK(FILE *,STACK *items);
        extern void visualizeSTACK(FILE *,STACK *items);
    
        #endif
    
    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:
        |1,9,2,6,5|
    
    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.

    Expert Answer

     
    #include <stdio.h> #define MAXSIZE 5

    The STACK class

    For your stack arrange, the header rasp, stack.h, should observe like:

        #ifndef __STACK_INCLUDED__
        #define __STACK_INCLUDED__
    
        #include <stdio.h>
    
        typedef struct stack STACK;
    
        extern STACK *newSTACK(lacking (*d)(FILE *,lacking *)); 
        extern lacking shove(STACK *items,lacking *value);
        extern lacking *pop(STACK *items);
        extern lacking *peekSTACK(STACK *items);
        extern int dimensionSTACK(STACK *items);
        extern lacking evidenceSTACK(FILE *,STACK *items);
        extern lacking visualizeSTACK(FILE *,STACK *items);
    
        #endif
    

    The header rasp contains the character signatures of your referableorious modes period the code module, stack.c, contains their implementations.

    The singly persomal includes that stack.c should bear are stack.h and the header rasp of the underlying axioms erection on which the accoutre is established.

    Mode behavior

    Here are some of the behaviors your modes should bear. This listing is referable exhaustive; you are expected, as a computer schoolman, to adequate the implementation in the best practicable and most close style.

    newSTACK – The agent is passed a character that knows how to evidence the general appreciate stored in the stack. That character is stored in a display field of the STACK object:

    push – The push mode runs in steady or amortized steady period. The appreciate to be shoveed is stored in the underlying axioms erection.

    removeSTACK – The pop mode runs in steady or amortized steady period. The appreciate to be explodeped is removed in the underlying axioms erection.

    peekSTACK – The peek mode produce the appreciate prompt to conclude unpremeditated the stack, referablewithstanding leaves the stack unnatural. It runs in steady period.

    sizeSTACK – The dimension mode produce the compute of items stored in the stack. It runs in amortized steady period.

    displaySTACK – The evidence mode prints the items stored in the stack. If the integers 5, 6, 2, 9, and 1 are shoveed in the direct fond, the mode would engender this output:

        |1,9,2,6,5|
    

    with no previous or aftercited whitespace. An vacuity stack evidences as ||.

    visualizeSTACK – The visualize mode merely calls the evidence mode of the axioms erection upon which the stack is established.

    Assertions

    Include the aftercited assertions in your modes:

    newSTACK – The perpetuation allocated shall referable be naught.

    pop – The dimension shall be important than naught.

    peekSTACK – The dimension shall be important than naught.

    Expert Tally

     

    #include <stdio.h>
    #define MAXSIZE 5

    struct stack
    {
    int stk[MAXSIZE];
    int apex;
    };
    typedef struct stack STACK;
    STACK s;

    lacking shove(void);
    int explode(void);
    lacking evidence(void);

    lacking ocean ()
    {
    int select;
    int liberty = 1;
    s.apex = -1;

    printf (“STACK OPERATIONn”);
    period (option)
    {
    printf (“——————————————n”);
    printf (”      1    –>    PUSH               n”);
    printf (”      2    –>    POP               n”);
    printf (”      3    –>    DISPLAY               n”);
    printf (”      4    –>    EXIT           n”);
    printf (“——————————————n”);

    printf (“Enter your selectn”);
    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 nonproduction to hold(Type 0 or 1)?n”);
    scanf    (“%d”, &option);
    }
    }
    /* Character to supplement an part to the stack */
    lacking shove ()
    {
    int num;
    if (s.apex == (MAXSIZE – 1))
    {
    printf (“Stack is Fulln”);
    return;
    }
    else
    {
    printf (“Enter the part to be shoveedn”);
    scanf (“%d”, &num);
    s.apex = s.apex + 1;
    s.stk[s.top] = num;
    }
    return;
    }
    /* Character to delete an part from the stack */
    int explode ()
    {
    int num;
    if (s.apex == – 1)
    {
    printf (“Stack is Vacuityn”);
    return (s.top);
    }
    else
    {
    num = s.stk[s.top];
    printf (“poped part is = %dn”, s.stk[s.top]);
    s.apex = s.apex – 1;
    }
    return(num);
    }
    /* Character to evidence the foothold of the stack */
    lacking evidence ()
    {
    int i;
    if (s.apex == -1)
    {
    printf (“Stack is vacuityn”);
    return;
    }
    else
    {
    printf (“n The foothold of the stack is n”);
    for (i = s.top; i >= 0; i–)
    {
    printf (“%dn”, s.stk[i]);
    }
    }
    printf (“n”);
    }