顺序表的操作集合
2016-09-19 23:16
176 查看
包括对线性表的插入,删除,销毁,排序,交换等等操作
#define MULTIPLE 2
#define ERROR -1
typedef int elem_type;
#define INIT_SIZE 10
typedef struct _DSEQ_LIST
{
elem_type *data;
int length;
int total_length;
}Dlist;
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"list_des.h"
Dlist *init_dse_list()
{
Dlist *phead = (Dlist *)malloc(sizeof(Dlist));
elem_type *p = (elem_type *)malloc(sizeof(elem_type)*INIT_SIZE);
assert(phead != NULL && p != NULL);
phead->data = p;
phead->length = 0;
phead->total_length = INIT_SIZE;
return phead;
}
bool destory_dse_list(Dlist *p)
{
if (p == NULL) //研究将其写成宏函数形式
{
return false;
}
free(p->data);
free(p);
return true;
}
//其存在的意义,1,调用函数增加开销,2,直接通过结构体指针就能直接找到
//对不属于自己的进行操作使得程序崩溃
//保护数据,防止对其意外修改
bool clear_seq_list(Dlist *p)
{
if (p == NULL)
{
return false;
}
for(int i=0; i<get_length(p); i++)
{
*(p->data+i) = 0;
}
p->length = 0;
return true;
}
bool insert(Dlist *p, int pos, elem_type e)
{
if (p == NULL || pos>get_length(p) || !is_full(p))
{
return false;
}
for (int i=get_length(p)+1; i>=pos; i--) //向后挪动
{
*(p->data + i+1) = *(p->data + i);
}
*(p->data+pos) = e;
p->length++;
return true;
}
bool insert_head_majia(Dlist *p, elem_type e)
{
return insert(p,0,e);
}
bool insert_tail_majia(Dlist *p, elem_type e)
{
return insert(p,get_length(p),e);
}
bool insert_tail(Dlist *p, elem_type e)
{
if(p == NULL)
{
return false;
}
if (false == is_full(p))
{
return false;
}
*(p->data + p->length) = e;
p->length++;
return true;
}
bool insert_head(Dlist *p, elem_type e)
{
if(p == NULL)
{
return false;
}
if (false == is_full(p))
{
return false;
}
if (p->length != 0)
{
for(int i=p->length; i>0; i--)
{
*(p->data+i) = *(p->data+i-1);
}
}
*(p->data) = e;
p->length++;
return true;
}
bool is_full(Dlist *p)
{
if (p == NULL)
{
return false;
}
if (get_length(p) == p->total_length) //从零开始,第九个的时候已经满了,p->length = 10;
{
//elem_type *data void *realloc( void *memblock, size_t size );
//p->data = (elem_type *)realloc(p->data, (p->total_length) * MULTIPLE);
p->data = (elem_type *) realloc(p->data, (p->total_length) * MULTIPLE*sizeof(elem_type));
assert(p->data != NULL);
p->total_length = MULTIPLE * (p->total_length);
}
return true;
}
bool is_empty(Dlist *p)
{
if (!(p != NULL && p->length == 0)) //
{
return false;
}
return true;
}
bool show(Dlist *p, void (*pfunc)(elem_type *))
{
if (p == NULL)
{
return false;
}
//此处的p已经经过检查,可以直接采用p->length形式,这样写避免函数的使用的代价
for(int i=0; i<get_length(p); i++)
{
pfunc(p->data+i);
}
return true;
}
bool get_pos_by_value(Dlist *p, elem_type e,int *n)
{
while (p == NULL)
{
return false;
}
int i = 0;
while (i < p->length)
{
if (*(p->data+i)==e)
{
*n = i;
return true;
}
i++;
}
return false;
}
bool search_yes_or_no(Dlist *p, elem_type e)
{
int n=0;
bool x =get_pos_by_value(p,e,&n);
if (!x)
{
return false;
}
return true;
}
bool sort(Dlist *p)
{
if (p == NULL)
{
return false;
}
for(int i=0; i<p->length; i++)
{
for(int j=0; j<p->length-i-1; j++)
{
if (*(p->data+j) > *(p->data+j+1))
{
elem_type temp = 0;
temp = *(p->data+j);
*(p->data+j) = *(p->data+j+1);
*(p->data+j+1) =temp;
}
}
}
return true;
}
bool get_max(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
int max = *p->data;
for(int i=1; i<get_length(p); i++)
{
if (*(p->data+i)>max)
{
max = *(p->data+i);
}
}
*e = max;
return true;
}
bool get_min(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
int min = *p->data;
for(int i=1; i<get_length(p); i++)
{
if (*(p->data+i)<min)
{
min = *(p->data+i);
}
}
*e = min;
return true;
}
int get_length(Dlist *p)
{
if (p == NULL)
{
return ERROR;
}
return p->length;
}
bool del_head_majia(Dlist *p, elem_type *e)
{
return del(p, 0, e);
}
bool del_tail_majia(Dlist *p, elem_type *e)
{
return del(p, get_length(p), e);
}
bool del_head(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
*e = *p->data;
for (int i=0; i<get_length(p); i++)
{
*(p->data+i) = *(p->data+i+1);
}
p->length--;
return true;
}
bool del_tail(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
*e = *(p->data+get_length(p)-1);
p->length--;
return true;
}
bool del(Dlist *p,int pos, elem_type *e)
{
if (p == NULL) //get_length中包含对p为空的判断
{
return false;
}
if (pos > get_length(p))
{
return false;
}
*e = *(p->data+pos);
for(int i=pos; i<get_length(p); i++)
{
*(p->data+i) = *(p->data+i+1);
}
p->length--;
return true;
}
bool put(Dlist *p, int pos, elem_type e)
{
if (!(p != NULL && pos < get_length(p)))
{
return false;
}
*(p->data + pos)=e; //让其下标皆从零开始
return true;
}
bool get(Dlist *p, int pos, elem_type *e)
{
if (!(p != NULL && pos < get_length(p)))
{
return false;
}
*e = *(p->data+pos);
return true;
}
bool meger(Dlist *p, Dlist *s)//p=p+s;
{
if (p == NULL || s == NULL)
{
return false;
}
elem_type temp;//如果当其为整型进行初始化,那如果是结构体就用不了
for (int i=0; i<get_length(s); i++)
{
bool bo = get(s, i, &temp);
if (bo == false)
{
return false;
}
insert_tail(p, temp);
}
return false;
}
bool swap(Dlist *p, Dlist *s)
{
if (p == NULL || s == NULL)
{
return false;
}
int i = 0;
int x = get_length(p);
int y = get_length(s);
int plength = x;
int slength = y;
while(x>0 && y>0)
{
is_full(s);
is_full(p);
elem_type temp1;
elem_type temp2;
bool s1 = get(p, i, &temp1);
bool s2 = get(s, i, &temp2);
if (s1 == false || s2 ==false)
{
return false;
}
//如果将两个判断合并起来,则在拿出放进之后,如果得到的就是错误的数据,已经放进去再退出这个函数,对数据有污染
bool s3 = put(s, i, temp1);
bool s4 = put(p, i, temp2);
if (s3 == false || s4 == false)
{
return false;
}
x--;
y--;
i++;
}
elem_type temp;
if (x == 0)
{
while (y-- != 0)
{
get(s, i, &temp);
insert(p,i,temp);
i++;
}
}
else
{
while (x-- != 0)
{
get(p, i, &temp);
insert(s, i, temp);
i++;
}
}
p->length = slength;
s->length = plength;
return true;
}
bool merger_by_order(Dlist *p, Dlist *s)//p=p+s
{
if (p == NULL || s == NULL)
{
return false;
}
bool s1 = sort(p);//不好,改变了原来数据
bool s2 = sort(s);
if (!s || !s2)
{
return false;
}
elem_type temp1; // p m
elem_type temp2; // s n
int m = 0;
int n = 0;
int plength = get_length(p);
int slength = get_length(s);
elem_type pp = *(p->data+plength-1);
elem_type ss = *(s->data+slength-1);
int tmp = get_length(p) < get_length(s) ? get_length(p) : get_length(s); //得出较小值
get(p, m, &temp1);
get(s, n, &temp2);
for (int i=0; pp != temp1 && ss != temp2 ; i++)
{
bool s1 = get(p, m, &temp1);
bool s2 = get(s, n, &temp2);
if (!s1 || !s2)
{
return false;
}
//p m 9 s n 23
if(temp1 < temp2)
{
m++;
}
else
{
insert(p, m, temp2);
n++;
}
}
if(plength == tmp)
{
while(tmp < slength)
{
get(s, n, &temp1);
insert_tail(p,temp1);
n++;
m++;
tmp++;
}
}
return false;
}
bool same_data(Dlist *p, Dlist *s, Dlist *m)//p与s中相同的取出放到m线性表中去
{
if (p == NULL || s == NULL || m == NULL)
{
return false;
}
for (int i=0; i<get_length(p); i++)
{
if (search_yes_or_no(s, *(p->data+i)))
{
insert_tail(m,*(p->data+i));
}
}
return true;
}
#define MULTIPLE 2
#define ERROR -1
typedef int elem_type;
#define INIT_SIZE 10
typedef struct _DSEQ_LIST
{
elem_type *data;
int length;
int total_length;
}Dlist;
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"list_des.h"
Dlist *init_dse_list()
{
Dlist *phead = (Dlist *)malloc(sizeof(Dlist));
elem_type *p = (elem_type *)malloc(sizeof(elem_type)*INIT_SIZE);
assert(phead != NULL && p != NULL);
phead->data = p;
phead->length = 0;
phead->total_length = INIT_SIZE;
return phead;
}
bool destory_dse_list(Dlist *p)
{
if (p == NULL) //研究将其写成宏函数形式
{
return false;
}
free(p->data);
free(p);
return true;
}
//其存在的意义,1,调用函数增加开销,2,直接通过结构体指针就能直接找到
//对不属于自己的进行操作使得程序崩溃
//保护数据,防止对其意外修改
bool clear_seq_list(Dlist *p)
{
if (p == NULL)
{
return false;
}
for(int i=0; i<get_length(p); i++)
{
*(p->data+i) = 0;
}
p->length = 0;
return true;
}
bool insert(Dlist *p, int pos, elem_type e)
{
if (p == NULL || pos>get_length(p) || !is_full(p))
{
return false;
}
for (int i=get_length(p)+1; i>=pos; i--) //向后挪动
{
*(p->data + i+1) = *(p->data + i);
}
*(p->data+pos) = e;
p->length++;
return true;
}
bool insert_head_majia(Dlist *p, elem_type e)
{
return insert(p,0,e);
}
bool insert_tail_majia(Dlist *p, elem_type e)
{
return insert(p,get_length(p),e);
}
bool insert_tail(Dlist *p, elem_type e)
{
if(p == NULL)
{
return false;
}
if (false == is_full(p))
{
return false;
}
*(p->data + p->length) = e;
p->length++;
return true;
}
bool insert_head(Dlist *p, elem_type e)
{
if(p == NULL)
{
return false;
}
if (false == is_full(p))
{
return false;
}
if (p->length != 0)
{
for(int i=p->length; i>0; i--)
{
*(p->data+i) = *(p->data+i-1);
}
}
*(p->data) = e;
p->length++;
return true;
}
bool is_full(Dlist *p)
{
if (p == NULL)
{
return false;
}
if (get_length(p) == p->total_length) //从零开始,第九个的时候已经满了,p->length = 10;
{
//elem_type *data void *realloc( void *memblock, size_t size );
//p->data = (elem_type *)realloc(p->data, (p->total_length) * MULTIPLE);
p->data = (elem_type *) realloc(p->data, (p->total_length) * MULTIPLE*sizeof(elem_type));
assert(p->data != NULL);
p->total_length = MULTIPLE * (p->total_length);
}
return true;
}
bool is_empty(Dlist *p)
{
if (!(p != NULL && p->length == 0)) //
{
return false;
}
return true;
}
bool show(Dlist *p, void (*pfunc)(elem_type *))
{
if (p == NULL)
{
return false;
}
//此处的p已经经过检查,可以直接采用p->length形式,这样写避免函数的使用的代价
for(int i=0; i<get_length(p); i++)
{
pfunc(p->data+i);
}
return true;
}
bool get_pos_by_value(Dlist *p, elem_type e,int *n)
{
while (p == NULL)
{
return false;
}
int i = 0;
while (i < p->length)
{
if (*(p->data+i)==e)
{
*n = i;
return true;
}
i++;
}
return false;
}
bool search_yes_or_no(Dlist *p, elem_type e)
{
int n=0;
bool x =get_pos_by_value(p,e,&n);
if (!x)
{
return false;
}
return true;
}
bool sort(Dlist *p)
{
if (p == NULL)
{
return false;
}
for(int i=0; i<p->length; i++)
{
for(int j=0; j<p->length-i-1; j++)
{
if (*(p->data+j) > *(p->data+j+1))
{
elem_type temp = 0;
temp = *(p->data+j);
*(p->data+j) = *(p->data+j+1);
*(p->data+j+1) =temp;
}
}
}
return true;
}
bool get_max(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
int max = *p->data;
for(int i=1; i<get_length(p); i++)
{
if (*(p->data+i)>max)
{
max = *(p->data+i);
}
}
*e = max;
return true;
}
bool get_min(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
int min = *p->data;
for(int i=1; i<get_length(p); i++)
{
if (*(p->data+i)<min)
{
min = *(p->data+i);
}
}
*e = min;
return true;
}
int get_length(Dlist *p)
{
if (p == NULL)
{
return ERROR;
}
return p->length;
}
bool del_head_majia(Dlist *p, elem_type *e)
{
return del(p, 0, e);
}
bool del_tail_majia(Dlist *p, elem_type *e)
{
return del(p, get_length(p), e);
}
bool del_head(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
*e = *p->data;
for (int i=0; i<get_length(p); i++)
{
*(p->data+i) = *(p->data+i+1);
}
p->length--;
return true;
}
bool del_tail(Dlist *p, elem_type *e)
{
if (p == NULL)
{
return false;
}
*e = *(p->data+get_length(p)-1);
p->length--;
return true;
}
bool del(Dlist *p,int pos, elem_type *e)
{
if (p == NULL) //get_length中包含对p为空的判断
{
return false;
}
if (pos > get_length(p))
{
return false;
}
*e = *(p->data+pos);
for(int i=pos; i<get_length(p); i++)
{
*(p->data+i) = *(p->data+i+1);
}
p->length--;
return true;
}
bool put(Dlist *p, int pos, elem_type e)
{
if (!(p != NULL && pos < get_length(p)))
{
return false;
}
*(p->data + pos)=e; //让其下标皆从零开始
return true;
}
bool get(Dlist *p, int pos, elem_type *e)
{
if (!(p != NULL && pos < get_length(p)))
{
return false;
}
*e = *(p->data+pos);
return true;
}
bool meger(Dlist *p, Dlist *s)//p=p+s;
{
if (p == NULL || s == NULL)
{
return false;
}
elem_type temp;//如果当其为整型进行初始化,那如果是结构体就用不了
for (int i=0; i<get_length(s); i++)
{
bool bo = get(s, i, &temp);
if (bo == false)
{
return false;
}
insert_tail(p, temp);
}
return false;
}
bool swap(Dlist *p, Dlist *s)
{
if (p == NULL || s == NULL)
{
return false;
}
int i = 0;
int x = get_length(p);
int y = get_length(s);
int plength = x;
int slength = y;
while(x>0 && y>0)
{
is_full(s);
is_full(p);
elem_type temp1;
elem_type temp2;
bool s1 = get(p, i, &temp1);
bool s2 = get(s, i, &temp2);
if (s1 == false || s2 ==false)
{
return false;
}
//如果将两个判断合并起来,则在拿出放进之后,如果得到的就是错误的数据,已经放进去再退出这个函数,对数据有污染
bool s3 = put(s, i, temp1);
bool s4 = put(p, i, temp2);
if (s3 == false || s4 == false)
{
return false;
}
x--;
y--;
i++;
}
elem_type temp;
if (x == 0)
{
while (y-- != 0)
{
get(s, i, &temp);
insert(p,i,temp);
i++;
}
}
else
{
while (x-- != 0)
{
get(p, i, &temp);
insert(s, i, temp);
i++;
}
}
p->length = slength;
s->length = plength;
return true;
}
bool merger_by_order(Dlist *p, Dlist *s)//p=p+s
{
if (p == NULL || s == NULL)
{
return false;
}
bool s1 = sort(p);//不好,改变了原来数据
bool s2 = sort(s);
if (!s || !s2)
{
return false;
}
elem_type temp1; // p m
elem_type temp2; // s n
int m = 0;
int n = 0;
int plength = get_length(p);
int slength = get_length(s);
elem_type pp = *(p->data+plength-1);
elem_type ss = *(s->data+slength-1);
int tmp = get_length(p) < get_length(s) ? get_length(p) : get_length(s); //得出较小值
get(p, m, &temp1);
get(s, n, &temp2);
for (int i=0; pp != temp1 && ss != temp2 ; i++)
{
bool s1 = get(p, m, &temp1);
bool s2 = get(s, n, &temp2);
if (!s1 || !s2)
{
return false;
}
//p m 9 s n 23
if(temp1 < temp2)
{
m++;
}
else
{
insert(p, m, temp2);
n++;
}
}
if(plength == tmp)
{
while(tmp < slength)
{
get(s, n, &temp1);
insert_tail(p,temp1);
n++;
m++;
tmp++;
}
}
return false;
}
bool same_data(Dlist *p, Dlist *s, Dlist *m)//p与s中相同的取出放到m线性表中去
{
if (p == NULL || s == NULL || m == NULL)
{
return false;
}
for (int i=0; i<get_length(p); i++)
{
if (search_yes_or_no(s, *(p->data+i)))
{
insert_tail(m,*(p->data+i));
}
}
return true;
}
相关文章推荐
- MySQL/sqlserver查询in操作 查询结果按in集合顺序显示
- MySQL查询in操作 查询结果按in集合顺序显示
- MySQL/sqlserver查询in操作 查询结果按in集合顺序显示
- MySQL查询in操作 查询结果按in集合顺序显示
- 集合去重操作,保证顺序不变,以集合中第一次出现的数据为准
- 《数据结构》 定长顺序串常用操作代码集合
- MySQL查询in操作 查询结果按in集合顺序显示
- MySQL查询in操作 查询结果按in集合顺序显示(转)
- MySQL/sqlserver查询in操作 查询结果按in集合顺序显示
- MySQL查询in操作 查询结果按in集合顺序显示
- java 集合的常用操作(排序、查找、打乱顺序)
- 集合的顺序存储及操作实现
- MySQL 查询in操作,查询结果按in集合顺序显示
- java 集合的常用操作(排序、查找、打乱顺序)
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例
- MySQL查询in操作 查询结果按in集合顺序显示
- 【BIEE】由于排序顺序不兼容,集合操作失败
- java 集合的常用操作(排序、查找、打乱顺序)
- MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家
- MySQL查询in操作 查询结果按in集合顺序显示