找回密码
 入驻
搜索
查看: 124387|回复: 3

[补水]泛型实现的万能链表

[复制链接]
发表于 2007-5-24 12:07:49 | 显示全部楼层 |阅读模式

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace TLists
  5. {
  6.     class TList<T>
  7.     {
  8.         public TList()
  9.         {
  10.             this.head = null;
  11.         }
  12.         class Node
  13.         {
  14.             private Node next;
  15.             private T data;
  16.             public Node()
  17.             { }
  18.             public Node(T data)
  19.             {
  20.                 this.data = data;
  21.                 this.next = null;
  22.             }
  23.             public T Data
  24.             {
  25.                 get { return data; }
  26.                 set { data = value; }
  27.             }
  28.             public Node Next
  29.             {
  30.                 get { return next; }
  31.                 set { next = value; }
  32.             }
  33.         }
  34.         public void Add(T data)
  35.         {
  36.             Node newNode = new Node(data);
  37.             newNode.Next = head;
  38.             head = newNode;
  39.         }
  40.         public void Show()
  41.         {
  42.             Node temp = this.head;
  43.             while (!(temp == null))
  44.             {
  45.                 Console.Write(temp.Data.ToString() + "\t");
  46.                 temp = temp.Next;
  47.             }
  48.         }
  49.         private Node head;
  50.     }
  51.     class MainClass
  52.     {
  53.         static void Main()
  54.         {
  55.             TList<int> list = new TList<int>();
  56.             Random random = new Random();
  57.             for (int i = 0; i < 5;i++ )
  58.             {
  59.                 int r = random.Next() % 30;
  60.                 list.Add(r);
  61.             }
  62.             list.Show();
  63.             Console.ReadLine();
  64.         }
  65.     }
  66. }
复制代码

<>里面是什么就能装什么,这就是泛型的作用
发表于 2007-5-24 12:40:34 | 显示全部楼层
太爱国了~~
不懂是什么东西~~~
回复

使用道具 举报

发表于 2007-5-24 12:41:01 | 显示全部楼层
c语言呀~~~~~~~~多发些上来呀~~~
回复

使用道具 举报

 楼主| 发表于 2007-5-24 13:06:25 | 显示全部楼层
你翻翻一大堆`````````
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 入驻

本版积分规则

QQ|Archiver|手机版|小黑屋|思明论坛

GMT+8, 2024-9-28 17:24 , Processed in 0.021208 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表