线性表的顺序表示终于马马虎虎的勉强写完了,
写的不是很完整,开始时比较不理解就是动态分
配内存,现在懂一点点了,数据结构落下很多了,
这几天要好好整了
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct{
int* elem;
int length;
int listsize;
}Sqlist;
Status InitList(Sqlist &L) //线性表的初始化
{
L.elem = (int *)malloc(10 * sizeof(ElemType));
L.length = 0;
L.listsize = 10;
return OK;
}
Status CreatList(Sqlist &L) //线性表的创建
{
Status i,n;
cout<<"please input list length:"<<endl;
cin>>n;
L.length = n;
cout<<"please input L.elem:"<<endl;
for(i=0;i<n;i++)
cin>>L.elem[i];
return OK;
}
Status DisplayList(Sqlist &L) //线性表元素展示
{
Status i;
for(i=0;i<L.length;i++)
cout<<L.elem[i]<<" ";
cout<<endl;
return OK;
}
Status ClearList(Sqlist &L) //线性表的清空
{
Status i;
for(i=0;i<L.length;i++)
L.elem[i] = 0;
L.length = 0;
return OK;
}
Status ListEmpty(Sqlist &L) //判断线性表是否为空
{
if(L.length == 0)
return TRUE;
else
return FALSE;
}
Status ListLength(Sqlist &L) //求线性表的长度
{
return L.length;
}
Status GetElem(Sqlist &L, Status &num, ElemType &e) //用e返回线性表中第i个元素的值
{
e = L.elem[num-1];
return e;
}
Status LocateElem(Sqlist &L, ElemType &e) //返回元素e的位序,若不存在返回0
{
Status i;
for(i=0;i<L.length;i++)
{
if(L.elem[i] == e)
break;
}
if(i<L.length)
return (i+1);
else
return 0;
}
Status PriorElem(Sqlist &L, ElemType &cur_e, ElemType &pre_e) //求元素的前驱
{
Status i;
i = LocateElem(L,cur_e);
if(i == 1)
{
//free(pre_e);
return ERROR;
}
else
{
pre_e = L.elem[i-2];
return pre_e;
}
}
Status NextElem(Sqlist &L, ElemType &cur_e, ElemType &next_e) //求元素后继
{
Status i;
i = LocateElem(L, cur_e);
if(i == L.length)
{
//free(next_e);
return ERROR;
}
else
{
next_e = L.elem[i];
return next_e;
}
}
Status ListInsert(Sqlist &L, Status &num, ElemType &e) //在线性表i前插入元素e
{
if(num<1 || num>L.length)
return ERROR;
else
{
Status j;
if(L.length == 10)
{
L.elem = (int *)realloc(L.elem, (10+20)*sizeof(ElemType));
L.listsize = 10 + 20;
}
for(j=L.length-1;j>=num-1;j--)
L.elem[j+1] = L.elem[j];
L.elem[num-1] = e;
L.length++;
return OK;
}
}
Status ListDelete(Sqlist &L, Status &num, ElemType &e) //删除第i个数据元素
{
Status j;
if(num<1 || num>L.length)
return ERROR;
else
{
e = L.elem[num-1];
for(j=num;j<L.length;j++)
L.elem[j-1] = L.elem[j];
L.length--;
return e;
}
}
Status DistroyList(Sqlist &L) //线性表的销毁
{
free(L.elem);
return OK;
}
Status main()
{
int temp;
Sqlist L;
ElemType e, cur_e, next_e;
Status num;
InitList(L);
CreatList(L);
DisplayList(L);
cin>>num;
temp = ListDelete(L, num, e);
//ClearList(L);
DisplayList(L);
//temp = ListEmpty(L);
cout<<temp<<endl;
DistroyList(L);
system("pause");
return 0;
}
分享到:
相关推荐
线性表的顺序表示和实现源码。 Blog: http://blog.csdn.net/algorithm_only
线性表的顺序表示和实现,博客链接: http://blog.csdn.net/u013071074/article/details/27336291
线性表的顺序表示以及实现(C语言编写),有完整的注释。
线性表的顺序表示与实现 含有插入 删除 顺序表大小
C语言实现线性表的顺序表示的源码文件 SeqList InitList(); // 初始化线性表 void DestroyList(); // 销毁线性表 void ClearList(); // 清空线性表 int ListEmpty(); // 判断线性表是否为空 int ListLength(); ...
线性表的顺序表的建立、插入、删除、输出等操作。
线性表的顺序表示和插入、删除、归并C语言代码实现。根据严蔚敏编的C语言版程序编写。
1. 熟练掌握 Visual Studio 进行项目和类的建立和管理 3. 在 exp2lib 项目中编程实现一个包含起标志作用的头结点的单向链表 4. 将任务
线性表的顺序表示和实现 数据结构用C语言编写的 这是我们的实验书上的
线性表的顺序表示(数组)及C++实现。创建CArrayList类,实现顺序表的各项主要功能:初始化、追加、插入、删除、查找、排序、倒置、打印等。
有关线性表的顺序表示.建立顺序表,插入删除查找等相关操作
线性表的顺序表示和实现。
通常来说顺序表是在计算机的内存中以数组的形式保存的线性表,是用一组地址连续的存储单元依次存储数据元素的线性数据结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存...
实验一 顺序结构线性表基本操作的实现 线性表基本操作的实现 线性表的顺序存储有何优缺点?
详细的介绍了线性表中的各种操作。
线性表的动态分配顺序表示和实现 线性表的动态分配顺序表示和实现 C++
线性表的顺序表示和实现,附全套C++代码实现
数据结构线性表的顺序表示和实现的实习报告.doc