线性表顺序存储方式的C语言实现
/*
编译器VC6++
文件名1.cpp
代码版本号:1.0
时间:2015年9月14日16:39:21
*/
#include <stdio.h>
#include <stdlib.h> #define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 10 typedef int ElemType;
typedef int Status;
typedef struct{
ElemType *base;
int length;
int listsize;
} Sqlist; Status initSqlist(Sqlist *l)//初始化线性表,分配容量
{
(*l).base = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!((*l).base))
exit(OVERFLOW);
(*l).length =;
(*l).listsize =LIST_INIT_SIZE;
return OK;
} Status assignSqlist(Sqlist *l) //按位序assign值到线性表中
{
int i;
if(l->base!=NULL)
{
l->length=LIST_INIT_SIZE;
for (i = ; i < LIST_INIT_SIZE; i++)
(l->base)[i] = i;
return OK;
}
else
return ERROR;
} Status ListInsert(Sqlist *l, int i, ElemType e) //元素插入
{
ElemType *newbase=NULL;
ElemType *q;
//ElemType *p = (*l).base + i - 1;
if (i< || i>(*l).length + )
{
printf("请插入在第1-%d个元素之间", (*l).length + );
return ;
}
if ((*l).length >= (*l).listsize)
{
printf("我执行了");
newbase = (ElemType *)realloc((*l).base,
((*l).listsize + LIST_INCREMENT) * sizeof(ElemType));
if (!newbase)
exit();
(*l).base = newbase; // 新基址
(*l).listsize += LIST_INCREMENT; // 增加存储容量
}
for (q = (*l).base + (*l).length - ; q >= (*l).base + i-;q--)
*(q + ) = *q; *((*l).base + i - ) = e;
(*l).length++;
return OK;
} Status GetElem(Sqlist *l,int i,ElemType *e)
{
if(i>=&&i<=l->length)
{
*e=l->base[i-];
return OK;
}
else
{
printf("您输入的元素位序不合法\n");
return ERROR;
}
} Status mergeList(Sqlist *lc,Sqlist la,Sqlist lb)
{
int i=;
int j=;
ElemType *a=la.base;
ElemType *b=lb.base;
ElemType *c;
lc->listsize=lc->length=la.length+lb.length;
lc->base=(ElemType *)malloc(lc->listsize*sizeof(ElemType));
if(!lc->base)
exit(OVERFLOW); c=lc->base; while(i<=la.length&&j<=lb.length)
{
if(*a<=*b)
{
*c++=*a++;
i++;
}
else
{
*c++=*b++;
j++;
}
} while(i<=la.length)
{
*c++=*a++;
i++;
}
while(j<=lb.length)
{
*c++=*b++;
j++;
} return OK;
} Status listDelete(Sqlist *l,int i,ElemType *e)
{
if(i<||i>l->length) return ERROR;
ElemType *p=l->base+i-;
*e=*p;
for(*p;p<(l->base+l->length-);p++)
*p=*(p+); l->length--;
return OK;
} Status priorElem(Sqlist *l,ElemType e,ElemType *prior_elem)
{
int i=;
while(i<=l->length&&l->base[i-]!=e)
{
i++;
}
if(i==(l->length+))
{
printf("\n元素%d不在线性表中\n",e);
return ERROR;
}
else
{
if(i==)
{
printf("该元素没有前驱\n");
return ERROR;
}
else
{
*prior_elem=l->base[i-];
return OK;
}
}
} Status nextElem(Sqlist *l,ElemType e,ElemType *next_elem)
{
int i=;
while(i<=l->length&&l->base[i-]!=e)
{
i++;
}
if(i==(l->length+))
{
printf("\n元素%d不在线性表中\n",e);
return ERROR;
}
else
{
if(i==l->length)
{
printf("该元素没有后驱\n");
return ERROR;
}
else
{
*next_elem=l->base[i];
return OK;
}
}
}
/*
返回L中第1个与e满足关系compare()的数据元素的位序。
若这样的数据元素不存在,则返回值为0。
*/
int locateElem(Sqlist *l,ElemType e,bool (*compare)(ElemType e,ElemType a))
{
int i=;
while(i<=l->length&&!compare(e,l->base[i]))
i++;
if(i<=l->length)
return i+;
else
return ;
} bool comp(ElemType e,ElemType a){
if(e==a)
return TRUE;
else
return FALSE;
} Status printLinklist(Sqlist l) //遍历输出线性表中的元素
{ for (int i = ; i < l.length; i++){
printf("\n第%d个数据为%d",i+,(l.base)[i]);
} return OK;
} Status destroyList(Sqlist *l){//销毁线性表,释放空间 if(l->base!=NULL)
{
free(l->base);
l->length=;
l->listsize=; return OK;
}
return ERROR;
} Status clearList(Sqlist *l)//将线性表重置为长度为0的空表
{
l->length=;
return OK;
} bool isEmpty(Sqlist *l) //判断线性表是否是空表
{
if(l->length==)
return TRUE;
else
return FALSE;
} void main()
{
Sqlist L;
Sqlist La,Lb,Lc;
Status s = ;//s为程序运行状态
int i;
ElemType theElem;
ElemType prior_elem;
ElemType next_elem;
ElemType e; s = initSqlist(&L); //合并3个线性表开始
initSqlist(&La);
initSqlist(&Lb);
initSqlist(&Lc); int a[]={,,,,};
int b[]={,,,,,,,,,}; for(i=;i<;i++)
ListInsert(&La,La.length+,a[i]); for(i=;i<;i++)
ListInsert(&Lb,Lb.length+,b[i]); printLinklist(La);
printf("\n==================\n");
printLinklist(Lb);
printf("\n==================\n"); s=mergeList(&Lc,La,Lb);
printLinklist(Lc);
printf("\n==================\n"); //合并3个线性表结束 if(s)
s=assignSqlist(&L); printLinklist(L);
printf("\n==================\n");
printf("请输入要获得的元素的位序: ");
scanf("%d",&i);
s=GetElem(&L,i,&e);
printf("\n线性表中位序为%d的元素为%d",i,e);
printf("\n==================\n");
printf("\n元素6在线性表表中的位序为%d",locateElem(&L,,comp));
/*printf("请输入您要插入的元素: ");
scanf("%d", &theElem);
printf("请输入您要插入元素的位置: ");
scanf("%d", &i);
ListInsert(&L, i, theElem);*/
printf("\n==================\n");
printf("\n您删除第一个元素后的线性表为\n");
listDelete(&L,,&e);
printLinklist(L);
printf("\n您删除的这个元素为%d",e);
printf("\n==================\n");
//前驱开始
priorElem(&L,,&prior_elem);
printf("\n==================\n");
printf("元素1的前驱为%d",prior_elem);
printf("\n==================\n");
//前驱结束
//后继开始
nextElem(&L,,&next_elem);
printf("\n==================\n");
printf("元素9的后继为%d",next_elem);
printf("\n==================\n");
//后继结束
if(s)
{
clearList(&L);
} printLinklist(L);
system("pause");
}
线性表顺序存储方式的C语言实现的更多相关文章
- 数据结构 - 静态顺序线性表的实行(C语言)
数据结构 - 静态顺序线性表的实行(C语言) 1 获取元素操作 对于线性表的顺序存储结构来说,如果我们要实现GetElem操作,即将线性表L中的第i个位置元素值返回,其实是非常简单的. 只要i的数值在 ...
- 线性表的基本操作(C语言实现)
文章目录 这里使用的工具是DEV C++ 可以借鉴一下 实现效果 顺序存储代码实现 链式存储存储实现 这里使用的工具是DEV C++ 可以借鉴一下 一.实训名称 线性表的基本操作 二.实训目的 1.掌 ...
- 线性表->顺序存储
文字描述: 用一组地址连续的存储单元依次存储线性表的数据元素,只要确定了存储线性表的起始位置,线性表中任一数据元素都可随机存取,所以线性表的顺序存储结构是一种随机存取的存储结构. 即是,线性表的顺序存 ...
- 01线性表顺序存储_List--(线性表)
#include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...
- 线性表 顺序存储 链式存储 ---java实现
首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...
- [数据结构 - 第3章] 线性表之双向链表(C语言实现)
一.什么是双向链表? 双向链表(double linked list)是在单链表的每个结点中,再设置一个指向其前驱结点的指针域.所以在双向链表中的结点都有两个指针域,一个指向直接后继,另一个指向直接前 ...
- 线性表顺序存储_List
#include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include &q ...
- 栈的顺序存储方式的C语言实现
/* 编译器:Dev-c++ 5.4.0 文件名:stack.cpp 代码版本号:1.0 时间:2015-10-10 20:08:54 */ #include <stdio.h> #inc ...
- 2.2_线性表的顺序存储结构_参考集合ArrayList
[线性表的顺序存储从结构] 指的是用一段连续的存储单元一次储存线性表的数据元素. [线性表的顺序存储的结构代码 C语言版] #define MAXSIZE 20 /*存储空间初始分配量*/ typed ...
随机推荐
- Makefile中的wildcard和patsubst
makefile 里的函数跟它的变量很相似——使用的时候,你用一个 $ 符号跟开括号,函数名,空格后跟一列由逗号分隔的参数,最后用关括号结束. 例如,在 GNU Make 里有一个叫 'wild ...
- 【贪心+堆】XMU 1584 小明的烦恼
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1584 题目大意: 给n(n<=100 000)个任务的耗时和截至时间,问最少不能 ...
- jquery 异步请求Demo【转载】
$(document).ready(function() { $.ajax({ url : "/AjaxAction/LoginArea.ashx", data : {userna ...
- [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...
- B - I Hate It - hdu 1754
Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟 ...
- Oracle Dataguard三种保护模式
Oracle Dataguard提供了三种数据保护模式,在此分别总结一下三种数据保护模式的特点. 1.最大保护模式1)这种模式提供了最高级别的数据保护能力:2)要求至少一个物理备库收到重做日志后,主库 ...
- Office2007图标变成白框,但是还能使用问题解决办法
在Windows 8中,Office图标变成白框了.不能显示. 解决办法:从其他电脑的Windows\Installer中拷贝一下所有文件夹到问题电脑.然后重启,问题解决. {90120000-002 ...
- Java并发编程:CopyOnWrite容器的实现
Java并发编程:并发容器之CopyOnWriteArrayList(转载) 原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW ...
- spring mvc model.addAttribute页面c:forEach取不到
昨天和今天都在解决一个问题,即: @RequestMapping(value = "/listAccounts", method = RequestMethod.GET) publ ...
- 网络编程TCP总结及实践-C语言
网络变成首先要注意IP和port的转换,如今电脑基本上是主机字节序,存储依照小端方式,而在网络中传输统一使用大端方式,所以网络变成首先要注意字节序的转换. 一个经常使用的ip转换程序的实现: #inc ...