自己写顺序线性链表~~出错咯~~
头文件:
程序代码:/*....This is a head files...*/
#include<stdlib.h>
#define ERROR 0
#define OverFlow -1
#define OK 1
#define INCREASE_ADDRESS_MIN 50
#define INCREASE_ADDRESS_MAX 100
typedef int status;//声明status为int型
int GetNumberofUnit(char *p);
char *CreateOrderList(char **p,int num);
void DealError();
status Deal_If_Char(char The_Value);
void InputData(char *p);
void OutPut(char *p);
status InsertValue(char The_value,int Insert_Positon,char *OriginAdress);
status DelData(char *First_Address,int position);
void All_View(char *First_Address);
int GetNumberofUnit(char *p)//计算顺序表单元个数
{
int i;
for(i=0;*(p+i)!=NULL;i++);
return i;
}
char* CreateOrderList(char **p,int num)//p用来操作指针变量,num为要创建的顺序表的单元个数
{
*p=(char*)malloc((num+1)*sizeof(char));//
if(!(*p))
{
DealError();
}
*(*p+num)=NULL;//结束标志
return (*p);
}
void DealError()//错误处理
{
printf("Error!");
exit(OverFlow);
}
status Deal_If_Char(char The_Value)//判断是否为字符
{
if((The_Value>65&&The_Value<90||The_Value>97&&The_Value<122))
return OK;
else
{
printf("Error!!!Not a char\n");
printf("Please Try Again!!\n");
return ERROR;
}
}
void InputData(char *p)//输入数据,传入一级指针即可
{
char *D_address;//动态变化地址
D_address=p;
printf("%d",GetNumberofUnit(p));//测试语句
while(D_address<=p+GetNumberofUnit(p))
{
scanf("%c",D_address);//D_address所指的内存单元地址
D_address++;
}
}
void OutPut(char *p)//输出函数
{
char *D_address;//动态地址
D_address=p;
printf("%d",GetNumberofUnit(p));//测试语句
while(D_address<=p+GetNumberofUnit(p))
{
printf("%c",*p);//取一级指针p所指内存单元的值
p++;//地址前进一个sizeof(char)大小
}
}
status InsertValue(char The_Value,int Insert_Position,char *OriginAdress)
{
int Current_Address,i;
char *new_address; char *Point_The_Value;
Current_Address=GetNumberofUnit(OriginAdress);//获取当前已分配内存单元
Deal_If_Char(The_Value);
if(Insert_Position>0&&Insert_Position<Current_Address)//判断插入位是否合法
{
new_address=(char*)malloc((INCREASE_ADDRESS_MIN+Current_Address)*sizeof(char));//分配一块更大的内存块
if(!new_address)
{//
DealError();
}//
Point_The_Value=new_address+Insert_Position-1;//计算插入位
*Point_The_Value=The_Value;//将值插入
for(i=0;i<Insert_Position-1;i++)//将插入位前段数据复制到新内存
*(new_address+i)=*(OriginAdress+i);
for(i=Insert_Position-1;i<Current_Address-Insert_Position+2;i++)//
*(new_address+i+1)=*(OriginAdress+i);//此时i为原始内存单元的末位
i=i+2; //末位为标志位
*(new_address+i)=NULL;
return OK;
}
return ERROR;
}
status DelData(char *First_Address,int position)
{
int i,end_address;char *p;
end_address=GetNumberofUnit(First_Address);
p=First_Address+position;//要删除位的后一位
if(position<0||position>end_address) return ERROR;
while(*p!=NULL)
{
*(p-1)=*p;
p++;
}
*(p-1)=*p;
return OK;
}
void All_View(char *First_Address)//遍历函数
{
printf("准备输出:");
while(*First_Address!=NULL)
{
printf("%c ",*First_Address);
First_Address++;
}
}主程序
程序代码:#include <stdio.h>
#include "OrderList.h"
void main()
{
char *T;
printf("Start Creating OrderList,");
T=CreateOrderList(&T,5);//创建顺序线性表.
printf("please type data\n");
InputData(T); //输入数据
printf("输出第一次输入的数据\n");
All_View(T);//遍历顺序线性表
} 测试时当测试输出总是3,不知CreateOrderList有什么错的~~~。。。
偶的思路是用CreateOrderList分配一块内存单元,num位这块内存单元中char类型元素的个数~~~
偶不才,那位大虾帮看看吧~~~~~









