Sequence table:
Creation of sequence table:
Define structure: the structure member contains an integer array and an integer variable that records the last data subscript:
#define DATASIZE 5 typedef struct { int data[DATASIZE]; int last; }sqlist_t;
Write a function to create a sequence table: return the first address of the sequence table
sqlist_t *sqlist_create(void) { sqlist_t *list = NULL; list = malloc(sizeof(sqlist_t)); if(list == NULL) return NULL; list->last = -1; return list; }
Write the function to destroy the sequence table: parameters: the first address of the sequence table
void sqlist_destroy(sqlist_t *list) { free(list); }
Addition, deletion, modification and query of sequence table:
Write data sequence table full function: used to judge whether the sequence table data is full when adding. Return value: true when full, false if none. Parameter: sequence table first address
int sqlist_isfull(sqlist_t *list) { if(list->last + 1 == DATASIZE) return 1; return 0; }
Write data addition function: including adding data and inserting data
Insert data function: return value: 0 for success, non-0 for failure, parameters: list: address at the beginning of the sequence table, data: data to be inserted, index: subscript position to be inserted.
int sqlist_insert(sqlist_t *list,int data,int index) { int i; if(sqlist_isfull(list)) return -1; if(index < 0 || index > list->last + 1) return -2; for(i = list->last+1;i > index; i--) { list->data[i] = list->data[i-1];//Insert data, move data back } list->data[index] = data; list->last++; return 0; }
Append data function: return value: 0 for success, non-0 for failure, parameters: list: the first address of the sequence table, data: the data to be appended.
Append is directly added at the end of the sequence table. You can directly write the code added at the end, or call the insertion function for secondary encapsulation and append at the end
int sqlist_append(sqlist_t *list,int data) { return sqlist_insert(list,data,list->last+1); /* if(sqlist_isfull(list)) return -1; list->last ++ list->data[list->last] = data; return 0; */ }
Write data traversal function: you can traverse all data
Traverse all data functions: parameters: list: the first address of the sequence table.
void sqlist_display(sqlist_t *list) { int i; for(i = 0; i <= list->last ; i++) { printf("%d\n",list->data[i]); } }
Write data deletion function: it can be deleted according to the corresponding subscript or the corresponding data.
Function deleted according to the subscript: return value: 0 for success, non-0 for failure, parameters: list: the first address of the sequence table, index: the subscript of the data to be deleted.
int sqlist_delete(sqlist_t *list,int index) { int i; if(index < 0 || index > list->last) return -1; for(i = index ; i < list->last; i++) { list->data[i] = list->data[i+1];//Delete data, move data forward } list->last--; return 0; }
Return the corresponding subscript according to the data search function: return value: the corresponding subscript of the data is returned successfully, and - 1 is returned in failure. Parameters: list: sequential table header address, data: the data to be searched
int sqlist_find(sqlist_t *list,int data) { int i,j; for(i = 0,j = list->last; i <= j; i++,j--)//Bidirectional traversal { if(list->data[i] == data) return i; if(list->data[j] == data) return j; } /* for(i = 0; i <= list->last ; i++)//Unidirectional traversal { if(list->data[i] == data) return i; } */ return -1; }
Function to delete according to data: return value: 0 for success, non-0 for failure, parameters: list: the first address of the sequence table, data: the data to be deleted.
int sqlist_delete_data(sqlist_t *list,int data) { int index; index = sqlist_find(list,data); if(index < 0) return -1; return sqlist_delete(list,index); }
Write modify data function: modify data according to the corresponding subscript or corresponding data.
Modify data according to the subscript function: return value: 0 for success, non-0 for failure, parameters: list: address at the beginning of the sequence table, index: subscript of the data to be modified, newData: new data.
int sqlist_update(sqlist_t *list,int index,int newData) { int i; if(index < 0 || index > list->last) return -1; list->data[index] = newData; return 0; }
Modify new data according to the original data function: return value: 0 for success, non-0 for failure, parameters: list: sequence table header address, oldData: original data, newData: new data.
int sqlist_update_data(sqlist_t *list,int oldData,int newData) { int index; index = sqlist_find(list,oldData); return sqlist_update(list,index,newData); }
Full code:
sqlist.c
#include <stdlib.h> #include <stdio.h> #include "sqlist.h" sqlist_t *sqlist_create(void) { sqlist_t *list = NULL; list = malloc(sizeof(sqlist_t)); if(list == NULL) return NULL; list->last = -1; return list; } int sqlist_append(sqlist_t *list,int data) { return sqlist_insert(list,data,list->last+1); /* if(sqlist_isfull(list)) return -1; list->last ++; list->data[list->last] = data; return 0; */ } int sqlist_isfull(sqlist_t *list) { if(list->last + 1 == DATASIZE) return 1; return 0; } int sqlist_insert(sqlist_t *list,int data,int index) { int i; if(sqlist_isfull(list)) return -1; if(index < 0 || index > list->last + 1) return -2; for(i = list->last+1;i > index; i--) { list->data[i] = list->data[i-1]; } list->data[index] = data; list->last++; return 0; } void sqlist_display(sqlist_t *list) { int i; for(i = 0; i <= list->last ; i++) { printf("%d\n",list->data[i]); } } void sqlist_destroy(sqlist_t *list) { free(list); } int sqlist_find(sqlist_t *list,int data) { int i,j; for(i = 0,j = list->last; i <= j; i++,j--) { if(list->data[i] == data) return i; if(list->data[j] == data) return j; } /* for(i = 0; i <= list->last ; i++) { if(list->data[i] == data) return i; } */ return -1; } int sqlist_delete(sqlist_t *list,int index) { int i; if(index < 0 || index > list->last) return -1; for(i = index ; i < list->last; i++) { list->data[i] = list->data[i+1]; } list->last --; return 0; } int sqlist_delete_data(sqlist_t *list,int data) { int index; index = sqlist_find(list,data); if(index < 0) return -1; return sqlist_delete(list,index); } int sqlist_update(sqlist_t *list,int index,int newData) { int i; if(index < 0 || index > list->last) return -1; list->data[index] = newData; return 0; } int sqlist_update_data(sqlist_t *list,int oldData,int newData) { int index; index = sqlist_find(list,oldData); return sqlist_update(list,index,newData); }
sqlist.h
#ifndef SQLIST_H__ #define SQLIST_H__ #define DATASIZE 5 typedef struct { int data[DATASIZE]; int last; }sqlist_t; sqlist_t *sqlist_create(void); int sqlist_append(sqlist_t *list,int data); int sqlist_insert(sqlist_t *list,int data,int index); void sqlist_display(sqlist_t *list); void sqlist_destroy(sqlist_t *list); int sqlist_isfull(sqlist_t *list); int sqlist_find(sqlist_t *list,int data); int sqlist_delete(sqlist_t *list,int index); int sqlist_delete_data(sqlist_t *list,int data); int sqlist_update(sqlist_t *list,int index,int newData); int sqlist_update_data(sqlist_t *list,int oldData,int newData); #endif
main.c
#include <stdio.h> #include "sqlist.h" int main(void) { sqlist_t *list = NULL; int index; //sqlist_t *list2 = NULL; //list2 = sqlist_create(); list = sqlist_create(); if(list == NULL){ printf("sqlist create is failed!\n"); return -1; } sqlist_append(list,10); sqlist_append(list,20); sqlist_append(list,30); sqlist_insert(list,40,3); sqlist_display(list); printf("--------------\n"); //index = sqlist_find(list,40); //index = sqlist_find(list,20); //sqlist_delete(list,index); sqlist_delete_data(list,20); sqlist_display(list); sqlist_destroy(list); /* printf("%d\n",sqlist_append(list,60)); sqlist_display(list); sqlist_append(list2,100); sqlist_append(list2,200); sqlist_append(list2,300); sqlist_display(list2); sqlist_destroy(list); sqlist_destroy(list2); */ return 0; }