Advertisement
Guest User

stacks

a guest
Feb 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. //
  2. //  cw1.cpp
  3. //
  4. //
  5. //  Created by Maya Golan on 28/01/2020.
  6. //
  7.  
  8. #include "stack_char.hpp"
  9. #include <iostream>
  10.  
  11. typedef char stak_t;
  12. // easier to keep the type more abstract
  13. // (many operations would work also if
  14. // a type other than char was used to define stak_t)
  15.  
  16. struct stacknode {
  17.     stak_t data;
  18.     stacknode *next;
  19. };
  20.  
  21. typedef stacknode* stackptr;
  22. // more abstract, avoids cumbersome notations
  23. // such as having to write stacknode*& below
  24.  
  25. void stack_push(stak_t item, stackptr& s);
  26. // Adds an item to a stack (we pass a pointer to the top of the stack)
  27.  
  28. stak_t stack_pop(stackptr& s);
  29. // Returns the top item of a stack (and removes it from the stack)
  30.  
  31. stackptr stack_copy(const stackptr& original);
  32. // Returns a copy of an existing stack
  33. // (with appropriate memory allocation so that the original
  34. // and the copy don't point to the same memory)
  35.  
  36. void stack_print(const stackptr& s);
  37. // Prints the items of a stack
  38.  
  39. int stack_length(const stackptr& s);
  40. // Returns the length (can also be called the height) of a stack
  41.  
  42. bool stack_is_empty(const stackptr& s);
  43. // Returns true if a stack is empty. Returns false otherwise.
  44.  
  45. void stack_deallocate(stackptr& s);
  46. // Deallocates the memory of a stack
  47.  
  48.  
  49.  
  50. int main(){
  51.     stackptr la=NULL;
  52.     stackptr lb=NULL;
  53.     stackptr lc=NULL;
  54.  
  55.     int n1, n2, length, position;
  56.     stak_t el, search;
  57.  
  58.     ///////////////// LIST 1 /////////
  59.     std::cout<<"how many elements for LIST 1?"<<std::endl;
  60.     std::cin>>n1;
  61.     for(int i=0; i<n1;i++){
  62.         std::cout<<"enter element: "<<i+1<<std::endl;
  63.         std::cin>>el;
  64.         stack_push(el, la);
  65.         stack_push(el, lb);//pass in the current element, return the next
  66.         // lb=consret(el, lb);               //print out lb which is
  67.     }
  68.  
  69.     stack_print(la);
  70.     lc = stack_copy(la);
  71.     std::cout<<"LIST 2 length is: "<< stack_length(lc) <<std::endl;
  72.     stack_print(lc);
  73.     stack_pop(la);
  74.     stack_print(la);
  75.     stack_print(lb);
  76.     return 0;
  77. }
  78.  
  79.  
  80.  
  81. void stack_push(stak_t item, stackptr& s){
  82.     stackptr tmpp= new stacknode;
  83.     tmpp->data=item;
  84.     tmpp->next=s;
  85.     s=tmpp;
  86. }
  87.  
  88. stak_t stack_pop(stackptr& s){
  89.     stackptr tmp = s;
  90.     s=s->next;
  91.     return tmp->data;
  92. }
  93.  
  94. stackptr stack_copy(const stackptr& original){
  95.     stackptr copy_node = new stacknode;
  96.     stackptr current_node = original;
  97.     const stackptr top_of_stack = copy_node;
  98.  
  99.     copy_node->data = current_node->data;
  100.     current_node = current_node->next;
  101.  
  102.     while(current_node != NULL) {
  103.         copy_node->next = new stacknode;
  104.         copy_node = copy_node->next;
  105.         copy_node->data = current_node->data;
  106.         current_node = current_node->next;
  107.         copy_node->next = NULL;
  108.  
  109.     }
  110.     return top_of_stack;
  111. }
  112.  
  113.  
  114. void stack_print(const stackptr& s){
  115.     stackptr tmp=s;
  116.     while(tmp!=NULL){
  117.         std::cout<<tmp->data<<" ";
  118.         tmp=tmp->next;
  119.     }
  120.     std::cout<<std::endl;
  121. }
  122.  
  123. void stack_deallocate(stackptr& s){
  124.     while (s != NULL){
  125.         stackptr nexts = s->next;
  126.         delete s;
  127.         s=nexts;
  128.     }
  129. }
  130.  
  131. int stack_length(const stackptr& s){
  132.     stackptr tmp=s;
  133.     int n=0;
  134.     while (tmp != NULL){
  135.         stackptr nexts = tmp->next;
  136.         tmp=nexts;
  137.         n++;
  138.     }
  139.     return n;
  140. }
  141.  
  142. bool stack_is_empty(const stackptr& s){
  143.     if( s == NULL){
  144.         return 1;
  145.     }
  146.     else{
  147.         return 0;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement