Define the ADT for stack. Show the implementation of the stack data structure using linked list.
The ADT Stack is specified by
one constructor, namely,
stack *create_stack(void)
that constructs a data structure of type stack
, empty for the moment,
and returns a pointer to it,
two access functions, namely,
boolean stack_is_empty(stack *s)
that, given a pointer s
to a stack,
returns TRUE
if the stack is empty and returns FALSE
otherwise,
stack_object *top_of_stack(stack *s)
that, given a pointer s
to a nonempty stack,
returns a pointer to the object on the top of the stack,
two manipulator functions, namely,
void push_on_stack(stack *s, stack_object *object);
that, given a pointer s
to a stack and given a pointer object
to an object,
pushes the object on the top of the stack,
void pop_stack(stack *s);
that, given a pointer s
to a nonempty stack,
pops the stack.
Implementing Stack as a linked list
typedef struct stack_item { stack_object content; struct stack_item *next; } stack_item;
typedef stack_item *stack;
stack *create_stack(void) { stack *s; s = (stack *) malloc(sizeof(stack)); if(s==NULL) { printf("OUT OF MEMORY!\n"); exit(1); }
*s = NULL; return s; }
boolean stack_is_empty(stack *s) { return( (*s==NULL) ? TRUE : FALSE ); }
stack_object *top_of_stack(stack *s) { if(stack_is_empty(s)==TRUE) { printf("TRIED TO FIND THE TOP OF AN EMPTY STACK!\n"); exit(1); }
return &((*s)->content); }
void push_on_stack(stack *s, stack_object *object) { stack_item *new_item; new_item = (stack_item *) malloc(sizeof(stack_item)); if(new_item==NULL) { printf("OUT OF MEMORY!\n"); exit(1); }
new_item->content=*object; new_item->next=*s; *s=new_item; }
void pop_stack(stack *s) { stack_item *memo;
if(stack_is_empty(s)==TRUE) { printf("TRIED TO POP AN EMPTY STACK!\n"); exit(1); }
memo=*s; *s=memo->next; free(memo); }
a) Define big O notations.
b) \( {T(n) = 4n^{2}+3n \log_{}{n} } \), express T( n ) in Big( O ) notations.
Describe a string reversal algorithm. Write the difference between a [ ] [ ] and **a.
Discuss the different stages of ‘Capability Maturity Model’.
Write a C program to check a year is leap year or not.
Write a C program to manage array operation using switch case.
Write a C program to convert binary to decimal using function.