/*
编译器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语言实现的更多相关文章

  1. 数据结构 - 静态顺序线性表的实行(C语言)

    数据结构 - 静态顺序线性表的实行(C语言) 1 获取元素操作 对于线性表的顺序存储结构来说,如果我们要实现GetElem操作,即将线性表L中的第i个位置元素值返回,其实是非常简单的. 只要i的数值在 ...

  2. 线性表的基本操作(C语言实现)

    文章目录 这里使用的工具是DEV C++ 可以借鉴一下 实现效果 顺序存储代码实现 链式存储存储实现 这里使用的工具是DEV C++ 可以借鉴一下 一.实训名称 线性表的基本操作 二.实训目的 1.掌 ...

  3. 线性表->顺序存储

    文字描述: 用一组地址连续的存储单元依次存储线性表的数据元素,只要确定了存储线性表的起始位置,线性表中任一数据元素都可随机存取,所以线性表的顺序存储结构是一种随机存取的存储结构. 即是,线性表的顺序存 ...

  4. 01线性表顺序存储_List--(线性表)

    #include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...

  5. 线性表 顺序存储 链式存储 ---java实现

    首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...

  6. [数据结构 - 第3章] 线性表之双向链表(C语言实现)

    一.什么是双向链表? 双向链表(double linked list)是在单链表的每个结点中,再设置一个指向其前驱结点的指针域.所以在双向链表中的结点都有两个指针域,一个指向直接后继,另一个指向直接前 ...

  7. 线性表顺序存储_List

    #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include &q ...

  8. 栈的顺序存储方式的C语言实现

    /* 编译器:Dev-c++ 5.4.0 文件名:stack.cpp 代码版本号:1.0 时间:2015-10-10 20:08:54 */ #include <stdio.h> #inc ...

  9. 2.2_线性表的顺序存储结构_参考集合ArrayList

    [线性表的顺序存储从结构] 指的是用一段连续的存储单元一次储存线性表的数据元素. [线性表的顺序存储的结构代码 C语言版] #define MAXSIZE 20 /*存储空间初始分配量*/ typed ...

随机推荐

  1. 【日语】secret base

    君(きみ)と夏(なつ)の终(お)わり 将来(しょうらい)の梦(ゆめ)Kimi to natsu no owari shourai no yume大(おお)きな希望(きぼう) 忘(わす)れないOoki ...

  2. C++: Command Line Processing

    Save this code here for studying and using it. Surce code is here. CmdLine.h File #pragma once #incl ...

  3. HDOJ 1028 Ignatius and the Princess III(递推)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  4. UVAlive3523 Knights of the Round Table(bcc)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122 [思路] 点-双连通分量 求出bcc,判断每个bcc是否为 ...

  5. vim 插件

    自动补全插件更换:youcompleteme github:https://github.com/Valloric/YouCompleteMe

  6. TeamViewer或者向日葵等无法成功远程登录

    之前一直能正常远程的两台电脑,今天不知道什么原因,莫名其妙的就无法登录了. 更悲催的时,今天早上走的时候,忘把TeamViewer或者向日葵软件启动了. 还好,我登录向日葵官网,在管理中心设置里面开启 ...

  7. jenkins服务器安装

    http://www.360doc.com/content/13/0412/09/10504424_277718090.shtml

  8. 使用单例模式实现自己的HttpClient工具类

    引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient 来方便我们使用各种Http服务.你可以把HttpCli ...

  9. c#中WebBrowser控件的使用方法

    首先先来简单介绍一下webbrowser控件,这个控件是可以实现在form窗体中添加网页内容的.如图,我在form中加入了百度api,(百度地图api调用博客里有讲) 使用这个控件其实很简单 (1)第 ...

  10. vim 开发配置(转载)

    原文:http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有 ...