Question

c++ Computational Complexity

Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the list. For example, for the list 12->3->5->777->111, if we add 2 to the tail, then the list becomes 12->3->5->777-111->2. Remove from head: If the list is empty, then do nothing. Otherwise, remove the head node from the list For example, for the list 12-5-777-11, if we remove a node from its head, then it becomes 5777>11 . Find middle node: Find the middle node(s) from the list and returns the corresponding data field(s). If the number of nodes is odd, then the middle node is just the one in the middle of the list. For the list 12→3→5→777-111, there are five nodes, the middle nodes data field contains 5. If the number of nodes is even, then the middle nodes are the two nodes in the middle of the list. For the list 12→3→5→777-111->2, there are six nodes, the middle nodes, data fields contain 5 and 777. If the number of nodes is zero, then return 0.

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

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class LinkedList{
    // Struct inside the class LinkedList
    // This is one node which is not needed by the caller. It is just
    // for internal work.
    struct Node {
        int x;
        Node *next;
        //Node *head;
    };

// public member
public:

    Node *head;
    Node *last;
    // constructor
    LinkedList(){
        head = NULL; // set head to NULL
        last = NULL;
    }

    // This adds a new value at the end of the list
    void addValue(int val){
        Node *n = new Node();   // create new Node
        n->x = val;             // set value
        n->next = NULL;         // make the node point to NULL.
        if (this->head==NULL){
            head = n;
            last = n;
        }
        else{
            last->next = n;
            last = n;
        }

    }

    // returns the first element in the list and deletes the Node.
    // caution, no error-checking here!
    int popValue(){
        Node *n = head;
        int ret = n->x;

        head = head->next;
        delete n;
        return ret;
    }

// private member
/*private:
    Node *head; // this is the private member variable. It is just a pointer to the first Node
};*/
};

int main() {
    LinkedList list;
    string input;
    cout<<"Please provide input of values: ";
    getline(cin, input);
    istringstream iss(input);
    string operation;
    while (iss>>operation){
        if (operation.length()==1){
            list.popValue();
        }
        else{
            int number = stoi(operation.substr(1, operation.length()));
            //int number = atoi(a.c_str());
            list.addValue(number);
        }
    }

    //list.addValue(5);
    //list.addValue(10);
    //list.addValue(20);
    if (list.head==NULL){
        cout<<"empty"<<endl;
    }
    while(list.head!=NULL){
        cout << list.popValue()<<" -> ";
    }

    //cout << list.popValue() << endl;
    //cout << list.popValue() << endl;
    // because there is no error checking in popValue(), the following
    // is undefined behavior. Probably the program will crash, because
    // there are no more values in the list.
    // cout << list.popValue() << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...
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
  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the l...

    Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the list is empty. * * Parameter other points to the first node in a linked list, or is * NULL if the list is empty. * * Extend the linked list pointed to by head so that it contains * copies of the values stored in the linked list pointed to by other. *...

  • Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables sh...

    Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables should be initialized to reflect this state. This function is already fully implemented. 1. List(const List<Type>& other): Copy constructor for the linked list. This should create an entirely new linked list with the same number of Nodes and the Values stored these Nodes in the same order as seen the...

  • 1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head...

    1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head reference refers to the first node contains an Integer with value 4. If Node curr = head; curr= curr.next; are applied to this list, what is the number you can find in the first node? 2) Given a singly linked list contains 6 nodes, which is simply shown as 1->2->3->4->5->6. Assume head refers to the first node (contains 1) on the list. How many...

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