Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are many Data structures used in computer science to store sequence of ele

ID: 3835254 • Letter: T

Question

There are many Data structures used in computer science to store sequence of elements. Two such data structures that store and retrieve elements in some sequence are Stacks and Queues.

A Stack is a list of elements in which any new element is inserted at the top of the list, which is stack terms is called push. An element is retrieved also from the top of the list, in stack, this is called pop. Other important function is first which returns the value of the element which is at the top; however it does not remove it from the list.

Similar to Stack, a Queue also stores list of elements, an element is inserted at the end of the queue, this function is called enqueue, the element is removed from the top of the list, this in queuing terms is called dequeue. Similar to stack, it also has function first with similar functionality.

You role is to provide implementation for these data structures, with all the functionality mentioned above.

( To design, document, build and test a small scale c++ .

Explanation / Answer

#include<iostream.h>
#include<string>
struct node {
     int info;
     struct node *link;
};   
class Stack {
     private:
         node *top;
     public:
         Stack(){
             top = NULL;
         }
         void push(int n){
             node *temp = new (struct node);
             temp->link = top;
             temp->info = n;
             top = temp;
         }
         int pop(){
             int n;
             if (top!= NULL){
                n = top->info;
                top = top->link;
             }
             else{
                n = -1; // -1 means stack is empty
             }
             return n;
         }
         int first(){
             if (top != NULL)
                return top->info;
             else
                return -1;
         }
};

class Queue {
     private:
         node *head;
     public:
         Queue(){
             head = NULL;
         }
         void enqueue(int n){
             node *temp;
             if (head == NULL){
                 head = new (struct node);
                 head->link = NULL;
                 head->info = n;
                 return;
             }
             temp = head;
             while (temp->link != NULL)
                  temp = temp->link;
             temp->link = new (struct node);
             temp = temp->link;
             temp->info = n;
             temp->link = NULL;
         }
         int dequeue(){
             int n;
             if (head != NULL){
                n = head->info;
                head = head->link;
             }
             else{
                n = -1;// -1 means Queue is empty
             }
             return n;
         }
         int first(){
             if (head != NULL)
                return head->info;
             else
                return -1;
         }
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote