Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Una pila (detta anche stack) è un ADT in cui l’accesso ai dati è realizzato con strategia LIFO (Last In First Out): l’operazione di estrazione restituisce l’elemento più recentemente inserito.
- I metodi sono:
- [init], crea e inizializza un’istanza vuota dell’ADT;
- [push], inserisce un elemento;
- [pop], estrae l’elemento più recentemente inserito;
- [empty], restituisce vero se la pila è vuota, viceversa falso;
- [destroy], elimina l’istanza dell’ADT.
- NB: mettere top come variabile globale!
- int cpp_pop_stack(){
- if(top!=NULL){
- nodo * nuovo = top;
- top=top->link;
- delete(nuovo);
- return top->info;
- }
- return NULL;
- }
- void cpp_push_stack(int n){
- nodo * nuovo = new nodo();
- nuovo -> info = n;
- if(top==NULL){
- nuovo->link=NULL;
- }else{
- nuovo->link=top;
- }
- top=nuovo;
- }
- void display(nodo* head){
- if(head){
- cout<<head->info<<" ";
- display(head->link);
- }
- }
- ++++++++++++++++++++
- void push(int value)
- {
- struct Node *newNode;
- newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = value;
- if(top == NULL)
- newNode->next = NULL;
- else
- newNode->next = top;
- top = newNode;
- printf("\nInsertion is Success!!!\n");
- }
- void pop()
- {
- if(top == NULL)
- printf("\nStack is Empty!!!\n");
- else{
- struct Node *temp = top;
- printf("\nDeleted element: %d", temp->data);
- top = temp->next;
- free(temp);
- }
- }
- void display()
- {
- if(top == NULL)
- printf("\nStack is Empty!!!\n");
- else{
- struct Node *temp = top;
- while(temp->next != NULL){
- printf("%d--->",temp->data);
- temp = temp -> next;
- }
- printf("%d--->NULL",temp->data);
- }
- }
Add Comment
Please, Sign In to add comment