-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleLinkedList.cs
More file actions
173 lines (147 loc) · 2.86 KB
/
SingleLinkedList.cs
File metadata and controls
173 lines (147 loc) · 2.86 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Created by SharpDevelop.
* User: anton
* Date: 18.11.2010
* Time: 18:34
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace LinkedListLab
{
/// <summary>
/// Односвязный нециклический список
/// </summary>
public class SingleLinkedList
{
Node first;
public Node First {
set { first = value; }
get { return first; }
}
public bool isEmpty {
get {
if (First==null) return true;
return false;
}
}
public SingleLinkedList()
{
}
public SingleLinkedList(int[] data)
{
Node p;
first = null;
for (int i=0;i<data.Length;i++){
p = new Node(data[i],first);
first = p;
}
}
/// <summary>
///
/// </summary>
/// <param name="data">строка вида 1 2 3 4 5 6</param>
public SingleLinkedList(string data){
string[] sa = data.Split(' ');
int[] a = new int[sa.Length];
for (int i = 0; i<sa.Length; i++){
a[i] = int.Parse(sa[i]);
}
Node last;
if (a.Length == 0) first = null;
else {
First = new Node(a[0],null);
last = First;
for (int i=1;i<a.Length;i++){
last.Link = new Node(a[i],null);
last = last.Link;
}
}
}
public void Create(int[] data){
Node last;
if (data.Length == 0) first = null;
else {
First = new Node(data[0],null);
last = First;
for (int i=1;i<data.Length;i++){
last.Link = new Node(data[i],null);
last = last.Link;
}
}
}
/*
public void Create2(int[] data){
Node p = new Node(int[] data);
first = p;
Node last = p;
last.Link = null;
for (int i=1;i<data.Length;i++){
Node p = new Node(data[i],null);
last.Link = p;
last = p;
}
}
*/
public void DeleteFirst(){
if (first!=null){
first = first.Link;
}
}
public Node DeleteFirstAndGet(){
Node p = first;
if (first!=null)
first = first.Link;
return p;
}
public int Count(){
Node p = first;
int cnt = 0;
while (p!=null){
cnt++;
p=p.Link;
}
return cnt;
}
public void InsertAfterLast(int d){
if (first!=null){
Node p = first;
while (p.Link!=null){
p = p.Link;
}
p.Link = new Node(d,null);
}
else first = new Node(d,null);
}
public void InsertAfter(Node p,int d){
if (p!=null) {
p.Link = new Node(d,p.Link);
}
}
public void DeleteAfter(Node p){
if(p!=null && p.Link!=null){
p.Link = p.Link.Link;
}
}
public Node DeleteAfterAndGet(Node p){
Node Q = null;
if (p!=null && p.Link!=null){
Q = p.Link;
p.Link = Q.Link;
}
return Q;
}
public override string ToString()
{
string s = "";
Node p = First;
while (p!=null)
{
s+=p.Info+"->";
p = p.Link;
}
return s;
}
// TODO: Add Find node method
}
}