Question

In: Computer Science

Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...

Please use C++ and linked list to solve this problem


Linked list 1 -> 3 -> 4 -> 5-> 6 ->7

replaceNode( 5 , 6) // Replace 5 with 6

    result 1 -> 3 -> 4 -> 6 -> 6 ->7

Base code

#include <iostream>

using namespace std;

class Node {
public:
    int data;
    Node *next;

    Node(int da = 0, Node *p = NULL) {
        this->data = da;
        this->next = p;
    }
};

class LinkedList {
private:
    Node *head, *tail;
    int position;
public:
    LinkedList() { head = tail = NULL; };

    ~LinkedList() {
        delete head;
        delete tail;
    };

    void print();
    void Insert(int da = 0);


}


void LinkedList::Insert(int da) {
    if (head == NULL) {
        head = tail = new Node(da);
        head->next = NULL;
        tail->next = NULL;
    } else {
        Node *p = new Node(da);
        tail->next = p;
        tail = p;
        tail->next = NULL;

}

}


int main() {
    cout << "Hello World!" << endl;
    LinkedList l1;
    l1.Insert(1);
    l1.Insert(3);
    l1.Insert(4);
    l1.Insert(5);

    l1.Insert(6);

    l1.Insert(7);


    l1.print();

   l1.replaceNode( 5 , 6)

   l1.print();
    cout << "The End!" << endl;
    return 0;
}

    }

}

Solutions

Expert Solution

#include <iostream>

using namespace std;

class Node {
public:
int data;
Node *next;

Node(int da = 0, Node *p = NULL) {
this->data = da;
this->next = p;
}
};

class LinkedList {
private:
Node *head, *tail;
int position;
public:
LinkedList() { head = tail = NULL; };

~LinkedList() {
delete head;
delete tail;
};

void print();
void Insert(int da = 0);
   void replaceNode(int a, int b);

};

void printList(Node* n)
{
while (n != NULL) {
cout << n->data << " ";
n = n->next;
}
}

void LinkedList::print() {
   printList(head);
}

void LinkedList::Insert(int n)
{
Node *tmp = new Node;
tmp->data = n;
tmp->next = NULL;

if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}

void updateElement(Node* n , int a, int b){
   while (n != NULL) {
if(n->data == a) n->data = b;
n = n->next;
}
}

void LinkedList::replaceNode(int a, int b) {
       updateElement(head, a, b);
}


int main() {
cout << "Hello World!" << endl;
LinkedList l1;
l1.Insert(1);
l1.Insert(3);
l1.Insert(4);
l1.Insert(5);
l1.Insert(6);
l1.Insert(7);
l1.print();

l1.replaceNode( 5 , 6);
   cout<<endl<<endl;
l1.print();
   cout<<endl;
cout << "The End!" << endl;
return 0;
}


Related Solutions

Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
*In C++ Please! This problem uses the concept of linked list What is a C++ structure?...
*In C++ Please! This problem uses the concept of linked list What is a C++ structure? Store product-id, product-name, and price per unit in a C++ structure. Use a C++ class with a member variable of that structure and provide read and write member functions for the product details. Suppose the product names are stored in alphabetical order, Write the C++ insert function to insert a new product ‘tooth brush’ in that linked list. Suppose the product names are stored...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h>...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h> using namespace std; struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(nullptr) {} }; class LinkedList { private: ListNode *head = nullptr; public: void addNode(int x) { ListNode *p = new ListNode(x); if (head == nullptr) head = p; else { ListNode *q = head; while (q->next != nullptr) q = q->next; q->next = p; } } void display() { ListNode...
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;    }...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
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...
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. ·...
Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6,...
Solve the following Double Linked List sort: use int. array = {8, 3, 12, 9, 6, 2} and provide visualization development to the solution.
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT