Linear table order representation and implementation

Source: Internet
Author: User

Linear tables are the most basic, simplest, and most commonly used data structures. The relationship between data elements in a linear table is a one-to-one relationship, that is, except for the first and last data elements, the other data elements are both end-to-tail. The logic structure of linear table is simple and easy to implement and operate. Therefore, the data structure of linear table is widely used in practical application.

The more important sequential tables, unidirectional linked lists, and circular linked lists are in the linear table. This paper mainly discusses the implementation of sequential tables.

List.java interface classes for linear tables

1  Packagecom.yeyan.linearlist;2 3 /**4 * Linear Table interface5  * @authorYeyan6 * @date 2014-12-077 * * API:8 * IsEmpty (): Determine if the linear table is empty9 * Length (): Table lengthTen * GET (int index): Gets the element of index One * Add (int index, T Element): Adds an element after the index position A * Remove (Index): Remove the element indexed as index - * Clear (): Clear linear table -  * the  */ -  -  Public InterfaceList<t> { -      Public BooleanisEmpty (); +      Public intlength (); -      PublicT Get (intIndexthrowsException; +      Public BooleanAddintIndex, T Element)throwsException; A      PublicT Remove (intIndexthrowsException; at      Public voidClear (); -}

Seqlist.java

1  Packagecom.yeyan.linearlist;2 /**3 * Sequential table implementation class4  * @authorYeyan5 * @date 2014-12-076  * @param<T>7  */8 9  Public classSeqlist<t>ImplementsList<t> {Ten     //a base class array One     Privateobject[] table; A     //current linear table length -     Private intsize; -     //Maximum length the     Private intmaxSize; -     //Default Length -     Private Final intDefaultSize = 20; -     /* + * No parameter constructor -      */ +      Publicseqlist () { AMaxSize =defaultsize; at          This. Size = 0; -Table =NewObject[maxsize]; -     } -     /** - * Parametric Constructors -      * @paramsize in      */ -      PublicSeqlist (intsize) { toMaxSize =size; +          This. Size = 0; -Table =NewObject[maxsize]; the     } *     /** $ * Get the number of elementsPanax Notoginseng      */ -      Public intLength () { the         returnsize; +     } A     /** the * is empty +      */ -      Public BooleanIsEmpty () { $         returnSize = = 0; $     } -  -     /** the * Gets the element indexed to index -      */Wuyi      PublicT Get (intIndexthrowsException { the         if(Index < 0 | | index >size) { -             Throw NewException ("Paramether error!"); Wu         } -         return(T) table[index]; About     } $     /** - * After adding an element to index -      */ -      Public BooleanAddintIndex, Object Element)throwsException { A         if(element = =NULL)return false; +         if(Size = =maxSize) { the             Throw NewException ("Linear size is full£¡"); -         } $         if(Index < 0 | | index >maxSize) { the             Throw NewException ("parameter!"); the         } the         //Move Element the          for(inti = size; i > Index; I--){ -Table[i] = table[i-1]; in         } theTable[index] =element; theSize + +; About         return true; the     } the     /** the * Delete the element indexed as index +      */ -      PublicT Remove (intIndexthrowsException { theT element =(T) table[index];Bayi         if(IsEmpty ()) { the             Throw NewException ("Linear list is null£¡"); the         } -         if(Index < 0 | | index > SIZE-1){ -             Throw NewException ("Parameter error£¡"); the         } the         //Move Element the          for(inti = index; i < size-1; i + +){ theTable[i] = table[i+1]; -         } theSize--; the         returnelement; the     }94     /** the * Empty Order table the      */ the      Public voidClear () {98          for(inti = 0; i < size; i + +){ AboutTable[i] =NULL; -         }101Size = 0;102     }103}

Test.java

1  Packagecom.yeyan.linearlist;2 /**3 * Sequential table test class4  * @authorYeyan5 * @date 2014-12-076  */7  Public classTest {8      Public Static voidMain (string[] args)throwsexception{9Seqlist<integer> seqlist =NewSeqlist<integer>();TenSeqlist.add (0, 100); OneSeqlist.add (1, 102); ASeqlist.add (2, 104); - System.out.println (Seqlist.length ()); - System.out.println (Seqlist.isempty ()); theSystem.out.println (Seqlist.get (0)); -System.out.println (Seqlist.remove (1)); -     } -}

Efficiency analysis of sequential table operation

The static characteristics of the sequential table are very good, the dynamic characteristics are poor, the specific description is as follows:

1, the physical storage order of sequential table elements directly reflects the logical order of linear table elements, and the order represents a random access structure. The time complexity of the Get (), set () method is O (1).

2, insert and delete efficiency is very low. If the probability of inserting an element at each location is the same, the complexity is O (n).

Linear table order representation and implementation

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.