Linear Table C # implementation

Source: Internet
Author: User

Concept

For more information, see C # data structure.

Linear table and sequence table

Code

Linear Table Interface

using System;using System.Collections.Generic;using System.Text;namespace CSharpDemo{    public interface ILinearList<T>    {        int GetLength();        void Clear();        bool IsEmpty();        void Append(T item);        void Insert(T item, int i);        void Delete(int i);        T GetElement(int i);        int Locate(T item);    }}

 Sequence Table

Using system; using system. collections. generic; using system. text; namespace csharpdemo {// <summary> // sequence table implementation /// </Summary> /// <typeparam name = "T"> </typeparam> public class sequencelist <t>: ilinearlist <t> {# region field and property private int maxsize; private T [] data; private int last; Public int maxsize {get {return this. maxsize;} set {This. maxsize = value ;}} public int last {get {RET Urn this. last ;}# endregion public t this [int I] {get {return data [I];} set {data [I] = value ;}} public sequencelist (INT size) {This. maxsize = size; Data = new T [size]; last =-1 ;}# region ilinearlist <t> member public int getlength () {return last + 1 ;} public void clear () {last =-1;} public bool isempty () {return last =-1? True: false;} public void append (T item) {data [++ last] = item;} public void insert (T item, int index) {for (INT I = last; I >= index; I --) {data [I + 1] = data [I];} data [Index] = item; ++ last;} public void Delete (INT index) {for (INT I = index; I <= last; I ++) {data [I] = data [I + 1] ;}-- last;} public t getelement (INT index) {If (isempty () {return default (t );} return data [Index];} public int locate (T item) {int flag =-1; for (INT I = 0; I <= last; I ++) {If (item. equals (data [I]) {flag = I; break ;}} return flag ;}# endregion public override string tostring () {string datastring = string. empty; For (INT I = 0; I <= last; I ++) {datastring + = data [I] + "" ;}return datastring ;}}}

 

 

 

  

  

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.