Commit 849256d4 authored by michael lundquist's avatar michael lundquist
Browse files

Adding the circular linked list.

parent 76f2f0b1
Pipeline #3988 passed with stage
in 1 minute and 20 seconds
package com.ds.fourthassignment;
/**
*
* @author Michael Lundquist
*/
public class CircularLinkedList {
private LNode head;
private LNode tail;
......@@ -9,7 +14,7 @@ public class CircularLinkedList {
public CircularLinkedList(){
head= null;
tail = null;
size =0;
size = 0;
}
public void addToHead(LNode newNode){
......@@ -48,16 +53,18 @@ public class CircularLinkedList {
* @param newNode the node to be added to the middle of the list
*/
public void addToMiddle(LNode newNode) {
int curPos = 0;
int curPos = 1;
LNode curNode = head;
if(this.size > 2){
while(curPos < this.size/2){
if(this.size <= 1 ){
addToHead(newNode);
}else{
while(curPos < (this.size - 1)/2){
curPos ++;
curNode = curNode.getNext();
}
newNode.setNext(curNode.getNext());
curNode.setNext(newNode);
}
newNode.setNext(curNode.getNext());
curNode.setNext(newNode);
}
public String display(){
......
......@@ -6,7 +6,7 @@ public class Test_Circular_LinkedList {
public static void main(String args[]){
CircularLinkedList clist = new CircularLinkedList();
String menu = "1- Add to head\n2-Add to tail\n3- Display values\n4- Exit.";
String menu = "1- Add to head\n2-Add to tail\n3- Add to middle\n4- Display values\n5- Exit.";
int choice;
do{
choice =Integer.parseInt(JOptionPane.showInputDialog(menu));
......@@ -18,12 +18,15 @@ public class Test_Circular_LinkedList {
clist.addToTail(new LNode(Integer.parseInt(JOptionPane.showInputDialog("Enter an integer value")), null));
break;
case 3:
JOptionPane.showMessageDialog(null, clist.display());
clist.addToMiddle(new LNode(Integer.parseInt(JOptionPane.showInputDialog("Enter an integer value")), null));
break;
case 4:
JOptionPane.showMessageDialog(null, clist.display());
break;
case 5:
System.exit(0);
}
}while(choice!=4);
}while(choice!=5);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment