数据结构基础之数组实现线性表各种操作
2016-10-13 09:55
676 查看
转自:http://blog.csdn.net/thefutureisour/article/details/7830062
前言:
对于线性结构,有两种保存的方法,一种是使用C语言中内置的数组,这样的结构成为顺序表;另一种使用指针,这样的结构成为链表。
对于线性结构,有12种基本的操作,分别是:初始化、删除、清空、判断是否为空、遍历、求表的长度、求某个元素在表中的位置、返回特定序号的元素、求某个元素的前一个元素、求某个元素的后一个元素、插入一个元素、删除一个元素。
这一小节介绍如何利用数组实现线性表。
代码:
#include <iostream>
#include <malloc.h>
//#define len 1000
typedef int ElemType;
//1.初始化顺序表:给出初始化长度
typedef struct arraylist
{
ElemType *Array; //实际存放元素的数组
int length; //数组中已经使用了多少元素
int size; //数组的容量
}arrayList;
//2.初始化顺序表:给出初始化长度
bool initialArray(arrayList *arrLst, int len)
{
arrLst->length = 0;
arrLst->size = len;
arrLst->Array = (ElemType*)malloc(len*sizeof(ElemType));//分配len大小内存
if (NULL == arrLst->Array)
return false;
else
return true;
}
//3.求有多少个元素
int arrayLength(arrayList *arrLst)
{
return arrLst->length;
}
//4.判断某个元素的位置
int locateElem(arrayList *arrLst, ElemType e)//判断e元素的位置
{
for (int i = 0; i < arrayLength(arrLst); ++i)
{
if (e == arrLst->Array[i])
return i;
}
return -1;
}
//5.清空顺序表
void clearArry(arrayList *arrLst)
{
arrLst->length = 0;
}
//6.判断线性表示否为空
bool Array_is_empty(arrayList *arrLst)
{
if (arrLst->length == 0)
{
printf("the arrayList is empty !\n");
return true;
}
else
{
printf("the arrayList is not empty !\n");
return false;
}
}
//7.删除顺序表
void deleteArray(arrayList *arrLst)
{
arrLst->length = 0;
arrLst->size = 0;
free(arrLst->Array);
arrLst->Array = NULL;
}
//8.取出某个元素
bool getElem(arrayList *arrLst, int index, ElemType *e)
{
if (index < 0 || index >arrLst->length)//索引溢出
return false;
else
{
*e = arrLst->Array[index];
return true;
}
}
//9.删除某个元素
int deleteElem(arrayList *arrLst, int index, ElemType *e)
{
if (index < 0 || index >arrLst->length)
return -1;
*e = arrLst->Array[index];//将预删除的元素保留在指针数组e中
//将删除点的元素依次左移
for (int i = index; i < arrayLength(arrLst); ++i)
{
arrLst->Array[i] = arrLst->Array[i + 1];
}
--arrLst->length;//元素的个数减1
return 1;
}
//10.遍历顺序表,并打印
void printArray(arrayList *arrLst)
{
printf("the elements of the Array are :\n");
for (int i = 1; i <= arrLst->length; i++)
{
printf("%d\n", arrLst->Array[i]);
}
}
//11.求某个元素的前驱并返回前驱元素的下标
void find_pre_element(arrayList *arrLst,int element/*, arrayList *f*/)
{
for (int i = 1; i <= arrLst->length; i++)
{
if (element = arrLst->Array[i])
{
if (i==1)
{
printf("the element of you find is the first element ");
}
else
{
printf("the pre_element is %d", arrLst->Array[i-1]);
}
}
}
printf("we can not find the pre_element! ");
}
//12.将元素插入到指定的index位置
bool insert_element(arrayList *arrLst, int index, int e)
{
if (index<0 || index>arrLst->length)
return false;
if (arrLst->length == arrLst->size)
{
arrLst->Array = (ElemType*)realloc(arrLst->Array, 2 * arrLst->size*sizeof(ElemType));//改变已有指针arrLst->Array内存大小
if(NULL == arrLst->Array)
//分配失败,程序退出
return false;
else
//分配成功,扩容
arrLst->size *= 2;
}
for (int i = index; i <= arrLst->length; i++)
{
arrLst->Array[i + 1] = arrLst->Array[i];
}
arrLst->Array[index] = e;//插入元素e
++arrLst->length;
return true;
}
说明:
再插入元素时,如果线性表已满,需要重新分配内存空间,新分配的内存空间设定为原来的2倍。这个倍数也不是我随便给出的,我是参考C++中STL里面的vector给出的。相信那些专家,肯定考虑了倍数过小而导致多次分配内存与内存分配太大的折中,我也就照猫画虎的这样做了。其他的复杂的操作,大多数都能通过这些基本操作来完成,这里就不在列出了。
我们可以看出,利用数组来表示线性结构,最大的优点在于由于数组是连续储存的,所以随机访问速度非常快,只需要用数组的首地址+下标*sizeof(结构体)就能计算指定元素的地址了。而它的缺点也很明显:就是插入、删除时效率很低,因为要移动大量的元素,甚至需要重新分配内存。
最后说一句,程序是没有标准答案的,我自己写的程序也难免有各种错误,如果指出错误,我将不胜感激。
前言:
对于线性结构,有两种保存的方法,一种是使用C语言中内置的数组,这样的结构成为顺序表;另一种使用指针,这样的结构成为链表。
对于线性结构,有12种基本的操作,分别是:初始化、删除、清空、判断是否为空、遍历、求表的长度、求某个元素在表中的位置、返回特定序号的元素、求某个元素的前一个元素、求某个元素的后一个元素、插入一个元素、删除一个元素。
这一小节介绍如何利用数组实现线性表。
代码:
#include <iostream>
#include <malloc.h>
//#define len 1000
typedef int ElemType;
//1.初始化顺序表:给出初始化长度
typedef struct arraylist
{
ElemType *Array; //实际存放元素的数组
int length; //数组中已经使用了多少元素
int size; //数组的容量
}arrayList;
//2.初始化顺序表:给出初始化长度
bool initialArray(arrayList *arrLst, int len)
{
arrLst->length = 0;
arrLst->size = len;
arrLst->Array = (ElemType*)malloc(len*sizeof(ElemType));//分配len大小内存
if (NULL == arrLst->Array)
return false;
else
return true;
}
//3.求有多少个元素
int arrayLength(arrayList *arrLst)
{
return arrLst->length;
}
//4.判断某个元素的位置
int locateElem(arrayList *arrLst, ElemType e)//判断e元素的位置
{
for (int i = 0; i < arrayLength(arrLst); ++i)
{
if (e == arrLst->Array[i])
return i;
}
return -1;
}
//5.清空顺序表
void clearArry(arrayList *arrLst)
{
arrLst->length = 0;
}
//6.判断线性表示否为空
bool Array_is_empty(arrayList *arrLst)
{
if (arrLst->length == 0)
{
printf("the arrayList is empty !\n");
return true;
}
else
{
printf("the arrayList is not empty !\n");
return false;
}
}
//7.删除顺序表
void deleteArray(arrayList *arrLst)
{
arrLst->length = 0;
arrLst->size = 0;
free(arrLst->Array);
arrLst->Array = NULL;
}
//8.取出某个元素
bool getElem(arrayList *arrLst, int index, ElemType *e)
{
if (index < 0 || index >arrLst->length)//索引溢出
return false;
else
{
*e = arrLst->Array[index];
return true;
}
}
//9.删除某个元素
int deleteElem(arrayList *arrLst, int index, ElemType *e)
{
if (index < 0 || index >arrLst->length)
return -1;
*e = arrLst->Array[index];//将预删除的元素保留在指针数组e中
//将删除点的元素依次左移
for (int i = index; i < arrayLength(arrLst); ++i)
{
arrLst->Array[i] = arrLst->Array[i + 1];
}
--arrLst->length;//元素的个数减1
return 1;
}
//10.遍历顺序表,并打印
void printArray(arrayList *arrLst)
{
printf("the elements of the Array are :\n");
for (int i = 1; i <= arrLst->length; i++)
{
printf("%d\n", arrLst->Array[i]);
}
}
//11.求某个元素的前驱并返回前驱元素的下标
void find_pre_element(arrayList *arrLst,int element/*, arrayList *f*/)
{
for (int i = 1; i <= arrLst->length; i++)
{
if (element = arrLst->Array[i])
{
if (i==1)
{
printf("the element of you find is the first element ");
}
else
{
printf("the pre_element is %d", arrLst->Array[i-1]);
}
}
}
printf("we can not find the pre_element! ");
}
//12.将元素插入到指定的index位置
bool insert_element(arrayList *arrLst, int index, int e)
{
if (index<0 || index>arrLst->length)
return false;
if (arrLst->length == arrLst->size)
{
arrLst->Array = (ElemType*)realloc(arrLst->Array, 2 * arrLst->size*sizeof(ElemType));//改变已有指针arrLst->Array内存大小
if(NULL == arrLst->Array)
//分配失败,程序退出
return false;
else
//分配成功,扩容
arrLst->size *= 2;
}
for (int i = index; i <= arrLst->length; i++)
{
arrLst->Array[i + 1] = arrLst->Array[i];
}
arrLst->Array[index] = e;//插入元素e
++arrLst->length;
return true;
}
说明:
再插入元素时,如果线性表已满,需要重新分配内存空间,新分配的内存空间设定为原来的2倍。这个倍数也不是我随便给出的,我是参考C++中STL里面的vector给出的。相信那些专家,肯定考虑了倍数过小而导致多次分配内存与内存分配太大的折中,我也就照猫画虎的这样做了。其他的复杂的操作,大多数都能通过这些基本操作来完成,这里就不在列出了。
我们可以看出,利用数组来表示线性结构,最大的优点在于由于数组是连续储存的,所以随机访问速度非常快,只需要用数组的首地址+下标*sizeof(结构体)就能计算指定元素的地址了。而它的缺点也很明显:就是插入、删除时效率很低,因为要移动大量的元素,甚至需要重新分配内存。
最后说一句,程序是没有标准答案的,我自己写的程序也难免有各种错误,如果指出错误,我将不胜感激。
相关文章推荐
- 数据结构基础之数组实现线性表各种操作(二)
- 数据结构基础 之 双链表 与 循环链表 各类操作、思想与实现
- 数据结构基础(9) --单链表的设计与实现(2)之高级操作
- 数据结构基础(1)--数组C语言实现--动态内存分配
- [C语言][数据结构]基础的线性表的顺序表示和实现
- C++ 数据结构线性表-数组实现
- 【数据结构与算法基础】以数组实现的循环队列 / Circular Queue implemented by array
- 数据结构基础(9) --单链表的设计与实现(2)之高级操作
- 数据结构——用数组实现的栈的基础功能
- 【数据结构与算法】(一) c 语言实现数组的简单操作
- 数据结构基础(9) --单链表的设计与实现(2)之高级操作
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- Java之美[从菜鸟到高手演变]之数据结构基础、线性表、栈和队列、数组和字符串
- 数据结构之数组实现基础队列结构
- 数据结构基础(9) --单链表的设计与实现(2)之高级操作
- Java之美[从菜鸟到高手演变]之数据结构基础、线性表、栈和队列、数组和字符串
- Java之数据结构基础、线性表、栈和队列、数组和字符串,树—学习笔记
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- 数据结构和算法 (二)数据结构基础、线性表、栈和队列、数组和字符串
- 数据结构-顺序线性表基本操作实现