- What is linear structure?
Linear structure is a finite sequence of n data elements with the same characteristics, also known as linear table. - What is included in the linear structure?
Linear structure is a kind of data structure widely used in practice, common linear structure: sequence list, linked list, stack, queue, string - What is a sequence table? Classification of sequence table?
Sequential table is a linear structure in which data elements are sequentially stored in a continuous storage unit with a physical address. Generally, array storage is used. Add, delete, check and modify the data on the array - Dynamic sequence table
Functions to be realized by sequence table:
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <malloc.h> typedef int DataType; typedef struct Seqlist { int* arr; int capacity; int size; }Seqlist,*PSeq; typedef struct Seqlist Seqlist; typedef struct Seqlist* PSeq; #define SIZE 5 // Initialization of sequence table void SeqListInit(PSeq ps, int capacity); // Insert an element with a value of data at the end of the order table void SeqListPushBack(PSeq ps, DataType data); // Delete last element of order table void SeqListPopBack(PSeq ps); // Insert an element with a value of data in the header of the order table void SeqListPushFront(PSeq ps, DataType data); // Delete elements in order table header void SeqListPopFront(PSeq ps); // Insert the element with the value of data in the pos position of the order table void SeqListInsert(PSeq ps, int pos, DataType data); // Delete elements on pos position in order table void SeqListErase(PSeq ps, int pos); // Find the element whose value is data in the sequence table, find the subscript that returns the element in the sequence table, otherwise return - 1 int SeqListFind(PSeq ps, DataType data); // Check whether the sequence table is empty. If it is empty, non-zero value will be returned. If it is not empty, 0 will be returned int SeqListEmpty(PSeq ps); // Returns the number of valid elements in the order table int SeqListSize(PSeq ps); // Return the capacity size of the order table int SeqListCapacity(PSeq ps); // Empty elements in the order table void SeqListClear(PSeq ps); // Delete the first data element in the order table void SeqListRemove(PSeq ps, DataType data); // Destruction sequence table void SeqListDestroy(PSeq ps); // Expansion of sequence table void CheckCapacity(PSeq ps);
Program code:
#include "sqelist.h" void SeqlistPrint(PSeq ps) { assert(ps); for (int i = 0; i < ps->size; ++i) { printf("%d", ps->arr[i]); } printf("\n"); } void SeqlistInit(PSeq ps, int capacity) { ps->arr = (int*)malloc(capacity * sizeof(int)); if (ps == NULL) { assert(0); return; } ps->capacity = capacity; ps->size = 0; } void SeqlistPushBack(PSeq ps,DataType data) { assert(ps); if (ps->size == ps->capacity) { CheckCapacity(ps); } ps->arr[ps->size] = data; ++ps->size; } void SeqlistPopBack(PSeq ps) { assert(ps); if (SeqlistEmpty(ps)) { return; } --ps->size; } void SeqlistPushFront(PSeq ps, DataType data) { assert(ps); if (ps->size == ps->capacity) { CheckCapacity(ps); } for (int i = ps->size; i > 0; --i) { ps->arr[i] = ps->arr[i - 1]; } ps->arr[0] = data; ++ps->size; } void SeqlistPopFront(PSeq ps) { assert(ps); if (SeqlistEmpty(ps)) { return; } for (int i = 0; i < ps->size; ++i) { ps->arr[i] = ps->arr[i + 1]; } --ps->size; } void SeqlistInsert(PSeq ps, int pos, DataType data) { assert(ps); if (pos < 0 || pos > ps->size) { return; } if (ps->size == ps->capacity) { CheckCapacity(ps); } for (int i = ps->size; i > pos; --i) { ps->arr[i] = ps->arr[i - 1]; } ps->arr[pos] = data; ++ps->size; } void SeqlistErase(PSeq ps, int pos) { assert(ps); if (pos > ps->size||pos < 0) { return; } for (int i = pos; i < ps->size - 1; ++i) { ps->arr[i] = ps->arr[i + 1]; } --ps->size; } int SeqlistFind(PSeq ps, DataType data) { assert(ps); for (int i = 0; i < ps->size; ++i) { if (ps->arr[i] == data) { return i; } } return -1; } int SeqlistEmpty(PSeq ps) { assert(ps); if (ps->size) { return 0; } else { return -1; } } int SeqlistSize(PSeq ps) { assert(ps); return ps->size; } int SeqlistCapacity(PSeq ps) { assert(ps); return ps->capacity; } void SeqlistRemov(PSeq ps, DataType data) { assert(ps); SeqlistErase(ps,SeqlistFind(ps,data)); } void SeqlistClear(PSeq ps) { assert(ps); ps->size = 0; } void SeqListDestroy(PSeq* ps) { assert(*ps); free((*ps)->arr); (*ps)->arr = NULL; (*ps)->capacity = 0; (*ps)->size = 0; } void CheckCapacity(PSeq ps) { assert(ps); ps->arr = (int*)realloc(ps->arr, (ps->capacity + SIZE)*sizeof(int)); if (ps == NULL) { assert(0); return; } ps->capacity += SIZE; } int main() { Seqlist seqlist; PSeq ps = &seqlist; SeqlistInit(ps, SIZE); system("pause"); return 0; }