数据结构。顺序表
2015-12-01 18:35
525 查看
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef struct Point //element type
{
int x;
int y;
}Point;
typedef Point ElemType;
typedef struct //list type
{
ElemType *elem; //data
int length; //current length
int listsize; //maximum size
}Sqlist;
int cpy(ElemType *des,ElemType data)
{
des->x=data.x;
des->y=data.y;
return 0;
}
int elem_cmp(ElemType data1,ElemType data2);
int list_init(Sqlist *mylist);
int list_insert(Sqlist *mylist,ElemType data);
int list_find(Sqlist *mylist,ElemType data);
int list_delete(Sqlist *mylist,ElemType data);
int list_disp(Sqlist mylist);
int main()
{
Sqlist mylist;
list_init(&mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
printf("Hello world!\n");
Point tempt;
tempt.x=11;
tempt.y=12;
list_insert(&mylist,tempt);
list_disp(mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
getchar();
tempt.x=21;
tempt.y=22;
list_insert(&mylist,tempt);
list_disp(mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
getchar();
list_delete(&mylist,tempt);
list_disp(mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
getchar();
return 0;
}
int elem_cmp(ElemType data1,ElemType data2)
{
if( (data1.x==data2.x) &&(data1.y==data2.y) )
{
return 1;
}
else
{
return 0;
}
}
int list_init(Sqlist *mylist)
{
mylist->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(mylist->elem!=NULL)
{
mylist->length=0;
mylist->listsize=LIST_INIT_SIZE;
printf("%d,%d\n",mylist->length,mylist->listsize);
return 0;
}
else
{
printf("allocate memory is failed! sorry\n");
return 1;
}
}
int list_insert(Sqlist *mylist,ElemType data)
{
if(mylist->length>=mylist->listsize)
{
//if the memo is not enougth to use,realloc
mylist->elem=(ElemType*)realloc(mylist->elem,LIST_INIT_SIZE+LIST_INCREMENT);
if(mylist->elem==NULL)
{
printf("memo realloc is failed! sorry,friend\n");
return 1;
}
else
{
mylist->listsize+=LIST_INCREMENT;
}
}
cpy(&mylist->elem[mylist->length],data);
mylist->length+=1;
return 0;
}
int list_find(Sqlist *mylist,ElemType data)
{
int i=0;
for(i=0;i<mylist->length;i++)
{
if(elem_cmp(mylist->elem[i],data)>0)
{
return i+1;
}
}
return 0;
}
int list_delete(Sqlist *mylist,ElemType data)
{
int i=0,result=list_find(mylist,data);
if(result==0)
{
printf("not found the data \n");
return 1;
}
else
{
for(i=result-1;i<mylist->length;i++)
{
mylist->elem[i]=mylist->elem[i+1];
}
mylist->length--;
return 0;
}
}
int list_disp(Sqlist mylist)
{
system("clear");
int j=0;
for(j=0;j<mylist.length;j++)
{
printf("{%d,%d}\t",mylist.elem[j].x,mylist.elem[j].y);
}
return 0;
}
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef struct Point //element type
{
int x;
int y;
}Point;
typedef Point ElemType;
typedef struct //list type
{
ElemType *elem; //data
int length; //current length
int listsize; //maximum size
}Sqlist;
int cpy(ElemType *des,ElemType data)
{
des->x=data.x;
des->y=data.y;
return 0;
}
int elem_cmp(ElemType data1,ElemType data2);
int list_init(Sqlist *mylist);
int list_insert(Sqlist *mylist,ElemType data);
int list_find(Sqlist *mylist,ElemType data);
int list_delete(Sqlist *mylist,ElemType data);
int list_disp(Sqlist mylist);
int main()
{
Sqlist mylist;
list_init(&mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
printf("Hello world!\n");
Point tempt;
tempt.x=11;
tempt.y=12;
list_insert(&mylist,tempt);
list_disp(mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
getchar();
tempt.x=21;
tempt.y=22;
list_insert(&mylist,tempt);
list_disp(mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
getchar();
list_delete(&mylist,tempt);
list_disp(mylist);
printf("%d,%d\n",mylist.length,mylist.listsize);
getchar();
return 0;
}
int elem_cmp(ElemType data1,ElemType data2)
{
if( (data1.x==data2.x) &&(data1.y==data2.y) )
{
return 1;
}
else
{
return 0;
}
}
int list_init(Sqlist *mylist)
{
mylist->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(mylist->elem!=NULL)
{
mylist->length=0;
mylist->listsize=LIST_INIT_SIZE;
printf("%d,%d\n",mylist->length,mylist->listsize);
return 0;
}
else
{
printf("allocate memory is failed! sorry\n");
return 1;
}
}
int list_insert(Sqlist *mylist,ElemType data)
{
if(mylist->length>=mylist->listsize)
{
//if the memo is not enougth to use,realloc
mylist->elem=(ElemType*)realloc(mylist->elem,LIST_INIT_SIZE+LIST_INCREMENT);
if(mylist->elem==NULL)
{
printf("memo realloc is failed! sorry,friend\n");
return 1;
}
else
{
mylist->listsize+=LIST_INCREMENT;
}
}
cpy(&mylist->elem[mylist->length],data);
mylist->length+=1;
return 0;
}
int list_find(Sqlist *mylist,ElemType data)
{
int i=0;
for(i=0;i<mylist->length;i++)
{
if(elem_cmp(mylist->elem[i],data)>0)
{
return i+1;
}
}
return 0;
}
int list_delete(Sqlist *mylist,ElemType data)
{
int i=0,result=list_find(mylist,data);
if(result==0)
{
printf("not found the data \n");
return 1;
}
else
{
for(i=result-1;i<mylist->length;i++)
{
mylist->elem[i]=mylist->elem[i+1];
}
mylist->length--;
return 0;
}
}
int list_disp(Sqlist mylist)
{
system("clear");
int j=0;
for(j=0;j<mylist.length;j++)
{
printf("{%d,%d}\t",mylist.elem[j].x,mylist.elem[j].y);
}
return 0;
}
相关文章推荐
- 数据结构中的排序
- bzoj 2120 数颜色 树状数组套平衡树
- 最近邻算法和向量模型——第二部分——算法和数据结构
- 【数据结构】求最小生成树的权值之和——Prim算法
- 数据结构与算法-----单向线性链表(逆转和反向打印)
- 数据结构与算法-----堆栈-使用链表(链式结构)实现
- 网络设备驱动程序数据结构
- Linux USB驱动数据结构
- [数据结构]合并有序数组
- 数据结构实践—— 英文单词的基数排序
- 数据结构---线段树
- 数据结构实践——归并排序算法的改进
- 数据结构C++语言描述专题系列 (二) 栈
- 数据结构C++语言描述专题系列 (一) 绪论
- 【高级数据结构】[SPOJ QTREE]树链剖分/动态树各一模板
- 7.数据结构之哈希表
- 数据结构实践——大数据集上排序算法性能的体验
- [数据结构]折半搜索、归并排序( 分治思想)
- EKAlgorithms-常用数据结构Objective-C语言实现一数组排序
- 《大话数据结构》之二叉树的四种遍历