1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace Datastructure
7: {
8: class Program
9: {
10: static void Main(string[] args)
11: {
12: OneWayLinearLinkedList linkedList = new OneWayLinearLinkedList();
13:
14: Console.WriteLine("Insert:");
15: for(int i = 0; i < 6; i++)
16: {
17: linkedList.Insert(new Node(i));
18: }
19: linkedList.Print();
20:
21: Console.WriteLine("Insert At:");
22: linkedList.InsertAt(new Node(11), 3);
23: linkedList.Print();
24:
25: Console.WriteLine("Delete:");
26: linkedList.Delete(linkedList.NodeAt(2));
27: linkedList.Print();
28:
29: Console.WriteLine("Delete At:");
30: linkedList.DeleteAt(2);
31: linkedList.Print();
32:
33: Console.WriteLine("Clear:");
34: linkedList.Clear();
35: linkedList.Print();
36:
37: Console.ReadKey();
38: }
39: }
40:
41: /// <summary>
42: /// One way linear linked list
43: /// </summary>
44: public class OneWayLinearLinkedList
45: {
46: private Node head;
47:
48: public OneWayLinearLinkedList()
49: {
50: head = null;
51: }
52:
53: /// <summary>
54: /// Gets the node at a specific postion
55: /// </summary>
56: public Node NodeAt(int index)
57: {
58: if (head == null)
59: return null;
60: else if (index == 0)
61: {
62: return head;
63: }
64: else
65: {
66: Node currentNode = head.NextNode;
67: Node currentNodeNext = currentNode.NextNode;
68:
69: for (int i = 1; i < index; i++)
70: {
71: currentNode = currentNode.NextNode;
72: currentNodeNext = currentNode.NextNode;
73: }
74:
75: return currentNode;
76: }
77: }
78:
79: /// <summary>
80: /// Inserts a node at the begining of the linked list
81: /// </summary>
82: public void Insert(Node newNode)
83: {
84: if (head == null)
85: {
86: head = newNode;
87: newNode.NextNode = null;
88: }
89: else
90: {
91: newNode.NextNode = head;
92: head = newNode;
93: }
94: }
95:
96: /// <summary>
97: /// Inserts a node at a specific position
98: /// </summary>
99: public void InsertAt(Node newNode, int index)
100: {
101: if (index == 0)
102: {
103: newNode.NextNode = head;
104: head = newNode;
105: }
106: else
107: {
108: Node currentNode = head;
109: Node currentNodeNext = head.NextNode;
110: for (int i = 1; i < index; i++)
111: {
112: currentNode = currentNode.NextNode;
113: currentNodeNext = currentNode.NextNode;
114: }
115: if (currentNode != null)
116: {
117: currentNode.NextNode = newNode;
118: newNode.NextNode = currentNodeNext;
119: }
120:
121: }
122: }
123:
124: /// <summary>
125: /// Deletes a node
126: /// </summary>
127: public void Delete(Node deleteNode)
128: {
129: bool isSuccess = false;
130:
131: if (head == null) return;
132:
133: else if (head.Equals(deleteNode))
134: {
135: head = null;
136: }
137: else
138: {
139: Node currentNode = head;
140: Node currentNodeNext = head.NextNode;
141: while (currentNodeNext != null)
142: {
143: if (currentNodeNext.Equals(deleteNode))
144: {
145: currentNode.NextNode = currentNodeNext.NextNode;
146: currentNodeNext = null;
147: break;
148: }
149: else
150: {
151: currentNode = currentNodeNext;
152: currentNodeNext = currentNodeNext.NextNode;
153: }
154: }
155: }
156: }
157:
158: /// <summary>
159: /// Delets a node at a specific position
160: /// </summary>
161: public void DeleteAt(int index)
162: {
163: if (head == null) return;
164:
165: if (index == 0)
166: {
167: head = null;
168: }
169: else
170: {
171: Node currentNode = head;
172: Node currentNodeNext = head.NextNode;
173: for (int j = 1; j < index; j++)
174: {
175: currentNode = currentNode.NextNode;
176: currentNodeNext = currentNode.NextNode;
177: }
178: if (currentNode != null)
179: {
180: currentNode.NextNode = currentNodeNext.NextNode;
181: }
182: }
183: }
184:
185: /// <summary>
186: /// Prints the linear linked list
187: /// </summary>
188: public void Print()
189: {
190: Node firstNode = head;
191: while (firstNode != null)
192: {
193: Console.Write(firstNode.Data + " ");
194: firstNode = firstNode.NextNode;
195: }
196:
197: Console.Write("\n");
198: }
199:
200: /// <summary>
201: /// Clears the linked list
202: /// </summary>
203: public void Clear()
204: {
205: Node currentNode = head;
206: Node currentNodeNext;
207:
208: while (currentNode != null)
209: {
210: currentNodeNext = currentNode.NextNode;
211: currentNode = null;
212: currentNode = currentNodeNext;
213: }
214:
215: head = null;
216: }
217:
218:
219:
220: }
221:
222: /// <summary>
223: /// Node for the one way linear linked list
224: /// </summary>
225: public class Node
226: {
227: public int Data { get; set; }
228: public Node NextNode { get; set; }
229:
230: public Node(int data)
231: {
232: this.Data = data;
233: }
234: }
235: }