1. Modify the following code as needed to run in your environment. As well as the values for M and N given in the code, test all combinations of values of M = 2, 3, 5, 10 and N = 103, 104, 105, 106.
2. Explain the “node” class and the “main” function through comments in the code file and a written summary that includes your displayed test results.
3. Create the following functions for processing nodes: remove(), insert(), next(), annd item(). Test them independently of the processing done in the code below. Be sure to document your code, summarize the way you implemented them, and how you tested the functions.
4. Replace any operations in the code below with equivalent functions that you implemented in problem 3 above, where appropriate. Document your replacements and give proof in your summary that you get the same results as with the original code.
#include <iostream>
using namespace std;
class node
{
public:
int item; node* next;
node(int x, node* t)
{ item = x; next = t; }
};
typedef node *link;
int main()
{ int i, N = 9, M = 5;
link t = new node(1, 0); t->next = t;
link x = t;
for (i = 2; i <= N; i++)
x = (x->next = new node(i,
t));
while (x != x->next)
{
for (i = 1; i < M;
i++) x = x->next;
x->next =
x->next->next;
}
cout << x->item << endl;
}
Program plan for the modified code:
The modified code is as follows:
Program screenshots:
Code to copy:
//Include the required header files.
#include <iostream>
#include <cmath>
//Use the standard namespace.
using namespace std;
//Define the node class.
class node
{
public:
//Define the data members of the class.
int item;
node* next;
//Define the constructor.
node(int x, node* t)
{
//Set the values.
item = x;
next = t;
}
};
//Define the function to insert a node after the
// given node.
node* insert(node* x, int value, node* nextnode = nullptr)
{
//Create a new node with the given value.
x->next = new node(value, nextnode);
//Return the modified node.
return x;
}
//Define a function to remove the next node.
node* remove(node* x)
{
//Move the pointer to skip the next node.
x->next = x->next->next;
//Return the modified node.
return x;
}
//Define the function to set the next pointer.
node* setnext(node *x, node* nextnode)
{
//Set the next pointer to the given node.
x->next = nextnode;
//Return the modified node.
return x;
}
//Define the function to return the next node.
node* next(node* x)
{
//Return the pointer to the next node.
return x->next;
}
//Define the function to return the item.
int item(node* x)
{
//Return the item value.
return x->item;
}
//Define the main() function.
int main()
{
//Declare the required variables.
int i, N, M;
//Set the value of N and M.
M = 10;
N = pow(10,6);
//Create a linked list to store the nodes.
node* t = new node(1, 0);
//Set the next pointer.
setnext(t,t);
//Create a reference to the linked list.
node* x = t;
//Start the loop.
for (i = 2; i <= N; i++)
{
//Insert the value i in the linked list
// pointing to the node t.
insert(x,i,t);
//Move the pointer to the next node.
x = next(x);
}
//Start the while loop.
while (x != next(x))
{
//Start the for loop.
for (i = 1; i < M; i++)
{
//Move the pointer to the next node.
x = next(x);
}
//Remove the next node of the current node.
x = remove(x);
}
//Display the item value.
cout << item(x) << endl;
//Return 0 and exit the program.
return 0;
}
Testing
Some of the test cases/sample outputs of both the original code and the modified code as shown below:
Case 1: M = 2, N = 103
Sample output (Original code):
Sample output (Modified code):
Case 2: M = 3, N = 104
Sample output (Original code):
Sample output (Modified code):
Case 3: M = 5, N = 105
Sample output (Original code):
Sample output (Modified code):
Case 4: M = 10, N = 106
Sample output (Original code):
Sample output (Modified code):
Conclusion/Summary:
As tested above, the output generated by the original code and the modified code for a given input test case is the same.
Therefore, the modified code works the same as the original code.
1. Modify the following code as needed to run in your environment. As well as the...
starter code
To write a program using the starter code which is
TestLinkedList to see if the LinkedList program has bugs. It will
produce ether a pass or fail.More information is in the first two
pictures.
LinkedList.java
/**
* @author someone
*
* Implements a double-linked list with four errors
*/
public class LinkedList<E>
{
// The first and last nodes in the list
private Node<E> head, tail;
// Number of items stored in the list
private int size;
//...
A) Fix any errors to get the following program to run in your environment. B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...
Modify the following code in order for it to perform the following operations: *This code is meant to simulate Floyd's cycle finding algorithm* A) Start with two pointers at the head of the list. We'll call the first one tortoise and the second one hare B) Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic C) Advance tortoise by one...
Below are the node_struct, link, list_struct and list as defined in class and in hw3: typedef struct node_struct * link; struct node_struct { int item; link next; }; typedef struct list_struct * list; struct list_struct { link first; int length; }; a) (15 pts) Write a function that swaps the first and last node in a list. If there are not enough nodes, it does not do anything to the list. You have to update the links yourself. DO NOT...
Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...
Please Modify TestPart2 to test the correctness and
efficiency of FasterDefaultList. Thanks
import java.util.List;
import java.util.AbstractList;
import java.util.Map;
import java.util.HashMap;
public class DumbDefaultList<T> extends AbstractList<T> {
Map<Integer,T> map;
public DumbDefaultList() {
map = new HashMap<Integer,T>();
}
public int size() {
return Integer.MAX_VALUE;
}
public T get(int i) {
return map.get(i);
}
public T set(int i, T x) {
return map.put(i, x);
}
public void add(int i, T x) {
Map<Integer, T> map2 = new HashMap<Integer,T>();
for (Integer k : map.keySet())...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 { template<class item> class node { public: typedef item value_type; ...
a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...