顺序结构:元素逻辑相邻,物理也相邻
线性表的顺序实现:顺序表 (按索引值从小到大放在一片相邻的连续区域)
顺序表定义:一组内存地址连续的存储单元依次存储数据元素,也称向量。
顺序表可分为:定长顺序表和可变长顺序表
优点:根据下标随机取元素
缺点:更新付出的代价大,开始就要分配一大块内存空间。
下面是代码实现:
DESQ_LIST.h
#ifndef _DSEQ_LIST_H_
#define _DSEQ_LIST_H_
typedef int elem_tpye;
#define INIT_SIZE 10
typedef struct _DSEQ_LIST
{
elem_tpye *data;
int length;
int total_length;
}DSEQ_LIST;
DSEQ_LIST *init_dseq_list();
bool destory_dseq_list(DSEQ_LIST *p);
bool insert_d(DSEQ_LIST *p, int pos, elem_tpye *e);
bool is_full_d(DSEQ_LIST *p);
bool is_empty_d(DSEQ_LIST *p);
void print_int(elem_tpye *e);
bool show_d(DSEQ_LIST *p, void (*pfunc)(elem_tpye *));
bool clear_seq_list(DSEQ_LIST *p);
bool insert_head(DSEQ_LIST *p, elem_tpye *e);
bool insert_tail(DSEQ_LIST *p, elem_tpye *e);
bool del_head(DSEQ_LIST *p, elem_tpye *e);
bool del_tail(DSEQ_LIST *p, elem_tpye *e);
bool del(DSEQ_LIST *p,int pos, elem_tpye *e);
bool put(DSEQ_LIST *p, int pos, elem_tpye *e);
bool get(DSEQ_LIST *p, int pos, elem_tpye *e);
bool get_pos_by_value(DSEQ_LIST *p,elem_tpye *e,int *n);
bool sort(DSEQ_LIST *p);
bool get_max(DSEQ_LIST *p, elem_tpye *e);
bool get_min(DSEQ_LIST *p, elem_tpye *e);
DSEQ_LIST *intersec_a_and_b(DSEQ_LIST *a,DSEQ_LIST *b);
bool meger(DSEQ_LIST *p,DSEQ_LIST *s,DSEQ_LIST *rs);
#endif
DESQ_LIST.c
#include "DESQ_LIST.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//初始化不定长顺序表
DSEQ_LIST *init_dseq_list()
{
//动态开辟顺序表结构体空间
DSEQ_LIST* p = (DSEQ_LIST *)malloc(sizeof(DSEQ_LIST));
assert(p != NULL);
p->length = 0;
p->total_length = INIT_SIZE;
//动态开辟数据空间
p->data = (elem_tpye *)malloc(sizeof(elem_tpye)*INIT_SIZE);
assert(p->data != NULL);
return p;
}
//销毁顺序表
bool destory_dseq_list(DSEQ_LIST *p)
{
if(p == NULL||p->data == NULL)
{
return false;
}
free(p->data);
free(p);
return true;
}
//顺序表空间加倍增长
static void inc(DSEQ_LIST *p)
{
p->data = (elem_tpye *)realloc(p->data,sizeof(elem_tpye)*p->total_length*2);
p->total_length = p->total_length*2;
}
//指定位置插入数据
bool insert_d(DSEQ_LIST *p, int pos, elem_tpye *e)
{
if(p == NULL||pos<0||pos>p->length)
{
return false;
}
if(is_full_d(p))
{
inc(p);
}
for(int i =p->length -1;i>=pos;i--)
{
p->data[i+1] = p->data[i];
}
p->data[pos] = *e;
p->length++;
return true;
}
//判断顺序表是否已满
bool is_full_d(DSEQ_LIST *p)
{
return p->length == p->total_length;
}
//判断顺序表是否为空
bool is_empty_d(DSEQ_LIST *p)
{
return p->length == 0;
}
void print_int(elem_tpye *e)
{
printf("%d ",*e);
}
bool show_d(DSEQ_LIST *p, void (*pfunc)(elem_tpye *))
{
if(p == NULL)
{
return false;
}
for(int i = 0;i<p->length;i++)
{
pfunc(&p->data[i]);
}
return 0;
}
//清空顺序表,元素全部置为0
bool clear_seq_list(DSEQ_LIST *p)
{
if(p == NULL)
{
return false;
}
for (int i = 0;i<p->length;i++)
{
p->data[i] = 0;
}
return true;
}
//头插
bool insert_head(DSEQ_LIST *p, elem_tpye *e)
{
if (p == NULL)
{
return false;
}
if(is_full_d(p))
{
inc(p);
}
for (int 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_tpye *e)
{
if (p == NULL)
{
return false;
}
if(is_full_d(p))
{
inc(p);
}
p->data[p->length] = *e;
p->length++;
return true;
}
//头删
bool del_head(DSEQ_LIST *p, elem_tpye *e)
{
if (p == NULL)
{
return false;
}
*e = p->data[0];
for(int 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_tpye *e)
{
if (p == NULL)
{
return false;
}
*e = p->data[p->length-1];
p->data[p->length-1] = 0;
p->length--;
return true;
}
//指定位置删除
bool del(DSEQ_LIST *p,int pos, elem_tpye *e)
{
if (p == NULL||pos<0||pos>p->length)
{
return false;
}
*e = p->data[pos-1];
for (int i = pos-1;i<p->length-1;i++)
{
p->data[i] = p->data[i+1];
}
p->length--;
return true;
}
//顺序表中元素置数
bool put(DSEQ_LIST *p, int pos, elem_tpye *e)
{
if(p == NULL||pos<0||pos>p->length+1)
{
return false;
}
if (pos = p->length+1)
{
p->data[pos-1] = *e;
p->length++;
}
else
{
p->data[pos-1] = *e;
}
return true;
}
bool get(DSEQ_LIST *p, int pos, elem_tpye *e)
{
if(p == NULL||pos<0||pos>p->length)
{
return false;
}
*e = p->data[pos-1];
return true;
}
//取指定位置的元素值
bool get_pos_by_value(DSEQ_LIST *p,elem_tpye *e,int* n)
{
if (p == NULL)
{
return false;
}
for (int i = 0;i<p->length;i++)
{
if (p->data[i] == *e)
{
*n = i+1;
return true;
}
}
//printf("该顺序表中无此数\n");
return false;
}
//取线性表最大值
bool get_max(DSEQ_LIST *p, elem_tpye *e)
{
if (p == NULL)
{
return false;
}
if(is_empty_d(p))
{
return false;
}
elem_tpye max = p->data[0];
for (int i = 0;i<p->length;i++)
{
if(p->data[i]>max)
{
max = p->data[i];
}
}
*e = max;
return true;
}
取线性表最小值
bool get_min(DSEQ_LIST *p, elem_tpye *e)
{
if (p == NULL)
{
return false;
}
if(is_empty_d(p))
{
return false;
}
elem_tpye min = p->data[0];
for (int i = 0;i<p->length;i++)
{
if(p->data[i]<min)
{
min = p->data[i];
}
}
*e = min;
return true;
}
//顺序表元素排序
bool sort(DSEQ_LIST *p)
{
if (p == NULL)
{
return false;
}
elem_tpye tmp;
for (int i = 0;i<p->length;i++)
{
for (int j = 0;j<p->length-i;j++)
{
if(p->data[i]>p->data[i+1])
{
tmp = p->data[i];
p->data[i] = p->data[i+1];
p->data[i+1] = tmp;
}
}
}
return true;
}
//删除a中在b中出现的元素
DSEQ_LIST *intersec_a_and_b(DSEQ_LIST *a,DSEQ_LIST *b)
{
if (a == NULL|| b==NULL)
{
return NULL;
}
if (is_empty_d(b))
{
return a;
}
if (is_empty_d(a))
{
return NULL;
}
elem_tpye tmp = 0;
int pos = 0;
elem_tpye rs = 0;
for(int i = 0;i<b->length;i++)
{
tmp = b->data[i];
if(get_pos_by_value(a,&tmp,&pos))
{
del(a,pos,&rs);
}
}
return a;
}
测试主函数main.c
#include"DESQ_LIST.h"
#include<stdio.h>
#include<vld.h>
int main()
{
DSEQ_LIST *p;
DSEQ_LIST *s;
elem_tpye e1 = 19;
elem_tpye e2 = 8;
elem_tpye e3 = 56;
elem_tpye e4 = 23;
elem_tpye e5 = 77;
elem_tpye e6 = 34;
elem_tpye e = 0;
p = init_dseq_list();
s = init_dseq_list();
insert_head(s,&e4);
insert_head(s,&e6);
insert_head(s,&e1);//19 34 23
insert_d(p,0,&e1);//19
insert_head(p,&e2);// 8 19
insert_tail(p,&e3);//8 19 56
show_d(p,print_int);//8 19 56
printf("\n");
p = intersec_a_and_b(p,s);//8 56
show_d(p,print_int);
printf("\n");
printf("%d\n",is_full_d(p));//0
printf("%d\n",is_empty_d(p));//0
del_head(p,&e);//19 56
del_tail(p,&e);//19
del(p,1,&e);//无元素
printf("%d\n",is_empty_d(p));//1
printf("%d\n",e);//e =19;
printf("%d\n",put(p,3,&e));//0
put(p,1,&e);//19
put(p,2,&e4);//19 23
put(p,3,&e5);//19 23 77
show_d(p,print_int);
printf("\n");
get(p,2,&e);
printf("%d\n",e);//e= 23
int n = 0;
get_pos_by_value(p,&e,&n);
printf("%d\n",n);//n =2;
insert_head(p,&e6);
get_max(p,&e);
printf("max-%d\n",e);//77
get_min(p, &e);
printf("min-%d\n",e);//19
sort(p);
show_d(p,print_int);//19 23 34 77
clear_seq_list(p);
show_d(p,print_int);//0 0 0 0
destory_dseq_list(p);
destory_dseq_list(s);
return 0;
}