Created
October 26, 2026 08:27
-
-
Save thinkphp/a1e07351882d2322e45e5bf6387e02ba to your computer and use it in GitHub Desktop.
stiva implementata ca Vector structura de baza
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Laborator 3 - exercise 2 Stiva LIFO implementata ca vector | |
| index: 0 1 2 3 .... MAX - 1 | |
| [4] [15] [2] [17] []..... | |
| stiva plina = top = MAX - 1 | |
| stiva goala = top = -1 | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdbool.h> | |
| #define MAX 100 | |
| void stack_init(int *top) { | |
| *top = -1; | |
| } | |
| bool stack_is_empty(const int *top) { | |
| return *top == -1; | |
| } | |
| bool stack_is_full(const int *top) { | |
| return *top == MAX - 1; | |
| } | |
| /*--------------Operatii fundamentale---------------*/ | |
| //adauga un element in vaful stivei O(1) | |
| //overflow: stiva plina --->afiseaza eroare si nu modifica stiva | |
| //top = -1 | |
| //5 (top=0) | |
| //7 (top=1) | |
| // 1 2 3 4 5 6 8 VARF | |
| void push(int *stack, int *top, int key) { | |
| if(*top == MAX - 1) { | |
| fprintf(stderr, "Err: overflow - stiva este plina\n"); | |
| return; | |
| } | |
| (*top)++; | |
| stack[*top] = key; | |
| } | |
| //pop: scoate elementul din varful stive si ii returneaza cheia - O(1) | |
| //undeflow: stiva este goala -> afiseaza eroare si returneaza -1 | |
| int pop(int *stack, int *top) { | |
| if(*top == -1) { | |
| fprintf(stderr, "Err: undeflow - stiva este empty\n"); | |
| return -1; | |
| } | |
| int key = stack[ *top ]; | |
| (*top)--; | |
| return key; | |
| } | |
| /*returneaza varful stivei fara sa-l stearga*/ | |
| int peek(const int* stack ,const int *top) { | |
| if(*top == -1) { | |
| fprintf(stderr, "err: stiva este goala"); | |
| return -1; | |
| } | |
| return stack[*top]; | |
| } | |
| void stack_print(const int *stack, const int *top) { | |
| if(*top == -1) { | |
| printf("STIVA ESTE GOALA! {} top = -1\n"); | |
| return; | |
| } | |
| printf("VARF -> "); | |
| for(int i = *top; i >= 0; i--) { | |
| printf("%d", stack[i]); | |
| if(i > 0) printf(" -> "); | |
| } | |
| printf(" -> baza\n"); | |
| } | |
| int main(int argc, char const *argv[]) | |
| { | |
| //s->first : SLL | |
| int stack[ MAX ]; | |
| int top; | |
| stack_init( &top ); | |
| stack_print(stack, &top); | |
| //push | |
| push(stack, &top, 4); | |
| push(stack, &top, 15); | |
| push(stack, &top, 2); | |
| push(stack, &top, 7); | |
| printf("\nDupa push(4, 15, 2, 7): \n"); | |
| stack_print(stack, &top); /* 7 --> 2 ---> 15 ---> 4*/ | |
| printf("\nTOP = %d (NU STERGE)\n", peek(stack, &top)); /* 7 , nu STERGE */ | |
| printf("\npop() = %d\n", pop(stack, &top)); /* 7 */ | |
| printf("\npop() = %d\n", pop(stack, &top)); /* 2 */ | |
| stack_print(stack, &top); /* 15 ---> 4*/ | |
| printf("\nTOP = %d (NU STERGE)\n", peek(stack, &top)); /* 7 , nu STERGE */ | |
| push(stack, &top, 14); | |
| printf("\nDupa push(14): \n"); | |
| stack_print(stack, &top); /* 14 ---> 15 ----> 4 */; | |
| //GOLIRE STIVA; top revine la -1, nu este nevoie FREE | |
| top = -1; | |
| printf("\n Dupa golire: \n"); | |
| stack_print(stack, &top); | |
| printf("TEST undeflow: \n"); | |
| pop(stack, &top); | |
| pop(stack, &top); | |
| pop(stack, &top); | |
| printf("\nTEST overflow: \n"); | |
| for( int i = 0; i <= MAX; ++i ) { | |
| push(stack, &top, i); //ultimul push -->EROARE | |
| } | |
| stack_print(stack, &top); | |
| push(stack, &top, 1011); //ultimul push -->EROARE | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment