您的位置:首页 > 编程语言 > C语言/C++

顺序表的操作集合

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 线性表