set.iterator().......where am i wrong? - java

Trying to implement a SET interface and to use an Iterator for the union method. Inside the union method it never enters the while loop. It does not add the elements from the parameter "set" that the union method has.
Any ideas? :)
package linkedSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import linkedList.LinearNode;
public class LinkedSet implements SetADT {
private int size;
private LinearNode<T> front;
private boolean allowNullElement = false;
// /////////////////////////////////////////////////////
private class LinkedSetIterator implements Iterator<T> {
private LinearNode<T> currentNode;
public LinkedSetIterator() {
currentNode = front;
}
#Override
public boolean hasNext() {
return currentNode == null;
}
#Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T temp = currentNode.getElement();
currentNode = currentNode.getNext();
return temp;
}
}
///////////////////////////////////////////////////
#Override
public Iterator<T> iterator() {
return new LinkedSetIterator();
}
public LinkedSet(boolean allowNullElement) {
size = 0;
front = new LinearNode<T>();
this.allowNullElement = allowNullElement;
}
public String toString() {
String str = "{";
for (int i = 0; i < size; i++) {
str += getNode(i).getElement();
if (i < size - 1) {
str += (", ");
}
}
return str + "}";
}
private LinearNode<T> getNode(int index) {
LinearNode<T> current = front;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
return current;
}
#Override
public void add(T element) {
if (element == null && !allowNullElement) {
throw new IllegalArgumentException(
"Null element is not allowed to add");
}
front = new LinearNode<T>(element, front);
size++;
}
#Override
public T remove(T element) {
if (!(contains(element))) {
throw new IllegalStateException();
}
T temp = null;
;
for (int i = 0; i < size; i++) {
temp = getNode(i).getElement();
if (temp.equals(element) || temp == element) {
if (i == 0) {
front = front.getNext();
break;
} else {
getNode(i).setElement(front.getElement());
front = front.getNext();
break;
}
}
}
size--;
return temp;
}
#Override
public boolean contains(T element) {
for (int i = 0; i < size; i++) {
if (getNode(i).getElement() != null
&& getNode(i).getElement().equals(element)) {
return true;
}
if (getNode(i).getElement() == null
&& getNode(i).getElement() == element) {
return true;
}
}
return false;
}
#Override
public boolean isEmpty() {
return size == 0;
}
#Override
public int size() {
return size;
}
#Override
public boolean isSubset(SetADT<T> set) {
if (set == null) {
throw new IllegalArgumentException();
}
for (int i = 0; i < size; i++) {
T temp = getNode(i).getElement();
if (!(set.contains(temp))) {
return false;
}
}
return true;
}
#Override
public SetADT<T> intersection(SetADT<T> set) {
if (set == null) {
throw new IllegalArgumentException();
}
LinkedSet<T> temp = new LinkedSet<T>(allowNullElement);
for (int i = 0; i < size; i++) {
if (set.contains(getNode(i).getElement())) {
temp.add(getNode(i).getElement());
}
}
return temp;
}
public SetADT<T> union(SetADT<T> set) {
if (set == null) {
throw new IllegalArgumentException();
}
LinkedSet<T> temp = new LinkedSet<T>(allowNullElement);
LinearNode<T> node = front;
for (int i = 0; i < size; i++) {
T el = node.getElement();
temp.add(el);
node = node.getNext();
}
Iterator<T> iterator = set.iterator();
while (iterator.hasNext()) {
temp.add(iterator.next());
}
return temp;
}
}

The LinkedSetIterator.hasNext() method will return false if the set is nonempty. This could be corrected with inverting the condition:
#Override
public boolean hasNext() {
return currentNode != null;
}

Related

how can I sort the class generic type when read from file with

I have this class and I read from file the data and my program is add the word and I count every word for example:
(WData{word=success, count=1} WData{word=automata, count=1} Data{word=theory, count=2}) How can I sort the output and start with character A to Z I mean the word and. this my code
Class Sa
class SA:
public class SA<T> {
int head;
int size;
T[] nodes;
public SA(int maxsize) {
head = 0;
nodes = (T[]) new Object[maxsize];
size = 0;
}
public int size() {
return size;}
public boolean empty() {
return size == 0;
public void insert(T e) {
nodes[size] = e;
size++;
}
public T find(T s) {
if (size != 0) {
for (int i = 0; i < size; i++) {
if (nodes[i].equals(s)) {
return nodes[i];
}
}
}
return null;
}
public void display() {
for (int i = 0; i < size; i++) {
System.out.println(nodes[i].toString());
}}}
and here class TextAnalyzer:
Public class TextAnalyzer<T> {
private static class WData {
#Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + Objects.hashCode(this.word);
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final WData other = (WData) obj;
if (!Objects.equals(this.word, other.word)) {
return false;
}
return true;
}
String word;
int count;
public WData(String w, int f) {
word = w;
count = f;
}
#Override
public String toString() {
return "WData{" + "word=" + word + ", count=" + count + '}';
}
} SA<WData>list;
public TextAnalyzer() {
list=new SA<WData>(11000);
}
public void processText(String filename) {
long count = 0;
try {
Scanner sc = new Scanner(new File(filename));;
while (sc.hasNext()) {
String line = sc.next();
String st = line.replaceAll("[^a-zA-Z ]", "").toLowerCase();
processWord(st);}
list.display();
} catch (FileNotFoundException ex) {
System.out.println("There are error in processText");
}
}
public void processWord(String word) {
WData w = new WData(word, 1);
WData s = list.find(w);
if (s == null) {
list.insert(w);
} else {
s.count = s.count + 1;
}}}

Queue reverse implementation - code not working

I'm new to programming and trying to implement an algorithm which reverse K elements in a queue using linkedlist and stack but for some reason, I'm not able to execute the algorithm, any help is greatly appreciated.
I found queue is coming as empty eventhough I add values to it & I see added values in console but when I checked queue.isEmpty() is coming as true.
Here is my code
public class QueueReverseK {
public void reverse(QueueADT queue, int k) {
if (queue.isEmpty()) {
System.out.println("in if empty");
return;
}
if (k > queue.size()) {
k = queue.size;
} else if (k < 0) {
k = 0;
}
StackADT tempStack = new StackADT(k);
QueueADT newQueue = new QueueADT();
for (int i = 0; i < k; i++) {
tempStack.push(queue.deQueue().getData());
}
while (!tempStack.isEmpty()) {
newQueue.enQueue(tempStack.pop());
}
while (!queue.isEmpty()) {
newQueue.enQueue(queue.deQueue().getData());
}
queue = newQueue;
}
Queue class
public class Queue {
LinkedList items;
int size;
Node head;
Node tail;
LinkedListADT list = new LinkedListADT();
public Queue() {
items = new LinkedList();
size = 0;
head = null;
tail = null;
}
public int size() {
return size;
}
public boolean isEmpty() {
if (head == null) return true;
else return false;
}
public void enQueue(int i) {
items.addHead(i);
}
public Node deQueue() {
return items.deleteHead();
}
public void printQueue() {
items.printList();
}
}
LinkedList Class
public class LinkedList {
Node head;
Node tail;
LinkedList() {
head = null;
tail = null;
}
public void addHead(int val) {
Node n = new Node(val);
if (head == null) {
head = n;
tail = n;
} else {
Node tempNode = head;
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = n;
}
}
public void addTail(int val) {
Node n = new Node(val);
if (head == null) {
head = n;
tail = n;
} else {
tail.next = n;
tail = n;
}
}
public int deleteTail() {
Node n = tail;
if (head == null) {
return -1;
} else if (head == tail) {
head = null;
tail = null;
} else {
Node cur = head;
while (cur.getNext() != tail)
cur = cur.next;
tail = cur;
tail.next = null;
}
return n.getData();
}
public Node deleteHead() {
Node n = head;
head = head.next;
return n;
}
public int count() {
int size = 0;
Node n = head;
while (n != null) {
n = n.getNext();
size++;
}
return size;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
public void printList() {
if (this.head == null) {
return;
}
// print all nodes
Node tempNode = this.head;
while (tempNode != null) {
System.out.print(tempNode.data + "->");
tempNode = tempNode.next;
}
}
void printMiddle(int n) {
Node slow_ptr = head;
Node fast_ptr = head;
if (head != null) {
while (fast_ptr != null && fast_ptr.next != null) {
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
System.out.print(slow_ptr.data + " ");
for (int i = 1; i <= n && slow_ptr != null; i++) {
slow_ptr = slow_ptr.next;
System.out.print(slow_ptr.data + " ");
}
}
}
}
Main Class
public static void main(String[] args) {
Queue Q = new Queue();
Q.enQueue(1);
Q.enQueue(2);
Q.enQueue(3);
Q.enQueue(4);
QueueReverseK k = new QueueReverseK();
k.reverse(Q, 2)
Q.printQueue();
}
Stack Class
public class Stack {
private int top;
private int items[];
private int max;
public StackADT(int n) {
this.max = n;
top = 0;
items = new int[n];
}
public boolean isEmpty() {
return top == 0;
}
public boolean isFull() {
return top == max;
}
public void push(int item) {
if (isFull()) throw new IllegalArgumentException();
else items[top++] = item;
}
public int pop() {
if (isEmpty()) throw new IllegalArgumentException();
else return items[--top];
}
public int size() {
return top;
}
}
}
Example
input Queue: 12 34 65 76 23 12 36 90
output Queue : 12 34 65 76 90 36 12 23
I'm not entirely positive, but it looks like your queue is acting like a stack. You're enQueue is pushing to the head, not the tail of the queue.

Adding and removing elements to a double ended queue

I have a to create a double ended queue which can add,remove and peek elements from both sides (i.e, front and rear)
I got the methods for the adding,removing and peeking the elements at the 'head' but i am unable to figure out the methods to do the same thing to the 'tail' of the queue
Here is my code so far:-
public class dequeue
{
private Node rear;
private Node front;
private int counter;
class Node
{
private String item;
private Node link;
}
public Node()
{
rear = front = null;
counter = 0;
}
public void hAdd(String o)
{
Node temp = new Node();
temp.item =o;
temp.link =null;
if (rear==null)
front = rear = temp;
else
{
rear.link = temp;
rear = temp;
}
counter++;
}
public boolean isEmpty()
{
if(counter==0)
return true;
return false;
}
public Object hPeek()
{
return rear.item;
}
public Object hRemove()
{
if (isEmpty())
return null;
else
{
Object temp=front.item;
front = front.link;
if (front == null)
rear = null;
counter--;
return temp;
}
}
public int size()
{
return counter;
}
public void tAdd(String o)
{
Node temp = new Node();
temp.item =o;
temp.link =null;
if (front==null)
front=rear=temp;
else
{
front.link=temp;
front=temp;
}
counter++;
}
public Object tPeek()
{
return front.item;
}
public Object tRemove()
{
if (isEmpty())
return null;
else
{
Object temp=rear.item;
rear=rear.link;
if (rear==null)
front=null;
counter--;
return temp;
}
}
public String toString()
{
StringBuilder result=new StringBuilder();
Node curr=front;
while(curr!=null)
{
result.append(curr.item+" \n");
curr = curr.link;
}
return result.toString();
}
}
public class Dequeue {
int arr[];
int front;
int rear;
int size;
public Dequeue(int size){
this.front = -1;
this.rear=0;
this.size = size;
arr = new int[size];
}
public Dequeue(){
this.front = -1;
this.rear=0;
this.size = 10;
arr= new int[size];
}
boolean isQueueEmpty(){
return (front == -1);
}
boolean isQueueFull(){
return front == ((rear+1)%size);
}
int getFront(){
if(isQueueEmpty()){
return -1;
}
return arr[front];
}
int getRear(){
if(isQueueEmpty() || rear < 0){
return -1;
}
return arr[rear];
}
void insertFront(int data){
if(isQueueFull()){
System.out.println(-1);
return;
}
if(front == -1){
front = 0;
rear=0;
}
else if(front == 0){
front = size - 1 ;
}else{
front = front-1;
}
arr[front] = data;
}
void insertRear(int data){
if(isQueueFull()){
System.out.println(-1);
return;
}
if(front == -1){
front =0;
rear=0;
}
else{
rear =((rear+1)%size);
}
arr[rear] = data ;
}
void deleteFront(){
if(isQueueEmpty()){
System.out.println("-1");
return;
}
if(front == rear){
front = -1;
rear = -1;
}else{
front = ((front+1)%size);
}
}
void deleteRear(){
if(isQueueEmpty()){
System.out.println("-1");
return;
}
if(front == rear){
front =-1;
rear=-1;
}
else if(rear == 0)
rear = size-1;
else
rear = rear-1;
} }

I call a method and it gives me an error

My source code:
import java.util.*;
public class Arvore {
public Arvore() {
root = null;
}
public void inserir(String x) {
root = insert(x, root);
}
public void remove(String x) {
root = remove(x, root);
}
private No remove(String x, No t) {
if (t == null) {
return t;
}
int comp = x.compareTo(t.str);
if (comp < 0) {
t.left = remove(x, t.left);
} else if (comp > 0) {
t.right = remove(x, t.right);
} else if (t.left != null && t.right != null) {
t.str = findMin(t.right).str;
t.right = remove(t.str, t.right);
} else {
t = (t.left != null) ? t.left : t.right;
}
return balance(t);
}
public String findMin() {
return findMin(root).str;
}
public String findMax() {
return findMax(root).str;
}
public boolean contains(String x) {
return contains(x, root);
}
public void makeEmpty() {
root = null;
}
public boolean isEmpty() {
return root == null;
}
private static final int maxBal = 1;
private No balance(No t) {
if (t == null) {
return t;
}
if (height(t.left) - height(t.right) > maxBal) {
if (height(t.left.left) >= height(t.left.right)) {
t = rotateWithLeftChild(t);
} else {
t = doubleWithLeftChild(t);
}
} else if (height(t.right) - height(t.left) > maxBal) {
if (height(t.right.right) >= height(t.right.left)) {
t = rotateWithRightChild(t);
} else {
t = doubleWithRightChild(t);
}
}
t.height = Math.max(height(t.left), height(t.right)) + 1;
return t;
}
public void checkBalance() {
checkBalance(root);
}
private int checkBalance(No t) {
if (t == null) {
return -1;
}
if (t != null) {
int hl = checkBalance(t.left);
int hr = checkBalance(t.right);
if (Math.abs(height(t.left) - height(t.right)) > 1
|| height(t.left) != hl || height(t.right) != hr) {
System.out.println("OOPS!!");
}
}
return height(t);
}
private No insert(String x, No t) {
if (t == null) {
return new No(x, null, null);
}
int comp = x.compareTo(t.str);
if (comp < 0) {
t.left = insert(x, t.left);
} else if (comp > 0) {
t.right = insert(x, t.right);
} else
;
t.occ+=1;
return balance(t);
}
private No findMin(No t) {
if (t == null) {
return t;
}
while (t.left != null) {
t = t.left;
}
return t;
}
private No findMax(No t) {
if (t == null) {
return t;
}
while (t.right != null) {
t = t.right;
}
return t;
}
private boolean contains(String x, No t) {
while (t != null) {
int comp = x.compareTo(t.str);
if (comp < 0) {
t = t.left;
} else if (comp > 0) {
t = t.right;
} else {
return true;
}
}
return false;
}
private void printTree(No t) {
if (t != null) {
printTree(t.left);
System.out.println(t.str + ": " + t.occ);
printTree(t.right);
}
}
private int height(No t) {
return t == null ? -1 : t.height;
}
private No rotateWithLeftChild(No k2) {
No k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
k1.height = Math.max(height(k1.left), k2.height) + 1;
return k1;
}
private No rotateWithRightChild(No k1) {
No k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
k2.height = Math.max(height(k2.right), k1.height) + 1;
return k2;
}
private No doubleWithLeftChild(No k3) {
k3.left = rotateWithRightChild(k3.left);
return rotateWithLeftChild(k3);
}
private No doubleWithRightChild(No k1) {
k1.right = rotateWithLeftChild(k1.right);
return rotateWithRightChild(k1);
}
private class No {
No(String tStr) {
this(tStr, null, null);
}
No(String tStr, No lt, No rt) {
str = tStr;
left = lt;
right = rt;
height = 0;
occ = 1;
}
String str;
No left;
No right;
int height;
int occ = 0;
}
private No root;
public static void main(String[] args) {
Arvore t = new Arvore();
System.out.println("AVL TREE TEST\n");
String msg;
String[] inputs;
Scanner sc = new Scanner(System.in);
ArrayList palavras = new ArrayList();
int i = 0;
while (true) {
msg = sc.nextLine();
if (msg.equals("")) {
break;
}
inputs = msg.split(" ");
i = 0;
while (i < inputs.length) {
palavras.add(inputs[i]);
}
}
i = 0;
while (i < palavras.size()) {
if (palavras.get(i).equals("REMOVE")) {
palavras.remove(palavras.get(i));
palavras.remove(palavras.get(i + 1));
i += 2;
} else {
t.insert(palavras.get(i));
}
i++;
}
t.printTree();
}
}
I can't figure out why i have an error when i call insert and printTree, on the main function.
And i have a warning by adding a string to my arraylist, when i do palavras.add(inputs[i]).
Your loop:
i = 0;
while (i < inputs.length) {
palavras.add(inputs[i]);
}
is not incrementing i, so you will eventually run out of space for the array as you try to insert an infinite number of references to inputs[0].
The warning is because you are using a raw ArrayList. Try declaring palavras as:
ArrayList<String> palavras = new ArrayList<>(); // new ArrayList<String>(); if pre Java 7
Your other loop:
i = 0;
while (i < palavras.size()) {
if (palavras.get(i).equals("REMOVE")) {
palavras.remove(palavras.get(i));
palavras.remove(palavras.get(i + 1));
i += 2;
} else {
t.insert(palavras.get(i));
}
i++;
}
will generate an IndexOutOfBoundsException if the first branch of the if is taken when i == palavras.size() - 1 when it tries to remove the second element. Even if you are guaranteed that "REMOVE" is always followed by another element, you need to either reverse the order of the removals:
palavras.remove(palavras.get(i + 1));
palavras.remove(palavras.get(i));
or remove the element at i twice:
palavras.remove(palavras.get(i));
palavras.remove(palavras.get(i));
(because when you call remove() it moves all succeeding elements down one position in the array).
You are calling t.printTree() but, there is no such method defined in class Arvore; the method you did define takes a tree as an argument. Same thing with insert(): the actual method that's available takes a String and a No as arguments.

Array-based implementation of a sorted linked list

I need to write a java linked list which needs to be array based and sorted. So the array contains nodes which have 2 fields: the data, and the index of the next element in the list. The last element of the list needs to have an index of -1.
Can someone help how to add an element to such list. this is the code I wrote so far but does not seems to be right because I can add elements but the indexes are not right.
package listpackage;
import java.io.IOException;
public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;
public ArrayLL(){
list = new ANode[MAX_CAP];
list[list.length-1] = new ANode(null, -1);
for(int i = 0; i < list.length-1; i++){
list[i] = new ANode(null, i+1);
}
size = 0;
}
public void addElem(String s) throws IOException{
if(this.getSize() == 0){
ANode a = new ANode(s, -1);
list[0] = a;
}else if(size == MAX_CAP + 1){
throw new IOException("List is full");
}else{
int index = 0;
for(int i=0; i < list.length; i++){
if(list[i].getData() == null){
index = i;
break;
}
}
ANode b = new ANode(s);
list[index] = b;
if(this.getSize()==1){
if (list[index].getData().compareTo(list[0].getData()) < 0){
list[index].setLink(0);
list[0].setLink(-1);
}else{
list[index].setLink(-1);
list[0].setLink(index);
}
}else{
int i = 0;
while(list[i].getData() != null){
if(list[index].getData().compareTo(list[i].getData()) < 0){
list[index].setLink(i);
if(i>0)
list[i-1].setLink(index);
}else{
i++;
}
}
}
}
size++;
}
public ANode[] getList(){
return list;
}
public int getSize(){
return size;
}
}
class ANode{
private String data;
private int link;
public ANode(String d){
data = d;
link = -1;
}
public ANode(String d, int l){
data = d;
link = l;
}
public String getData(){
return data;
}
public int getLink(){
return link;
}
public void setLink(int l){
link = l;
}
}
It was fun to solve this tricky program... :-)... i enjoyed it...here is the working solution...I tested using various scenarios...
In order to make sure I do not change much of your code...I did not optimize the code..there are many places where code can be more simpler and readable...
import java.io.IOException;
public class ArrayLL {
public static void main(String[] args) throws IOException {
ArrayLL myList = new ArrayLL();
myList.addElem("c");
myList.addElem("b");
myList.addElem("a");
myList.addElem("d");
int i = myList.startOfListIndex;
while(myList.list[i].getLink()!=-1)
{
System.out.println(myList.list[i].getData());
i = myList.list[i].getLink();
}
System.out.println(myList.list[i].getData());
}
private int MAX_CAP = 100;
private ANode[] list;
private int size;
private int startOfListIndex = 0;
public ArrayLL() {
list = new ANode[MAX_CAP];
for (int i = 0; i < list.length; i++) {
list[i] = new ANode(null);
}
size = 0;
}
public void addElem(String s) throws IOException {
if (this.getSize() == 0) {
ANode a = new ANode(s, -1);
list[0] = a;
} else if (size == MAX_CAP + 1) {
throw new IOException("List is full");
} else {
int index = 0;
for (int i = 0; i < list.length; i++) {
if (list[i].getData() == null) {
index = i;
break;
}
}
ANode b = new ANode(s);
list[index] = b;
if (this.getSize() == 1) {
if (list[index].getData().compareTo(list[0].getData()) < 0) {
list[index].setLink(0);
list[0].setLink(-1);
startOfListIndex = index;
} else {
list[index].setLink(-1);
list[0].setLink(index);
}
} else {
int i = startOfListIndex;
int prevIndex = -1;
while (i!=-1 && list[i].getData() != null) {
if (list[index].getData().compareTo(list[i].getData()) < 0) {
list[index].setLink(i);
if(prevIndex!=-1)
list[prevIndex].setLink(index);
else
startOfListIndex = index;
break;
} else {
prevIndex = i;
i=list[i].getLink();
}
}
if(i==-1)
{
list[prevIndex].setLink(index);
}
}
}
size++;
}
public ANode[] getList() {
return list;
}
public int getSize() {
return size;
}
}
class ANode {
private String data;
private int link;
public ANode(String d) {
data = d;
link = -1;
}
public ANode(String d, int l) {
data = d;
link = l;
}
public String getData() {
return data;
}
public int getLink() {
return link;
}
public void setLink(int l) {
link = l;
}
}
This is the full solution i believe as it works for all the test i have done, but there might be something i haven't thought of. The solution includes the removeElem() method too.
package listpackage;
import java.io.IOException;
public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;
private int startOfListIndex = 0;
public ArrayLL() {
list = new ANode[MAX_CAP];
list[list.length-1] = new ANode(null, -1);
for(int i = 0; i < list.length-1; i++){
list[i] = new ANode(null, i+1);
}
size = 0;
}
public void addElem(String s) throws IOException {
if (this.getSize() == 0) {
ANode a = new ANode(s, -1);
list[0] = a;
}else if (size == MAX_CAP + 1) {
throw new IOException("List is full");
}else{
int index = 0;
for (int i = 0; i < list.length; i++) {
if (list[i].getData() == null) {
index = i;
break;
}
}
ANode b = new ANode(s);
list[index] = b;
if (this.getSize() == 1) {
if (list[index].getData().compareTo(list[0].getData()) < 0) {
list[index].setLink(0);
list[0].setLink(-1);
startOfListIndex = index;
} else {
list[index].setLink(-1);
list[0].setLink(index);
}
} else {
int i = startOfListIndex;
int prevIndex = -1;
while (i!=-1 && list[i].getData() != null) {
if (list[index].getData().compareTo(list[i].getData()) < 0) {
list[index].setLink(i);
if(prevIndex!=-1)
list[prevIndex].setLink(index);
else
startOfListIndex = index;
break;
}else{
prevIndex = i;
i=list[i].getLink();
}
}
if(i==-1)
{
list[prevIndex].setLink(index);
}
}
}
size++;
}
public void removeElem(String s) throws IOException {
if (this.getSize() == 0) {
throw new IOException("List is empty");
}else{
int firstEmpty = 0;
for(int i = 0; i< list.length; i++){
if(list[i].getData() == null){
firstEmpty = i;
break;
}
}
int elemindex = 0;
int prev = -1;
int next=-1;
for(int i = 0; i < list.length; i++){
if(list[i].getData() != null && list[i].getData().compareTo(s) == 0){
elemindex = i;
next = list[i].getLink();
for(int j = 0; j < list.length; j++){
if(list[j].getLink() == elemindex){
prev = j;
break;
}
}
list[elemindex].setDataNull();
list[elemindex].setLink(firstEmpty);
if(next != -1)
list[prev].setLink(next);
else
list[prev].setLink(-1);
size--;
}else{
elemindex++;
}
}
}
}
public ANode[] getList() {
return list;
}
public int getSize() {
return size;
}
}
class ANode {
private String data;
private int link;
public ANode(String d) {
data = d;
link = -1;
}
public ANode(String d, int l) {
data = d;
link = l;
}
public String getData() {
return data;
}
public void setDataNull(){
this.data = null;
}
public int getLink() {
return link;
}
public void setLink(int l) {
link = l;
}
}

Categories

Resources