Advertisement
Guest User

Queues

a guest
Feb 28th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. /*
  2.     First name  XXX
  3.     Last name   XXX
  4.     Student ID  XXXXXX (e.g. 01234567)
  5.     Username    XXX (e.g. aaa123)
  6. */
  7.  
  8. //IMPORTANT: before submitting your source code, please make sure to comment your main function (needed for automated testing)
  9.  
  10. #include <iostream>
  11.  
  12. // do not use using namespace std
  13.  
  14. // do not alter the typedef
  15. typedef char queue_t;
  16. // easier to keep the type more abstract
  17. // (many operations would work also if
  18. // a type other than char was used to define queue_t)
  19.  
  20. // do not alter the struct declaration
  21. struct queuenode {
  22.     queue_t data;
  23.     queuenode *next;
  24. };
  25.  
  26. // do not alter the typedef
  27. typedef queuenode* queueptr;
  28. // more abstract, avoids cumbersome notations
  29. // such as having to write queuenode*& below
  30.  
  31. /////////////////////////////////////////
  32. // Functions declarations (prototypes) //
  33. /////////////////////////////////////////
  34.  
  35. // do not alter these functions declarations
  36.  
  37. void queue_push(queue_t item, queueptr& s);
  38. // Adds an item to the back of a queue (we pass a pointer to the front of the queue)
  39.  
  40. ///////////////////////////////////////////////////
  41. // CHANGES IN THE SPECS
  42. //  queue_t queue_pop(queueptr& s); // DEPRECATED. See queue_pop() and queue_front().
  43. //  // Returns the first item (i.e. the front) of a queue (and removes it from the queue)
  44.  
  45. void queue_pop(queueptr& s);
  46. // Removes the first item (i.e. at the front) of a queue
  47.  
  48. queue_t & queue_front(const queueptr& s);
  49. // Returns the first item (i.e. at the front) of a queue (without removing it)
  50.  
  51. // END OF THE CHANGES.
  52. ///////////////////////////////////////////////////
  53.  
  54. queueptr queue_copy(const queueptr& original);
  55. // Returns a copy of an existing queue
  56. // (with appropriate memory allocation so that the original
  57. // and the copy don't point to the same memory)
  58.  
  59. void queue_print(const queueptr& s);
  60. // Prints the items of a queue
  61.  
  62. int queue_length(const queueptr& s);
  63. // Returns the length (can also be called the height) of a queue
  64.  
  65. bool queue_is_empty(const queueptr& s);
  66. // Returns true if a queue is empty. Returns false otherwise.
  67.  
  68. void queue_deallocate(queueptr& s);
  69. // Deallocates the memory of a queue
  70.  
  71. // you can define and add use additional functions if you might need to
  72.  
  73. ///////////////////
  74. // main function //
  75. ///////////////////
  76.  
  77. //IMPORTANT (Reminder): before submitting your source code, please make sure to comment your main function (needed for automated testing)
  78. /*int main() {
  79.  
  80.     // YOUR CODE HERE
  81.  
  82.  
  83.     return 0;
  84. }*/
  85.  
  86.  
  87.  
  88.  
  89. /////////////////////////////////////////////
  90. // Functions definitions (implementations) //
  91. /////////////////////////////////////////////
  92.  
  93. // YOUR CODE HERE
  94.  
  95. void queue_push(queue_t item, queueptr& s){
  96.     queueptr temp = new queuenode;
  97.     temp->data = item;
  98.     temp->next = NULL;
  99.  
  100.     if (queue_is_empty(s)){
  101.         s = temp;
  102.     } else{
  103.         queueptr current_node = s;
  104.         while(current_node->next!=NULL){
  105.             current_node = current_node->next;
  106.         }
  107.         current_node->next = temp;
  108.     }
  109.  
  110. }
  111.  
  112. void queue_pop(queueptr& s){
  113.     s = s->next;
  114. }
  115.  
  116. queue_t & queue_front(const queueptr& s){
  117.     return s->data;
  118. }
  119.  
  120. queueptr queue_copy(const queueptr& original){
  121.     queueptr current_node = original;
  122.     queueptr copied_queue = NULL;
  123.     while(current_node!=NULL){
  124.         queue_push(current_node->data, copied_queue);
  125.         current_node=current_node->next;
  126.     }
  127.     return copied_queue;
  128. }
  129.  
  130. void queue_print(const queueptr& s){
  131.     queueptr temp=s;
  132.     while(temp!=NULL){
  133.         std::cout<<temp->data<<" ";
  134.         temp=temp->next;
  135.     }
  136.     std::cout<<std::endl;
  137. }
  138.  
  139. int queue_length(const queueptr& s){
  140.     queueptr temp=s;
  141.     int n=0;
  142.     while (temp != NULL){
  143.         temp = temp->next;
  144.         n++;
  145.     }
  146.     return n;
  147. }
  148.  
  149. bool queue_is_empty(const queueptr& s){
  150.     return (s==NULL);
  151. }
  152.  
  153. void queue_deallocate(queueptr& s){
  154.     while (s != NULL){
  155.         queueptr next_node = s->next;
  156.         delete s;
  157.         s=next_node;
  158.     }
  159. }
  160.  
  161. /*int main(){
  162.     queueptr Queue1 = NULL;
  163.  
  164.     int n1;
  165.     char el;
  166.  
  167.     std::cout<<"how many elements for Queue 1?"<<std::endl;
  168.     std::cin>>n1;
  169.     for(int i=0; i<n1;i++) {
  170.         std::cout << "enter element: " << i + 1 << std::endl;
  171.         std::cin >> el;
  172.         queue_push(el, Queue1);
  173.     }
  174.     std::cout<<"This is your list: ";
  175.     queue_print(Queue1);
  176.     std::cout<<"This is the first item: "<<queue_front(Queue1)<<std::endl;
  177.     std::cout<<"Is queue empty? Result: "<<queue_is_empty(Queue1)<<std::endl;
  178.     std::cout<<"This is the length: "<<queue_length(Queue1)<<std::endl;
  179.     queueptr Queue2 = queue_copy(Queue1);
  180.     std::cout<<"Copying the queue Removing something: ";
  181.     queue_pop(Queue1);
  182.     queue_print(Queue1);
  183.     std::cout<<"Copied queue remains untouched: ";
  184.     queue_print(Queue2);
  185.     std::cout<<"Even when original is deallocated: ";
  186.     queue_deallocate(Queue1);
  187.     queue_print(Queue2);
  188.     std::cout<<"Is deallocated queue empty now? Result: "<<queue_is_empty(Queue1)<<std::endl;
  189.  
  190.  
  191.  
  192.     return 0;
  193. }
  194. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement