c语言 线性表的生成代码

来源:互联网 发布:苹果越狱软件源大全 编辑:程序博客网 时间:2024/06/06 19:24

c文件:

#include <stdio.h>
typedef struct User_type
{
int row;
int col;
}User_type;
#include "linear.h"
int sort(User_type a,User_type b);
int sort(User_type a,User_type b)
{
int rule;
if(a.row>b.row) rule=1;
else if(a.row<b.row) rule=-1;
else rule=0;
return rule;
}
int main()
{
Linear *point1=NULL;
User_type data;
int (*sort1)();
int row;
int col;
initLinear(&point1,300);
printf("请输入坐标的值,以-1结束输入:\n");
while(point1->count<point1->Maxroom)
{
scanf("%d %d",&row,&col);
data.row=row;
data.col=col;
if(row==-1||col==-1)
break;
appendLinearElement(point1,data);
}
printf("您已结束输入坐标或者空间已满!\n");
showLinearElement(*point1);
printf("\n\n\n\n");
removeLinearElementAt(point1,0);
showLinearElement(*point1);
destroyLinear(&point1);
}


h文件

#ifndef _LINEAR_H_
#define _LINEAR_H_


#include <malloc.h>
#include <stdio.h>


typedef struct Linear
{
User_type *data;
unsigned int Maxroom;
unsigned int count;
}Linear;


typedef  unsigned int  Boolean;
#define  True   1
#define  False  0
//初始化 
Boolean initLinear(Linear **head,unsigned int Maxroom);
//销毁线性表 
Boolean destroyLinear(Linear **head);
//增加线性表中的元素 
Boolean appendLinearElement(Linear *head,User_type data);
//重置线性表 
Boolean resetLinear(Linear *head);
//在线性表中的指定位置插入元素 
Boolean insertLinearElementAt(Linear *head,int Index,User_type data);
//获得线性表中指定位置的元素
Boolean getLinearElementAt(Linear head,int Index,User_type *data); 
//输出线性表中的元素
Boolean showLinearElement(Linear head); 
//给线性表中的元素排序(由用户提供一个排序的函数)
Boolean sortLinearElement(Linear head,int (*sort)()); 
//删除指定位置的元素
Boolean removeLinearElementAt(Linear *head,int Index);
//获得线性表中元素的个数
Boolean getLinearElementCount(Linear head);
Boolean getLinearElementCount(Linear head)
{
return head.count;
}
Boolean removeLinearElementAt(Linear *head,int Index)
{
int i;
int k=Index;
Boolean ok=False;
if(Index<0||Index>=head->count)
{
printf("非法操作!\n");
ok=False;
}
else
{
for(i=Index+1;i<head->count;i++)
head->data[k++]=head->data[i];
head->count--;
ok=True;
}
return ok;
}
Boolean sortLinearElement(Linear head,int (*sort)())
{
int i,j;
int rule;
User_type middle_num;
Boolean ok=False;
if(head.count<=0)
{
printf("非法操作!");
ok=False;
}
else
{
ok=True;
for(i=0;i<head.count;i++)
for(j=0;j<head.count-i;j++)
{
rule=sort(head.data[j],head.data[j+1]);
if(rule==-1)
{
middle_num=head.data[j];
head.data[j]=head.data[j+1];
head.data[j+1]=middle_num;
}

}

}
}
Boolean showLinearElement(Linear head)
{
int i;
User_type data;
if(head.count<0)
printf("非法操作!");
else
{
for(i=0;i<head.count;i++)
{
getLinearElementAt(head,i,&data); 
printf("(%d,%d)\n",data.row,data.col);
}
}
}
Boolean getLinearElementAt(Linear head,int Index,User_type *value) 
{


*value=head.data[Index];

}
Boolean insertLinearElementAt(Linear *head,int Index,User_type data)
{
Boolean ok=False;
int i,c=head->count;
if(Index>head->count||Index<0||head->count>=head->Maxroom)
{
printf("非法操作!");
ok=False;
}
else
{
for(i=head->count-1;i>=Index;i--)
head->data[c--]=head->data[i];
head->data[c]=data;
ok=True;
head->count++; 
}
return ok;

}
Boolean resetLinear(Linear *head)
{
head->count=0;
}
Boolean appendLinearElement(Linear *head,User_type data)
{
Boolean ok=False;
if(head->count>=head->Maxroom)
{
printf("对不起,你所申请的空间不足!");
ok=False; 

else
{
head->data[head->count++]=data;

ok=True;
}
return ok;
}


Boolean destroyLinear(Linear **head)
{
Boolean ok=False;
free((*head)->data);
free(*head);
ok=True;
return ok;
}


Boolean initLinear(Linear **head,unsigned int Maxroom)
{
Boolean ok=False;
if(!(*head)&&Maxroom>0)
{
*head=(Linear *)(malloc(sizeof(Linear)));
if(*head)
{
(*head)->data=(User_type *)(calloc(sizeof(User_type),Maxroom));
if((*head)->data)
{
(*head)->Maxroom=Maxroom;
(*head)->count=0;
ok=True;
}
else 
{
printf("对不起,你所申请的空间不足!");
ok=False;
}
}

else 
{
printf("对不起,你所申请的空间不足!");
ok=False;
}

}
else 
{
printf("对不起,非法错误!");
ok=False;
}



return ok;
}
#endif 

1 0