Sequential table (sequential storage structure of linear table) and C language implementation

Source: Internet
Author: User

The data elements that are linearly distributed on the logical structure are also adjacent to each other in the actual physical storage structure, which is called the sequential storage structure of the linear table.

In other words, the logically linear relationship of data is stored in a whole block of contiguous memory space in the order of the pre-and post-order, and there is no gap between them, so the storage structure is called sequential storage structure.

Using sequential storage of the data stored in the structure, the address of the first element is the first address of the storage space. Through the first address, you can easily access to the storage of all the data, as long as the first address is not lost, the data will always be able to find (a rope on the grasshopper, to have all).

A table that is generated using the sequential storage structure of a linear table, called the sequential table.


Fig. 1 Implementation method of sequential table structure Order table

The characteristics of the data stored in the sequential table and the data types in the array are exactly the same, so the implementation of the sequential table uses arrays.

When an array implements the storage structure of a sequential table, it is important to be aware of the need to pre-request large enough memory space to avoid the lack of storage space, resulting in data overflow, resulting in unnecessary program errors or even crashes. Storage structure of sequential tables

In order to establish the sequential table, in addition to the pre-application of memory space, but also need to record the length of the sequential table in real time and the order table itself to request the size of memory, easy to later on the data elements in the sequential table to be adjusted.

struct table{int * HEAD; // declares an array of indeterminate length named head, also called a "dynamic array" int length; // record the length of the current sequential table int size; // Record Order table allocated storage capacity } Table
Creation of sequential tables

The establishment of the sequential table, which is the sequential table initialization, assigns the initial value to the variable size and length while pre-applying the memory space:

Table Inittable ()
{table T; T.head=(int*)malloc(size*sizeof(int));//constructs an empty sequential table that dynamically requests storage space    if(!t.head)//If the application fails, make a prompt and exit the program directly    {printf ("initialization failed"); Exit (0); } t.length=0;//the length of the empty table is initialized to 0T.size=size;//empty table has an initial storage space of size    returnt;}

Sequential Table lookup Elements

When looking for a data element in an array, you can take a variety of search algorithms, such as binary lookup, interpolation lookup, Fibonacci lookup algorithm, and so on.

Specific search algorithms and their respective time complexity are described in subsequent chapters.

Based on the characteristics of the data stored in the sequential table, select the appropriate algorithm. Here, the sequential lookup algorithm (the normal traversal algorithm) is used.

Implementation code:

// The lookup function, where elem represents the value of the data element to find int int elem)
{ for (int i=0; i<t.length; i++
{ if
{ return i+1; }} return -1; // If the lookup fails, return-1}
Changing elements in a sequential table

The simplest and most straightforward way to change a data element in a sequential table is to call the lookup algorithm to find the location of the data element and change it directly at that location.

Implementation code:

// change the function, where Elem is the element to be changed, newelem the new data element int int Newelem)
{ intselecttable (t, Elem);  T.head[add-1] = Newelem; // Since the position of the element in the order table is returned, 1 is the subscript of the element in the array return t;}  

Sequential Table Insert Element

Insert data element, there are three cases:

    1. Insert in table header
    2. Inserts a position in the middle of a table
    3. Direct trailing order table, as the last element of the table


Regardless of where the data element is inserted in the sequential table, the solution is to locate the position to be inserted, move the subsequent data element back one position, and finally insert the data element directly in the vacated position.

Implementation code:

//Insert function, where elem is the inserted element, and add is the position inserted into the order tableTable addtable (Table T,intElemintadd) {//determine if there is a problem with the insert itself (if the insertion element position is larger than the length of the entire table +1 (if equal, is the case of trailing),//or the inserted location itself does not exist, the program as a hint and automatically exits)    if(add>t.length+1|| add<1)
{printf ("There is a problem with the insertion position"); returnT; }//when doing an insert operation, you first need to see if the order table has extra storage space provided to the inserted element, and if not, you need to apply    if(T.length = =t.size)
{T.head=(int*)realloc(T.head, (t.size+1)*sizeof(int)); if(!t.head)
{printf ("Storage Allocation Failure"); returnT; } t.size+=1; }//insert operation, you need to move the subsequent elements starting from the insertion position, one after the other    for(inti=t.length-1; i>=add-1; i--)
{T.head[i+1] =T.head[i]; }//After the move is complete, add the desired insertion element directly to the corresponding position in the order tablet.head[add-1] =Elem; //since the element was added, the length +1t.length++; returnt;}

Note: In this program, it is not optimal to use the REALLOC function to request 1 extra int storage space each time when there is not enough storage space for the array. The best way is to find that space is not enough, to apply for a few memory space, the advantage is: in the subsequent insert operation does not need to run the ReAlloc function every time, improve the efficiency of the program running.

Sequential Table Delete Element

When you delete an element in an array, you only need to move all the data elements in the position of the element forward one position at a time.

In the process of moving forward, the deleted element is overwritten by the latter element, and the delete operation is implemented indirectly.

Implementation code:

Table deltable (Table T,intadd)
{if(Add>t.length | | add<1)
{printf ("the location of the deleted element is incorrect"); Exit (0); }//Delete Operation    for(intI=add; i<t.length; i++)
{T.head[i-1]=T.head[i]; } t.length--; returnt;}

The Complete program
#include <stdio.h>#include<stdlib.h>#defineSize 4
typedefstructTable
{int*Head; intlength; intsize;} Table
Table Inittable ()
{table T; T.head=(int*)malloc(size*sizeof(int)); if(!t.head) {printf ("initialization failed"); Exit (0); } t.length=0; T.size=Size; returnT;}
Table addtable (Table T,intElemintadd) {if(add>t.length+1|| add<1)
{printf ("There is a problem with the insertion position"); returnT; }if(T.length >=t.size)
{T.head= (int*)realloc(T.head, (t.size+1)*sizeof(int)); if(!t.head)
{printf ("Storage Allocation Failure"); } t.size+=1; } for(inti=t.length-1; i>=add-1; i--)
{T.head[i+1]=T.head[i]; } T.head[add-1] =Elem; T.length++; returnT;}
Table deltable (Table T,intadd)
{if(Add>t.length | | add<1)
{printf ("the location of the deleted element is incorrect"); Exit (0); } for(intI=add; i<t.length; i++)
{T.head[i-1] =T.head[i]; } t.length--; returnT;}
intSelecttable (Table T,intElem
{ for(intI=0; i<t.length; i++)
{if(t.head[i]==elem)
{returni+1; }}return-1;}
Table amendtable (Table T,intElemintNewelem)
{intAdd =selecttable (t, Elem); T.head[add-1] =Newelem; returnT;}
voiddisplaytable (Table T)
{ for(intI=0; i<t.length; i++)
{printf ("%d", T.head[i]); } printf ("\ n");}
intMain ()
{Table T1=inittable (); for(intI=1; i<=size; i++)
{T1.head[i-1] =i; T1.length++; } printf ("Original order table: \ n");  Displaytable (t1); printf ("Delete element 1:\n"); T1=deltable (T1,1);  Displaytable (t1); printf ("insert element at 2nd position 5:\n"); T1= addtable (T1,5,2);  Displaytable (t1); printf ("find the location of element 3: \ n"); intAdd = selecttable (T1,3); printf ("%d\n", add); printf ("change element 3 to 6:\n"); T1= Amendtable (T1,3,6);  Displaytable (t1); return 0;}
Output: Original order table:1234Delete element 1:234Insert Element 5 in the 2nd position:2534find the location of element 3:3change element 3 to 6:2564
Advantages and disadvantages of sequential tables

The basis of the sequential table implementation, fully borrowing the data type of the array, the advantage is that when the data is traversed, the data is stored in a continuous physical space, the search speed is relatively fast.

However, due to the limitations of the array itself, when adding or deleting data elements in the sequential table, if there are a lot of data elements in subsequent operations, all subsequent data elements need to be moved forward, but the overall efficiency of the program is not high.

Sequential table (sequential storage structure of linear table) and C language implementation

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.