[Data Structure] Linear Table-Sequential Storage

Linear table

Linear tables are finite sequences of n data elements of the same data type. n can be 0. These data elements are logically linear in order, so they are called linear tables.

Storage structure of linear tables

  • Sequential Storage: Storage in Continuous Address Storage Space
  • Chain Storage: Storage in Storage Space with Addresses Not necessarily Continuous

Insertion, deletion and value-by-value search efficiency of sequential tables

Chain Storage Structure of Linear List

  • Single linked list: Each node in the table is divided into two parts, one part is its own data, the other part is pointer field pointing to the next node.
  • Double linked list: Each node in the table is divided into three parts, one part is its own data, one part points to the former node, and one part points to the latter node.
  • Loop list: The pointer field of the endpoint points points to the header node is not NULL.
  • Static Link List: Described by arrays, each node consists of two parts, one part is its own data, and the other part represents the array subscript of the next node (not a pointer).

 

Implementation of Sequential Storage Structure for Linear Tables

seqlist.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include<cstdio>
#include<malloc.h>
#include<assert.h>
#define SEQLIST_INIT_SIZE 8
#define INC_SIZE 3//The size of the spatial increment
typedef int ElemType;
typedef struct Seqlist {
    ElemType *base;
    int capacity; //Sequence table capacity
    int size; //Table size
}Seqlist;

bool Inc(Seqlist *list);//Increase the capacity of sequential tables
void InitSeqlist(Seqlist *list); //Initialization Sequence Table
void push_back(Seqlist *list, ElemType x); //Insert elements at the end of the sequence table
void push_front(Seqlist *list, ElemType x); //Insert elements at the head of the sequence table
void show_list(Seqlist *list); //Display the elements in the sequence table
void pop_back(Seqlist *list); //Delete the last element of the sequence table
void pop_front(Seqlist *list); //Delete the first element of the sequence table
void insert_pos(Seqlist *list, int pos, ElemType x);//Insert data on the selected location of the sequence table
int find(Seqlist *list, ElemType key); //Find the subscript of the element key in the sequence table
int length(Seqlist *list);//Find the Length of the Sequence Table
void delete_pos(Seqlist *list, int pos); //Delete data elements at specific locations in a sequence table
void delete_val(Seqlist *list, int key);//Delete data elements with key in the order table
void sort(Seqlist *list);//Bubble sort
void reverse(Seqlist *list);//Inverse Sequence List
void clear(Seqlist *list);//Clear all elements in the sequence table
void destroy(Seqlist *list);//Destruction Sequence List
void merge(Seqlist *lt, Seqlist *la, Seqlist *lb);//Merge two sequential lists

#endif //__SEQLIST_H__

seqlist.cpp

#include"seqlist.h"

bool Inc(Seqlist *list) {
    ElemType *newbase = (ElemType*)realloc(list, sizeof(ElemType)*(list->capacity + INC_SIZE));  //Reallocation of memory space
    if (newbase == NULL) {
        printf("Memory space is full, no more memory space can be allocated!\n");
        return false;
    }
    list->base = newbase;
    list->capacity += INC_SIZE;
    return true;
}

void InitSeqlist(Seqlist *list) {
    list->base = (ElemType*)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE);
    assert(list->base != NULL);
    list->capacity = SEQLIST_INIT_SIZE;
    list->size = 0;
}

void push_back(Seqlist *list, ElemType x) {
    if (list->size >= list->capacity && !Inc(list)) { //Inc(list) is used to determine whether increasing the capacity of sequential tables is successful, and only if it fails will it enter the if statement.
        printf("Sequence table capacity is full, can not continue to insert new elements at the end of the table!\n");
        return;
    }
    list->base[list->size] = x;
    list->size++;
}

void push_front(Seqlist *list, ElemType x) {
    if (list->size >= list->capacity && !Inc(list)) {
        printf("Sequence table capacity is full, you can no longer insert new elements in the header!\n");
        return;
    }
    for (int i = list->size;i > 0;i--) {
        list->base[i] = list->base[i - 1];
    }
    list->base[0] = x;
    list->size++;
}

void show_list(Seqlist *list) {
    for (int i = 0;i < list->size;i++) {
        printf("%d ", list->base[i]);
    }
    printf("\n");
}

void pop_back(Seqlist *list) {
    if (list->size == 0) {
        printf("Sequence table is empty, you can no longer delete elements at the end of the table!\n");
        return;
    }
    list->size--;
}

void pop_front(Seqlist *list) {
    if (list->size == 0) {
        printf("Sequence table is empty, you can't delete elements in the header anymore!\n");
        return;
    }
    for (int i = 0;i < list->size - 1;i++) {
        list->base[i] = list->base[i + 1];
    }
    list->size--;
}

void insert_pos(Seqlist *list, int pos, ElemType x) {
    if (pos<0 || pos>list->size) {
        printf("The insertion position is not valid and the element cannot be inserted!\n");
        return;
    }
    if (list->size >= list->capacity && !Inc(list)) {
        printf("Sequence table capacity is full, unable to insert new elements!\n");
        return;
    }
    for (int i = list->size;i > pos;i--) {
        list->base[i] = list->base[i - 1];
    }
    list->base[pos] = x;
    list->size++;
}

int find(Seqlist *list, ElemType key) {
    for (int i = 0;i < list->size;i++) {
        if (list->base[i] == key)
            return i;
    }
    return -1;
}

int length(Seqlist *list) {
    return list->size;
}

void delete_pos(Seqlist *list, int pos) {
    if (pos < 0 || pos >= list->size) {
        printf("The deletion location is illegal and the element cannot be deleted!\n");
        return;
    }
    for (int i = pos;i < list->size - 1;i++) {
        list->base[i] = list->base[i + 1];
    }
    list->size--;
}

void delete_val(Seqlist *list, int key) {
    int pos = find(list, key);
    if (pos == -1) {
        printf("There is no such element in the sequence table!\n");
        return;
    }
    delete_pos(list, pos);
}

void sort(Seqlist *list) {
    for (int i = 0;i < list->size - 1;i++) {//Number of sorted trips (for example, 5 data needs to be compared 4 times)
        for (int j = 0;j < list->size - 1 - i;j++) {//Number of comparisons in each comparison (for example, 5 data need to be compared 4 times in the 0th one)
            if (list->base[j] > list->base[j + 1]) {
                ElemType temp = list->base[j];
                list->base[j] = list->base[j + 1];
                list->base[j + 1] = temp;
            }
        }
    }
}

void reverse(Seqlist *list) {
    if (list->size == 0 || list->size == 1) return;
    int low = 0, high = list->size - 1;
    while (low < high) {
        ElemType temp = list->base[low];
        list->base[low] = list->base[high];
        list->base[high] = temp;
        low++;
        high--;
    }
}

void clear(Seqlist *list) {
    list->size = 0;
}

void destroy(Seqlist *list) {
    free(list->base);
    list->base = NULL;
    list->capacity = 0;
    list->size = 0;
}

void merge(Seqlist *lt, Seqlist *la, Seqlist *lb) {
    lt->capacity = la->size + lb->size;
    lt->base = (ElemType*)malloc(sizeof(ElemType)*lt->capacity);
    assert(lt->base != NULL);

    int ia = 0, ib = 0, ic = 0;
    while (ia < la->size&&ib < lb->size) {
        if (la->base[ia] < lb->base[ib]) {
            lt->base[ic++] = la->base[ia++];
        }
        else {
            lt->base[ic++] = lb->base[ib++];
        }
    }
    while (ia < la->size) {
        lt->base[ic++] = la->base[ia++];
    }
    while (ib < lb->size) {
        lt->base[ic++] = lb->base[ib++];
    }
    lt->size = la->size + lb->size;
    show_list(lt);
}

 

Posted by swjohnson on Fri, 04 Oct 2019 13:32:00 -0700