Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int stack_create (int * * st, int n, int * curr, int * head, int * tail);
- int stack_delete (int * st, int n, int * curr, int * head, int * tail);
- int stack_append (int * st, int n, int * curr, int * head, int * tail, int el);
- int stack_delete_el (int * st, int n, int * curr, int * head, int * tail);
- int stack_read (int * st, int n, int * curr, int * head, int * tail, int * previous);
- int stack_print (int * st, int n, int * curr, int * head, int * tail);
- int stack_create (int * * st, int n, int * curr, int * head, int * tail) {
- if (*curr != -1 )
- return -1;
- if (n <= 0)
- return -1;
- *st = (int *) malloc(n*sizeof(int));
- if (*st == NULL)
- return -1;
- *curr = 0;
- *head = 0;
- *tail = 0;
- return 0;
- }
- int stack_delete (int * st, int n, int * curr, int * head, int * tail) {
- if (st == NULL)
- return -1;
- free(st);
- *curr = -1;
- return 0;
- }
- int stack_append (int * st, int n, int * curr, int * head, int * tail, int el) {
- if (*curr == -1)
- return -1;
- if (*curr == n)
- return -1;
- st[*tail] = el;
- *tail = (*tail+1) % n;
- *curr = *curr + 1;
- return 0;
- }
- int stack_delete_el (int * st, int n, int * curr, int * head, int * tail) {
- if (*curr == -1)
- return -1;
- if (*curr == 0)
- return -1;
- *head = (*head+1) % n;
- *curr = *curr - 1;
- return 0;
- }
- int stack_read (int * st, int n, int * curr, int * head, int * tail, int * previous) {
- if (*curr == -1)
- return -1;
- if (*curr == 0)
- return -1;
- *previous = st[*tail];
- return 0;
- }
- int stack_print (int * st, int n, int * curr, int * head, int * tail) {
- if (*curr == -1)
- return -1;
- int curr_local, empty;
- curr_local = *curr;
- if (curr_local == 0 ) {
- for (int i = 0; i < n; i++)
- printf("*\n");
- return 0;
- }
- if (*curr == n)
- empty = 0;
- else
- empty = n-curr_local;
- int i;
- i = *head;
- while ((i < n) && (curr_local > 0)) {
- printf("%d\n", st[i]);
- i++;
- curr_local--;
- }
- i = 0;
- while ((i <=(*tail)) && (curr_local > 0)) {
- printf("%d\n", st[i]);
- i++;
- curr_local--;
- }
- for (int i = 0; i < empty; i++)
- printf("*\n");
- return 0;
- }
- int main() {
- int * st;
- int curr, head, tail, N, el, a;
- curr = -1;
- head = -1;
- tail = -1;
- int choice, code;
- st = NULL;
- while (1) {
- printf("Select value: \n");
- printf("1 - create stack\n");
- printf("2 - delete stack\n");
- printf("3 - append element to stack\n");
- printf("4 - delete element from stack\n");
- printf("6 - print stack\n");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter number of elements: ");
- scanf("%d", &N);
- printf("\n");
- code = stack_create(&st, N, &curr, &head, &tail);
- if (code == -1) printf("Forbidden\n");
- break;
- case 2:
- code = stack_delete(st, N, &curr, &head, &tail);
- if (code == -1) printf("Forbidden\n");
- break;
- case 3:
- printf("Enter element to be appended: ");
- scanf("%d", &el);
- printf("\n");
- code = stack_append(st, N, &curr, &head, &tail, el);
- if (code == -1) printf("Forbidden\n");
- break;
- case 4:
- code = stack_delete_el(st, N, &curr, &head, &tail);
- if (code == -1) printf("Forbidden\n");
- break;
- case 5:
- code = stack_read(st, N, &curr, &a, &head, &tail);
- if (code == -1) printf("Forbidden\n");
- break;
- case 6:
- code = stack_print(st, N, &curr, &head, &tail);
- if (code == -1) printf("Forbidden\n");
- break;
- default:
- printf("Wrong Choice!\n");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement