Advertisement
Guest User

Untitled

a guest
Apr 14th, 2012
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include "ass6_stack.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define MAX_STK 100
  6.  
  7. Stack * initialize_stk( void );
  8. void push( Stack *my_stack, int newElement);
  9.  
  10. int main( void ) {
  11.     Stack *my_stack;
  12.     my_stack = initialize_stk();
  13.     printf("Size of stack is %d\n", my_stack->size);
  14.     push(my_stack, 100);
  15.     printf("The value at the top of the stack is %d\n", my_stack->top->data);
  16.     return 0;
  17. }
  18.  
  19. Stack * initialize_stk( void ) {
  20.     Stack *p;
  21.     p = (Stack *) malloc(sizeof(Stack));
  22.     if( p == NULL ) {
  23.         printf("Error: malloc failed in initialize_stk\n");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.     p->size = 0;
  27.     p->top = NULL;
  28.     return p;
  29. }
  30.  
  31. int size( Stack *my_stack ) {
  32.     return my_stack->size;
  33. }
  34.  
  35. void push( Stack *my_stack, int newElement ) {
  36.     Node_s *newNode;
  37.     newNode = (Node_s *) malloc(sizeof(Node_s));
  38.     if( newNode == NULL ) {
  39.         printf("Error: malloc failed in push\n");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.     newNode->data = newElement;
  43.     newNode->next = my_stack->top;
  44.     my_stack->top = newNode;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement