CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA:
//LinkedString.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
//declare a node datastruct
struct Node
{
char c;
Node *next;
};
class LinkedString
{
Node *head;
public:
LinkedString();
LinkedString(char[]);
LinkedString(string);
char charAt(int) const;
string concat(const LinkedString &obj)
const;
bool isEmpty() const;
int length() const;
LinkedString substring(int, int) const;
//added helper function to add to linekd list
private:
void add(char c);
};
==============================================
//LinkedString.cpp
#include"LinkedString.h"
LinkedString::LinkedString()
{
head = NULL;
}
LinkedString::LinkedString(char s[])
{
Node *newNode, *cur = head;
for (int i = 0; s[i] != '\0'; i++)
{
newNode = new Node;
newNode->c = s[i];
newNode->next = NULL;
if (head == NULL)
{
//assign newNode
to head
head =
newNode;
}
else
{
//traverse
through list and add new char at end
cur =
head;
while
(cur->next != NULL)
cur = cur->next;
cur->next =
newNode;
}
}
}
LinkedString::LinkedString(string s)
{
Node *newNode, *cur = head;
int n = s.length();
for (int i = 0; i<n; i++)
{
newNode = new Node;
newNode->c = s[i];
newNode->next = NULL;
if (head == NULL)
{
//assign newNode
to head
head =
newNode;
}
else
{
//traverse
through list and add new char at end
cur =
head;
while
(cur->next != NULL)
cur = cur->next;
cur->next =
newNode;
}
}
}
char LinkedString::charAt(int i) const
{
Node *cur = head;
int count = 0;
if (i < 0 && i >= length())
{
cout << "Index
error!!"<<endl;
return 0;
}
while (cur != NULL)
{
if (count == i)
break;
++count;
cur = cur->next;
}
if (count >= 0)
return cur->c;
else
return count;
}
string LinkedString::concat(const LinkedString &obj)
const
{
string constr = "";
for (int i = 0; i < length(); i++)
{
constr += charAt(i);
}
for (int i = 0; i < obj.length(); i++)
{
constr += obj.charAt(i);
}
return constr;
}
bool LinkedString::isEmpty() const
{
if (head == NULL)
return true;
return false;
}
int LinkedString::length() const
{
Node *cur = head;
int count = 0;
while (cur != NULL)
{
++count;
cur = cur->next;
}
return count;
}
void LinkedString::add(char c)
{
Node *cur = head, *newNode;
newNode = new Node;
newNode->c = c;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
cur = head;
while (cur->next != NULL)
cur =
cur->next;
cur->next = newNode;
}
}
LinkedString LinkedString::substring(int j, int k) const
{
LinkedString ret;
Node *cur = head;
int count = 0;
while (cur != NULL)
{
if (j == count)
{
break;
}
++count;
cur = cur->next;
}
while (count++ <=k)
{
if (cur == NULL)
break;
ret.add(cur->c);
cur = cur->next;
}
return ret;
}
=============================
//Main.cpp
#include<iostream>
#include"LinkedString.h"
using namespace std;
int main()
{
char str[] = "Hello World!!";
//declare LinkedString object
LinkedString s1("Test"),s2(str);
//display linekd LinkedString
cout << "Linked string s1: ";
for (int i = 0; i < s1.length(); i++)
{
cout << s1.charAt(i);
}
cout << endl;
cout<<"Concatinated string :
"<<s1.concat(s2)<<endl;
cout << "Substring: ";
LinkedString substr = s2.substring(6, 11);
//print substr
for (int i = 0; i < substr.length(); i++)
{
cout <<
substr.charAt(i);
}
}
/*Output
Linked string s1: Test
Concatinated string : TestHello World!!
Substring: World!
*/
Dear Student,
Converting above program in JAVA.
/********************************** IN JAVA ********************************/
class Node
{
char c;
Node next;
}
//class LinkedString
class LinkedString
{
Node head;
public LinkedString(){
head = null;
}
public LinkedString(char s[]){
Node newNode, cur = head;
for (char i: s)
{
newNode = new Node();
newNode.c = i;
newNode.next = null;
if (head == null)
{
//assign newNode to head
head = newNode;
}
else
{
//traverse through list and add new char at end
cur = head;
while (cur.next != null)
cur = cur.next;
cur.next = newNode;
}
}
}
public LinkedString(String s){
Node newNode, cur = head;
int n = s.length();
char ch[] = s.toCharArray();
for (char i:ch)
{
newNode = new Node();
newNode.c = i;
newNode.next = null;
if (head == null)
{
//assign newNode to head
head = newNode;
}
else
{
//traverse through list and add new char at end
cur = head;
while (cur.next != null)
cur = cur.next;
cur.next = newNode;
}
}
}
public char charAt(int i){
Node cur = head;
int count = 0;
if (i < 0 && i >= length())
{
System.out.println("Index error!!");
return 0;
}
while (cur != null)
{
if (count == i)
break;
++count;
cur = cur.next;
}
return cur.c;
}
public String concat(LinkedString obj){
String constr = "";
for (int i = 0; i < length(); i++)
{
constr += charAt(i);
}
for (int i = 0; i < obj.length(); i++)
{
constr += obj.charAt(i);
}
return constr;
}
public boolean isEmpty(){
if (head == null)
return true;
return false;
}
//claculating length of list
public int length(){
Node cur = head;
int count = 0;
while (cur != null)
{
++count;
cur = cur.next;
}
return count;
}
public LinkedString substring(int j, int k){
LinkedString ret = new LinkedString();
Node cur = head;
int count = 0;
while (cur != null)
{
if (j == count)
{
break;
}
++count;
cur = cur.next;
}
while (count++ <= k)
{
if (cur == null)
break;
ret.add(cur.c);
cur = cur.next;
}
return ret;
}
//added helper function to add to linekd list
private void add(char c){
Node cur = head, newNode;
newNode = new Node();
newNode.c = c;
newNode.next = null;
if (head == null)
{
head = newNode;
}
else
{
cur = head;
while (cur.next != null)
cur = cur.next;
cur.next = newNode;
}
}
}
public class Main
{
public static void main(String[] args) {
char str[] = "Hello World!!".toCharArray();
//declare LinkedString object
LinkedString s1= new LinkedString("Test");
LinkedString s2 = new LinkedString(str);
//display linekd LinkedString
System.out.print("Linked string s1: ");
for (int i = 0; i < s1.length(); i++)
{
System.out.print(s1.charAt(i));
}
System.out.println();
System.out.println("Concatinated string : " + s1.concat(s2));
System.out.print("Substring: ");
LinkedString substr = s2.substring(6, 11);
//print substr
for (int i = 0; i < substr.length(); i++)
{
System.out.print(substr.charAt(i));
}
}
}
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...
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,...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
#include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node { Contact item; NodePtr next; friend class ContactList; }; class ContactList { public: ContactList(char* clfile) ; ~ContactList(); void display (ostream & output) const; int insert (Contact record_to_insert); int insert (ContactList contact_list); int remove (Contact record_to_delete); int size () const; int save () const; void find_by_lname (ostream & output, string lname) const; void...
#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...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string 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 display() { while (tmp...
#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;
}
...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use...
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
const int index = 5;
int head = 0;
string s[index];
int flag = 1;
int choice;
while (flag)
{
cout << "\n1. Add an Item in
the Chores List.";
cout << "\n2. How many Chores
are in the list.";
cout << "\n3. Show the list
of Chores.";
cout << "\n4. Delete an...
Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...