Define implementation definition structure definition action Create Order table initialization Order table Insert element Delete element destroy Order table
definition
A data structure is a collection of elements that have one or more specific relationships between each other. Depending on the different characteristics of the relationships between data elements, there are usually 4 basic structures: Collections: There is no relationship between the data elements in a structure except the relationship of "belonging to one set". such as: Generalized table . Linear structure: There is a relationship between the data elements in a structure. such as: linked list . Tree structure: There is a relationship between data elements in a structure that is multiple. such as: two fork Tree . Diagram (NET) structure: There are multiple pairs of relationships between data elements in a structure. such as: figure . 1
In the linear structure, according to the storage mode is divided into sequential table , linked list , according to the operating restrictions on the table, divided into stacks and queues .
A sequential table is characterized by a continuous storage unit in memory that can be simply understood as an array of sequential tables. The memory units occupied by the sequential table are dynamically allocated in the actual application according to the need. While the array is compiled, the specified size of the memory unit is allocated, so the following code snippet will make an error at compile time.
int len = ten;
Char Arr[len];
But the sequential table will have all the features of the data: it can be accessed directly from the subscript, inconvenient to insert and delete elements (because the following elements need to be moved). Implement Define Structure
typedef int SEQTYPE; Storage unit Type
typedef struct{
Seqtype *elem;//Storage space base address
int length;//Current length
int listsize;// The current allocated storage capacity (in sizeof (Elemtype))
} sqlist;
Structure body, there are three elements: storage space base address, similar to the first address of the array; current length, number of active storage units in the Record order table, current allocated storage capacity, and the maximum number of storage units in the sequential table. when all the storage units in the order table are already in use, new storage units will be required before the next insertion of the element. This is an attribute that the array does not have.
* Note: Define a storage cell type Seqtype to make the order table fit and more data types, modify the Seqtype type when used . Defining Actions Create order Table
/**
* Create order table
/sqlist createlist_sq () {
//sqlist list;
return list;
sqlist* list = (sqlist*) malloc (sizeof (sqlist));
return *list;
}
Here are two kinds of code to create a sequential table, one is the memory occupied by the system allocation list, one is dynamically allocated memory, and needs to manually free up memory space before the program runs. Initialization Order Table
/**
* Initialization Order Table
* Returns 1 for initialization success
* Returns 0 for initialization failure/
int initlist_sq (sqlist &l) {//only exists
in C + + for references L.elem = (Seqtype *) malloc (sizeof (seqtype) * list_init_size);
if (! L.elem)
return 0;//memory allocation failure, storage space is not enough
l.length = 0;//indicates that the order table is empty
l.listsize = list_init_size;//representation of the order sheet, maximum number of storage cells C10/>return 1;
}
Allocates the storage unit of the sequential table, initializing the value of the Order table property. inserting elements
/**
* Insert Order Table
* Subscript is a negative number inserted to the
end
/int insertlist_sq (sqlist &l, int index, Seqtype val) {
if (index) ; L.length) {///Storage The following table exceeds the actual length of the sequential table
printf ("The inserted subscript exceeds the actual length of the order table");
return 0;
}
if (Index < 0)//subscript is negative, insert to end
index = l.length;
if (l.length = = l.listsize) {//The storage unit of the sequential table is already filled
with printf ("The Order table's storage unit is full, the new storage unit continues to be allocated.") ");
seqtype* newbase = (seqtype*) realloc (L.elem,
(l.listsize + listincrement) * sizeof (SEQTYPE));//Continue allocating storage units
if ( !newbase) {
printf ("Allocate memory unit Failed");
return 0;
}
L.elem = newbase;
L.listsize + + listincrement;
}
Looking for the appropriate insertion position, the element behind the index moves backwards for
(int i = l.length i > index; i--) {
l.elem[i] = l.elem[i-1];//move back
}< C25/>l.elem[index] = val; Insert element
l.length++;
return 1;
}
Inserts an element into the specified location. Before inserting, you need to determine whether the order table is full, and then add the storage unit as needed, and finally insert the element.
/**
* Insert Order table (end position)
* With the above function is the duplicate function, this is called function overload, in C + + support
/int insertlist_sq (SqList &l, Seqtype val) { Return
insertlist_sq (L, L.length, Val);
}
* References and overloads are supported in C + +, and therefore need to be compiled in a CPP file. Delete Element
/**
* Delete specified element
* Return 0 The specified element could not be found and the deletion failed.
* Return 1 to find the element to be deleted, delete succeeded.
* *
int removelist_sq (sqlist &l, Seqtype val) {
int index =-1;//record matching subscript for
(int i = 0; i < L.length; i++) {
if (l.elem[i] = = val) {
//Find a matching Val, end loop
index = i;
break;
}
}
if (Index < 0) return
0;
for (; index < l.length-1; index++) {
L.elem[index] = l.elem[index + 1];
}
l.length--;
return 1;
}
To delete the specified element, you need to find the subscript first. Move the node after the subscript in turn to modify the length value.
/**
* Deletes the specified node according to the subscript and returns the value of the element
* Returns 0 subscript exceeds the order table length, deletion failed.
* Return 1 subscript Correct, delete element, and transfer deleted element value to Elem
*
/int removelist_sq (sqlist &l, int index, Seqtype &elem) {
if (Index >= l.length)//subscript exceeds the length of the order table return
0;
index = Index < 0? L.length:index; Subscript negative numbers indicate the deletion of the last node
elem = L.elem[index];
for (int i = index; i < l.length-1; i++) {
L.elem[i] = l.elem[i + 1];
}
l.length--;
return 1;
}
First, take the element that specifies the subscript, assign it to Elem, and then move the node after the subscript in turn. Finally, the length value is modified. Destroy Order table
/**
* Destroy sequence table */
void destorylist_sq (SqList &l) {
free (l.elem);//release storage space
l.length = 0;
l.listsize = 0;
Free (&l);
}
A storage unit that focuses on releasing sequential tables. If the order table itself is also dynamically allocated memory, it needs to be manually released.
Finally attached, the definition of the header file.
* * * sqlist.h * * * * * Created on:2016 August 30
* author:flueky/
#ifndef Sqlist_h_
#define SQLIST_H_
#define LIST_INIT_SIZE
#define LISTINCREMENT a
typedef int SEQTYPE ; Storage unit Type
typedef struct{
Seqtype *elem;//Storage space base address
int length;//Current length
int listsize;// The current allocated storage capacity (in sizeof (Elemtype))
} sqlist;
/**
* Create order Table
*
/SqList createlist_sq ();
/**
* Initialization sequence table *
/int initlist_sq (SqList &);
/**
* Insert Order Table
*
/int insertlist_sq (sqlist &,int index,seqtype);
/**
* Insert Order table (end position)/
int insertlist_sq (sqlist &,seqtype);
/**
* Removes the specified position element in the order table, subscript starting from 0
/int removelist_sq (sqlist &,int,seqtype &);
/**
* Delete the specified element in the Order
table
/int removelist_sq (sqlist &,seqtype);
/**
* Destruction Sequence Table
*
/void destorylist_sq (SqList &);
#endif/* Sqlist_h_ * *
Excerpted from the Min version of the data structure. ↩