Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Stack.h"
- #include <iostream>
- #include <memory.h>
- using namespace std;
- //Stack* next = NULL; //index of first empty cell on the stack
- //trzeba w jakis sposob zapisywac znacznik head, bo teraz nigdzie go nie dodaję
- Stack* createStack(){
- //zarezerwuj wystarczajacy obszar pamieci
- Stack *stack = ( Stack* )malloc( sizeof( Stack ) );
- stack->next = NULL;
- //zwroc znacznik do stosu
- return stack;
- }
- void push(Stack** head, char c)
- {
- //stworz nowy element typu stack
- Stack* el = createStack();
- //do nowego elementu przypisz podana wartosc oraz obecny ostatni element jako nastepny
- el->value = c;
- el->next = *head;
- //nowy element staje sie ostatni na liscie
- *head = el;
- }
- char pop(Stack** head)
- {
- char c = top(head);
- if (c)
- {
- del(head);
- }
- else
- cout << "\n* Stack undefined!!!\n\n\n";
- return c;
- }
- char top(Stack** head) //jesli stos pusty to nie wypisuje bledu, ale zwraca znak o kodzie 0, co odpowiada wartosci FALSE
- {
- if (!isEmpty(head))
- return (**head).value;
- return 0;
- }
- void del(Stack** head)
- {
- if (!isEmpty(head))
- {
- Stack* el = *head;
- *head = (**head).next;
- free(el);
- }
- else
- cout << "\n* Stack underflow!!! (del)\n\n\n";
- }
- int isEmpty(Stack** head)
- {
- return !(**head).next;
- //znacznik do nastepnego elementu. jesli dojdziemy do konca bedzie !NULL wiec = 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement