Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int stack_create (int * * st, int n, int * curr, int * head, int * tail);
  5. int stack_delete (int * st, int n, int * curr, int * head, int * tail);
  6. int stack_append (int * st, int n, int * curr, int * head, int * tail, int el);
  7. int stack_delete_el (int * st, int n, int * curr, int * head, int * tail);
  8. int stack_read (int * st, int n, int * curr, int * head, int * tail, int * previous);
  9. int stack_print (int * st, int n, int * curr, int * head, int * tail);
  10.  
  11. int stack_create (int * * st, int n, int * curr, int * head, int * tail) {
  12.     if (*curr != -1 )
  13.         return -1;
  14.     if (n <= 0)
  15.         return -1;
  16.     *st = (int *) malloc(n*sizeof(int));
  17.     if (*st == NULL)
  18.         return -1;
  19.     *curr = 0;
  20.     *head = 0;
  21.     *tail = 0;
  22.     return 0;
  23. }
  24. int stack_delete (int * st, int n, int * curr, int * head, int * tail) {
  25.     if (st == NULL)
  26.         return -1;
  27.         free(st);
  28.         *curr = -1;
  29.         return 0;
  30. }
  31. int stack_append (int * st, int n, int * curr, int * head, int * tail, int el) {
  32.     if (*curr == -1)
  33.         return -1;
  34.     if (*curr == n)
  35.         return -1;
  36.     st[*tail] = el;
  37.     *tail = (*tail+1) % n;
  38.     *curr = *curr + 1;
  39.  
  40.  
  41.     return 0;
  42. }
  43. int stack_delete_el (int * st, int n, int * curr, int * head, int * tail) {
  44.    
  45.     if (*curr == -1)
  46.         return -1;
  47.     if (*curr == 0)
  48.         return -1;
  49.     *head = (*head+1) % n;
  50.     *curr = *curr - 1;
  51.     return 0;
  52. }
  53. int stack_read (int * st, int n, int * curr, int * head, int * tail, int * previous) {
  54.     if (*curr == -1)
  55.         return -1;
  56.     if (*curr == 0)
  57.         return -1;
  58.     *previous = st[*tail];
  59.     return 0;
  60. }
  61. int stack_print (int * st, int n, int * curr, int * head, int * tail) {
  62.     if (*curr == -1)
  63.         return -1;
  64.     int curr_local, empty;
  65.     curr_local = *curr;
  66.     if (curr_local == 0 ) {
  67.         for (int i = 0; i < n; i++)
  68.         printf("*\n");
  69.         return 0;
  70.     }
  71.     if (*curr == n)
  72.         empty = 0;
  73.     else
  74.         empty = n-curr_local;
  75.     int i;
  76.     i = *head;
  77.     while ((i < n) && (curr_local > 0)) {
  78.         printf("%d\n", st[i]);
  79.         i++;
  80.         curr_local--;
  81.     }
  82.     i = 0;
  83.     while ((i <=(*tail)) && (curr_local > 0)) {
  84.         printf("%d\n", st[i]);
  85.         i++;
  86.         curr_local--;
  87.     }
  88.     for (int i = 0; i < empty; i++)
  89.         printf("*\n");
  90.     return 0;
  91. }
  92.  
  93.  
  94. int main() {
  95.     int * st;
  96.     int curr, head, tail, N, el, a;
  97.     curr = -1;
  98.     head = -1;
  99.     tail = -1;
  100.     int choice, code;
  101.     st = NULL;
  102.     while (1) {
  103.         printf("Select value: \n");
  104.         printf("1 - create stack\n");
  105.         printf("2 - delete stack\n");
  106.         printf("3 - append element to stack\n");
  107.         printf("4 - delete element from stack\n");
  108.         printf("6 - print stack\n");
  109.         scanf("%d", &choice);
  110.         switch (choice) {
  111.             case 1:
  112.                 printf("Enter number of elements: ");
  113.                 scanf("%d", &N);
  114.                 printf("\n");
  115.                 code = stack_create(&st, N, &curr, &head, &tail);
  116.                 if (code == -1) printf("Forbidden\n");
  117.                 break;
  118.             case 2:
  119.                 code = stack_delete(st, N, &curr, &head, &tail);
  120.                 if (code == -1) printf("Forbidden\n");
  121.                 break;
  122.             case 3:
  123.                 printf("Enter element to be appended: ");
  124.                 scanf("%d", &el);
  125.                 printf("\n");
  126.                 code = stack_append(st, N, &curr, &head, &tail, el);
  127.                 if (code == -1) printf("Forbidden\n");
  128.                 break;
  129.             case 4:
  130.                 code = stack_delete_el(st, N, &curr, &head, &tail);
  131.                 if (code == -1) printf("Forbidden\n");
  132.                 break;
  133.             case 5:
  134.                 code = stack_read(st, N, &curr, &a, &head, &tail);
  135.                 if (code == -1) printf("Forbidden\n");
  136.                 break;
  137.             case 6:
  138.                 code = stack_print(st, N, &curr, &head, &tail);
  139.                 if (code == -1) printf("Forbidden\n");
  140.                 break;
  141.             default:
  142.                 printf("Wrong Choice!\n");
  143.                 break;
  144.         }
  145.        
  146.        
  147.        
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement