Question

class Node public: double v Node next;/ Points to the nert Node Node *prev/ Points to the previous Node class LList Node head

***Using C++ and also please label "a" and "c" accordingly. Thank you!!****

a) Write the member function void deleteNode() that will delete the first node from the linked list.

b) Modify the LList to create a data member Node *cur that points to the current node. Write the following member functions:

• void forward() that moves the cur pointer to the next node.

• void backward() that moves the cur pointer to the previous node.

• void delete() that removes the node that is pointed to by the cur pointer. ( Think of how you will set the cur pointer after the delete and state your strategy.)

• double getValue() that returns the value of the node that is pointed to by the cur pointer.

c) Write the member function Node *findFirstNodeByValue(double x) that will return the reference of the first node that has the value x. You start your search from the pointer cur.

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

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>


using namespace std;

//Node Class
class Node{
public:
    double v;
    Node *next;
    Node *prev;
};

//LinkedList class
class LList{
    //Head pointer
    Node *head=nullptr;
public:
    //Current pointer
    Node *cur = nullptr;
    //This adds a new node at the end of linked list
    void addNode(double val){
        Node *nnode = new Node();
        nnode->v = val;
        if(head==nullptr){
            head = nnode;
            cur = nnode;
            return;
        }
        Node *temp = head;
        //Move to the end and add new node
        while(temp->next!=nullptr){
            temp = temp->next;
        }
        temp->next = nnode;
        nnode->prev = temp;
    }

    //This deletes head node
    void deleteNode(){
        if(head==nullptr){
            return;
        }
        Node *temp = head;
        head = head->next;
        head->prev = nullptr;
        cur = head;
        free(temp);
    }

    void forward(){
        cur = cur->next;
    }

    void backward(){
        cur = cur->prev;
    }

    //This deletes current node
    void deleteCurrent(){
        if(cur==head){
            deleteNode();
            return;
        }
        Node *del = cur;
        Node *prev = cur->prev;
        Node *next = cur->next;
        prev->next = next;
        next->prev = prev;
        free(del);
    }

    double getValue(){
        return cur->v;
    }

    //This finds node by value
    Node * findFirstNodeByValue(double x){
        Node *temp = cur;
        while(temp!=nullptr){
            if(temp->v==x){
                return temp;
            }
            temp = temp->next;
        }
        return nullptr;
    }

    //This prints the linked list
    void print(){
        Node *temp = head;
        while(temp!=nullptr){
            cout<<temp->v<<"->";
            temp = temp->next;
        }
        cout<<"NULL"<<endl;
    }
};

int main(){
    LList a;
    a.addNode(3);
    a.addNode(4);
    a.addNode(5);
    a.addNode(6);
    a.addNode(7);
    a.addNode(8);
    a.print();
    a.deleteNode();
    a.print();
    cout<<"Current "<<a.cur->v<<endl;
    a.forward();
    a.forward();
    a.forward();
    a.backward();
    cout<<"Current "<<a.cur->v<<endl;
    a.deleteCurrent();
    a.print();

    return 0;
}

OUTPUT :

D\CodingiCODE BLOCKSSampleCpplbin\DebuglSampleCpp.exe 3->4->5->6->7->8->NULL 4->5->6->7-8->NULL Current 4 Current 6 ->5->7-

Add a comment
Know the answer?
Add Answer to:
***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member...
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
  • C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and In...

    C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...

  • CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a...

    CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a ItemType and pointer // (to a NodeType) field struct NodeType {    ItemType value;    NodeType * next; }; bool Delete (NodeType * & firstPtr, ItemType value) {    NodeType * prev, * cur; cur = Search (firstPtr, value, prev); if (cur==NULL) return false; else { // remove cur node from the linked list if (prev!=NULL) { //not the first one prev->next = cur->next;...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • 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...

  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Modify listlink.java program (non generic) by adding the following methods: public void insertsorted(x); // Inert x...

    Modify listlink.java program (non generic) by adding the following methods: public void insertsorted(x); // Inert x in a sorted list. public void deletex(x); //Search for x in the sorted list, if found, delete it from the sorted list. Assume you have a data file p2.txt with the following contents: 8 4 15 23 12 36 5 36 42 3 5 14 4 and your java program is in xxxxx.java file, where xxxxx is the first 5 characters of your last...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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