Question

In: Computer Science

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 = 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(2;
    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

Solution: -

// program to replace two nodes of a linked list
#include <iostream>
using namespace std;
class Node { // Linked list node
public:
int data;
class Node* next;
Node(int val, Node* next) // Constructor
: data(val), next(next)
{
}
void printList() // Used to print linked list
{
Node* node = this;
while (node != NULL) {
cout <<"->";
cout << node->data;
node = node->next;
}
cout << endl;
}
};
void Insert(Node** head_ref, int new_data) // Insert a node beginning of Linked list
{
(*head_ref) = new Node(new_data, *head_ref);
}
void swap(Node*& a, Node*& b) // Used to swap a,b
{
Node* temp = a;
a = b;
b = temp;
}
void replaceNode(Node** head_ref, int x, int y) // replaceNode is used to repace two nodes
{
if (x == y) // x and y are same do nothing
return;
Node **a = NULL, **b = NULL;
while (*head_ref) { // Search for x and y and save pointer in a,b
if ((*head_ref)->data == x) {
a = head_ref;
}
else if ((*head_ref)->data == y) {
b = head_ref;
}
head_ref = &((*head_ref)->next);
}
if (a && b) { // Replace current pointer and next pointer of a,b
swap(*a, *b);
swap(((*a)->next), ((*b)->next));
}
}

int main()
{
Node* node= NULL;
Insert(&node, 7); // Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7
Insert(&node, 6);
Insert(&node, 5);
Insert(&node, 4);
Insert(&node, 3);
Insert(&node, 2);
Insert(&node, 1);
cout << "Before calling replaceNode() Linked list is : ";
node->printList();
replaceNode(&node, 5, 6); // Replace 5 with 6
cout << "After   calling replaceNode() Linked list is : ";
node->printList();
}
Output: For the above program,

=========================================================
Comment if any query.


Related Solutions

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;     } };...
*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;    }...
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
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)...
Use linked list and write in C++ Food ordering system 1. Place Order 2. View the...
Use linked list and write in C++ Food ordering system 1. Place Order 2. View the food details 3. Modify food details 4. Delete food details Order should let the customer to enter the food code, flavor, weight(kg), unit price, qty, customerID, name, address and contact number. It will also have an order id automatically assigned with a unique ID when new order is added. When view the food details, it should also calculate the unit price * qty as...
Use linked list and write in C++ Food ordering system 1. Place Order 2. View the...
Use linked list and write in C++ Food ordering system 1. Place Order 2. View the food details 3. Modify food details 4. Delete food details Order should let the customer to enter the food code, flavor, weight(kg), unit price, qty, customerID, name, address and contact number. It will also have an order id automatically assigned with a unique ID when new order is added. When view the food details, it should also calculate the unit price * qty as...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT