Cpp Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// implementing the dynamic List ADT using Linked List
class Node
{
private:
int data;
Node* nextNodePtr;
public:
Node() {}
void setData(int d)
{
data = d;
}
int getData()
{
return data;
}
void setNextNodePtr(Node* nodePtr)
{
nextNodePtr = nodePtr;
}
Node* getNextNodePtr()
{
return nextNodePtr;
}
};
class List
{
private:
Node *headPtr;
public:
List()
{
headPtr = new Node();
headPtr->setNextNodePtr(0);
}
Node* getHeadPtr()
{
return headPtr;
}
bool isEmpty()
{
if (headPtr->getNextNodePtr() == 0)
return true;
return false;
}
void insert(int data)
{
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
while (currentNodePtr != 0)
{
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
}
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(0);
prevNodePtr->setNextNodePtr(newNodePtr);
}
void insertAtIndex(int insertIndex, int data)
{
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0)
{
if (index == insertIndex)
break;
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(currentNodePtr);
prevNodePtr->setNextNodePtr(newNodePtr);
}
int read(int readIndex)
{
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0)
{
if (index == readIndex)
return currentNodePtr->getData();
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
return -1; // an invalid value indicating
// index is out of range
}
void modifyElement(int modifyIndex, int data)
{
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0)
{
if (index == modifyIndex)
{
currentNodePtr->setData(data);
return;
}
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
}
void deleteElement(int deleteIndex)
{
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
Node* nextNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0)
{
if (index == deleteIndex)
{
nextNodePtr = currentNodePtr->getNextNodePtr();
break;
}
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
prevNodePtr->setNextNodePtr(nextNodePtr);
}
void IterativePrint()
{
Node* currentNodePtr = headPtr->getNextNodePtr();
while (currentNodePtr != 0)
{
cout << currentNodePtr->getData() << " ";
currentNodePtr = currentNodePtr->getNextNodePtr();
}
cout << endl;
}
bool isUnique()
{
Node* mainNode = headPtr; //The main list
while (mainNode != 0)
{
Node* subNode = mainNode->getNextNodePtr(); //sublist from next
node of main list
while (subNode != 0)
{
if(subNode->getData() == mainNode->getData()){ //checking if
sublist contains the main list values
return false; //if so then break
}
subNode = subNode->getNextNodePtr();
}
mainNode = mainNode->getNextNodePtr();
}
return true; //return true if all sub list and main list value do
not have repeating values
}
};
int main()
{
srand(time(NULL));
int listSize;
cout << "Enter the number of elements you want to insert:
";
cin >> listSize;
int maxValue;
cout << "Enter the max. value for an element in the list:
";
cin >> maxValue;
int numTrials = 1000;
int numUniqueTrials = 0;
for (int trials = 1; trials <= numTrials; trials++)
{
List integerList; // Create an empty list
for (int i = 0; i < listSize; i++)
{
int value = 1 + rand() % maxValue;
integerList.insertAtIndex(i, value);
}
if (integerList.isUnique()) // You need to implement the isUnique(
) function in the List class
{
numUniqueTrials++;
}
}// end trials loop
cout << "Probability for a list to be unique is: "
<< (((double) numUniqueTrials)/numTrials) <<
endl;
system("pause");
return 0;
}
Sample Output:
Enter the number of elements you want to insert: 10
Enter the max. value for an element in the list: 20
Probability for a list to be unique is: 0.063
You are given the code for a singly linked list-based implementation of the List ADT. You...
PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Node{
private:
int data;
Node* nextNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return
data;
}
void setNextNodePtr(Node*
nodePtr){
nextNodePtr = nodePtr;
}
...
In this assignment, you will use a Hashtable to determine the
common elements in all the lists. If an element appears more than
once in one or more lists, the algorithm should capture the
instances the element is present in all the lists.
You are given a code that implements a Hashtable as an array of
linked lists. You are also given a main function that will create
an array of Lists using the input variable values that you enter....
A binary tree is a complete binary tree if all the internal nodes (including the root node) have exactly two child nodes and all the leaf nodes are at level 'h' corresponding to the height of the tree. Consider the code for the binary tree given to you for this question. Add code in the blank space provided for the member function checkCompleteBinaryTree( ) in the BinaryTree class. This member function should check whether the binary tree input by the...
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...
The source code I have is what I'm trying to fix for the assignment at the bottom. Source code: //Group 1 - Jodie Butterworth, Brandon Kidd, Matt Heckler //11-21-19 //CSC 201 // Exercise to practice the basic manipulations of a linked list // Note: Uses nullptr, so need to make sure your compiler is set to use C++11 // In Code::Blocks this is under Settings==>Compiler #include <iostream> using namespace std; // Structure to contain data for a node (Could be...
above in the image 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
C++ language
Make sure its corect and runs without errors and I promise to
give you a thumbs up.
#include iostream» using namespace std; class Node int data;...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
You are required to write the following functions using this class:
class Doubly_linked_list // Use a class Doubly_linked_list to represent an object
{
public:
// constructor initialize the nextPtr
Doubly_linked_list()
{
prevPtr = 0; // point to null at the beginning
nextPtr = 0; // point to null at the beginning
}
// get a number
int GetNum()
{
return number;
}
// set a number
void SetNum(int num)
{
number = num;
}
// get the prev pointer
Doubly_linked_list ...