Algorithms and Data Structures python - sequence table (37)

 

1, describes the sequence table

  The simplest sequence table is of a linear structure, logically adjacent data storage location in the computer is adjacent the first few elements can quickly locate, allowed free intermediate, the insertion, need to move a large number of delete element. Sequence table can be assigned a contiguous storage space Maxsize with elem record base address, length records with the actual number of elements, i.e., the sequence length of the table, 

  1 shows the basic form of the sequence table, the data storage element itself continuously, the size of each element of the memory cell share the same fixed, the standard element of its logical address , the physical address of the storage element (actual memory address) via storage area calculated from the size of the product storage unit (c), the start address Loc (e0) plus the logical address (i-th element) , i.e.: Loc (element i) = Loc (e0) + C * i

Therefore, when accessing the specified element does not need to traverse from scratch, can be obtained by calculating the address corresponding to its time complexity is O (1).

  If the size of the element is not uniform, shall be in the form of an external element of FIG. 2, the actual data storage element separately, sequentially in the respective cell location table holding address information (i.e., links) of the corresponding element. Since the same amount of storage required for each link, the above equation can be calculated from the storage position of the element links, and then follow the link to find data elements that are actually stored. Note that, in FIG. 2 c is no longer a size of the data elements, but the amount of memory required to store a link address, the amount is generally small.

FIG 2 in that order on the index table is also referred to the actual data, which is the simplest index structure.

2, structure of sequence table 

  

  A sequence of complete information table comprises two parts, a table set of elements, the other part for proper operation and to be recorded information, i.e., information about the overall situation on a table, which is part of the information including the capacity element storage area and number two elements already in the current table.

3, two kinds of basic implementation of the sequence table

  1为一体式结构,存储表信息的单元与元素存储区以连续的方式安排在一块存储区里,两部分数据的整体形成一个完整的顺序表对象。一体式结构整体性强,易于管理。但是由于数据元素存储区域是表对象的一部分,顺序表创建后,元素存储区就固定了。

  2为分离式结构,表对象里只保存与整个表有关的信息(即容量和元素个数),实际数据元素存放在另一个独立的元素存储区里,通过链接与基本表对象关联。

 4、元素存储区替换

  一体式结构由于顺序表信息区与数据区连续存储在一起,所以若想更换数据区,则只能整体搬迁,即整个顺序表对象(指存储顺序表的结构信息的区域)改变了。分离式结构若想更换数据区,只需将表信息区中的数据区链接地址更新即可,而该顺序表对象不变。

5、元素存储区扩充

  采用分离式结构的顺序表,若将数据区更换为存储空间更大的区域,则可以在不改变表对象的前提下对其数据存储区进行了扩充,所有使用这个表的地方都不必修改。只要程序的运行环境(计算机系统)还有空闲存储,这种表结构就不会因为满了而导致操作无法进行。人们把采用这种技术实现的顺序表称为动态顺序表,因为其容量可以在使用中动态变化。

扩充的两种策略

  • 每次扩充增加固定数目的存储位置,如每次扩充增加10个元素位置,这种策略可称为线性增长。

    特点:节省空间,但是扩充操作频繁,操作次数多。

  • 每次扩充容量加倍,如每次扩充增加一倍存储空间。

    特点:减少了扩充操作的执行次数,但可能会浪费空间资源。以空间换时间,推荐的方式。

6、顺序表的增删改查操作的Python代码实现

# 创建顺序表
class Sequence_Table():
    
    # 初始化
    def __init__(self):
        self.date = [None]*100
        self.length = 0
    
    # 判断是否已经满了
    def isFull(self):
        if self.length>100:
            print("该顺序表已满,无法添加元素")
            return 1
        else:
            return 0
    
    # 按下表索引查找
    def selectByIndex(self,index):
        if index>=0 and index<=self.length-1:
            return self.date[index]
        else:
            print("你输入的下标不对,请重新输入\n")
            return 0
        
    # 按元素查下标
    def selectByNum(self,num):
        isContain = 0
        for i in range(0,self.length):
            if self.date[i] == num:
                isContain = 1
                print("你要查找的元素下标是%d\n"%i)
        if isContain == 0:
            print("没有找你你要的数据")
    
    # 追加数据
    def addNum(self,num):
        if self.isFull() == 0:
            self.date[self.length] = num
            self.length += 1
            
    # 打印顺序表
    def printAllNum(self):
        for i in range(self.length):
            print("a[%s]=%s"%(i,self.date[i]),end=" ")
        print("\n")
        
    # 按下标插入数据
    def insertNumByIndex(self,num,index):
        if index<0 or index>self.length:
            return 0
        self.length += 1
        for i in range(self.length-1,index,-1):
            temp = self.date[i]
            self.date[i] = self.date[i-1]
            self.date[i-1] = temp
        self.date[index] = num
        return 1
    # 按下标删除数据
    def delectNumByIndex(self,index):
        if self.length <= 0:
            print("该顺序表内没有数据,不用删除")
            
        for i in range(index,self.length-1):
            temp = self.date[i]
            self.date[i] = self.date[i + 1]
            self.date[i + 1] = temp
        self.date[self.length-1] = 0
        self.length -= 1

def main():
    # 创建顺序表对象
    seq_t = Sequence_Table()
    
    # 插入三个元素
    seq_t.addNum(1)
    seq_t.addNum(2)
    seq_t.addNum(3)
    
    # 打印验证
    seq_t.printAllNum()
    
    # 按照索引查找
    num = seq_t.selectByIndex(2)
    print("你要查找的数据是%d\n" % num)
    
    # 按照索引插入数据
    seq_t.insertNumByIndex(4, 1)
    seq_t.printAllNum()
    
    # 按照数字查下标
    seq_t.selectByNum(4)
    
    #删除数据
    seq_t.delectNumByIndex(1)
    seq_t.printAllNum()
     
if __name__ == "__main__":
    main()

运行结果为:

a[0]=1 a[1]=2 a[2]=3 

你要查找的数据是3

a[0]=1 a[1]=4 a[2]=2 a[3]=3 

你要查找的元素下标是1

a[0]=1 a[1]=2 a[2]=3 

7、顺序表的增删改查操作的C语言代码实现

#include<stdio.h>
// 1、定义顺序表的储存结构
typedef struct
{
    //用数组存储线性表中的元素
    int data[100];
    // 顺序表中的元素个数
    int length;
}Sequence_table,*p_Sequence_table;

// 2、顺序表的初始化,
void initSequenceTable(p_Sequence_table T)
{
    // 判断传过来的表是否为空,为空直接退出
    if (T == NULL)
    {
        return;
    }
    // 设置默认长度为0
    T->length = 0;
}

// 3、求顺序表的长度
int lengthOfSequenceTable(p_Sequence_table T)
{
    if (T==NULL)
    {
        return 0;
    }
    return T->length;
}

// 4、判断顺序表是否已满
int isFull(p_Sequence_table T)
{
    if (T->length>=100)
    {
        printf("该顺序表已经装满,无法再添加元素");
        return 1;
    }
    return 0;
}

// 5、按序号查找
int selectSequenceTableByIndex(p_Sequence_table T,int index)
{
    if (index>=0&&index<=T->length-1)
    {
        return T->data[index];
    }
    printf("你输入的序号不对,请重新输入\n");
    return 0;
}

// 6、按内容查找是否存在
void selectSequenceTableByNum(p_Sequence_table T,int num)
{
    int isContain = 0;
    for (int i=0; i<T->length; i++)
    {
        if (T->data[i] == num)
        {
            isContain = 1;
            printf("你要找的元素的下标是:%d\n",i);
        }
    }
    if (isContain == 0)
    {
        printf("没有找到你要的数据\n");
    }
}

// 7、添加元素(在队尾添加)
void addNumber(p_Sequence_table T,int num)
{
    // 顺序表还没有满的时候
    if (isFull(T) == 0)
    {
        T->data[T->length] = num;
        T->length++;
    }
}

// 8、顺序表的遍历
void printAllNumOfSequenceTable(p_Sequence_table T)
{
    for (int i = 0; i<T->length; i++)
    {
        printf("T[%d]=%d ",i,T->data[i]);
    }
    printf("\n");
}

//9、插入操作
int insertNumByIndex(p_Sequence_table T, int num,int index)
{
    if (index<0||index>T->length)
    {
        return 0;
    }
    T->length++;
    for (int i = T->length-1; i>index; i--)
    {
        int temp = T->data[i];
        T->data[i] = T->data[i-1];
        T->data[i-1] = temp;
    }
    T->data[index] = num;
    return 1;
}

// 10、删除元素
void delectNum(p_Sequence_table T,int index)
{
    if (T->length <= 0)
    {
        printf("该顺序表中没有数据,不用删除");
    }
    for (int i = index;i<T->length-1; i++)
    {
        int temp = T->data[i];
        T->data[i] = T->data[i+1];
        T->data[i+1] = temp;
    }
    T->data[T->length-1] = 0;
    T->length--;
}



int main(int argc, const char * argv[]) {
    
    // 创建顺序表的结构体
    Sequence_table seq_t;
    // 初始化
    initSequenceTable(&seq_t);
    // 添加数据
    addNumber(&seq_t, 1);
    addNumber(&seq_t, 2);
    addNumber(&seq_t, 3);
    // 打印验证
    printAllNumOfSequenceTable(&seq_t);
    // 根据索引下标查内容
    int num = selectSequenceTableByIndex(&seq_t, 2);
    printf("你查的数据是:%d\n",num);
    // 插入
    insertNumByIndex(&seq_t, 4, 1);
    printAllNumOfSequenceTable(&seq_t);
    // 根据内容查下标
    selectSequenceTableByNum(&seq_t, 4);
    // 根据下标删除数据
    delectNum(&seq_t, 1);
    printAllNumOfSequenceTable(&seq_t);
    return 0;
}

运行结果为:

T[0]=1 T[1]=2 T[2]=3 
你查的数据是:3
T[0]=1 T[1]=4 T[2]=2 T[3]=3 
你要找的元素的下标是:1
T[0]=1 T[1]=2 T[2]=3 

 

Guess you like

Origin www.cnblogs.com/Se7eN-HOU/p/11086749.html