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

JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
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)
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
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;    }...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT