Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ass6_stack.h"
- #include <stdlib.h>
- #include <stdio.h>
- #define MAX_STK 100
- Stack * initialize_stk( void );
- void push( Stack *my_stack, int newElement);
- int main( void ) {
- Stack *my_stack;
- my_stack = initialize_stk();
- printf("Size of stack is %d\n", my_stack->size);
- push(my_stack, 100);
- printf("The value at the top of the stack is %d\n", my_stack->top->data);
- return 0;
- }
- Stack * initialize_stk( void ) {
- Stack *p;
- p = (Stack *) malloc(sizeof(Stack));
- if( p == NULL ) {
- printf("Error: malloc failed in initialize_stk\n");
- exit(EXIT_FAILURE);
- }
- p->size = 0;
- p->top = NULL;
- return p;
- }
- int size( Stack *my_stack ) {
- return my_stack->size;
- }
- void push( Stack *my_stack, int newElement ) {
- Node_s *newNode;
- newNode = (Node_s *) malloc(sizeof(Node_s));
- if( newNode == NULL ) {
- printf("Error: malloc failed in push\n");
- exit(EXIT_FAILURE);
- }
- newNode->data = newElement;
- newNode->next = my_stack->top;
- my_stack->top = newNode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement