[Data structure] Sequence table of linear table

Table of contents

Basic concepts and operations:

Implementation method and principle

Application Scenarios and Precautions for Use

Algorithm and complexity analysis:

Comparison with other data structures:

PS: If there are any mistakes or omissions, please correct me


Basic concepts and operations:

Sequence table is a common linear data structure, which usually consists of continuous storage units, and any element in it can be accessed randomly. A sequential table is a data structure implemented by an array, in which elements are stored consecutively in memory, and each element occupies a fixed-size space. The sequential table has the advantage of random access, and can traverse, search, modify and other operations more efficiently. However, the insertion and deletion operations are time-consuming and need to move the following elements, which may not be suitable for frequent insertion and deletion scenarios.

The following are commonly used operations on sequence tables:

  1. Initialize the sequence table: Initialize the sequence table is to create an empty sequence table for storing the linear table, the creation process is as follows:

step operate
1 Initialize maxsize to the actual value
2 Apply for a storage space that can store maxsize data elements for an array, and the type of data elements depends on the actual application
3 Initialize length to 0

     2. Insertion operation: Inserting data elements (here only talk about forward insertion) means assuming that there are already length (0≤length≤maxsize-1) data elements in the sequence table, the i (1≤i ≤length+1) Insert a new data element at the data element position. The creation process is shown in the table below:

step operate
1

If the insertion position is not specified, the data element will be inserted into the last position of the sequence table; if the insertion position i is specified, if the insertion position i<1 or pos>length+1, it cannot be inserted, otherwise it will be transferred step2.

2

Move the length-i+1 data elements from the length-th to the i-th storage location in sequence, and place the new data element at the i-th location

3

Add 1 to the length of the sequence table

      3. Delete operation

      Assuming that there are already length(1≤length≤maxsize) data elements in the sequence table, delete the data element at the specified position. The specific algorithm is as follows:

step operate
1

If the list is empty, or does not meet 1≤i≤length, then prompt the element to be deleted, otherwise go to step 2

2

Move the i+1th to length (total length-i) data elements forward sequentially

3

Decrease the table length length of the sequence table by 1

       

Other operations related to the linear table, such as fetching table elements, locating elements, finding the length of the table, and judging that it is empty, are relatively simple to implement in Shun. For details, see the following C# code:

    /// <summary>
    /// 顺序表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqList<T> : ILinarList<T>
    {
        private int maxsize;//顺序表的最大容量
        private T[] data;///数组,用于存储顺序表中的数据元素
        private int length;//顺序表的实际长度

        /// <summary>
        /// 实际长度属性
        /// </summary>
        public int Length
        {
            get 
            { 
                return length; 
            }
        }

        /// <summary>
        /// 最大容量属性
        /// </summary>
        public int Maxsize
        {
            get
            {
                return maxsize;
            }
            set
            {
                maxsize = value;
            }
        }

        /// <summary>
        /// 初始化线性表
        /// </summary>
        /// <param name="size">设置的顺序表的最大容量</param>
        public SeqList(int size)
        {
            maxsize = size;
            data = new T[maxsize];
            length = 0;
        }

        /// <summary>
        /// 在顺序表的末尾追加数据元素
        /// </summary>
        /// <param name="value"></param>
        public void InsertNode(T value)
        {
            if (IsFull())
            {
                Console.WriteLine("List is tull");
                return;
            }
            data[length] = value;
            length++;
        }

        /// <summary>
        /// 在顺序表的第i个数据元素的位置插入一个数据元素
        /// </summary>
        /// <param name="value"></param>
        /// <param name="i"></param>
        public void InsertNode(T value, int i)
        {
            if (IsFull())
            {
                Console.WriteLine("List is full");
                return;
            }
            if(i<1 || i>length + 1)
            {
                Console.WriteLine("Position is error!");
                return;
            }
            for (int j = length-1; j >= i-1; j--)
            {
                data[j + 1] = data[j];
            }
            data[i - 1] = value;
            length++;
        }

        /// <summary>
        /// 删除顺序表的第i个数据元素
        /// </summary>
        /// <param name="i"></param>
        public void DeleteNode(int i)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty");
                return;
            }
            if(i<1 || i > length)
            {
                Console.WriteLine("Position is error!");
                return;
            }
            for (int j = i; j < length; j++)
            {
                data[j - 1] = data[j];
            }
            length--;
        }

        /// <summary>
        /// 获取顺序表第i个数据元素
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T SearchNode(int i)
        {
            if(IsEmpty() || i<1 || i > length)
            {
                Console.WriteLine("List is empty or position is error");
                return default(T); 
            }
            return data[i-1];
        }

        /// <summary>
        /// 在顺序表中查找值为value的数据元素
        /// </summary>
        /// <param name="value">要查找的数据元素</param>
        /// <returns></returns>
        public T SearchNode(T value)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty");
                return default(T);
            }
            for (int i = 0; i < length; i++)
            {
                if(data[i].Equals(value))
                {
                    return data[i];
                }
            }
            return default(T);
        }

        /// <summary>
        /// 求顺序表的长度
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            return length;
        }


        /// <summary>
        /// 清空顺序表
        /// </summary>
        public void Clear()
        {
            length = 0;
        }

       
        /// <summary>
        /// 判断顺序表是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
           if(length == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 反转顺序表
        /// </summary>
        public void ReverseList()
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty");
                return;
            }
            int left = 0;
            int right =length - 1;
            while (left < right)
            {
                T temp = data[left];
                data[left] = data[right];
                data[right] = temp;

                left++;
                right--;
            }
        }

        /// <summary>
        /// 判断顺序表是否已满
        /// </summary>
        /// <returns></returns>
        public bool IsFull()
        {

            if(length == maxsize)
            {
                return true;
            }
            return false;
        }
    }

Implementation method and principle

A sequential table is composed of a contiguous memory space in which each element occupies the same size space. In actual use, arrays can be used to implement sequential tables.

Specifically, in the C# language, List<T>a class or an ordinary array can be used to implement the sequence table. The following two implementation methods are introduced respectively:

  1. Use List<T>class: List<T>A class is one of the commonly used data structures in C#. It encapsulates a dynamic array and can perform addition and deletion operations efficiently. List<T>The class provides many methods, such as Add, Insert, Remove, Clear, etc., which are convenient for adding, deleting, modifying and checking the list. In actual use, you can List<T>create an empty list by calling the constructor of the class, and add elements to it at any time.

  2. Use ordinary arrays: An ordinary array is a basic data structure that enables efficient random access to any element within it. In actual use, you can implement a sequential table by defining a static array, and then use the subscript accessor ([]) to access and modify elements. In order to support dynamic addition and deletion of elements, it is necessary to expand the array when it is full, and at the same time use insertion and deletion techniques to ensure the continuity and order of elements.

In short, a sequence table is a common data structure that can be List<T>implemented by an array or a class. When using the sequence table, it is necessary to choose according to the specific application scenarios and requirements, and use appropriate operation skills to ensure the continuity and order of the elements.

Application Scenarios and Precautions for Use

Sequence table is a common data structure, which is suitable for scenarios that require random access to elements, and frequent search and modification operations. The following are some common application scenarios for sequence tables:

  1. Array: Array is the most basic sequence table, suitable for scenarios that require efficient random access to array elements, such as multidimensional arrays, image processing, etc.

  2. Database storage: In a relational database, table data can be stored in the form of a sequential table, each row of data corresponds to an element, and any row of data can be quickly and randomly accessed through subscript accessors. For example, in MySQL, each table corresponds to a physical file, in which data is stored and managed in the form of sequential tables.

  3. Graphical interface development: In graphical interface development, UI controls are usually stored and rendered in the form of sequential tables, such as the ListBox control in Windows Forms or the ItemsControl control in WPF.

When using sequence tables, you need to pay attention to the following points:

  1. The length of the sequence table is fixed: Since the memory space of the sequence table is continuous, its length is fixed. If the number of elements to be stored exceeds the capacity of the sequence table, an expansion operation is required.

  2. The insertion and deletion of elements is relatively time-consuming: In a sequential table, inserting and deleting elements usually requires moving subsequent elements, so it is relatively time-consuming. If frequent insertion and deletion operations are required, it is recommended to use a data structure such as a linked list.

  3. Array out-of-bounds problem: When using an array to implement a sequence table, an array out-of-bounds problem may occur, and attention should be paid to avoid this situation.

In conclusion, sequential table is a common data structure with excellent performance in scenarios that require random access and modification of elements. In actual use, it is necessary to select an appropriate data structure according to specific needs and scenarios, and pay attention to its characteristics and usage precautions.

Algorithm and complexity analysis:

Common algorithms for sequence tables are as follows:

  1. Insert an element at a specified position: move all elements at and after that position one bit backwards, and then insert a new element at that position. The time complexity is O(n).

  2. Delete an element at a specified position: move all elements after the position forward by one, and then delete the last element. The time complexity is O(n).

  3. Find element: You can find the specified element by sequential traversal or binary search. In an unordered sequence table, the time complexity of sequential traversal is O(n), while the time complexity of binary search is O(log n); in an ordered sequence table, the time complexity of using binary search is O( log n).

  4. Sorting the Sequence List: Various sorting algorithms can be used such as bubble sort, quick sort, merge sort, etc. Among them, the average time complexity of quick sort and merge sort is O(n log n), while the time complexity of bubble sort is O(n^2).

The space complexity of the sequential table is O(n), that is, space for storing n elements is required. In practical applications, due to the fixed length of the underlying array of the sequence table, overflow problems may occur, and expansion operations are required, resulting in higher space complexity.

In conclusion, sequential table is a common data structure with excellent random access efficiency. The time complexity of its operation mainly depends on operations such as element insertion, deletion, and search. In practical applications, it is necessary to select an appropriate algorithm and data structure according to the specific situation, and make trade-offs and compromises.

Comparison with other data structures:

A sequence table is a common data structure that has the following advantages and disadvantages compared to other data structures:

  1. Compared with the linked list, the sequential list has higher random access efficiency and can access any element in O(1) time. However, subsequent elements need to be moved when inserting and deleting elements, so it is less efficient (O(n)).

  2. Compared with stacks and queues, sequential tables can store more elements and support more flexible access methods. However, its insertion and deletion operations are relatively inefficient (O(n)), making them unsuitable for frequent operations.

  3. Compared with complex data structures such as trees and graphs, sequential tables are simple to implement, easy to understand and maintain. However, it is inefficient when searching and traversing large amounts of data, and is not suitable for such application scenarios.

  4. Compared with the hash table, the lookup efficiency of the sequential table is lower (O(n)), and it does not support fast insertion and deletion operations. However, it is simple to implement and does not need to deal with issues such as hash collisions.

In short, the sequential table is a common data structure with excellent random access efficiency, but it is inefficient in operations such as insertion, deletion, and search. It needs to be selected according to specific needs and application scenarios when using it. It should be noted that in practical applications, there may be trade-offs and compromises between different data structures, and the choice needs to be made according to the actual situation.

PS: If there are any mistakes or omissions, please correct me

Guess you like

Origin blog.csdn.net/beenles/article/details/131432444