Advertisement
lukicdarkoo

Stek (stack)

Jan 17th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define STACK_NULL -1
  5.  
  6. typedef int nodeType;
  7.  
  8. typedef struct nodeStackStruct {
  9.     struct nodeStackStruct *next;
  10.     nodeType data;
  11. } nodeStack;
  12.  
  13.  
  14. void push(nodeStack **top, nodeType data) {
  15.     nodeStack *new;
  16.    
  17.     new = (nodeStack *)malloc(sizeof(nodeStack));
  18.     new->data = data;
  19.     new->next = *top;  
  20.     *top = new;
  21. }
  22.  
  23. nodeType pop(nodeStack **top) {
  24.     nodeType data;
  25.     nodeStack *tmp;
  26.  
  27.     if (*top == NULL)
  28.         return STACK_NULL;
  29.    
  30.     data = (*top)->data;
  31.     tmp = *top;
  32.     *top = tmp->next;
  33.     free(tmp);
  34.    
  35.     return data;
  36. }
  37.  
  38. nodeType top(nodeStack *top) {
  39.     if (top == NULL)
  40.         return STACK_NULL;
  41.    
  42.     return top->data;
  43. }
  44.  
  45. nodeType deleteStack(nodeStack *top) {
  46.     if (top != NULL) {
  47.         deleteStack(top->next);
  48.         free(top);
  49.     }
  50. }
  51.  
  52. void printStack(nodeStack *top) {
  53.     if (top != NULL) {
  54.         printf("%d\n", top->data);
  55.         printStack(top->next);
  56.     }
  57. }
  58.  
  59. int main() {
  60.     nodeStack *top;
  61.     top = NULL;
  62.    
  63.     push(&top, 10);
  64.     push(&top, 20);
  65.    
  66.     printStack(top);
  67.    
  68.     deleteStack(top);
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement