डेटा स्ट्रक्चर क्लास, सिर, पूंछ और वर्तमान नोड्स के साथ एक सिंगल लिंक्ड-लिस्ट को लागू करना। एक विधि के साथ परेशानी होने के कारण, सही दिशा में एक झुकाव का उपयोग कर सकते हैं।जावा लिंक्ड लिस्ट - विधि
काम से, विधि लिखें:
(आइटम) जोड़ें: मद (स्ट्रिंग) सूची में वर्तमान नोड के बाद कहते हैं और वर्तमान सूचक नए नोड का उल्लेख करने के निर्धारित करता है।
मेरे प्रयास:
मेरे ऐड विधि केवल काम करने के लिए जब मैं सूची के बीच में आइटम डाले रहा हूँ, पर नहीं दोनों छोर लगता है। यदि मैं इसे कुछ आइटम जोड़ने के लिए उपयोग करता हूं और फिर सूची मुद्रित करता हूं, तो केवल पहले जो मैंने जोड़ा है, वह सूची में होगा, जबकि मेरे प्रीपेन्ड और एपेंड विधियों ने अभी ठीक परीक्षण किया है।
क्या मेरे कोड के साथ कोई चमकदार समस्या है? मुझे लगता है कि मुझे कुछ याद आ रही है।
सभी:
public class LinkedList {
Node head = null; /* Head of the list */
Node tail = null; /* Tail of the list */
Node curr = null; /* Current node in the list */
public void prepend(String item) {
if (head == null) {
head = tail = new Node(item, null);
curr = head;
} else {
head = new Node(item, head);
curr = head;
}
}
public void append(String item) {
if (head == null) {
head = tail = new Node(item, null);
curr = tail;
} else {
tail.next = new Node(item, null);
tail = tail.next;
curr = tail;
}
}
public void add(String item) {
if (curr != null) {
Node newNode = new Node(item, curr.next);
curr.next = newNode;
curr = newNode;
} else {
head = tail = new Node(item, null);
curr = head;
}
}
public void delete() {
if (curr.next == null) {
Node temp = head;
while (temp.next != curr) {
System.out.println(temp.item);
temp = temp.next;
}
temp.next = null;
curr = head;
}
}
public void find(String item) {
Node temp = new Node(curr.item, curr.next);
if (item.equals(temp.item))
curr = temp;
else {
temp = temp.next;
while (temp.next != null && temp != curr) {
if (item.equals(temp.item))
curr = temp;
}
}
}
public String get() {
if (curr != null)
return curr.item;
else
return "";
}
public boolean next() {
if (curr != tail) {
curr = curr.next;
return true;
} else
return false;
}
public void start() {
curr = head;
}
public void end() {
curr = tail;
}
public boolean empty() {
if (head == null)
return true;
else
return false;
}
}
Node
वर्ग:
class Node {
Node next;
String item;
Node(String item, Node next) {
this.next = next;
this.item = item;
}
}
शेष कोड के बारे में क्या? – fge
वह हिस्सा अच्छा दिखता है, इसलिए हमें आसपास के कोड दिखाएं, त्रुटि वहां होनी चाहिए। –
अतिरिक्त कोड जोड़ा गया – dysania