顺序表之不定长

顺序表之不定长----

顺序表:是在计算机内存中数组的形式保存的线性表,逻辑相邻,地址也相邻,可以通过下标任意访问。

分析:不定长顺序表,顾名思义就是你的顺序表的长度可以有由你控制,觉得不够你可以自己开辟;

            在定长的顺序表中,你的结构体中是只有两个变量,data值,p->length数组目前元素个数。

            在不定长的顺序表中,你的结构应该除了定长结构体的变量外,必须增加一个当前的总长度

           (p->total_length)。

代码:

自己创建的  DSEQ_LIST.h

#ifndef _DSEQ_LIST_H_  //防止重定义
#define _DSEQ_LIST_H_

#define ERROR -1
#define INIT_SIZE 10
typedef int elem_type;
typedef struct _DSEQ_LIST
{
	int length;
	int total_length;
	elem_type *data;
}DSEQ_LIST;


DSEQ_LIST *init_dseq_list(); //为顺序表开辟好空间
bool destory_dseq_list(DSEQ_LIST *p);//释放这个顺序表

bool insert(DSEQ_LIST *p, int pos, elem_type e);//指定位置插入


bool is_full(DSEQ_LIST *p);
bool is_empty(DSEQ_LIST *p);

bool show(DSEQ_LIST *p, void (*pfunc)(DSEQ_LIST *p,elem_type *));//打印这个顺序表

bool clear_dseq_list(DSEQ_LIST *p);//清空数据表

bool insert_head(DSEQ_LIST *p, elem_type e);//头插
bool insert_tail(DSEQ_LIST *p, elem_type e);//尾插


bool del_head(DSEQ_LIST *p, elem_type *e);//头删
bool del_tail(DSEQ_LIST *p, elem_type *e);//尾删
bool del(DSEQ_LIST *p,int pos, elem_type *e); //按位置删除
bool del_same(DSEQ_LIST *p,DSEQ_LIST *s);//删除p与s相同的元素

bool put(DSEQ_LIST *p, int pos, elem_type e);//在p->length范围内,放一个元素在指定位置
bool get(DSEQ_LIST *p, int pos, elem_type *e);//获取元素(按位置找值)




int get_pos_by_value(DSEQ_LIST *p,elem_type e,int n);//获取元素(根据值找下标)

bool sort(DSEQ_LIST *p);//冒泡排序

bool get_max(DSEQ_LIST *p, elem_type *e);
bool get_min(DSEQ_LIST *p, elem_type *e);


bool merge(DSEQ_LIST *p,DSEQ_LIST *s);//p和s合并后,按小到大存在p中

bool swap(DSEQ_LIST *p,DSEQ_LIST *s);//交换

#endif

实现:DSEQ_LIST.c

#include"DSEQ_LIST.h"
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdio.h>

DSEQ_LIST *init_dseq_list()//开辟结构体和数组
{
    DSEQ_LIST *p = (DSEQ_LIST *)malloc(sizeof(DSEQ_LIST));
	assert(p != NULL);
    p->data = (elem_type *)malloc(sizeof(elem_type) * INIT_SIZE);//INIT_SIZE== 10
	assert(p->data != NULL);
	p->length = 0;
	p->total_length = INIT_SIZE;
	return p;
}
bool destory_dseq_list(DSEQ_LIST *p)//销毁
{
	if(p == NULL)
	{
		return false;
	}
	free(p->data);
	free(p);
	return true;
}

static void inc(DSEQ_LIST *p)
{
	 p->data = (elem_type *)realloc(p->data,sizeof(elem_type) *(p->length) *2);
	 assert(p->data != NULL);
	 p->total_length = 2 * INIT_SIZE;
	 printf("申请空间成功!\n");
}
bool is_full(DSEQ_LIST *p) //满了
{
	return p->length == p->total_length;

}
bool is_empty(DSEQ_LIST *p)
{
	return p->length == 0;
}
bool insert(DSEQ_LIST *p, int pos, elem_type e)//在pos位置插入一个数
{
	int i = 0;
	if(p == NULL || pos < 0 || pos > p->length )
	{
		return false;
	}
	if(is_full(p))
	{
		inc(p);
	}
	for(i = p->length-1;i >= pos ; i --)
	{
		p->data[i+1] = p->data[i];
	}
	p->data[pos] = e ;
	p->length ++;
	return true;

}
bool show(DSEQ_LIST *p, void (*pfunc)(DSEQ_LIST *p,elem_type *))//打印一个结构体
{
	if(p == NULL)
	{
		return false;
	}
	pfunc(p,p->data);
	return true;
}
bool clear_dseq_list(DSEQ_LIST *p)//结构体清理
{
	if(p == NULL)
	{
		return false;
	}
	p->length = 0;
	return true;
}
bool insert_head(DSEQ_LIST *p, elem_type e)//头插
{
	int i = 0;
	if(p == NULL)
	{
		return false;
	}
	if(is_full(p))
	{
		inc(p);
	}
	for(i = p->length-1;i >= 0 ; i --)
	{
		p->data[i+1] = p->data[i];
	}
	p->data[0] = e;
	p->length ++;
	return true;
}
bool insert_tail(DSEQ_LIST *p, elem_type e)//尾插
{
	if(p == NULL)
	{
		return false;
	}
	if(is_full(p))
	{
		inc(p);
	}
	p->data[p->length] = e;
	p->length ++;
	return true;
}
bool del_head(DSEQ_LIST *p, elem_type *e)
{
	int i = 0;
	if(p == NULL)
	{
		return false;
	}
	*e = p->data[0];
	for(i = 0;i <p->length-1;i ++)
	{
         p->data[i] = p->data[i+1];
	}
	
	p->length --;
	return true;
}
bool del_tail(DSEQ_LIST *p, elem_type *e)
{
	if(p == NULL)
	{
		return false;
	}
	p->length --;
	return true;
}
bool del(DSEQ_LIST *p,int pos, elem_type *e)
{
	int i = 0;
	if(p == NULL|| pos < 0 || pos > p->length)
	{
		return false;
	}
	*e = p->data[pos];
	for(i = pos;i < p->length-1;i ++)
	{
		p->data[i] = p->data[i+1];
	}
	p->length --;
	return true;
}
bool put(DSEQ_LIST *p, int pos, elem_type e)
{
	if(p == NULL|| pos < 0 || pos >= p->length)
	{
		return false;
	}
	p->data[pos] = e;
	return true;
}
bool get(DSEQ_LIST *p, int pos, elem_type *e)
{
	if(p == NULL|| pos < 0 || pos > p->length)
	{
		return false;
	}
	
	*e = p->data[pos];
    return true;
}
int get_pos_by_value(DSEQ_LIST *p,elem_type e,int n)
{
	int i = 0;
	int flag = 0;
    if(p == NULL)
	{
		return false;
	}
	for(i = 0;i < p->length; i ++)
	{
		if(p->data[i] == e)
		{
			flag ++;
			if(flag == n)
			{
				return i;
			}
		}
	}
	return ERROR;

}
bool sort(DSEQ_LIST *p)
{
	int i = 0;
	int j = 0;
	elem_type tmp ;
	if(p == NULL)
	{
		return false;
	}
	for(i = 0;i < p->length-1;i ++)
	{
		for(j = 0;j < p->length-1-i;j ++)//tang
		{
			if(p->data[j] > p->data[j+1])
			{
                tmp = p->data[j];
				p->data[j] = p->data[j+1];
				p->data[j+1] = tmp;
			}
		}
	}
	return true;
}
bool get_max(DSEQ_LIST *p, elem_type *e)
{
	int i = 0;
	*e = p->data[0];
	if(p == NULL)
	{
		return false;
	}
	for(i = 1;i < p->length;i ++)
	{
		if(*e < p->data[i])
		{
			*e = p->data[i];
		}
	}
	return true;
	
}
bool get_min(DSEQ_LIST *p, elem_type *e)
{
	int i = 0;
	*e = p->data[0];
	if(p == NULL)
	{
		return false;
	}
	for(i = 1;i < p->length;i ++)
	{
		if(*e > p->data[i])
		{
			*e = p->data[i];
		}
	}
	return true;

}
bool merge(DSEQ_LIST *p,DSEQ_LIST *s)
{
	int i = 0;
	int j = 0;
	sort(s);
	sort(p);
    DSEQ_LIST *c = init_dseq_list();
	if(p == NULL || s == NULL)
	{
		return false;
	}
	if(c->total_length < (p->length + s->length))
	{
		int a = (p->length + s->length) / INIT_SIZE;
		for(i = 0;i < a; i ++)
		{
			inc(p);
			inc(c);
		}
	}
	i = 0;
	j = 0;
	for(;i < p->length ; i ++)
	{
		for(;j < s->length ;j ++)
		{
			if(p->data[i] < s->data[j])
			{
				insert_tail(c,p->data[i]);
				break;

			}
			else
			{
                insert_tail(c,s->data[j]);
			}
		}
	}
	if(i != p->length)
	{
		for(;i < p->length;i ++)
		{
             insert_tail(c,s->data[i]);
		}
	}
	if(j != s->length)
	{
		for(;j < s->length;j ++)
		{
			insert_tail(c,s->data[j]);
		}
	}
	memcpy(p->data , c->data,c->length * sizeof(elem_type));
	p->length = s->length + p->length;
	destory_dseq_list(c);
	return true;
	
}
bool swap(DSEQ_LIST *p,DSEQ_LIST *s)
{
	elem_type *tmp;
	int i = 0;
	int flag1 = p->length;
	int flag2 = s->length;
	if(p == NULL || s == NULL)
	{
		return false;
	}
	
	if(p->length < s->length)
	{
		if(p->total_length > s->length)
		{
			tmp = s->data;
			s->data = p->data;
			p->data = tmp;
			
		}
		else
		{
			for(i = 0;i < (int)((s->length - p->length)/INIT_SIZE); i++)
			{
				inc(s);
			}
			tmp = s->data;
			s->data = p->data;
			p->data = tmp;
		}
		s->length = flag1;
		p->length = flag2;
		 
	}
	else
	{
		if(s->total_length > p->length)
		{
			tmp = s->data;
			s->data = p->data;
			p->data = tmp;

		}
		else
		{
			for(i = 0;i < (int)((p->length - s->length)/INIT_SIZE); i++)
			{
				inc(s);
			}
			tmp = s->data;
			s->data = p->data;
			p->data = tmp;
		}
		s->length = flag1;
		p->length = flag2;

	}
	
	return true;
}
bool del_same(DSEQ_LIST *p,DSEQ_LIST *s)
{
	int i = 0;
	int j = 0;
	elem_type e;
	if(p == NULL || s == NULL)
	{
		return false;
	}
	for(i = 0;i < p->length;i ++)
	{
		for(j = 0;j < s->length;j ++)
		{
			if(p->data[i] == s->data[j])
			{
			     del(p,i,&e);
			}
		}
	}
	return true;
	
}

结束后在main.c引用就好了;

#include"DSEQ_LIST.h"
#include<vld.h>
#include<stdio.h>
void print_int (DSEQ_LIST *p,elem_type *e)
{
	int i = 0;
	for(i = 0;i < p->length;i ++)
	{
        printf("%d ", p->data[i]);
	}
	printf("\n");
	printf("************\n");
}
int main()
{
	DSEQ_LIST *s = init_dseq_list();
	DSEQ_LIST *p = init_dseq_list();
	insert_tail(s,3);
	insert(s,  2, 0);
    elem_type e;
	del_tail(s, &e);
    del_tail(s, &e);
    del(s,3,&e);
    del_head(s, &e);
	del_head(s, &e);
    bool m = is_empty(s);
    is_full(s);
	clear_dseq_list(s);
	insert(s, 2, 4);
	get(s,11,&e);
	get_max(s,&e);
	get_min(s,&e);
	int k = get_pos_by_value(s,7,3);
	sort(s);
	del_same(s,p);
	merge(p,s);
	swap(p,s);
	show(p,print_int);
	show(s,print_int);
	bool i = destory_dseq_list(s);
	bool j = destory_dseq_list(p);
	
	return 0;
}
就像这样子,你用那个函数,在main里面直接俄引用就好了!

简单的动态开辟内顺序表就结束了!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值