Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Circular Linked Lists
New Posts  All Forums:Forum Nav:

Circular Linked Lists

post #1 of 5
Thread Starter 
Hey guys
circular linked lists are confusing me. i need to delete the first item so i need to find the last item and make a pointer to it that wasnt hard i have that part figured out. Also i know that when i find the last item i need to rotate to make first the next item then point the last item to the first? then size --. But that is not working properly it looks to be working but it's only rotating and will not actually break a link. this is what im workign with any insight will be greatly appreciated

/**
* COMP 2140 Unit 3 walkthrough
*/
public class CircularLinkedList{
public static void main(String[] args){
CircularList list = new CircularList();

for(int i=0; i<10; i++){
list.add(i);//((int)(Math.random()*10));
}

System.out.println(list);
for(int i=0; i<10; i++){
list.remove();
System.out.println(list);
}


}// end main
}

class CircularList {
private Node first;
private int size;

public CircularList() {
first = null;
size = 0;
}

public void add(int value) {
if (first == null) {
first = new Node(value, null);
first.setNext(first);
} else {
Node newNode = new Node(value, first.getNext());
first.setNext(newNode);
}
size++;
// System.out.println("added " + value);
}

public int get() {
return first.getData();
}

public void rotate() {
if (first != null)
first = first.getNext();
}

public int size() {
return size;
}

public int count(int value) {
int count = 0;
for (int i = 0; i < size(); i++) {
if (get() == value)
count++;
rotate();
}
return count;
}

public void remove(){
Node temp = first.getNext();
Node previous = first;

while(temp.getData() != first.getData()){
temp=temp.getNext();
previous = previous.getNext();// pointer to last entry in list
}
rotate();
previous = previous.getNext();
//size--;
}

public String toString(){
String result = "";
for(int i=0; i<size; i++){
result += get() +" ";
rotate();
}
return result;
}
}

class Node {
private int data;
private Node next;

public Node(int data, Node next) {
this.data = data;
this.next = next;
}

public int getData() {
return data;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}
}
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #2 of 5
if you need to delete the first node...

- have the last node point to the second node lastnode.setNext(list.getNext());

- have list point to the second node. list = list.getNext();


obviously it would be up to you of determining what the last node is...which should be easy.
Edited by surfbumb - 5/27/11 at 1:08pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #3 of 5
Thread Starter 
i have to ask what is the benefits of a circular list over a linear list i dont see it
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #4 of 5
Thread Starter 
omg thank you so much i was doing that at one point but in the wrong order!!!!! man you guys here are so much help thank you so much
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #5 of 5
Quote:
Originally Posted by Sin Cities Sin View Post
omg thank you so much i was doing that at one point but in the wrong order!!!!! man you guys here are so much help thank you so much
I'm guessing you got it to work...if you need any other help let me know.
Edited by surfbumb - 5/27/11 at 1:28pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Circular Linked Lists