DS-001 27 数据结构:顺序表及代码实现

时间:2019-02-14
本文章向大家介绍DS-001 27 数据结构:顺序表及代码实现,主要包括DS-001 27 数据结构:顺序表及代码实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Data Structure: Code Implementation of Sequential table

1 Construction of the sequential table

1.1 Construct a sequential table

In order to facilitate the later use of the data in the table, the sequential table needs to record the following two items:
(1) The storage capacity applied by the sequential table;
(2) The length of the sequential table (the number of elements stored in the table).

typedef struct Table{
    int * head;			// Declares an array of indeterminate length: head (a dynamic array)
    int length;			// Records the length of the current sequential table
    int size;			// Record the storage capacity allocated by the sequential table
}table;

1.2 Initialize the sequential table

#define Size 5 			// Macro definition of 'Size'(To represent the space request of the sequential table
table initTable(){
    table t;
    t.head=(int*)malloc(Size*sizeof(int));		// Construct an empty sequential table, dynamically apply for storage space
    if (!t.head) 								// If failed, prompt and quit
    {
        printf("Initialization failed");
        exit(0);
    }
    t.length=0;			// Initialize the length of sequential table
    t.size=Size;		// Initialize the size of storage space
    return t;
}

1.3 Output

Now let’s try to out put the sequential table.

void displayTable(table t){
    for (int i=0;i<t.length;i++) {
        printf("%d ",t.head[i]);
    }
    printf("\n");
}
int main(){
    table t=initTable();
    for (int i=1; i<=Size; i++) {			// Add elements to the sequential table 
        t.head[i-1]=i;
        t.length++;
    }
    printf("The elements stored in the sequential table are:\n");
    displayTable(t);
    return 0;
}

Output:

2 Basic operations of sequential table

2.1 Insert elements

// Insert function (elem: inserted element, add: the position of the inserted place)
table addTable(table t,int elem,int add)
{
    if (add>t.length+1||add<1) {		// If the position doesn't exist, quit
        printf("Wrong position!");
        return t;
    }
    if (t.length==t.size) {				// Determines whether the sequential table has excess storage space, if not, apply for it
        t.head=(int *)realloc(t.head, (t.size+1)*sizeof(int));
        if (!t.head) {
            printf("Storage allocation failed");		// If failed, quit
            return t;
        }
        t.size+=1;
    } 
    for (int i=t.length-1; i>=add-1; i--) {				// Insert the element(Move back the subsequent elements of the inserted position)
        t.head[i+1]=t.head[i];
    }
    t.head[add-1]=elem;									// Now insert the element
    t.length++;											// The length should add one
    return t;
}

2.2 Delete elements

table delTable(table t,int add){
    if (add>t.length || add<1) {
        printf("Wrong position!");
        exit(0);
    }
    for (int i=add; i<t.length; i++) {		// Delete operation
        t.head[i-1]=t.head[i];
    }
    t.length--;
    return t;
}

2.3 Find the element

//Lookup function (elem: the value of the target element)
int selectTable(table t,int elem){
    for (int i=0; i<t.length; i++) {
        if (t.head[i]==elem) {
            return i+1;
        }
    }
    return -1;								// If failed, return -1
}

2.4 Alter the element

table amendTable(table t,int elem,int newElem){		// elem->newElem
    int add=selectTable(t, elem);
    t.head[add-1]=newElem;							// Change the element
    return t;
}

2.5 Check the code

Let’s see how it works:

int main(){
    table t1=initTable();
    for (int i=1; i<=Size; i++) {
        t1.head[i-1]=i;
        t1.length++;
    }
    printf("Origial sequential table:\n");
    displayTable(t1);
  
    printf("Delete element 1:\n");
    t1=delTable(t1, 1);
    displayTable(t1);
  
    printf("Insert element 5 at position 2:\n");
    t1=addTable(t1, 5, 2);
    displayTable(t1);
  
    printf("Look for element 3:\n");
    int add=selectTable(t1, 3);
    printf("%d\n",add);
  
    printf("Change element 3 for 6:\n");
    t1=amendTable(t1, 3, 6);
    displayTable(t1);
    return 0;
}

Out put: