问题描述
我目前C.编写一个程序来实现的ArrayList(或动态数组)嗯......我想我已经70 - 用它做80%,但是,我发现我的code的严重问题一对夫妇的机器测试它们的时候。
简单地说,我插在一组字符串(字符*)的到我的ArrayList中,并试图获得和运营后夫妻显示它们。然而,这是我得到了什么:
检查:1
检查:2
检查:ܗ¿è¿
检查:EàEàHAÿE؉Ⱥ
检查:5
检查:6
不幸的是,我仍然无法找出其中的问题是在我的codeS,即使我已经审查我的codeS的两倍。
arraylist.h
的#ifndef _ARRAYLIST_H
#定义_ARRAYLIST_H#包括LT&;&stdio.h中GT;的typedef的char * VALUE_TYPE;结构数组列表{
INT大小;
VALUE_TYPE *数据;
};EXTERN无效arraylist_initial(结构的ArrayList *名单);
EXTERN INT arraylist_get_size(常量结构ArrayList的名单);
EXTERN VALUE_TYPE * arraylist_get_data_collection(const的结构ArrayList的名单);
EXTERN无效arraylist_set_data_collection(结构的ArrayList *名单,* VALUE_TYPE数据);
EXTERN无效arraylist_add(结构的ArrayList *名单,VALUE_TYPE值);
EXTERN VALUE_TYPE arraylist_get(常量数组列表结构清单,INT指数);
EXTERN INT arraylist_indexof(常量数组列表结构清单,VALUE_TYPE值);#万一
arraylist.c
的#includearraylist.h无效arraylist_initial(结构的ArrayList *名单){
列表 - >大小= 0;
列表 - >数据= NULL;
}INT arraylist_get_size(常量结构ArrayList的列表){
返回则为list.size;
}VALUE_TYPE * arraylist_get_data_collection(const的结构ArrayList的列表){
返回list.data;
}无效arraylist_set_data_collection(结构的ArrayList *名单,* VALUE_TYPE数据){
列表 - >数据=数据;
}无效arraylist_add(结构的ArrayList *名单,VALUE_TYPE值){
INT大小= arraylist_get_size(*清单);
VALUE_TYPE NEW_DATA [尺寸+ 1]; INT索引= 0;
对于(;指数=大小;!++指数){
NEW_DATA [指数] = arraylist_get(*名单,指数);
}
NEW_DATA [指数] =值; arraylist_set_data_collection(列表,NEW_DATA); ++列表 - >大小;
}VALUE_TYPE arraylist_get(常量数组列表结构清单,INT指数){
如果(指数< arraylist_get_size(列表)){
返回list.data [指数]
}
其他{
返回NULL;
}
}INT arraylist_indexof(常量数组列表结构清单,VALUE_TYPE值){
INT索引= 0;
对于(;指数= arraylist_get_size(名单)!++指数){
如果(STRCMP(list.data [指数]值)== 0){
返回指数;
}
} 返回-1;
}诠释主要(无效){
结构体数组列表清单; arraylist_initial(安培;清单); arraylist_add(安培;列表中,1);
arraylist_add(安培;列表中,2);
arraylist_add(安培;列表中,3);
arraylist_add(安培;列表中,4);
arraylist_add(安培;列表中,5);
arraylist_add(安培;列表中,6); INT索引= 0;
为(;!指数= 6 ++指数){
的printf(请检查:%s的\\ n,arraylist_get(列表指数));
} 返回0;
}
正如其他人所指出的,问题是在 arraylist_add()
功能,这需要动态分配记忆。这个问题实际上是非常适合的realloc()
,这将扩大动态分配的数组(这意味着你不必做复制循环):
无效arraylist_add(结构的ArrayList *名单,VALUE_TYPE值){
INT大小= arraylist_get_size(*清单);
VALUE_TYPE * NEW_DATA; NEW_DATA = realloc的(列表 - >数据(大小+ 1)* sizeof的NEW_DATA [0]); 如果(NEW_DATA)
{
NEW_DATA [大小] =值;
arraylist_set_data_collection(列表,NEW_DATA);
++列表 - >大小;
}
}
这甚至会在第一次分配工作,因为的realloc()
就像的malloc()
如果您通过它 NULL
。
PS:
若要使执行效率,不应该扩大通过每次一个条目阵列 - 代替,从条目的数量分别保持分配的块的数目的轨道
I am currently writing a program to implement an arraylist (or dynamic array) in C. Hmm... I think I have 70 - 80% done with it, however, I found a serious problem with my code when testing them on a couple of machines.
Briefly, I inserted a group of strings( char* ) into my arraylist, and tried to get and display them after couples of operations. However, this is what I got:
CHECK: 1
CHECK: 2
CHECK: ܗ¿èۗ¿
CHECK: EàEàHAÿE؉Ⱥ
CHECK: 5
CHECK: 6
Unfortunately, I still cannot figure out where the problem is in my codes, even though I have reviewed my codes twice.
arraylist.h
#ifndef _ARRAYLIST_H
#define _ARRAYLIST_H
#include <stdio.h>
typedef char* value_type;
struct arraylist {
int size;
value_type* data;
};
extern void arraylist_initial(struct arraylist *list);
extern int arraylist_get_size(const struct arraylist list);
extern value_type* arraylist_get_data_collection(const struct arraylist list);
extern void arraylist_set_data_collection(struct arraylist *list, value_type* data);
extern void arraylist_add(struct arraylist *list, value_type value);
extern value_type arraylist_get(const struct arraylist list, int index);
extern int arraylist_indexof(const struct arraylist list, value_type value);
#endif
arraylist.c
#include "arraylist.h"
void arraylist_initial(struct arraylist *list) {
list->size = 0;
list->data = NULL;
}
int arraylist_get_size(const struct arraylist list) {
return list.size;
}
value_type* arraylist_get_data_collection(const struct arraylist list) {
return list.data;
}
void arraylist_set_data_collection(struct arraylist *list, value_type* data) {
list->data = data;
}
void arraylist_add(struct arraylist *list, value_type value) {
int size = arraylist_get_size(*list);
value_type new_data[size + 1];
int index = 0;
for(; index != size; ++index) {
new_data[index] = arraylist_get(*list, index);
}
new_data[index] = value;
arraylist_set_data_collection(list, new_data);
++list->size;
}
value_type arraylist_get(const struct arraylist list, int index) {
if(index < arraylist_get_size(list)) {
return list.data[index];
}
else {
return NULL;
}
}
int arraylist_indexof(const struct arraylist list, value_type value) {
int index = 0;
for(; index != arraylist_get_size(list); ++index) {
if(strcmp(list.data[index], value) == 0) {
return index;
}
}
return -1;
}
int main(void){
struct arraylist list;
arraylist_initial(&list);
arraylist_add(&list, "1");
arraylist_add(&list, "2");
arraylist_add(&list, "3");
arraylist_add(&list, "4");
arraylist_add(&list, "5");
arraylist_add(&list, "6");
int index = 0;
for(; index != 6; ++index) {
printf("CHECK: %s\n", arraylist_get(list, index));
}
return 0;
}
As others have noted, the problem is in the arraylist_add()
function, which needs to dynamically allocate memory. This problem is actually perfectly suited for realloc()
, which will expand the dynamically allocated array (meaning you don't have to do the copying loop):
void arraylist_add(struct arraylist *list, value_type value) {
int size = arraylist_get_size(*list);
value_type *new_data;
new_data = realloc(list->data, (size + 1) * sizeof new_data[0]);
if (new_data)
{
new_data[size] = value;
arraylist_set_data_collection(list, new_data);
++list->size;
}
}
This will even work for the first allocation, since realloc()
works like malloc()
if you pass it a NULL
.
PS:
To make the implementation more efficient, you shouldn't expand the array by one entry each time - instead, keep track of the number of allocated blocks separately from the number of entries.
这篇关于ArrayList的用C不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!