The sequential table of data structure Java implementation __arcinfo

Source: Internet
Author: User

The element defaults to the char type, with detailed comments in the code that can be compiled and run

Interface:

Package linearlist;

Public interface Iliist {public
	void Clear ()//null table public

	Boolean isempty ();//is empty public

	int length ();//number  According

	to the number of elements public char get (int i) throws exception;//returns the value of the first Data element public

	void Insert (int i, char x) throws exception;// Insert a data element that has a value of x before the first Data element public

	void Remove (int i) throws exception;//deletes and returns the first element public
	
	int indexOf (char x); Returns the bit ordinal of the specified data element for the first occurrence,
	                             //If the linear table does not contain this data element, returns-1 public
	
	void display ();//Output
}

Specific implementation:

Package linearlist; public class SqList implements iliist{private char[] listelem;//linear table storage space private int curlen;//Linear table Current length//order table constructor, construct a A storage space capacity bit maxSize linear table public sqlist (int maxSize) {curlen=0;//The current length of the order table is 0 listelem=new char[maxsize];//assigning maxSize storage to the sequential table Unit}//set an existing linear table into empty table public void Clear () {curlen=0;//The current length of the order table is 0}//Determine whether the number of data elements in the linear table is 0 public boolean isempt 
	Y () {return curlen==0;//is 0 returns true, otherwise return false}//Find the number of data elements in the linear table and return their value public int length () {return curlen;//returns the current length of the order table //Read to the first element of the linear table and return its value by the function,//Where I has a range of: 0<=i<=length ()-1//If I is not in this range then throws an exception public char get (int i) throws Excepti on{if (i<0| |  i>curlen-1) {//i is less than 0 or I is greater than the table long minus one throw new Exception ("The first" +i+ "element does not exist");//Throw exception} return listelem[i];//returns the first Data element}//
			Insert a data element with a value of x before the first Data element public void Insert (int i, char c) throws exception{if (curlen==listelem.length) {//To determine if the order table is full throw new Exception ("Sequential table full");//Throw exception} if (i<0| | I>curlen) {//If I am not valid throw new Exception ("Insert position not valid");//Throw exception} for (int j=curlen;j>i;j--) {listelem[j]=listelem[j-1];//the insertion position and all subsequent data elements after one bit//note is j=j-1, because cur Len is the length of the table} listelem[i]=c;//insert x curlen++;//table length plus 1}//delete and return the first element public void remove (int i) throws exception{if (i <0| | i>curlen-1) {//i illegal throw new Exception ("Delete location is illegal");//Throw exception} for (int j=i;j<curlen;j++) {Listelem[j]=listelem  [j+1];//all data elements after the deleted element moves left one storage location} curlen--;//table length minus 1}//Returns the bit ordinal of the specified data element for the first occurrence, and returns-1 public int indexOf If the linear table does not contain this data element (char x) {int j=0;//j indicates the data element to be compared in the order table, and its initial value indicates that the No. 0 data element in the sequential table while (j<curlen&&!
		listelem[j]== (x)) {//J + + in turn; } if (J<curlen) {//To determine whether J's position is returned in the order table j;//where the data element returning the value x is positioned in the order table} else return-1;//the data element with the value x does not exist in the order table}//
			Output public void display () {for (int j=0;j<curlen;j++) {System.out.print (listelem[j]+ "");
	} System.out.println ();
 }
}
Example:

Package exapmle;

Import linearlist.sqlist;

public class Example2_1 {public
	static void Main (string[] args) throws exception{sqlist l=new
		(a);
		L.insert (0, ' a ');
		L.insert (1, ' B ');
		L.display ();
	}



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.