Please comment this code. stackADT.c #include <stdio.h> #include <stdlib.h> #inc
ID: 3719465 • Letter: P
Question
Please comment this code.
stackADT.c
#include <stdio.h>
#include <stdlib.h>
#include "sqjuggler.h"
struct node{
Item data;
struct node *next;
};
struct stack_type{
struct node *top;
};
static void terminate(const char *message)
{
printf("%s ",message);
exit(EXIT_FAILURE);
}
stack create(int size)
{
Stack s=malloc(sizeof(struct stack_type));
if(s==NULL)
terminate("Error in create: stack could not be created.");
s->top==NULL;
return s;
}
void destroy(Stack s)
{
make_empty(s);
free(s);
}
void make_empty(Stack s)
{
while(!is_empty(s))
pop(s);
}
bool is_empty(Stack s)
{
return s->top==NULL;
}
bool is_full(Stack s)
{
return false;
}
void push(Stack s, int i)
{
struct node *new_node=malloc(sizeof(struct node));
if(new_node==NULL)
terminate("Error in push: stack is full.");
new_node->data=i;
new_node->next=s->top;
s->top=new_node;
}
Item pop(Stack s)
{
struct node *old_top;
Item i;
if(is_empty(s))
terminate("Error in pop: stack is empty.");
old_top=s->top;
i=old_top->data;
s->top=old_top->next;
free(old_top);
return i;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include "sqjuggler.h"
// declare a node
struct node{
Item data;
// point to the next node
struct node *next;
};
// define the structure of the stack
struct stack_type{
// point to the first node of list or the top of stack
struct node *top;
};
// function to run when the program is terminated
static void terminate(const char *message)
{
printf("%s ",message);
// exit th program
exit(EXIT_FAILURE);
}
// create a stack and return it
stack create(int size)
{
// allocate the memory of stck
Stack s = malloc(sizeof(struct stack_type));
// if the program was unable to create
if(s==NULL)
// terminate the program
terminate("Error in create: stack could not be created.");
// set the list as empty
s->top==NULL;
return s;
}
// destroy the stack and free its memory
void destroy(Stack s)
{
make_empty(s);
// free the memory of stack
free(s);
}
// empty the stack
void make_empty(Stack s)
{
// loop until the stack is empty
while(!is_empty(s))
// pop the top most element in stack
pop(s);
}
// return true if the stack is empty
bool is_empty(Stack s)
{
return s->top==NULL;
}
// return true if the stack is full
bool is_full(Stack s)
{
return false;
}
// push i into the stack s
void push(Stack s, int i)
{
// create a new node
struct node *new_node=malloc(sizeof(struct node));
// if the computer was unable to allocate memory to node
if(new_node==NULL)
terminate("Error in push: stack is full.");
// set the value of data
new_node->data=i;
// make the new node point to top of stack
new_node->next=s->top;
// make new node as new top of stack
s->top=new_node;
}
// remove the top most element of stack
Item pop(Stack s)
{
struct node *old_top;
Item i;
// if stack s is empty
if(is_empty(s))
// terminate the program
terminate("Error in pop: stack is empty.");
// make old top point to top most element in stack
old_top=s->top;
i=old_top->data;
// remove the top most element from stack
s->top=old_top->next;
// free the memory of node
free(old_top);
return i;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.