/*
编译器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. POJ 3281 牛双选问题

    题目大意:有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物 ...

  2. (2015年郑州轻工业学院ACM校赛题) A 彩票

    这是个简单的题目,其实就是判断是否是偶数, 对二进行特判一下就行了! 比赛时候我们还错两次................ 一看简单题就想抢一血,谁知到第一次提交CE, 再提交WA 汗........ ...

  3. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  4. 【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1033 http://poj.org/problem?id=1141 ZOJ目前挂了. ...

  5. Binary Search Tree Iterator——LeetCode

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. Foundation Data Structure

    LinkedList : /** * 考虑的比较的周全,并且包含了全部的情况,代码也不乱<b></b> * * @param index * 插入的位置 * @param c ...

  7. 关于C#中Thread.Join()的一点理解

    原文地址:http://www.cnblogs.com/slikyn/articles/1525940.html 今天是第一次在C#中接触Thread,自己研究了一下其中Thread.Join()这个 ...

  8. JavaScript中依赖注入详细解析

    计算机编程的世界其实就是一个将简单的部分不断抽象,并将这些抽象组织起来的过程.JavaScript也不例外,在我们使用JavaScript编写应用时,我们是不是都会使用到别人编写的代码,例如一些著名的 ...

  9. Object-C 对象 (创建/销毁 对象)-- 笔记

    创建/销毁 对象: Dog *dog = [Dog alloc]; //  通过alloc创建dog一个这样的对象, alloc相对于C语言中的new // *号既表示指针,也表示引用 初始化构造函数 ...

  10. DBParameter比拼接字符串慢的解决办法

    List<DBParameter> param = new List<DBParameter>(){       new DBParameter("@Question ...