-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List_Implementation.cs
63 lines (55 loc) · 1.1 KB
/
Linked_List_Implementation.cs
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
using System;
namespace LinkedListImplementatio
{
internal class Node
{
public Node Next;
public object Data;
}
public class LinkedList
{
private Node _head;
public void AddFirst(object data)
{
var toAdd = new Node {Data = data, Next = _head};
_head = toAdd;
}
public void AddLast(object data)
{
if (_head == null)
{
_head = new Node {Data = data, Next = null};
}
else
{
var toAdd = new Node {Data = data};
var current = _head;
while (current.Next!= null)
{
current = current.Next;
}
current.Next = toAdd;
}
}
public void Print()
{
var current = _head;
while (current != null)
{
Console.Write("--> " + current.Data);
current = current.Next;
}
}
}
internal static class Program
{
public static void Main(string[] args)
{
var linkedList = new LinkedList();
linkedList.AddFirst(5);
linkedList.AddFirst(6);
linkedList.AddLast(9);
linkedList.Print();
}
}
}