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

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

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

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

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

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

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

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