Theory
Roster Circle बंद करता है
Final BookBridge build: reading-room roster। 4 members 2 desks share करते हैं; Zoya की turn के बाद roster desk वापस Riya को handover करता है। Forever।
पिछले lesson की IssueList यह express नहीं कर सकती: इसकी tail null कहती है, full stop। आज tail कहती है "start पर वापस जाओ", और हर operation को एक नई reality respect करनी चाहिए: lean करने के लिए कोई null नहीं है। Create, traverse, insert, delete: same 4 verbs, बिना end वाली एक world के लिए rewired।
Theory
किसी भी Code से पहले एक Clever Choice
Class को कौन सी node याद रखनी चाहिए? Instinct कहता है head। Professionals इसके बजाय last node रखते हैं, और यह 1 की कीमत में 2 ends खरीदता है:
- head हमेशा instantly reachable है:
last.next - tail खुद
lastहै
तो FRONT और END दोनों में insert करने के लिए बिल्कुल walk नहीं चाहिए: दोनों वहीं होते हैं जहाँ last पहले से खड़ा है। एक singly list जो यह offer करती वह हर append के लिए अपनी पूरी chain walk करती।
Practical
Roster, एक Circle की तरह Wired
class CNode {
String name;
CNode next;
CNode(String n) { name = n; }
}
class Roster {
CNode last; // last.next IS the head
void addLast(String name) { // join at the end of the circle
CNode n = new CNode(name);
if (last == null) {
last = n;
n.next = n; // points at ITSELF: smallest circle
} else {
n.next = last.next; // 1: n grabs the head
last.next = n; // 2: old last points at n
last = n; // 3: n becomes the new last
}
}
void traverse() { // print each member exactly once
if (last == null) return;
CNode cur = last.next; // start at the head
do {
System.out.println(cur.name);
cur = cur.next;
} while (cur != last.next); // stop when the head reappears
}
void deleteFirst() { // the head leaves the circle
if (last == null) return;
if (last.next == last) { last = null; return; } // only node
last.next = last.next.next; // bypass the head
}
}
Theory
Trace: Circle बढ़ती है
Empty पर addLast("Riya"): last = Riya, और Riya.next = Riya। एक node, खुद को point करते हुए: एक complete, legal circle।
addLast("Aman"): step 1, Aman.next = last.next (= Riya, head); step 2, Riya.next = Aman; step 3, last = Aman।
Circle: Riya → Aman → Riya...
addLast("Zoya"): Zoya.next = Riya; Aman.next = Zoya; last = Zoya।
Circle: Riya → Aman → Zoya → वापस Riya तक। traverse() last.next (Riya) पर start होता है और Riya, Aman, Zoya print करता है, फिर Riya से फिर से मिलता है और रुक जाता है।
Quiz
Roster class में, empty roster पर addLast("Riya") के तुरंत बाद VERY FIRST node का next किसकी तरफ़ point करता है?
- null, जब तक एक दूसरा member join न हो
- Node खुद: Riya.next == Riya
- Roster object का last reference
- यह unassigned छोड़ा गया है, तो इसे पढ़ना एक compile error है
Show the answer
Node खुद: Riya.next == Riya
एक circular list को हमेशा अपना circle बंद करना चाहिए, एक member के साथ भी: n.next = n सबसे छोटा possible loop बनाता है, तो traverse और हर दूसरा operation "1 का circle" special-case किए बिना काम करता है। Option A इसे एक singly list बना देगा और do-while को instantly तोड़ देगा (यह null में walk कर जाएगा)। Option C Roster की एक field को chain की एक node से confuse करता है। Option D Unit 2 भूल जाता है: fields default से null होते हैं, और यहाँ constructor anyway explicitly next assign करता है।
Think first
Exit Trace कीजिए
Roster: Riya → Aman → Zoya (last = Zoya)। deleteFirst() call कीजिए, फिर traverse()। Tap करने से पहले हर reference walk कीजिए: कौन print होता है, और अभी Zoya.next क्या hold करता है?
Show the answer
deleteFirst last.next = last.next.next run करता है: Zoya.next Riya था, Riya.next Aman है, तो Zoya.next = Aman। Riya के पास कोई incoming reference नहीं बचा: garbage-collected। Circle Aman → Zoya → Aman है, और traverse Aman, Zoya print करता है। Notice कीजिए क्या कभी नहीं हुआ: कोई walking नहीं, कोई null checks नहीं, एक reassignment। और अगर roster में सिर्फ़ Riya होती, guard last.next == last fire होता और last = null set करता: 1 का circle nothing में empty हो जाता।
Watch out
Circle-Specific Traps
while (cur != null): हमेशा के लिए loop करता है; एक healthy circle में कोई next कभी null नहीं होता। Stop test positional है: वापस start पर।
do-while के बजाय Plain while: while (cur != last.next) पहले print से पहले false है (cur HEAD HAI), तो कुछ print नहीं होता। Body-first loop यहाँ optional नहीं है।
खुद last को Delete करना: bypass करना काफ़ी नहीं है; last को इससे PEHLE वाली node पर move करना चाहिए, और एक singly circle में वह node ढूँढने का मतलब है लगभग पूरा घूमना।
Theory
BookBridge, Complete
Step back कीजिए और देखिए semester ने क्या बनाया: एक console app जो variables से एक Book class में grow हुआ, polymorphism वाली एक Person family, guarded Strings और एक custom exception, एक background reminder thread, shelved packages, और अब इसकी queues run करने वाली 2 hand-built lists। यही BCA403 का पूरा syllabus है एक program में living। Circular pattern खुद वहाँ लौटता है जहाँ भी turns repeat होते हैं: BCA203 का round-robin scheduler, repeat पर playlists, और multiplayer game loops।
Summary
Key takeaways
- LAST node का एक reference रखिए: last.next head है, तो बिना walking के दोनों ends reachable हैं।
- एक circle की पहली node खुद को point करती है: n.next = n।
- End में Insert: n.next = last.next, last.next = n, last = n (front insert: same 2 rewires, last move करना skip कीजिए)।
- last.next से do-while से traverse कीजिए, head फिर से appear होने पर रुकते हुए; while-null कभी terminate नहीं होता।
- deleteFirst: last.next = last.next.next; एक single-node circle last = null में empty हो जाती है।
- खुद last को delete करने का मतलब है इसके predecessor तक walk करना और last को वहाँ move करना।
- Memory hook: एक का circle खुद को point करता है; null का wait करने वाला loop हमेशा wait करता है।