Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #ifndef DEBUG
- #define DEBUG(...) printf(__VA_ARGS__)
- #endif
- typedef struct Node{
- struct Node *next;
- int val;
- } node;
- int push(node **top, int val) {
- if(!*top){
- *top=(node *)malloc(sizeof(node));
- if(*top==NULL) return -1;
- (*top)->val=val;
- (*top)->next=NULL;
- return 0;
- }
- node *novi=(node *)malloc(sizeof(node));
- if(novi==NULL){ return -1;}
- novi->val=val;
- novi->next=*top;
- *top=novi;
- return 0;
- }
- int pop(node **top) {
- if(!*top) return -1;
- node *temp=(node *)malloc(sizeof(node));
- temp=*top;
- *top=(*top)->next;
- free(temp);
- return 0;
- }
- int print(node *top) {
- if(top==NULL) return -1;
- printf("Stoga: ");
- for(; top!=NULL; top=top->next){
- printf("%d ", top->val);
- }
- printf("\n");
- return 0;
- }
- int main() {
- node *top = NULL;
- char menu_choice;
- int val, retval;
- do {
- DEBUG("\n(d) dodaj - push\n(b) brisi - pop\n(i) ispis - print\n(e) Exit\n");
- scanf(" %c", &menu_choice);
- switch (menu_choice) {
- case 'd':
- scanf("%d", &val);
- retval = push(&top, val);
- if (retval==-1) printf("Alokacija nije uspjela.\n");
- break;
- case 'b':
- retval = pop(&top);
- if (retval==-1) printf("Stog je prazan.\n");
- break;
- case 'i':
- retval = print(top);
- if (retval==-1) printf("Stog je prazan.\n");
- break;
- }
- } while(menu_choice!='e');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement