-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_LinkedList.cs
More file actions
130 lines (113 loc) · 6.57 KB
/
Copy path03_LinkedList.cs
File metadata and controls
130 lines (113 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
namespace DataStructures
{
/// <summary>
/// Demonstrates the LinkedList<T> doubly-linked list in C#.
/// Each node holds a value and references to both the next and previous nodes,
/// enabling O(1) insertions/removals at known positions.
///
/// Common use cases: route planners, playlist navigation, undo/redo buffers,
/// insertion-heavy workloads where List<T> shifts would be expensive.
/// </summary>
public class LinkedListExamples
{
// ─── Entry Point ───────────────────────────────────────────────
public static void Main()
{
Console.WriteLine("═══════════════════════════════════════════════");
Console.WriteLine(" C# LinkedList<T> Examples ");
Console.WriteLine("═══════════════════════════════════════════════\n");
// ── 1. Building a Travel Route ──────────────────────────────
Console.WriteLine("── Building a Travel Route ────────────────────");
var route = new LinkedList<string>();
// Add cities to the route
route.AddLast("London");
route.AddLast("Paris");
route.AddLast("Rome");
Console.WriteLine(" Route: " + string.Join(" → ", route));
// Add a city at the beginning (starting point)
route.AddFirst("Edinburgh");
Console.WriteLine(" After AddFirst(\"Edinburgh\"): " + string.Join(" → ", route));
// Add a city after a specific node
LinkedListNode<string> parisNode = route.Find("Paris");
if (parisNode != null)
{
route.AddAfter(parisNode, "Barcelona");
Console.WriteLine(" After AddAfter(Paris, Barcelona): " + string.Join(" → ", route));
}
// Add a city before a specific node
LinkedListNode<string> romeNode = route.Find("Rome");
if (romeNode != null)
{
route.AddBefore(romeNode, "Zurich");
Console.WriteLine(" After AddBefore(Rome, Zurich): " + string.Join(" → ", route));
}
Console.WriteLine();
// ── 2. Navigating the Linked List ──────────────────────────
Console.WriteLine("── Navigating the Route ───────────────────────");
Console.WriteLine($" First city : {route.First.Value}");
Console.WriteLine($" Last city : {route.Last.Value}");
Console.WriteLine($" Total stops: {route.Count}");
Console.WriteLine();
// Forward traversal using nodes
Console.WriteLine(" Forward traversal:");
LinkedListNode<string> current = route.First;
int stop = 1;
while (current != null)
{
Console.WriteLine($" Stop {stop}: {current.Value}");
current = current.Next;
stop++;
}
Console.WriteLine();
// Backward traversal using nodes
Console.WriteLine(" Reverse traversal:");
current = route.Last;
stop = route.Count;
while (current != null)
{
Console.WriteLine($" Stop {stop}: {current.Value}");
current = current.Previous;
stop--;
}
Console.WriteLine();
// ── 3. Finding & Removing Nodes ────────────────────────────
Console.WriteLine("── Find & Remove Operations ──────────────────");
// Find a specific node
LinkedListNode<string> found = route.Find("Barcelona");
Console.WriteLine($" Find(\"Barcelona\") → {(found != null ? "Found" : "Not found")}");
// Find from the end (FindLast)
route.AddLast("Paris"); // duplicate — Paris appears twice
LinkedListNode<string> lastParis = route.FindLast("Paris");
Console.WriteLine($" FindLast(\"Paris\") → value = {lastParis.Value}");
Console.WriteLine($" Next after last Paris: {lastParis.Next?.Value ?? "(end of list)"}");
// Remove a specific value
route.Remove("Paris"); // removes first occurrence
Console.WriteLine($" After Remove(\"Paris\"): " + string.Join(" → ", route));
// Remove first and last
route.RemoveFirst();
route.RemoveLast();
Console.WriteLine($" After RemoveFirst + RemoveLast: " + string.Join(" → ", route));
// Remove a specific node
LinkedListNode<string> zurichNode = route.Find("Zurich");
if (zurichNode != null)
{
route.Remove(zurichNode);
Console.WriteLine($" After removing Zurich node: " + string.Join(" → ", route));
}
Console.WriteLine();
// ── 4. Contains & Clear ─────────────────────────────────────
Console.WriteLine("── Contains & Clear ──────────────────────────");
Console.WriteLine($" Contains(\"Rome\"): {route.Contains("Rome")}");
Console.WriteLine($" Contains(\"Tokyo\"): {route.Contains("Tokyo")}");
Console.WriteLine($" Count before clear: {route.Count}");
route.Clear();
Console.WriteLine($" Count after clear: {route.Count}");
Console.WriteLine("\n═══════════════════════════════════════════════");
Console.WriteLine(" Complexity: AddFirst/Last O(1) | AddAfter/Before O(1)");
Console.WriteLine(" Find O(n) | Remove(node) O(1) | Remove(value) O(n)");
Console.WriteLine("═══════════════════════════════════════════════");
}
}
}