Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implementacja Stosu
- #include <stdio.h>
- #include <stdlib.h>
- struct stack_node{
- int data;
- struct stack_node *next;
- } *top = NULL;
- struct stack_node *push(struct stack_node *top, int data)
- {
- struct stack_node *new_node = (struct stack_node*)malloc(sizeof(struct stack_node));
- if(new_node){
- new_node->data = data;
- new_node->next = top;
- top = new_node;
- }
- return top;
- }
- int pop(struct stack_node **top)
- {
- int tmp_data = -1;
- if(top){
- tmp_data = (*top)->data;
- struct stack_node *tmp = (*top)->next;
- free(*top);
- *top = tmp;
- }
- return tmp_data;
- }
- int peek(struct stack_node *top)
- {
- if(top)
- return top->data;
- else{
- fprintf(stderr, "Stos jest pusty\n");
- return -1;
- }
- }
- int main(void)
- {
- top = push(top, 2);
- top = push(top, 4);
- top = push(top, 6);
- printf("Szczytowy element stosu: %d\n", peek(top));
- printf("Usunieto element: %d\n", pop(&top));
- printf("Szczytowy element stosu: %d\n", peek(top));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement