test

代码
1 using System;
2  using System.Collections.Generic;
3 using System.Text;
4
5
6 namespace link
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 SLinkList p = new SLinkList();
13 // p.CreateListHead(); //头插法
14 p.CreateListTail(); //尾插法
15 p.PrintList(); //输出全部结点
16 Console.WriteLine("Length={0}",p.GetLength()); //输出单链表长度
17 }
18 }
19
20 //********************************************//
21 //链表类
22 class SLinkList
23 {
24 private SNode start;//单链表的头引用
25 int length=0;//单链表的长度
26
27 //初始化线性表
28 public SLinkList()
29 {
30 start = null;
31 }
32
33 //头插法创建单链表
34 //public void CreateListHead( )
35 //{
36 // int d;
37 // d = Int32.Parse(Console.ReadLine());
38 // while (d != -1)
39 // {
40 // SNode p = new SNode(d);
41 // p.Next = start;
42 // start = p;
43 // d = Int32.Parse(Console.ReadLine());
44 // }
45 //}
46
47 //尾插法创建单链表
48 public void CreateListTail()
49 {
50 SNode R = new SNode();
51 int d;
52
53 d = Int32.Parse(Console.ReadLine());
54 while (d != -1)
55 {
56 SNode p = new SNode(d,null);
57 if (start == null)
58 start = p;
59 else
60 R.Next = p;
61 R = p;
62 d = Int32.Parse(Console.ReadLine());
63 }
64 }
65
66 //输出单链表元素
67 public void PrintList( )
68 {
69 SNode node = start;
70 while (node != null)
71 {
72 Console.WriteLine("{0}", node.Data);
73 node = node.Next;
74 }
75 }
76
77 //求单链表的长度
78 public int GetLength()
79 {
80 SNode q = start;
81 while (q != null)
82 {
83 length++;
84 q = q.Next;
85 }
86 return length;
87 }
88 }
89
90 //*****************************************************//
91 //结点类
92 class SNode
93 {
94
95 private int data; //数据域
96 private SNode next; //引用域
97 public SNode(int val, SNode p)
98 {
99 data = val;
100 next = p;
101 }
102 public SNode(SNode p)
103 {
104 next = p;
105 }
106 public SNode(int val)
107 {
108 data = val;
109 next = null;
110 }
111 public SNode()
112 {
113 data = default(int);
114 next = null;
115 }
116 //数据域属性
117 public int Data
118 {
119 get
120 {
121 return data;
122 }
123 set
124 {
125 data = value;
126 }
127 }
128 //引用域属性
129 public SNode Next
130 {
131 get
132 {
133 return next;
134 }
135 set
136 {
137 next = value;
138 }
139 }
140 }
141 }
142
原文地址:https://www.cnblogs.com/luoxs/p/1848868.html