# Linear table - sequential storage structure

Posted by s.eardley on Sat, 07 Mar 2020 07:20:26 +0100

Definition: a finite sequence of zero or more data elements with line like properties. Except for the first element and the last element, each element has a precursor element and a successor element. The first element has no predecessor and the last element has no successor.
Data type: refers to a set of values with the same property and the general name of some operations defined on this set.
Abstract data type (ADT): refers to a mathematical model and a set of operations defined on this model.

## Standard format for abstract data types

```ADT Abstract data type name

Data  Logical relationship and definition between data elements

Operation  operation

```

## Some methods to realize linear table

``` InitList(): initialize linear table
ListEmpty(): judge whether the linear table is empty
ClearList(): clear linear table
GetElem(n,index): finds the element n with index as its subscript and returns it
LocateElem(e,index): finds an element equal to e and returns its index index
List insert (index, e): insert an element E in the index position
List delete (index): delete an element in the index position
ListLength(): get the length of the linear table
```

The sequential storage of linear tables has the characteristics of random storage, which is not suitable for adding and deleting tables frequently, but suitable for data access.

## Code example

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Linear_Table
{
class Program
{
static void Main(string[] args)
{
Random r = new Random();
My_LinearTable<int> m = new My_LinearTable<int>();
for(int i = 0; i < 9; i++)
{
int n = r.Next(1, 100);//random number
}
m.PrintLinear();
Console.WriteLine("Please enter the digital command:");
Console.WriteLine("1.Judge whether the table is full");
Console.WriteLine("2.Judge whether the table is empty");
Console.WriteLine("3.Empty form");
Console.WriteLine("4.Find element with subscript 4");
Console.WriteLine("5.Determine whether the table has elements equivalent to 5 and return the subscript value");
Console.WriteLine("6.Insert element 16 at table 6");
Console.WriteLine("7.Delete element with table subscript 7");
Console.WriteLine("8.Total list length");
Console.WriteLine("9.Current length of list");
while (true)
{
string s = Console.ReadLine();
int n = Int32.Parse(s);
if (n == 1)
{
Console.WriteLine(m.List_IsFull());

}
if (n == 2)
{
Console.WriteLine(m.List_IsEmpty());
}
if (n == 3)
{
m.ClearList();
}
if (n == 4)
{
Console.WriteLine(m.GetElem(4));
}
if (n == 5)
{
Console.WriteLine(m.LocateElem(5));
}
if (n == 6)
{
m.ListInsert(9, 16);
}
if (n == 7)
{
m.ListDelet(7);
}
if (n == 8)
{
Console.WriteLine(m.ListLength());
}
if (n == 9)
{
Console.WriteLine(m.List_CurrentLength());
}
m.PrintLinear();
}
}
}
//Define an interface to store linear table
public interface My_List<T>
{
bool List_IsFull();
bool List_IsEmpty();
void ClearList();
T GetElem( int index);
int LocateElem(T e);
void ListInsert(int index, T e);
void ListDelet( int index);
int ListLength();
int List_CurrentLength();
}
public class My_LinearTable<T> : My_List<T>
{
private int size = 0;
private readonly T[] list;//Write a generic array
private int count=0;//Used to judge whether the linear table is empty or full load
#region Here, two constructors are used to initialize the linear table
/// <summary>
///Give linear table an initial capacity
/// </summary>
/// <param name="n"></param>
public My_LinearTable(int n)
{
this.size = n;
list = new T[size];
}
/// <summary>
///If there is no initial capacity of the custom linear table, the initial capacity of the linear table is 10
/// </summary>
public My_LinearTable() : this(10)
{
}
#endregion
/// <summary>
///Add elements to the linear table
/// </summary>
/// <param name="date"></param>
public void Add(T date)
{
if (List_IsFull())
{
Console.WriteLine("This linear table is Full");
}
else
{
list[count] = date;
count++;
}
}
/// <summary>
///Clear linear table
/// </summary>
/// <param name="list"></param>
public void ClearList()
{
if (List_IsEmpty())
{
Console.WriteLine("This linear table is emperty,not to clear!");
}
else
{
count = 0;
}
}
/// <summary>
///Find the element with index index index from the linear table
/// </summary>
///< param name = "list" > linear table < / param >
///< param name = "index" > subscript value < / param >
///< param name = "e" > returned found value < / param >
/// <returns></returns>
public T GetElem( int index)
{
if (List_IsEmpty())//If the list is spatiotemporal, no value can be obtained
{
return default;
}
if (index - 1 >count || index - 1 <0)//If the subscript is out of range, no value can be obtained
{
Console.WriteLine("The subscript crossing the line!");
return default;
}

return list[index - 1];
}
/// <summary>
///Remove the element with index subscript from the linear table
/// </summary>
///< param name = "list" > linear table < / param >
///< param name = "index" > element subscript < / param >
public void ListDelet( int index)
{
if (index - 1 >=count || index - 1 < 0)
{
Console.WriteLine("The subscript crossing the line!");
return;
}
if (List_IsEmpty())
{
Console.WriteLine("The linear table is empty!");
return;
}
if (index == count)
{
ListReduce();
count--;
return;
}
for(int i = index - 1; i < count; i++)
{
list[i] = list[i + 1];
}
count--;
}
/// <summary>
///List delete a data
/// </summary>
public void ListReduce()
{
list[count - 1] = default;
}
/// <summary>
///Insert element e at index of linear table
/// </summary>
///<param name= "list" > linear table name </param>
///< param name = "index" > element subscript < / param >
///< param name = "e" > inserted element < / param >
public void ListInsert( int index, T e)
{
if (List_IsFull())
{
Console.WriteLine("This linear table is full,not to insert!");
return;
}
if (index - 1 >=count|| index - 1 < 0)
{
Console.WriteLine("The subscript crossing the line!");
return;
}
for (int i = index - 1; i < count; i++)
{
list[i + 1] = list[i];
}
list[index - 1] = e;
count++;
}
/// <summary>
///Get the length of linear table
/// </summary>
///< param name = "list" > linear table name < / param >
/// <returns></returns>
public int ListLength()
{
return list.Length;
}
/// <summary>
///Get the length of the current linear table
/// </summary>
///< param name = "list" > name of linear table < / param >
/// <returns></returns>
public int List_CurrentLength()
{
return count;
}
/// <summary>
///Whether the linear table is empty
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public bool List_IsEmpty()
{
return count == 0;
}
/// <summary>
///Find element subscripts equal to element e
/// </summary>
///< param name = "list" > linear table name < / param >
///< param name = "e" > element E < / param >
/// <returns></returns>
public int LocateElem(T e)
{
if (List_IsEmpty())
{
Console.WriteLine("This linear table is empty!");
return -1;
}
int temp = -1;
for(int i = 0; i < count; i++)
{
if (list[i].Equals(e))
{
temp = i;
}
}
return temp;
}
/// <summary>
///Is the linear meter at full load
/// </summary>
/// <returns></returns>
public bool List_IsFull()
{
return list.Length == count;
}
/// <summary>
///Print linear table
/// </summary>
public void PrintLinear()
{
if (List_IsEmpty())
{
Console.WriteLine("The linear table is emperty!");
return;
}
for(int i = 0; i < count; i++)
{
if (i == count - 1)
{
Console.Write(list[i]);
}
else
{
Console.Write(list[i] + ",");
}
}
Console.WriteLine("");
}
}
}
```
Published 16 original articles, won praise 1, visited 4393