Data Structure _ 1. Sequential linked list

Source: Internet
Author: User

All linked lists should have the following functions:

(1) Get the length of the linked list

(2) Clear all items in the linked list

(3) judge whether the linked list is empty

(4) Determine whether the linked list is full

(5) add a new item at the end of the linked list

(6) Insert a new item at the specified position of the linked list

(7) delete a project at the specified position of the linked list

(8) return the linked list item based on the serial number of the input item

(9) return the serial number of the item based on the value of the input list.

Define an excuse to constrain all linked list classes. The list Code is as follows:

 

Interface IListFunction <T>
{
Int GetLength ();
Void Clear ();
Bool IsEmpty ();
Bool IsFull ();
Void Append (T item );
Void Insert (T item, int index );
Void Delete (int index );
T GetItem (int index );
Int Locate (T value );
}

 

The code for implementing the sequence linked list is as follows:

Public class SequenceList <T>: IListFunction <T>
{
Private int max;
Private int last;
Private T [] data;
Public SequenceList (int max)
{
Data = new T [max];
This. max = max;
Last =-1;
}
/// <Summary>
/// Implement the Indexer
/// </Summary>
/// <Param name = "I"> </param>
/// <Returns> </returns>
Public T this [int I]
{
Get {return data [I];}
Set {data [I] = value ;}
}
/// <Summary>
/// Obtain the sequence table length
/// </Summary>
/// <Returns> </returns>
Public int GetLength ()
{
Return last + 1;
}
/// <Summary>
/// This is strange. Can I set the list to-1?
/// Set list to-1. The data stored in the linear table is not cleared. The first step is that the data in the linear table cannot be accessed.
/// But the data in the linear table still exists
/// </Summary>
Public void Clear ()
{
Last =-1;
}
/// <Summary>
/// Determine whether the sequence table is empty
/// </Summary>
/// <Returns> </returns>
Public bool IsEmpty ()
{
If (last =-1)
Return true;
Else
Return false;
}
/// <Summary>
/// Determine whether the sequence table is full
/// </Summary>
/// <Returns> </returns>
Public bool IsFull ()
{
If (last = max-1)
Return true;
Else
Return false;
}
/// <Summary>
/// Insert data at the end of the sequence table
/// </Summary>
/// <Param name = "item"> </param>
Public void Append (T item)
{
If (IsFull ())
Throw new Exception ("exceeds the maximum index value ");
Data [++ last] = item;
}
/// <Summary>
/// Insert a new value to the specified position of the sequence table
/// </Summary>
/// <Param name = "item"> </param>
/// <Param name = "index"> </param>
Public void Insert (T item, int index)
{
If (IsFull ())
Throw new Exception ("the linked list is full and data cannot be inserted ");
If (index <0 | index> last + 2)
Throw new Exception ("the insert position is smaller than zero or exceeds the maximum position of the linked list ");
If (index = last + 1)
{
Data [index] = item;
}
Else
{
For (int I = last + 1; I> index; I --)
{
Data [I] = data [I-1];
}
Data [index] = item;
}
++ Last;
If (AddItem! = Null)
{
ItemArgs e = new ItemArgs (index, item );
AddItem (this, e );
}
}
/// <Summary>
/// Delete the value at the specified position of the sequence table
/// </Summary>
/// <Param name = "index"> </param>
Public void Delete (int index)
{
If (index <0 | index> last + 1)
Throw new Exception ("the index to be deleted exceeds the limit ");
If (IsEmpty ())
Throw new Exception ("no items in the data table cannot be deleted ");
If (index = max-1)
Last = max-2;
Else
{
For (int I = index; I <last; I ++)
{
Data [I] = data [I + 1];
}
-- Last;
}
If (AddItem! = Null)
{
ItemArgs e = new ItemArgs (index );
AddItem (this, e );
}
}
/// <Summary>
/// Input values based on the sequence table position
/// </Summary>
/// <Param name = "index"> </param>
/// <Returns> </returns>
Public T GetItem (int index)
{
If (index <0 | index> last)
Throw new Exception ("cannot be obtained, the index exceeds the limit ");
Return data [index];
}
/// <Summary>
/// Locate the position of the value in the sequence table based on the Value
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public int Locate (T value)
{
Int resault =-1;
For (int I = 0; I <= last; I ++)
{
If (data [I]. Equals (value ))
{
Resault = I;
Break;
}
}
Return resault;
}

}


From the column of luxin

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.