C # Implementation Sequence table (linear table) Complete Example _c# tutorial

Source: Internet
Author: User
Tags int size

This article illustrates the method of C # implementation sequence table (linear table). Share to everyone for your reference, specific as follows:

The basic idea is to use an array as a container for the elements, the initial size of the array to be determined, and a pointer to point to the last element in the order table. The elements in the sequential table are subsets of the elements in the array. Sequential tables are contiguous in memory, the advantage is to find, the disadvantage is to insert elements and delete elements.

To avoid box unboxing, use generics here instead of object. Use an example of object can refer to this article: http://www.jb51.net/article/87603.htm, this link in the example of the implementation of the queue, and did not use pointer to identify the last element of the order table, Instead, it dynamically adjusts the size of the array, which is significantly different from this example, which is expensive to dynamically resize an array. Using object can also complete the data structure of the sequential table, but the frequent box-unboxing creates a large overhead and should be replaced with generics.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
    Namespace Linearlist {public interface ilistds<t> {int getlength ();
    void Insert (T item, int i);
    void Add (T item);
    BOOL IsEmpty ();
    T getelement (int i);
    void Delete (int i);
    void Clear ();
    int locateelement (T item);
  void Reverse (); }//Sequential table class class Sequencelist<t>:ilistds<t> {private int intmaxsize;//maximum capacity predetermined, use array must first determine capacity privat E t[] titems;//use an array to hold the element private int intpointerlast;//always point to the last element in the position public int MaxSize {get {return thi S.intmaxsize;
    set {this.intmaxsize = value;}
    The public T this[int i]//Indexer is convenient for returning {get {return this.titems[i];}
    public int Pointerlast {get {this.intpointerlast;}
      public sequencelist (int size) {this.intmaxsize = size; This.titems = new t[size];//Here initializes the most reasonable this.intpointerlast = -1;//The initial value is set to-1, at which point the number of elements in the array is 0
    public bool Isfull ()//judge whether or not to exceed the capacity {return this.intpointerlast+1 = = This.intmaxsize;
    #region ilistds<t> member public int getlength () {returns This.intpointerlast + 1;//cannot return the length of Titems public void Insert (T item, int i)//set I as the first element, starting from 1. The function represents inserting item {if (I < 1 | | i > this.intpointerlast + 1) after the first element {Console.WriteLine ("The INS
        Erting location is wrong! ");
      Return } if (this. Isfull ()) {Console.WriteLine ("This linear the list is full!
        Can ' t insert any new items! ");
      Return
      ///If you can add this.intpointerlast++;
      for (int j=this.intpointerlast;j>=i+1;j--) {This.titems[j] = this.titems[j-1];
    } This.titems[i] = Item; The public void Add (T item) {if, this. Isfull ())//If the maximum capacity is exceeded, the new element {Console.WriteLine ("This linear the list is full!" cannot be added).
      Can ' t add any new items! ");} else {this.Titems[++this.intpointerlast] = item;//table length +1}} public bool IsEmpty () {return this.intpointerlast
    = =-1; The public T getelement (int i)/set I minimum starting from 0 {if (this.intpointerlast = = 1) {Console.WriteLine ("Th
        ere are no elements in this linear list! ");
      return default (T); } if (I > This.intpointerlast| |
        i<0) {Console.WriteLine ("Exceed the capability!");
      return default (T);
    return this.titems[i]; The public void Delete (int i)/set I minimum starting from 0 {if (this.intpointerlast = 1) {Console.WriteLine ("Th
        ere are no elements in this linear list! ");
      Return
        } if (i > This.intpointerlast | | I < 0) {Console.WriteLine ("Deleting location is wrong!");
      Return
      for (int j = i; J < This.intpointerlast; J +) {This.titems[j] = this.titems[j + 1];
   this.intpointerlast--;//table Length-1} public void Clear () {this.intpointerlast =-1;  public int locateelement (T item) {if (this.intpointerlast = = 1) {Console.WriteLine ("There
        are no items in the list! ");
      return-1; for (int i = 0; I <= this.intpointerlast i++) {if (this.titems[i).
        Equals (item))//If the custom type, then the T class must be the Equals function override {return i;
      } Console.WriteLine ("Not Found");
    return-1; public void Reverse () {if (this.intpointerlast = = 1) {Console.WriteLine ("There are no ite
      MS in the list! ");}
        else {int i = 0; Int J = this.
          The GetLength ()/2;//result is a lower-bound integer, just for the loop while (I < j) {T tmp = this.titems[i];
          This.titems[i] = this.titems[this.intpointerlast-i];
          This.titems[this.intpointerlast-i] = tmp;
        i++; #endregion} class Program {STAtic void Main (string[] args) {}}}

 

Merge sort based on sequential tables:

Sequential table based merge sort
static private sequencelist<int> merge (sequencelist<int> s1,sequencelist<int> s2 )
{
  sequencelist<int> slist = new sequencelist<int>;
  int i = 0;
  int j = 0;
  while (I<=S1. Pointerlast&&j<=s2. Pointerlast)
  {
    if (S1[i] < s2[j])
    {
      slist.add (s1[i));
      i++;
    }
    else
    {
      slist.add (s2[j]);
      j + +;
    }
  }
  if (i > S1. Pointerlast)
  {while
    (J <= S2). Pointerlast)
    {
      slist.add (s2[j]);
      j + +;
    }
    return slist;
  }
  else//is j>s2. Pointerlast
  {While
    (I <= S1). Pointerlast)
    {
      slist.add (s1[i]);
      i++;
    }
    Return slist
  }
}

For more information on C # related content readers can view the site topics: "C # Data structure and algorithm tutorial", "C # traversal algorithm and Skills summary", "C # Design Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation skills Summary", "C # Common control usage tutorials, summary of WinForm control usage, summary of C # array operations tips, and the introductory course on C # object-oriented programming

I hope this article will help you with C # programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.