Question

In: Electrical Engineering

Solve this Write a C++ class that implements a stack using a linked list. The type...

Solve

this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. · int isEmpty(); // returns 1 if the stack is empty, 0 otherwise. 5 pts. · int numElements(); // returns the number of elements in the stack. 5 pts. · void print Elements(); // print out the current stack to the console. 5 pts. · A main function; // execute each method of your stack (except the destructor) at least once without asking input from the users.

Solutions

Expert Solution

Hello,
       Please find the answer attached below. If the answer has helped you please give a thumbs up rating. Thank you and have a nice day!

//   Creating a NODE Structure

struct node

{

    int data;

    struct node *next;

};

// Creating a class STACK

class stack

{

    struct node *top;

    public:

    stack() // constructor

    {

        top=NULL;

    }

    ~stack() // destructor

{

     cout << " Destructor invoked";

}

    void push(double value); // to insert an element

    double pop();  // to pop an element

    void print Elements(); // to show the stack

    int isEmpty(); //returns 1 if the stack is empty, 0 otherwise

    int numElements(); // returns the number of elements in the stack

};

// PUSH Operation

void stack::push(double value)

{

    struct node *ptr;

    ptr=new node;

    ptr->data=value;

    ptr->next=NULL;

    if(top!=NULL)

        ptr->next=top;

    top=ptr;

    cout<<"\nNew item inserted";

}

// POP Operation

double stack::pop()

{

    struct node *temp;

    if(top==NULL)

    {

        cout<<"\nempty stack";

    }

    temp=top;

    top=top->next;

    return temp;

}

int stack::isEmpty()

{

    struct node *temp;

    if(top==NULL)

          return 1;

     else

        return 0;

}

int stack::numElements()

{

    struct node *ptr1=top;

      int p;

    while(ptr1!=NULL)

    {

        p++;

        ptr1=ptr1->next;

    }

    return p;

}

// Show all elemnts in stack

void stack::printElements()

{

    struct node *ptr1=top;

    cout<<"\nThe stack is\n";

    while(ptr1!=NULL)

    {

        cout<<ptr1->data<<" ->";

        ptr1=ptr1->next;

    }

    cout<<"NULL\n";

}

// Main function

int main()

{

    stack sll;

        cout<<"\n-----------------------------------------------------------";

        cout<<"\n\t\tSTACK USING LINKED LIST\n\n";

                sll.push(23);

                sll.pop();

                sll.isEmpty();

                sll.numElements();

                s.printElements();

                return 0;

    }


Related Solutions

The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private...
Please solve this problem in java. (simple linked list) public class MyLinkedList implements MiniList{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */    private Node head;    private Node tail;       public class Node { // declare member variables (data and next)    Integer data;    Node next; // finish these constructors    public Node(int data, Node next) {               this.data=data;        this.next=next;    }...
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties: a) perimeter returns 4 × side. b) area returns side × side. c) diagonal returns the square root of the expression (2 × side2). The perimeter, area and diagonal should not have corresponding data attributes; rather, they should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT