Linear Table
linear structure : The most commonly used and simplest form of data structure, in which the elements are orderly and finite, and their basic features are:
- There is a unique data element called the first one .
- There is a unique data element called the last one
- In addition to the first element, each element has a single direct precursor
- In addition to the last element, each element has a single direct successor
2.1 Logical Structure of linear tables
definition of 2.1.1 Linear table
linear table (Linear list) : is a finite sequence of data elements (nodes) of the same data type as N. where n is an integer not less than 0. The number of data elements n is called the length of the linear table
Related Terms :
- When n = 0 o'clock, called empty table
- When n > 0 o'clock,
- A0 called the first node
- An-1 is called the tail node.
- All elements of the ordinal less than I are the precursors of Ai
Where Ai-1 is the direct precursor to Ai
- All elements of the ordinal greater than I are the successor of the Ai
Where ai+1 is the direct successor of Ai
logical structure of 2.1.2 Linear table
Types of nodes
- single-valued elements each element has only one data item (item)
- A record element contains multiple data items per element. This is called a domain where each data item is a node, where the domain group (one or more domains) that uniquely identifies each node is called the ' keyword '
2.2 Sequential storage of linear tables
sequential storage structure of 2.2.1 linear tables
Sequential storage : The nodes of a linear table are stored in a contiguous set of storage units in a sequence of logical structures . This is referred to as the sequential table
Features of the sequential table :
- The logical order of a linear table is consistent with the physical order
- The relationship between data elements is reflected in the ' physical location ' of the element within the computer.
A corresponding relational expression with a storage location similar to the following
LOC (Ai) =loc (Ai-1) +l , where L is the storage unit occupied by each element
LOC (AN) =loc (Ai) + (n-i) *l
2.2.2 Description of Sequential table order table and basic Operation
Basic Operation :
- Create
- Assign value
- Find
- Modify
- Insert
- Delete
- Request length
C + + language description and implementation of sequential tables
#include <stdio.h> //for printf #include <stdlib.h> //For Calloc,free #include <string.h> //For memmove #define Seqlist_default_size/// C + + language description for sequential tables///Description: T must support the composite type of the [] operation, or the base typeTemplate<TypeNameT>structseqlist { Public: Seqlist (intSize=seqlist_default_size) {init (SIZE); }voidDisplay () {getInfo (); for(intI=0; i<cursize;i++) {printf("%d elem is%d\n", I,data_[i]); } }intFind (T s) { for(intI=0; i<cursize;i++) {if(Data_[i] = = s) {returnI } }return-1; }//Print order table information voidGetInfo () {printf("seqlist[0x%x] capacity:%d, curlength:%d\n", data_,capacity,cursize); }/// Insert BOOLInsert (T Eleminsert,intidxinsert=-1) {ensuresize ();if(idxinsert>0){ for(intj=cursize-1; j>=idxinsert;j--) {data_[j+1]=DATA_[J]; } idxinsert--; }Else{idxinsert=cursize; } Data_[idxinsert]=eleminsert; cursize++;return true; }BOOLIsfull () {returnCursize+1>=capacity; }BOOLIsvalidindex (intIDX) {if(IDX <0|| IDX > Cursize) {return false; }return true; }intLength () {returnCursize; }Private: t* Data_;intCursize;intcapacity;//increase voidEnsuresize () {if(cursize<capacity) {return; }printf("[WARN] Order table full, auto-expand current size one times \ n"); capacity<<=1; t* nb= (t*)calloc(Capacity,sizeof(T));if(NB) {Memmove (nb,data_,cursize*sizeof(T)); Free(Data_); DATA_=NB; } }// Create intInitintNsize=seqlist_default_size) {data_= (t*)calloc(NSize,sizeof(T));if(NULL = = Data_) {return-1; } capacity=nsize; Cursize=0; GetInfo ();return 0; }};#ifndef DYLIBintMain () {seqlist<int> ISL;inttestarr[ One]={ +, A, the, +, -, $,142,143,144,145,146};//isl.init (); for(intI=0;i<sizeof(Testarr)/sizeof(testarr[0]); i++) {Isl.insert (testarr[i]); } isl.display ();//insert again for(i=0;i<sizeof(Testarr)/sizeof(testarr[0]); i++) {Isl.insert (testarr[i]); } isl.display ();return 0;}#endif
Data structure Notes (ii)--Sequential table