Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct stack_struct {
  5.     int data;
  6.     struct stack_struct *next;
  7. } stack_s;
  8.  
  9. void push(stack_s **head, int data) {
  10.     stack_s *tmp = malloc(sizeof(stack_s));
  11.     //printf("%d \n", head);
  12.     tmp->next = *head;
  13.     tmp->data = data;
  14.     *head = tmp;
  15.     //printf("%d \n", head);
  16. }
  17.  
  18. int pop(stack_s **head) {
  19.     stack_s *last;
  20.         int data;
  21.         if (*head == NULL) return 1;
  22.     last = *head;
  23.     *head = (*head)->next;
  24.     data = last->data;
  25.     free(last);
  26.     return data;
  27. }
  28.  
  29. void printStack(stack_s *head) {
  30.     printf("stack >");
  31.     while (head) {
  32.         printf("%d ", head->data);
  33.         head = head->next;
  34.     }
  35. }
  36.  
  37. int main(){
  38.     stack_s *head = NULL;
  39.     stack_s *tmp;
  40.     for (int i = 0; i < 300; i++) push(&head, i);
  41.     printStack(head);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement