Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ex1_header.h"
- //add a new member to list of the stack
- void Push(PStack s, int new_elem) {
- Item* item;
- //if stack is not empty
- if (s->size < NUM) {
- //init item:
- item = (Item*)malloc(sizeof(Item));
- if (!item)
- {
- Delete_Stack(&s);
- Error_Msg("Not Enought Memory");
- }
- //insert data to new item
- item->num = new_elem;
- item->next = s->head;
- //push item to stack
- s->head = item;
- s->size++;
- }
- else
- printf("Error: stack is FULL\n\n");
- }
- //delete member from the stack and return the deleted value using int * del_value
- int Pop(PStack s, int* del_value) {
- Item* tmp;
- //if stack is not null
- if (s->size > 0) {
- //save head in tmp, save the data into del_value
- tmp = s->head;
- *del_value = s->head->num;
- //set new head (next element)
- s->head = s->head->next;
- //dec stack size
- s->size--;
- //free original head (tmp)
- free(tmp);
- return 1;
- }
- //if stack is null return 0
- else {
- printf("\nError: stack is EMPTY\n\n");
- return 0;
- }
- }
- //print error and exit
- void Error_Msg(char* str)
- {
- printf("\n%s", str);
- exit(1);
- }
- //delete stack - free all elements
- void Delete_Stack(PStack stack)
- {
- Stack * tmp;
- while (stack->head) {
- tmp = stack->head;
- stack->head = stack->head->next;
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement