数据结构(C语言)-线性表1
代码内实现两个功能
1.array_diff()实现对数组的元素去重。
2.sgn()函数实现符号区分。
#include <stdlib.h>
#include <stdio.h>
//定义一个顺序表
typedef int DataType;
#define ListSize 100
typedef struct
{
DataType items[ListSize];
int length;
} orderList;
//初始化顺序表,传入指针
int initList(orderList *L);
//求顺序表的元素个数即长度
int ListLength(orderList L);
//判断顺序表是否为空
int ListEmpty(orderList L);
//顺序表插入
int ListInsert(orderList *L, int pos, DataType item);
//顺序表删除元素
int ListDelete(orderList *L, int pos);
//顺序表指定元素位置查找
int Find(orderList L,DataType item);
//顺序表查找指定位置元素
int GetElem(orderList L, int pos, DataType *item);
//遍历顺序表
int TraverseList(orderList L);
//顺序表去重算法函数
int array_diff(orderList* L);
//顺序表分类函数
int sgn(orderList L);
//下面将顺序表的实现数据去重和数据分类放一起
int main()
{
//==========================================================================//
//下面利用顺序表实现数据的去重
int test1_data[15] = {1,1,1,2,3,5,5,8,13,13,21,34,34,11,100};//初始化数列
orderList L1; //创建顺序表L1
initList(&L1); //初始化顺序表
for (int k = 0; k < 15; k++)
{
//将数列中的元素插入到顺序表中,使用插入函数ListInsert
if (!ListInsert(&L1, k +1, test1_data[k]))
{
printf("插入失败\n");//如果插入失败则打印失败信息
return 0;
}
}
//使用遍历函数遍历顺序表
printf("原序列:");
TraverseList(L1);
//用array_diff去重函数处理顺序表,并遍历显示处理后的顺序表
array_diff(&L1);
printf("去重后的序列:");
TraverseList(L1);
//===========================================================================//
//下面使用分类函数对数据分类,将<0的数与>=0的数分开
int m;
int test2_data[17]={1,-2,2,3,-5,-8,0,3,21,34,-55,77,-20,8,-2,3,13}; //初始化数列
orderList L2; //创建顺序表L2
initList(&L2); //初始化顺序表
for (m = 0; m < 17; m++)
{
//将数列中的元素插入到顺序表中,使用插入函数ListInsert
if (!ListInsert(&L2, m +1, test2_data[m]))
{
printf("插入失败\n");//如果插入失败则打印失败信息
return 0;
}
}
//遍历显示处理前的数列
printf("\n原序列:");
TraverseList(L2);//使用遍历函数
//处理并直接显示数据
sgn(L2);
//==============================================================================//
return 0;
}
int initList(orderList *L)
{/* L为指向顺序表的指针*/
L->length=0;
return 1;
}
int ListLength(orderList L)
{/* L为顺序表*/
return L.length;
}
int ListEmpty(orderList L)
{/* L为顺序表*/
if(L.length<=0) return 1;
else return 0;
}
int ListInsert(orderList *L, int pos, DataType item)
{/* L为指向顺序表的指针,pos为插入位置,item为待插入数据元素*/
int i;
//printf("%d",L->length);
if(L->length>=ListSize)
{
printf("顺序表已满,无法进行插入操作!");
return 0;
}
if(pos<=0 || pos>L->length+1)
{
printf("插入位置不合法,取值范围应在[1,length+1]内");
return 0;
}
for(i=L->length-1; i>=pos-1; i--) /*移动数据元素*/
L->items[i+1] = L->items[i];
L->items[pos-1] = item; /*插入*/
L->length++; /*表长增1*/
return 1;
}
int ListDelete(orderList *L, int pos)
{/* L为指向顺序表的指针,pos为删除位置,item用于返回被删除的数据元素*/
int i;
if(ListEmpty(*L))
{
printf("顺序表为空表,无法进行删除操作!");
return 0;
}
if(pos<1 || pos>L->length+1)
{
printf("删除位置不合法,取值范围因在[1,length+1]内");
return 0;
}
//*item=L->items[pos-1];
for(i=pos; i<=L->length-1; i++) /*移动数据元素*/
L->items[i-1] = L->items[i];
L->length--; /*表长减1*/
return 1;
}
int Find(orderList L,DataType item)
{/* L为顺序表,item为待查找的数据*/
int pos = 0;
if(ListEmpty(L))
{
printf("顺序表为空表,无法进行查找操作!");
return 0;
}
while( pos<L.length && L.items[pos]!=item) pos++;
if( pos<L.length) return pos+1;
else return 0;
}
int GetElem(orderList L, int pos, DataType *item)
{/* L为顺序表,pos为删除位置,item用于返回被删除的数据元素*/
if(ListEmpty(L)) return 0;
if( pos<=0 || pos>L.length)
{
printf("输入位置信息不合法,请重新输入!");
return 0;
}
*item =L.items[pos-1];
return 1;
}
int TraverseList(orderList L)
{/* L为顺序表*/
int i;
//printf("%d",L.length);
for( i =0 ; i < L.length ; i++ ){
printf("%d ",L.items[i]);
}
printf("\n");
return 1;
}
//数据去重函数array_diff
int array_diff(orderList* L)
{
//使用两层循环判断数据是否重复
for(int i=0; i<L->length; i++)
{
//i从0开始,取顺序表第一个元素与其后面的元素比较
int j=0;
for(j=i+1; j<L->length; j++)
{
//j从i后第一个往后取值
if(L->items[i]==L->items[j])
{
//若此时有重复的值,顺序表删除掉j处元素,并重新从该位置继续检索
ListDelete(L, j);
j=j-1;
}
}
}
return 0;
}
//分类函数sgn()
int sgn(orderList L)
{
int i=0,j=1,k=1;
orderList neg, pos; //创建两个顺序表存放分类后的数据
initList(&neg); //存放<=0的数据
initList(&pos); //存放>0的数据
for(i=0; i<L.length; i++){
//顺序表L元素比较,并放入相应的顺序表
if(L.items[i]<0){
ListInsert(&neg,j,L.items[i]);
j++;
}
else{
ListInsert(&pos,k,L.items[i]);
k++;
}
}
//遍历输出<0的数
printf("小于0的序列:");
TraverseList(neg);
//遍历输出>=0的数
printf("大于等于0的序列:");
TraverseList(pos);
return 0;
}