热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

C#数据结构之线性表

发布时间:2010年03月30日点击数: 生鱼片

一:介绍
线性表是最基本,最简单也是应用最广的数据结构。线性表是线性数据的抽象,其特点是结构中的数据元素之间存在着一对一的线性关系,
是一种数据元素序列的数据结构。
线性表是由n>=0个相同的数据元素构成的有限序列,有限指的是线性表中的数据元素个数是有限的,并且每一个数据元素都有自己的位置。
二:代码实现
1. 线性表的一些通用操作,我们统一为接口:

  1. public interface IListDS<T> 
  2.         int GetLength(); 
  3.         void Clear(); 
  4.         bool IsEmpty(); 
  5.         void Append(T item); 
  6.         void Insert(T item, int i); 
  7.         T Delete(int i); 
  8.         T GetElem(int i); 
  9.         int Locate(T value); 
  10.         void Reverse(); 

2. 线性表中常见的有顺序表,单链表,双向链表,循环链表。
3. 顺序表

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text;  
  5. namespace SeqDS 
  6.     public class SeqList<T>:IListDS<T> 
  7.     { 
  8.         private int maxsize;//顺序表的容量 
  9.         private T[] data;//数组,用于存储顺序表中的数据元素 
  10.         private int last;//指示顺序表最后一个元素的位置  
  11.         //索引器 
  12.         public T this[int index] 
  13.         { 
  14.             get { return data[index]; } 
  15.             set { data[index] = value; } 
  16.         }  
  17.         public int Last 
  18.         { 
  19.             get { return last; } 
  20.         }  
  21.         public int Maxsize 
  22.         { 
  23.             get{return maxsize;} 
  24.             set { maxsize = value; } 
  25.         }  
  26.         //构造器 
  27.         public SeqList(int size) 
  28.         { 
  29.             data = new T[size]; 
  30.             maxsize = size; 
  31.             last = -1; 
  32.         }  
  33.         //求顺序表的长度 
  34.         public int GetLength() 
  35.         { 
  36.             return last + 1; 
  37.         }  
  38.         //清空顺序表 
  39.         public void Clear() 
  40.         { 
  41.             last = -1; 
  42.         }  
  43.         //判断顺序表是否为空 
  44.         public bool IsEmpty() 
  45.         { 
  46.             if (last == -1) 
  47.                 return true
  48.             else 
  49.                 return false
  50.         }  
  51.         //判断顺序表是否为满 
  52.         public bool IsFull() 
  53.         { 
  54.             if (last == maxsize - 1) 
  55.                 return true
  56.             else 
  57.                 return false
  58.         }  
  59.         //在顺序表的末尾添加新元素 
  60.         public void Append(T item) 
  61.         { 
  62.             if (IsFull()) 
  63.             { 
  64.                 Console.WriteLine("List is full"); 
  65.                 return
  66.             } 
  67.             data[++last] = item; 
  68.         }  
  69.         //在顺序表的第i个数据元素的位置插入一个数据元素 
  70.         public void Insert(T item, int i) 
  71.         { 
  72.             if (IsFull()) 
  73.             { 
  74.                 Console.WriteLine("List is full"); 
  75.                 return
  76.             } 
  77.             if (i < 1 || i > last + 2) 
  78.             { 
  79.                 Console.WriteLine("Position is error!"); 
  80.                 return
  81.             } 
  82.             if (i == last + 2) 
  83.             { 
  84.                 data[last + 1] = item; 
  85.             } 
  86.             else 
  87.             { 
  88.                 for (int j = last; j > i - 1; --j) 
  89.                 { 
  90.                     data[j + 1] = data[j]; 
  91.                 } 
  92.                 data[i - 1] = item; 
  93.             } 
  94.             ++last; 
  95.         }  
  96.         //删除顺序表的第i个数据元素 
  97.         public T Delete(int i) 
  98.         { 
  99.             T tmp = default(T); 
  100.             if (IsEmpty()) 
  101.             { 
  102.                 Console.WriteLine("List is empty!"); 
  103.                 return tmp; 
  104.             } 
  105.             if (i < 1 || i > last + 1) 
  106.             { 
  107.                 Console.WriteLine("Position is error!"); 
  108.                 return tmp; 
  109.             } 
  110.             if (i == last + 1) 
  111.             { 
  112.                 tmp = data[last--]; 
  113.             } 
  114.             else 
  115.             { 
  116.                 tmp = data[i - 1]; 
  117.                 for (int j = i; j <= last; ++j) 
  118.                 { 
  119.                     data[j] = data[j + 1]; 
  120.                 } 
  121.             } 
  122.   
  123.             --last; 
  124.             return tmp; 
  125.         }  
  126.         //获得顺序表的第i个数据元素 
  127.         public T GetElem(int i) 
  128.         { 
  129.             if (IsEmpty() || (i < 1) || (i > last + 1)) 
  130.             { 
  131.                 Console.WriteLine("List is empty or Position is erroe!"); 
  132.                 return default(T); 
  133.             } 
  134.   
  135.             return data[i - 1]; 
  136.         }  
  137.         //在顺序表中查找值为value的数据元素 
  138.         public int Locate(T value) 
  139.         { 
  140.             if (IsEmpty()) 
  141.             { 
  142.                 Console.WriteLine("List is Empty!"); 
  143.                 return -1; 
  144.             } 
  145.             int i = 0; 
  146.             for (i = 0; i <= last; ++i) 
  147.             { 
  148.                 if (value.Equals(data[i])) 
  149.                     break
  150.             } 
  151.             if (i > last) 
  152.                 return -1; 
  153.             return i; 
  154.         }  
  155.         public void Reverse() 
  156.         { 
  157.             T tmp = default(T); 
  158.             int length = GetLength(); 
  159.             for (var i = 0; i < length / 2; i++) 
  160.             { 
  161.                 tmp = data[i]; 
  162.                 data[i] = data[length - i - 1]; 
  163.                 data[length - i - 1] = tmp;  
  164.             } 
  165.         }  
  166.     } 

4. 单向链表 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text;  
  5. namespace SeqDS 
  6.     public class LinkList<T>:IListDS<T> 
  7.     { 
  8.         private Node<T> head; 
  9.         public Node<T> Head 
  10.         { 
  11.             get { return head; } 
  12.             set { head = value; } 
  13.         }  
  14.         public LinkList() 
  15.         { 
  16.             head = null
  17.         }  
  18.         public int GetLength() 
  19.         { 
  20.             Node<T> p = head; 
  21.             int len = 0; 
  22.             while (p != null
  23.             { 
  24.                 ++len; 
  25.                 p = p.Next; 
  26.             } 
  27.             return len; 
  28.         }  
  29.         public bool IsEmpty() 
  30.         { 
  31.             if (head == null
  32.                 return true
  33.             else 
  34.                 return false
  35.         }  
  36.         public void Clear() 
  37.         { 
  38.             head = null
  39.         }  
  40.         public void Append(T item) 
  41.         { 
  42.             Node<T> q = new Node<T>(item); 
  43.             Node<T> p=new Node<T>(); 
  44.             if(head==null
  45.             { 
  46.                 head=p; 
  47.                 return
  48.             } 
  49.             p=head; 
  50.             while(p.Next!=null
  51.             { 
  52.                 p=p.Next; 
  53.             } 
  54.             p.Next=q; 
  55.         }  
  56.         public void Insert(T item, int i) 
  57.         { 
  58.             if(IsEmpty()||i<1) 
  59.             { 
  60.                 Console.WriteLine("List is empty or Position is error"); 
  61.                 return
  62.             }  
  63.             if (i == 1) 
  64.             { 
  65.                 Node<T> q = new Node<T>(item); 
  66.                 q.Next = head; 
  67.                 head = q; 
  68.                 return
  69.             } 
  70.             Node<T> p = head; 
  71.             Node<T> r = new Node<T>(); 
  72.             int j = 1; 
  73.             while (p.Next != null && j < i) 
  74.             { 
  75.                 r = p; 
  76.                 p = p.Next; 
  77.                 ++j; 
  78.             }  
  79.             if (j == i) 
  80.             { 
  81.                 Node<T> q = new Node<T>(item); 
  82.                 q.Next = p; 
  83.                 r.Next = q; 
  84.             } 
  85.         }  
  86.         public void InsertPost(T item, int i) 
  87.         { 
  88.             if (IsEmpty() || i < 1) 
  89.             { 
  90.                 Console.WriteLine("List is empty or Position is error"); 
  91.                 return
  92.             } 
  93.             if (i == 1) 
  94.             { 
  95.                 Node<T> q = new Node<T>(item); 
  96.                 q.Next = head.Next; 
  97.                 head.Next = q; 
  98.                 return
  99.             } 
  100.             Node<T> p = head; 
  101.             int j = 1; 
  102.             while (p != null && j < i) 
  103.             { 
  104.                 p = p.Next; 
  105.                 ++j; 
  106.             } 
  107.             if (j == i) 
  108.             { 
  109.                 Node<T> q = new Node<T>(item); 
  110.                 q.Next = p.Next; 
  111.                 p.Next = q; 
  112.             } 
  113.         }  
  114.         public T Delete(int i) 
  115.         { 
  116.             if (IsEmpty() || i < 0) 
  117.             { 
  118.                 Console.WriteLine("Link is empty or Positon is error"); 
  119.                 return default(T); 
  120.             } 
  121.             Node<T> q = new Node<T>(); 
  122.             if (i == 1) 
  123.             { 
  124.                 q = head; 
  125.                 head = head.Next; 
  126.                 return q.Data; 
  127.             } 
  128.             Node<T> p = head; 
  129.             int j = 1; 
  130.             while (p.Next != null && j < i) 
  131.             { 
  132.                 ++j; 
  133.                 q = p; 
  134.                 p = p.Next; 
  135.             }  
  136.             if (j == i) 
  137.             { 
  138.                 q.Next = p.Next; 
  139.                 return p.Data; 
  140.             } 
  141.             else 
  142.             { 
  143.                 Console.WriteLine("The ith node is not exist!"); 
  144.                 return default(T); 
  145.             } 
  146.         }  
  147.         public T GetElem(int i) 
  148.         { 
  149.             if (IsEmpty()) 
  150.             { 
  151.                 Console.WriteLine("List is empty"); 
  152.                 return default(T); 
  153.             }  
  154.             Node<T> p = new Node<T>(); 
  155.             p = head; 
  156.             int j = 1;  
  157.             while (p.Next != null && j < i) 
  158.             { 
  159.                 ++j; 
  160.                 p = p.Next; 
  161.             } 
  162.             if (j == i) 
  163.             { 
  164.                 return p.Data; 
  165.             } 
  166.             else 
  167.             { 
  168.                 Console.WriteLine("the ith node is not exist!"); 
  169.                 return default(T); 
  170.             } 
  171.         }  
  172.         public int Locate(T value) 
  173.         { 
  174.             if (IsEmpty()) 
  175.             { 
  176.                 Console.WriteLine("List is Empty"); 
  177.                 return -1; 
  178.             } 
  179.             Node<T> p = new Node<T>(); 
  180.             p = head; 
  181.             int i = 1; 
  182.             while (!p.Data.Equals(value) && p.Next != null
  183.             { 
  184.                 p = p.Next; ; 
  185.                 ++i; 
  186.             } 
  187.             return i; 
  188.         }  
  189.         public void Reverse() 
  190.         { 
  191.             Node<T> p = head.Next; 
  192.             Node<T> q = new Node<T>(); 
  193.             head.Next = null
  194.             while (p != null
  195.             { 
  196.                 q = p; 
  197.                 p = p.Next; 
  198.                 q.Next = head.Next; 
  199.                 head.Next = q; 
  200.             } 
  201.         } 
  202.     }  
  203.     public class Node<T> 
  204.     { 
  205.         private T data;//数据域 
  206.         private Node<T> next;//引用域 
  207.   
  208.         public Node(T val, Node<T> p) 
  209.         { 
  210.             data = val; 
  211.             next = p; 
  212.         }  
  213.         public Node(Node<T> p) 
  214.         { 
  215.             next=p; 
  216.         } 
  217.         public Node(T val) 
  218.         { 
  219.             data = val; 
  220.             next = null
  221.         } 
  222.         public Node() 
  223.         { 
  224.             data = default(T); 
  225.             next = null
  226.         }  
  227.         public T Data 
  228.         { 
  229.             get { return data; } 
  230.             set { data = value; } 
  231.         } 
  232.         public Node<T> Next 
  233.         { 
  234.             get { return next; } 
  235.             set { next = value; } 
  236.         }        
  237.     } 

下面实现两个创建链表的函数如下:

  1. //在头部插入结点建立单链表 
  2.         static LinkList<int> CreateListHead() 
  3.         { 
  4.             int d; 
  5.             LinkList<int> l = new LinkList<int>(); 
  6.             d = Int32.Parse(Console.ReadLine()); 
  7.             while (d != -1) 
  8.             { 
  9.                 Node<int> p = new Node<int>(d); 
  10.                 p.Next = l.Head; 
  11.                 l.Head = p; 
  12.                 d = Int32.Parse(Console.ReadLine()); 
  13.             } 
  14.             return l; 
  15.         }  
  16.         //在尾部插入节点建立单链表 
  17.         static LinkList<int> CreateListTail() 
  18.         { 
  19.             Node<int> r = new Node<int>(); 
  20.             int d; 
  21.             LinkList<int> l = new LinkList<int>(); 
  22.   
  23.             r = l.Head; 
  24.             d = Int32.Parse(Console.ReadLine()); 
  25.   
  26.             while (d != -1) 
  27.             { 
  28.                 Node<int> p = new Node<int>(d); 
  29.                 if (l.Head == null
  30.                 { 
  31.                     l.Head = p; 
  32.                 } 
  33.                 else 
  34.                 { 
  35.                     r.Next = p; 
  36.                 } 
  37.                 r = p; 
  38.                 d = Int32.Parse(Console.ReadLine()); 
  39.             } 
  40.             if (r != null
  41.             { 
  42.                 r.Next = null
  43.             } 
  44.             return l; 
  45.         }  

5. 双向链表
5.1.双向链表的结点实现如下:

  1. public class DbNode<T> 
  2.     { 
  3.         private T data; 
  4.         private DbNode<T> prev; 
  5.         private DbNode<T> next; 
  6.   
  7.         public DbNode(T val, DbNode<T> p) 
  8.         { 
  9.             data = val; 
  10.             next = p; 
  11.         } 
  12.         public DbNode(DbNode<T> p) 
  13.         {            
  14.             next = p; 
  15.         } 
  16.         public DbNode(T val) 
  17.         { 
  18.             data = val; 
  19.             next = null
  20.         } 
  21.         public DbNode() 
  22.         { 
  23.             data = default(T); 
  24.             next = null
  25.         }  
  26.         public T Data 
  27.         { 
  28.             get { return data; } 
  29.             set { data = value; } 
  30.         } 
  31.         public DbNode<T> Prev 
  32.         { 
  33.             get { return prev; } 
  34.             set { prev = value; } 
  35.         } 
  36.         public DbNode<T> Next 
  37.         { 
  38.             get { return next; } 
  39.             set { next = value; } 
  40.         } 

5.2. 插入
由于双向链表的结点有两个引用,所以在双向链表中插入和删除结点比单链表要复杂,双向链表中结点的插入也分前插和后插,例如:设p指向某一结点,即p存储的是结点的地址,现将s插入到p前面,插入过程如下图:

clip_image002

对应的操作如下:

  1. p.Next.Prev=s; 
  2. s.Prev=p; 
  3. s.Next=p.Next; 
  4. p.Next=s; 

5.3.删除
我们以删除p结点的后面的结点为例,删除过程如下:

clip_image004

操作如下:

  1. p.Next=p.Next.Next; 
  2. P.Next.Prev=p.Prev; 

双向链表的其他操作与单链表相似。

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作