Data structure (C language version)
introduction
1. In the process of computer operation, how to reasonably organize data and efficiently process data is data structure
2. Data structure includes two aspects: logical structure and storage structure of data
① Logical structure is to describe data from logical relationship. It usually has four categories: set, linear, tree and graph
② Storage structure is the storage representation of logical structure in computer. There are two types: sequential and chained
3. Abstract data type (ADT): provides the abstract description of type attributes and related operations. The following is the definition of the abstract data type of the linked list. After defining the abstract data type, you can develop and implement the interface
4. Algorithm is the operation method specified to solve a certain kind of problem
① The algorithm has five characteristics: finiteness, certainty, feasibility, input and output.
② The advantages and disadvantages of the algorithm should be evaluated from the following four aspects: correctness, readability, robustness and efficiency
5. The advantages and disadvantages of the algorithm are mainly time complexity and space complexity
Linked list
Establish abstraction
Type name: simple linked list Type attribute: can store a series of items Type operation: the initialization linked list is empty Confirm that the linked list is empty Make sure the linked list is full Determine the number of items in the linked list Add an item at the end of the linked list Traverse the linked list and process the items in the linked list Empty linked list
Establish interface
This linked list is mainly divided into two parts: representing the structure of data and the function of operating data
In the linked list, each link is called a node. Each node contains the information of the stored content and the pointer to the next node. First, the node is defined
struct LinkNode { void * data; struct LinkNode * next; };
The following defines the linked list structure, including node information and length information of the linked list
struct LList { //Head node struct LinkNode pHeader; //Linked list length int m_size; }; //Use typedef to define a null pointer as the return value of the linked list typedef void * LinkList;
Above, the attribute of the abstract data type is defined. Next, the operation method of the type is defined
//Initialize linked list LinkList init_LinkList() //Insert linked list void insert_LinkList(LinkList list, int pos, void * data) //Traversal linked list void foreach_LinkList(LinkList list, void(*myForeach)(void *)) //Delete linked list by location void removeByPos_LinkList(LinkList list, int pos)
Implementation interface
void init_LinkList() { struct LList * mylist = malloc(sizeof(strict LList)) if(mylist == NULL){return NULL;} mylist->pHeader.data = NULL; mylist->pHeader.next = NULL; mylist->m_size = 0; return mylist; } void insert_LinkList(LinkList list, int pos, void * data) { if(list == NULL){return;} if(data == NULL){return;} struct LList *mylist = list; if(pos<0 || pos>mylist->m_size){pos = mylist->m_size;} struct LinkNode * pCurrent = &mylist->pHeader; for(int i=0; i<pos; i++){pCurrent = pCurrent->next;} struct LinkNode * newNode = malloc(sizeof(struct LinkNode)); neNode->data = data; neNode->next = NULL; newNode->next = pCurrent->next; pCurrent->next = pCurrent; mylist->m_size++; } void foreach_LinkList(LinkList list, void(*myForeach)(void *)) { if (list ==NULL){return;} struct LList * mylist = list; struct LinkNode* pCurrent = mylist->pHeader.next; for (int i = 0; i < mylist->m_size;i++) { myForeach(pCurrent->data); pCurrent = pCurrent->next; } } void removeByPos_LinkList(LinkList list, int pos) { if ( list == NULL){return;} struct LList * mylist = list; if (pos < 0 || pos > mylist->m_size - 1){return;} struct LinkNode * pCurrent = &mylist->pHeader; for (int i = 0; i < pos;i++){pCurrent = pCurrent->next;} struct LinkNode * pDel = pCurrent->next; pCurrent->next = pDel->next; free(pDel); pDel = NULL; mylist->m_size--; }