\\头文件:sizeableArray.h
#ifndef _ARRAY_H_
#define _ARRAY_H_
typedef struct{
int *array;
int size;
}Array;
Array array_creat(int init_size);
void array_free(Array *a);
int array_size(const Array *a);
int* array_at(Array *a,int index);
void array_inflate(Array *a,int more_size);
#endif
\\源代码:sizeableArray.c
#include "sizeableArray.h"
Array array_create(int init_size){
Array a;
a.size=init_size;
a.array=(int*)malloc(sizeof(int)*init_size);
return a;
}
void array_free(Array *a)
{
free(a->array);
a->array=NULL;
a->size=0;
}
int array_size(Array *a)
{
return a->size;
}
int* array_at(Array *a,int index)
{
return *(a->array[index]);
}
void array_inflate(Array *a,int more_size)
{
int *p=(int*)malloc((a->size+more_size)*sizeof(int));
free(a->array);
int i;
for(i=0;i<a->size;i++){
p[i]=a->array[i];
}
a->array=p;
a->size+=more_size;
}
int main(int argc,char const *argv[])
{
Array a=array_create(100);
printf("array_size=%d\n",array_size(&a));
*array_at(&a,0)=10;
printf("array_at=%d\n",*array_at(&a,0));
array_free(&a);
}
可变数组存在的问题
链表
\\头文件:link.h
#ifndef _NODE_H_
#define _NODE_H_
typedef struct _node{
int value;
struct _node *next;
}Node;
#endif
\\源文件link.h
#include
#include "link.h"
int main(int argc,char const *argv[])
{
Node *head=NULL;
int number;
do{
scanf("%d",&number);
if(number!=-1){
Node *p=(Node*)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last=head;
if(last){
while(last->next){
last=last->next;
}
last->next=p;
}else{
head->next=p;
}
}
}while(number!=-1);
return 0;
}
#include
#include
#include "link.h"
typedef struct _list{
struct _node *head;
struct _node *tail;
}List;
int main(int argc,char const *argv[])
{
int number;
List list;
list.head=list.tail=NULL;
do{
scanf("%d",&number);
if(number!=-1){
add(&list,number);
}
}while(number!=-1);
}
void add(List *pList,int number)
{
Node *p=(Node*)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
if(pList->head){
pList->tail->next=p;
pList->tail=p;
}else{
pList->head->next=p;
pList->tail=p;
}
}
#include
#include
#include "link.h"
void print(List *pList); \\链表的遍历
void search(List *pList,int number); \\查找数据
int main(int argc,char const *argv[])
{
int number;
List list;
list.head=list.tail=NULL;
do{
scanf("%d",&number);
if(number!=-1){
add(&list,number);
}
}while(number!=-1);
print(&list);
scanf("%d",&number);
search(&list,number);
return 0;
}
void print(List *pList)
{
Node *p=NULL;
for(p=pList->head;p;p=p->next){
printf("%d\t",p->value);
}
printf("\n");
}
void search(List *pList,int number)
{
Node *p=pList->head;
int isFound=0;
for(;p;p=p->next){
if(p->value==number){
isFound=1;
break;
}
}
if(isFound){
printf("找到了\n");
}else{
printf("没找到\n");
}
}
#include
#include
#include "link.h"
void delete(List *pList,int number);
int main(int argc,char const *argv[])
{
int number;
List list;
list.head=list.tail=NULL;
do{
scanf("%d",&number);
if(number!=-1){
add(&list,number);
}
}while(number!=-1);
print(&list);
scanf("%d",&number);
delete(&list,number);
return 0;
}
void delete(List *pList,int number)
{
Node *q=NULL;
Node *p=pList->head;
for(;p;q=p,p=p->next){
if(p->value==number){
if(p==pList->tail){
pList->tail=q;
}
if(q){
q->next=p->next;
}else{
qList->head=p->next;
}
free(p);
free(q);
break;
}
}
return ;
}
#include
#include
#include "link.h"
int main(int argc,char const *argv[])
{
int number;
List list;
list.head=list.tail=NULL;
do{
scanf("%d",&number);
if(number!=-1){
add(&list,number);
}
}while(number!=-1);
free_list(&list);
return 0;
}
void free(List *pList)
{
Node *q;
Node *p=pList->head;
for(;p;p=q){
q=q->next;
free(p);
}
}