Denny707

pila.txt

Jun 18th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. 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.
  2. I metodi sono:
  3. [init], crea e inizializza un’istanza vuota dell’ADT;
  4. [push], inserisce un elemento;
  5. [pop], estrae l’elemento più recentemente inserito;
  6. [empty], restituisce vero se la pila è vuota, viceversa falso;
  7. [destroy], elimina l’istanza dell’ADT.
  8.  
  9. NB: mettere top come variabile globale!
  10. int cpp_pop_stack(){
  11. if(top!=NULL){
  12. nodo * nuovo = top;
  13. top=top->link;
  14. delete(nuovo);
  15. return top->info;
  16. }
  17. return NULL;
  18. }
  19. void cpp_push_stack(int n){
  20. nodo * nuovo = new nodo();
  21. nuovo -> info = n;
  22. if(top==NULL){
  23. nuovo->link=NULL;
  24. }else{
  25. nuovo->link=top;
  26. }
  27. top=nuovo;
  28. }
  29. void display(nodo* head){
  30. if(head){
  31. cout<<head->info<<" ";
  32. display(head->link);
  33. }
  34. }
  35. ++++++++++++++++++++
  36. void push(int value)
  37. {
  38. struct Node *newNode;
  39. newNode = (struct Node*)malloc(sizeof(struct Node));
  40. newNode->data = value;
  41. if(top == NULL)
  42. newNode->next = NULL;
  43. else
  44. newNode->next = top;
  45. top = newNode;
  46. printf("\nInsertion is Success!!!\n");
  47. }
  48. void pop()
  49. {
  50. if(top == NULL)
  51. printf("\nStack is Empty!!!\n");
  52. else{
  53. struct Node *temp = top;
  54. printf("\nDeleted element: %d", temp->data);
  55. top = temp->next;
  56. free(temp);
  57. }
  58. }
  59. void display()
  60. {
  61. if(top == NULL)
  62. printf("\nStack is Empty!!!\n");
  63. else{
  64. struct Node *temp = top;
  65. while(temp->next != NULL){
  66. printf("%d--->",temp->data);
  67. temp = temp -> next;
  68. }
  69. printf("%d--->NULL",temp->data);
  70. }
  71. }
Add Comment
Please, Sign In to add comment