C语言实现vector结构
1. 使用说明
本例vector结构以单链表方式实现,结合了stack与queue结构:pop_front+push_front使用方式为stack;pop_front+push_back使用方式是queue。
首尾插入和顶部弹出是运行效率最高的,此外还实现了任意位置的插入、移除和访问功能。
带有返回值的函数:返回值如果是void*类型,则NULL代表执行失败;如果是int类型,则0为成功,-1为失败。
2. 代码实现
vector_c.h
1 #ifndef __VECTOR_C_H 2 #define __VECTOR_C_H 3 4 typedef 5 struct unidirectional_node 6 { 7 void* p_data; 8 struct unidirectional_node* p_next; 9 }STRUCT_UD_NODE, 10 * P_STRUCT_UD_NODE; 11 12 typedef 13 struct vector_c 14 { 15 unsigned int count; 16 P_STRUCT_UD_NODE p_first; 17 P_STRUCT_UD_NODE p_last; 18 }STRUCT_VECTOR_C, 19 * P_STRUCT_VECTOR_C; 20 21 P_STRUCT_VECTOR_C 22 f_vector_construct(void); 23 24 void 25 f_vector_push_back(P_STRUCT_VECTOR_C const p_vec, void* const p_data); 26 27 void 28 f_vector_push_front(P_STRUCT_VECTOR_C const p_vec, void* const p_data); 29 30 void* 31 f_vector_pop_front(P_STRUCT_VECTOR_C const p_vec); 32 33 int 34 f_vector_data_insert(P_STRUCT_VECTOR_C const p_vec, void* const p_data, const unsigned int index); 35 36 void* 37 f_vector_data_remove(P_STRUCT_VECTOR_C const p_vec, const unsigned int index); 38 39 void* 40 f_vector_data_access(P_STRUCT_VECTOR_C const p_vec, const unsigned int index); 41 42 int 43 f_vector_data_index(P_STRUCT_VECTOR_C const p_vec, void* const p_data, unsigned int* const p_index); 44 45 void 46 f_vector_destroy(P_STRUCT_VECTOR_C const p_vec); 47 48 #endif // !__VECTOR_C_H
vector_c.c
1 #include "vector_c.h" 2 #include <stdlib.h> 3 4 #pragma warning(disable:6011) 5 6 P_STRUCT_VECTOR_C f_vector_construct(void) 7 { 8 P_STRUCT_VECTOR_C p_ret_vec = (P_STRUCT_VECTOR_C)malloc(sizeof(STRUCT_VECTOR_C)); 9 p_ret_vec->count = 0; 10 p_ret_vec->p_first = NULL; 11 p_ret_vec->p_last = NULL; 12 return p_ret_vec; 13 } 14 15 void innf_add_first_node(P_STRUCT_VECTOR_C const p_vec, void* const p_data) 16 { 17 p_vec->p_first = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE)); 18 p_vec->p_first->p_data = p_data; 19 p_vec->p_first->p_next = NULL; 20 p_vec->p_last = p_vec->p_first; 21 p_vec->count = 1; 22 } 23 24 void* innf_remove_last_node(P_STRUCT_VECTOR_C const p_vec) 25 { 26 void* p_ret_data = p_vec->p_first->p_data; 27 free(p_vec->p_first); 28 p_vec->p_first = NULL; 29 p_vec->p_last = NULL; 30 p_vec->count = 0; 31 return p_ret_data; 32 } 33 34 void f_vector_push_back(P_STRUCT_VECTOR_C const p_vec, void* const p_data) 35 { 36 switch (p_vec->count) 37 { 38 case 0: 39 innf_add_first_node(p_vec, p_data); 40 return; 41 } 42 P_STRUCT_UD_NODE p_node = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE)); 43 p_node->p_data = p_data; 44 p_node->p_next = NULL; 45 p_vec->p_last->p_next = p_node; 46 p_vec->p_last = p_node; 47 p_vec->count++; 48 } 49 50 void f_vector_push_front(P_STRUCT_VECTOR_C const p_vec, void* const p_data) 51 { 52 switch (p_vec->count) { 53 case 0: 54 innf_add_first_node(p_vec, p_data); 55 return; 56 } 57 P_STRUCT_UD_NODE p_node = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE)); 58 p_node->p_data = p_data; 59 p_node->p_next = p_vec->p_first; 60 p_vec->p_first = p_node; 61 p_vec->count++; 62 } 63 64 void* f_vector_pop_front(P_STRUCT_VECTOR_C const p_vec) 65 { 66 switch (p_vec->count) { 67 case 0: 68 return NULL; 69 case 1: 70 return innf_remove_last_node(p_vec); 71 } 72 void* p_ret_data = p_vec->p_first->p_data; 73 P_STRUCT_UD_NODE p_node = p_vec->p_first->p_next; 74 free(p_vec->p_first); 75 p_vec->p_first = p_node; 76 p_vec->count--; 77 return p_ret_data; 78 } 79 80 int f_vector_data_insert(P_STRUCT_VECTOR_C const p_vec, void* const p_data, const unsigned int index) 81 { 82 switch (index) { 83 case 0: 84 f_vector_push_front(p_vec, p_data); 85 return 0; 86 } 87 if (index == p_vec->count) { 88 f_vector_push_back(p_vec, p_data); 89 return 0; 90 } 91 else if (index > p_vec->count) { 92 return -1; 93 } 94 P_STRUCT_UD_NODE p_node = p_vec->p_first; 95 for (unsigned int i = 0 ; i < index - 1; i++) { 96 p_node = p_node->p_next; 97 } 98 P_STRUCT_UD_NODE p_node_next = p_node->p_next; 99 p_node->p_next = (P_STRUCT_UD_NODE)malloc(sizeof(STRUCT_UD_NODE)); 100 p_node->p_next->p_data = p_data; 101 p_node->p_next->p_next = p_node_next; 102 p_vec->count++; 103 return 0; 104 } 105 106 void* f_vector_data_remove(P_STRUCT_VECTOR_C const p_vec, const unsigned int index) 107 { 108 switch (p_vec->count) { 109 case 0: 110 return NULL; 111 } 112 switch (index) { 113 case 0: 114 return f_vector_pop_front(p_vec); 115 } 116 if (index >= p_vec->count) { 117 return NULL; 118 } 119 P_STRUCT_UD_NODE p_node = p_vec->p_first; 120 for (unsigned int i = 0; i < index - 1; i++) { 121 p_node = p_node->p_next; 122 } 123 P_STRUCT_UD_NODE p_node_next = p_node->p_next->p_next; 124 void* p_ret_data = p_node->p_next->p_data; 125 free(p_node->p_next); 126 p_node->p_next = p_node_next; 127 p_vec->count--; 128 return p_ret_data; 129 } 130 131 void* f_vector_data_access(P_STRUCT_VECTOR_C const p_vec, const unsigned int index) 132 { 133 switch (p_vec->count) { 134 case 0: 135 return NULL; 136 } 137 if (index >= p_vec->count) { 138 return NULL; 139 } 140 P_STRUCT_UD_NODE p_node = p_vec->p_first; 141 for (unsigned int i = 0; i < index; i++) { 142 p_node = p_node->p_next; 143 } 144 return p_node->p_data; 145 } 146 147 int f_vector_data_index(P_STRUCT_VECTOR_C const p_vec, void* const p_data, unsigned int* const p_index) 148 { 149 P_STRUCT_UD_NODE p_node = p_vec->p_first; 150 for (unsigned int i = 0; i < p_vec->count; i++) { 151 if (p_node->p_data == p_data) { 152 *p_index = i; 153 return 0; 154 } 155 } 156 return -1; 157 } 158 159 void f_vector_destroy(P_STRUCT_VECTOR_C const p_vec) 160 { 161 if (p_vec == NULL) { 162 return; 163 } 164 while (p_vec->count != 0) { 165 f_vector_pop_front(p_vec); 166 } 167 free(p_vec); 168 }
标签:node,const,STRUCT,void,C语言,vector,vec,data,结构 From: https://www.cnblogs.com/eternalmoonbeam/p/16815954.html