一个简单顺序表的基本操作
2016-04-07 18:12
375 查看
#include <stdio.h>
#include <stdlib.h>
#define Max_Size 100 /*线性表可能达到的最大长度*/
typedef int DataType;
typedef struct List
{
DataType data[Max_Size]; /*用于存放数据元素数组 */
int length; /* 当前表长度*/
}SeqList;
void InitList (SeqList &L)
{ L.length=0;
}
int Locate(SeqList L, DataType y)
{
int i=0;
while ((i<=L.length) &&(L.data[i]!=y)) /*顺序搜索表中的数据元素,直到找到*/
/*值等于y数据元素,或搜索到表尾而没找到*/
i++;
if (i>L.length)
return (0);
else
return(i);
}
/*静态数组存储结构下进行插入操作*/
void InsList(SeqList &L, int i, DataType y)
{ int j;
if(i<1||i>L.length+1) /*若i越界则插入失败*/
return;
for(j=L.length+1;j>=i;j--)
L.data[j]=L.data[j-1];
L.data[i-1]=y;
L.length++;
return;
}
void DispList(SeqList L)
{ int i;
for(i=1;i<=L.length;i++)
printf("%c",L.data[i-1]);
printf("\n");
}
DataType DelList(SeqList &L, int i, DataType &y)
{ int j;
y=L.data[i-1];/*因为数组的下标从0开开始,线性表的位置是从1开始*/
if(i<1||i>L.length)
exit(1);/* 执行此函数中止程序运行,此函数在stdlib.h中有定义 */
for(j=i;j<L.length;j++)
L.data[j-1]=L.data[j];
L.length--;
return y;
}
int DelElem(SeqList &L,DataType i) /*删除指定位置的数据元素*/
{ int j;
if(i<1||i>L.length)
return 0;
for(j=i;j<L.length;j++)
L.data[j-1]=L.data[j];
L.length--;
return 1;
}
int DelElemV(SeqList &L,DataType y) /*删除指定的数据元素*/
{
int i, j;
/* 从线性表中顺序查找出值为x的第一个元素 */
for(i = 0; i < L.length; i++){
if(L.data[i] == y){
break;
}
}
/* 若查找失败,表明不存在值为x的元素,返回0 */
if(i == L.length){
return 0;
}
/* 删除值为x的元素L->list[i] */
for(j = i + 1; j < L.length; j++){
L.data[j-1] = L.data[j];
}
L.length--;
return 1;
}
void main()
{
//验证
SeqList L;
DataType x;
InitList(L);
InsList(L,1,'a');
InsList(L,2,'c');
InsList(L,3,'a');
InsList(L,4,'e');
InsList(L,5,'d');
InsList(L,6,'b');
DispList(L);
printf("\n");
InsList(L,1,'y');
DispList(L);
printf("\n");
DelElem(L,3);
DispList(L);
printf("\n");
DelElemV(L,'a');
DispList(L);
printf("\n");
printf("%c",L.data[Locate(L, 'e')]);
printf("\n");
DispList(L);
printf("\n");
printf("%c",DelList(L, 3, x));
}
#include <stdlib.h>
#define Max_Size 100 /*线性表可能达到的最大长度*/
typedef int DataType;
typedef struct List
{
DataType data[Max_Size]; /*用于存放数据元素数组 */
int length; /* 当前表长度*/
}SeqList;
void InitList (SeqList &L)
{ L.length=0;
}
int Locate(SeqList L, DataType y)
{
int i=0;
while ((i<=L.length) &&(L.data[i]!=y)) /*顺序搜索表中的数据元素,直到找到*/
/*值等于y数据元素,或搜索到表尾而没找到*/
i++;
if (i>L.length)
return (0);
else
return(i);
}
/*静态数组存储结构下进行插入操作*/
void InsList(SeqList &L, int i, DataType y)
{ int j;
if(i<1||i>L.length+1) /*若i越界则插入失败*/
return;
for(j=L.length+1;j>=i;j--)
L.data[j]=L.data[j-1];
L.data[i-1]=y;
L.length++;
return;
}
void DispList(SeqList L)
{ int i;
for(i=1;i<=L.length;i++)
printf("%c",L.data[i-1]);
printf("\n");
}
DataType DelList(SeqList &L, int i, DataType &y)
{ int j;
y=L.data[i-1];/*因为数组的下标从0开开始,线性表的位置是从1开始*/
if(i<1||i>L.length)
exit(1);/* 执行此函数中止程序运行,此函数在stdlib.h中有定义 */
for(j=i;j<L.length;j++)
L.data[j-1]=L.data[j];
L.length--;
return y;
}
int DelElem(SeqList &L,DataType i) /*删除指定位置的数据元素*/
{ int j;
if(i<1||i>L.length)
return 0;
for(j=i;j<L.length;j++)
L.data[j-1]=L.data[j];
L.length--;
return 1;
}
int DelElemV(SeqList &L,DataType y) /*删除指定的数据元素*/
{
int i, j;
/* 从线性表中顺序查找出值为x的第一个元素 */
for(i = 0; i < L.length; i++){
if(L.data[i] == y){
break;
}
}
/* 若查找失败,表明不存在值为x的元素,返回0 */
if(i == L.length){
return 0;
}
/* 删除值为x的元素L->list[i] */
for(j = i + 1; j < L.length; j++){
L.data[j-1] = L.data[j];
}
L.length--;
return 1;
}
void main()
{
//验证
SeqList L;
DataType x;
InitList(L);
InsList(L,1,'a');
InsList(L,2,'c');
InsList(L,3,'a');
InsList(L,4,'e');
InsList(L,5,'d');
InsList(L,6,'b');
DispList(L);
printf("\n");
InsList(L,1,'y');
DispList(L);
printf("\n");
DelElem(L,3);
DispList(L);
printf("\n");
DelElemV(L,'a');
DispList(L);
printf("\n");
printf("%c",L.data[Locate(L, 'e')]);
printf("\n");
DispList(L);
printf("\n");
printf("%c",DelList(L, 3, x));
}
相关文章推荐
- python GUI学习——Tkinter
- eclipse修改背景颜色【代码区和Package Explorer】
- java开发常用的linux命令
- leetcode---Count Primes---素数筛法的改进
- 如何用未越狱手机安装ipa文件(更新版)
- FFMPEG使用参数详解
- Spring IOC
- iOS -- 怎么解决could not find developer disk image
- ios delegate, block, NSNotification用法
- javascript 剔除数组中相同的值,合并数组中相同项
- android 裁切照片适应小米系统的实践补充
- hadoop报错处理
- PHP文件处理--读取文件
- Android SDK帮助文档使用方法
- 如何判断一个JS对象是DOM对象
- 虚函数的作用及其底层实现机制
- UVA_10583_Ubiquitous Religions
- Jquery中的bind(),live(),delegate(),on()绑定事件方式
- Canvas绘制2D图像API列表
- My97 DatePicker