数据结构基础篇-------1. 顺序表的创建及运算

  • /*
     * 顺序表
     * 2018.10.22
     * @L.F
     *
     * */
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MaxSize 1024 //线性表可能的最大结点点数
    typedef int datatype_t;//节点类型为datatype,datatype_t可为任意类型,这里是Int型,则结点所占空间大小位4个字节
    typedef struct 
    {
    	datatype_t data[MaxSize];//线性表的个结点存储在datatype_t类型的一维数组中,第一个结点是data[0],旗下表为0,位置为一
    	int last;//线性表中最后一个结点的下标值,即线性表的长度  n = last+1;
    }SequenList;
    int flags = 0;
    SequenList *SequenList_create()//创建一个空的顺序表
    {
    	SequenList *sl = (SequenList *)malloc(sizeof(SequenList));//开辟空间
    	sl->last = -1;//初始化结构体
    	return sl;
    }
    int SequenList_full(SequenList *sl)//判断顺序表是否为满
    {
    	return sl->last == MaxSize - 1 ? 1 : 0;
    }
    int SequenList_insert(SequenList *sl, datatype_t value)//顺序表插入数据
    {
    	if(SequenList_full(sl) == 1)//判断顺序表是否为满
    	{
    		printf("顺序表满了\n");
    		return -1;
    	}
    	sl->last++;
    	sl->data[sl->last] = value;
    	return 0;
    }
    int SequenList_insert_pos(SequenList *sl,int pos,datatype_t value)//按照位置插入数据(*sl 表示指向需要插入结点的顺序表的指针变量,pos顺序表插入位置变量, value插入结点的变量 )
    {
    	int i;//定义的循环辅助变量,由于在插入结点的运算中,必须移动一序列结点,i为要移动结点的下标值。
    	if(SequenList_full(sl) == 1)//插入运算的空间容错处理
    	{
    		printf("顺序表空间已满\n");
    		return -1;
    	}
    	else if(pos < 0 || pos > sl->last +1)//插入位置的容错处理
    	{
    		printf("插入位置非法\n");
    		return -1;
    	}
    	else 
    	{
    		for( i = sl->last; i >= pos;  i--)//从最后一个结点往后移,空出sl->data[i]的位置
    		{
    			sl->data[i+1] = sl->data[i];
    		}
    		sl->data[pos] = value;
    		sl->last++;
    	}
    	return 0;
    }
    datatype_t SequenList_empty(SequenList *sl)
    {
    	return sl->last == -1 ? 1 : 0;
    }
    datatype_t SequenList_delete(SequenList *sl)//删除顺序表数据
    {
    	if(SequenList_empty(sl) == 1)
    	{
    		printf("顺序表为空\n");
    		return 0;
    	}
    	datatype_t value;
    	value = sl->data[sl->last];
    	sl->last--;
    	return value;
    }
    datatype_t SequenList_selete_pos(SequenList *sl,int pos)//按照位置删除数据(*sl 表示指向需要删除结点的顺序表的指针变量,pos表示删除位置变量)
    {
    	int i;//定义移动结点的下表辅助变量
    	datatype_t value;
    	if(SequenList_empty(sl) == 1)
    	{
    		printf("顺序表为空\n");
    		return -1;
    	}
    	else if(pos < 0 || pos > sl->last)
    	{
    		printf("删除位置出错\n");
    		return -1;
    	}
    	else 
    	{
    		value = sl->data[pos];
    		for( i = pos; i<sl->last; i++)
    		{
    			sl->data[i] = sl->data[i + 1];
    		}
    		sl->last-- ;
    	}
    	return value;
    }
    int SequenList_updata_value(SequenList *sl, datatype_t old_value,datatype_t new_value)//用数据修改数据
    {
    	int i;
    	for(i = 0; i<=sl->last; i++)
    	{
    		if(sl->data[i] == old_value)
    		{
    			sl->data[i] = new_value;
    			flags = 1;
    		}
    	}
    	if(flags == 0)
    	{
    		printf("%d 是旧的数据\n", old_value);
    	}
    	return i;
    }
    int SequenList_updata_pos(SequenList *sl, int pos, datatype_t value)//根据位置修改数据
    {
    	if(pos < 0 ||pos > sl->last)
    	{
    		printf("位置出错\n");
    		return -1;
    	}
    	sl->data[pos] = value;
    	return 0;
    
    }
    int SequenList_search_value(SequenList *sl, datatype_t value)//按照数据查找位置
    {
    	int i;
    	for(i =0; i<= sl->last;i++)
    	{
    		if(sl->data[i] == value)
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    datatype_t SequenList_search_pos(SequenList *sl, int pos)//按照位置查找数据
    {
    	if(pos < 0 || pos >sl->last)
    	{
    		printf("位置出错\n");
    		return (datatype_t)-1;
    	}
    	return sl->data[pos];
    }
    void SequenList_repear(SequenList *sl)//删除重复的数据
    {
    	int i,j;
    	for(i =0; i<sl->last; i++)
    	{
    		for(j =i+1; j<=sl->last;j++)
    		{
    			if(sl->data[i] == sl->data[j])
    			{
    				SequenList_selete_pos(sl,j);
    				j--;
    			}
    		}
    	}
    	return ;
    }
    void SequenList_union(SequenList *sl, SequenList *s2)//合并表
    {
    	int i;
    	for(i = 0; i<=s2->last;i++)
    	{
    		if(SequenList_search_value(sl,s2->data[i]) == -1)
    		{
    			SequenList_insert(sl,s2->data[i]);
    		}
    	}
    	return ;
    }
    void SequenList_show(SequenList *sl ) //打印
    {
    	int i;
    	for(i = 0; i <= sl->last; i++)
    	{
    		printf("        %d", sl->data[i]);
    	}
    	putchar(10);
    	return ;
    }
    int main(int argc, const char *argv[])
    {
    	int a,n,i,F;
    	SequenList *s = SequenList_create();
    	while(1)
    	{
    		printf("请输入您的需求:1.依次插入数据        2.按位置插入数据    3. 按位置删除数据\n");
    		printf("                4.依据数据修改数据    5.按位置修改数据    6. 按数据查找位置\n");
    		printf("                7.按照位置查找数据    8.删除重复的数据    9. 合并表\n");
    		scanf("%d",&F);
    		switch(F)
    		{
    		case 1:
    			printf("请输入您要输入的数据总数:");
    			scanf("%d",&n);
    			for(i =0 ; i< n;i++)
    			{
    				printf("请输入要插入的数据:");
    				scanf("%d", &a);
    				SequenList_insert(s,a);
    			}
    			SequenList_show(s);
    			break;
    		case 2:
    			SequenList_show(s);
    			printf("请输入您要插入的位置:");
    			scanf("%d", &n);
    			printf("请输入您要插入的数据:");
    			scanf("%d", &a);
    			SequenList_insert_pos(s,n,a);
    			SequenList_show(s);
    			break;
    		case 3:
    			printf("请输入要删除数据的位置:");
    			scanf("%d", &n);
    			SequenList_selete_pos(s,n);
    			SequenList_show(s);
    			break;
    		case 4:
    			SequenList_show(s);
    			printf("请输入您要修改的数据:");
    			scanf("%d",&a);
    			printf("请输入您要修改成为的数据:");
    			scanf("%d",&n);
    			SequenList_updata_value(s,a,n);
    			SequenList_show(s);
    			break;
    		case 5:
    			SequenList_show(s);
    			printf("请输入您要修改的位置:");
    			scanf("%d", &n);
    			printf("请输入您要的数据:");
    			scanf("%d", &a);
    			SequenList_updata_pos(s,n,a);
    			SequenList_show(s);
    			break;
    		case 6:
    			SequenList_show(s);
    			printf("请输入您要查找位置的数据:");
    			scanf("%d", &a);
    			printf("%d\n",SequenList_search_value(s,a));
    			break;
    		case 7:
    			SequenList_show(s);
    			printf("请输入您要查找数据的位置:");
    			scanf("%d", &n);
    			printf("%d\n", SequenList_search_pos(s,n));
    			break;
    		case 8:
    			SequenList_show(s);
    			SequenList_repear(s);
    			SequenList_show(s);
    		case 9:
    			SequenList_show(s);
    			SequenList *s2 = SequenList_create();
    			printf("请输入您要输入的数据总数:");
    			scanf("%d",&n);
    			for(i =0 ; i< n;i++)
    			{
    				printf("请输入要插入的数据:");
    				scanf("%d", &a);
    				SequenList_insert(s,a);
    			}
    			SequenList_union(s,s2);
    			SequenList_show(s);
    		}
    	}
    	return 0;
    }
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值