Question

It is C++ problems, please explain your each line of code as well.

Task 1: Implement/ Hard Code a Doubly Linked Lists and make it a Stack. Do not use ::list:: class from the STD library for th

0 0
Add a comment Improve this question Transcribed image text
Answer #1

task-1:

#include <iostream>
#include <fstream>
#include <string>

using std::cout;

class Node
{
public:
    Node *prev;
    Node *next;
    int shares;
    float pps;
    Node(int vshares, float vpps)
    {
        shares = vshares;
        pps = vpps;
        prev = next = nullptr;
    }
};

class dlist
{
public:
    Node *head;
    Node *tail;

    dlist()
    {
        head = tail = nullptr;
    }
    ~dlist()
    {
        destroy();
    }

    void push_back(int shares, float pps) 
    {
        Node *node = new Node(shares, pps);
        if (head == NULL) 
        {
            head = tail = node;
        }
        else 
        {
            tail->next = node;
            node->prev = tail;
            tail = node;
        }
    }

    void destroy() 
    {
        Node *walk = head;
        while (walk) 
        {
            Node *node = walk;
            walk = walk->next;
            delete node;
        }
        head = tail = nullptr;
    }
};

class stack
{
public:
    int maxsize;
    int count;
    dlist list;
    stack(int size)
    {
        count = 0;
        maxsize = size;
    }

    void push(int num, float price)
    {
        if (count < maxsize)
        {
            list.push_back(num, price);
            count++;
        }
    }

    void pop()
    {
        Node *tail = list.tail;
        if (!tail)
        {
            //already empty
            return;
        }

        if (tail == list.head)
        {
            //only one element in the list
            delete tail;
            list.head = list.tail = nullptr;
            count--;
        }
        else
        {
            Node *temp = list.tail->prev;
            delete list.tail;
            list.tail = temp;
            list.tail->next = nullptr;
            count--;
        }
    }

    void display()
    {
        Node  *walk = list.tail;
        while (walk)
        {
            cout << "("<< walk->pps << ") ";
            walk = walk->prev;
        }
        cout << "\n";
    }
};

int main()
{
    stack s(10);
     string str;
     cout<<"enter the string";
    getline(cin,str);
    for(int i=0;i<s.length();i++)
   {
    s.push(i,str[i]);
   }
    s.display();
}

task-2:

  1. #include <iostream>
  2. using namespace std;
  3. struct Node {
  4. int data;
  5. Node* next;
  6. };
  7. class Queue {
  8. struct Node* head,* tail;
  9. public:
  10. Queue() {
  11. head = tail = NULL;
  12. }
  13. void enQueue();
  14. void deQueue();
  15. void displayQueue();
  16. void menu();
  17. int elem;
  18. int choice;
  19. };
  20. void Queue::enQueue(char c) {
  21. Node* pointer = new Node;
  22. pointer -> data = c;
  23. pointer -> next = NULL;
  24. if(head == NULL) {
  25. head = pointer;
  26. }
  27. else
  28. tail -> next = pointer;
  29. tail = pointer;
  30. }
  31. void Queue::deQueue() {
  32. if(head == NULL){
  33. cout << "Queue is empty!" << endl;
  34. }
  35. Node* temp = head;
  36. head = head -> next;
  37. delete temp;
  38. }
  39. void Queue::displayQueue() {
  40. Node* pointer1 = head;
  41. while (pointer1 != NULL) {
  42. cout << pointer1 -> data;
  43. cout << pointer1 -> data ;
  44. pointer1 = pointer1 -> next;
  45. }
  46. cout << "End" << endl;
  47. }
  48. int main () {
  49. Queue q1;
  50. string str;
    cout<<"enter the string"; getline(cin,str);
    for(int i=0;i<s.length();i++)
    {
    q1.enque(s[i]);
    }
  51. q1.display();
  52. }
Add a comment
Know the answer?
Add Answer to:
It is C++ problems, please explain your each line of code as well. Task 1: Implement/...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Please do in C++) Implement a Queue using a vector or the STD ::queue:: class Note...

    (Please do in C++) Implement a Queue using a vector or the STD ::queue:: class Note the difference in what it takes to implement using a char[] array vs a vector or the STD ::queue:: class The user will input a string and the program will return the string with each letter in the string duplicated. Displaying correct Queue implementation. Utilize the conventional methods as needed. Please input String: happy hhaappyy

  • (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

    (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is...

    In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is to complete the implementation of the classes specified as below. Y 1 Your Task You are given a class “Item” that contains one integer value, and two pointers. You are going to build a doubly linked list class DLinkedList. I describe the tasks below. Task 1: Implement the constructors (default and copy) of DLinkedList. You need to make sure that the copy constructor makes...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

    C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT