In: Computer Science
using java language "Data Structures"
I have to write program which stores string entered by user into
cursor array implementation
here is the code
public static void main(String[] args) {
CursorArray sorted =new CursorArray();//the strings must added
here how can i store them
String []inputs = new
String[50];
for (int i=0; i< 50; i++)
{
System.out.println("Enter the words you want to sort and use exit
to stop");
if(input.next().equals("exit"))
break;
inputs[i] = input.nextLine();
}
}
you can put the code of reading in a method
and this is part of cursor class
public class CursorArray {
Node[] cursorArray = new Node[10];
public int initialization() {
for (int i = 0; i < cursorArray.length - 1; i++) {
cursorArray[i] = new Node(null, i + 1);
}
cursorArray[cursorArray.length - 1]
= new Node(null, 0);
return 0;
}
public int malloc() {
int p = cursorArray[0].next;
cursorArray[0].next =
cursorArray[p].next;
return p;
}
public void free(int p) {
cursorArray[p] = new Node(null,
cursorArray[0].next);
cursorArray[0].next = p;
}
}
public class Node {
String data;
int next; //index
public Node(String data, int next) {
this.data = data;
this.next = next;
}
}
as per the requirements of question, the question had been answered.
only taking user input and storing the strings in CursorArray by initialization method
Raw_code:
Driver.java:
import java.util.Scanner; // import Scanner class for taking user input
public class Driver{
public static void main(String[] args){
// creating Scanner object on standard input stream
Scanner input = new Scanner(System.in);
CursorArray sorted = new CursorArray();//the strings must added
here how can i store them
String []inputs = new String[50];
for (int i=0; i< 50; i++) {
System.out.println("Enter the words you want to sort and use exit
to stop");
if(input.next().equals("exit"))
break;
inputs[i] = input.nextLine();
}
// calling initialization method of CursorArray object with
inputs(string array) as argument
sorted.initialization(inputs);
}
}
// if you want to added the strings through constructor of CursorArray then just replace method name intialization to CursorArray and truncate the return type int