-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotnetLinkedList.cs
78 lines (71 loc) · 2.09 KB
/
DotnetLinkedList.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DataStructBackend
{
/*
* Linked list with simple operations:
* inserting an item at the beginning of the chain
* deleting an item at the beginning of the chain
* ability to find and delete specified link
*/
public class DotnetLinkedList
{
public int key;
public decimal dData;
public DotnetLinkedList first;
public DotnetLinkedList next;
public DotnetLinkedList(int id, decimal value)
{
key = id;
dData = value;
}
public bool IsEmpty()
{
return first == null;
}
public void InsertFirst(int id, decimal value)
{
DotnetLinkedList newLink = new DotnetLinkedList(id, value);
newLink.next = first;
first = newLink;
}
public void DeleteFirst()
{
first = first.next;
}
public DotnetLinkedList FindLinear(int key)
{
DotnetLinkedList currentLink = first; //start at the first link item
while (currentLink.key != key)
{
if (currentLink.next == null) return null;
currentLink = currentLink.next;
}
return currentLink;
}
// assumes a non empty list:
public DotnetLinkedList Delete(int key)
{
DotnetLinkedList currentLink = first;
DotnetLinkedList previousLink = first;
while (currentLink.key != key)
{
if (currentLink.next == null) return null;
previousLink = currentLink;
currentLink = currentLink.next;
}
if (currentLink == first)
{
first = first.next;
}
else
{
//currentLink which is the node to be deleted will be bypass
previousLink.next = currentLink.next;
}
return currentLink;
}
}
}