1. 线性表顺序存储的概念：指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素。

2. 采用的实现方式：一段地址连续的存储单元可以用固定数组或者动态存储结构来实现，这里采用动态分配存储结构。

3. 顺序表结构体示意图

```#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10
#define OK              1
#define ERROR           0
#define INFEASIBLE        -1
#define OVERFLOW        -2
#define TRUE            1
#define FALSE            0

typedef int Status;
typedef int ElemType;
typedef struct SqList
{
ElemType *elem;
int length;
int listsize;
}SqList;

Status InitList(SqList &L)
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
{
printf("ERROR\n");
return ERROR;
}
L.length = 0;
L.listsize = LIST_INIT_SIZE;

return OK;
}

Status ListEmpty(SqList L)    //判空
{
if (L.length = 0) return TRUE;
else return FALSE;
}

Status ListInsert(SqList &L, int i, ElemType e)    //插入
{
ElemType *p, *q;
ElemType *newbase;
int j;

if (i < 1 || i > L.length + 1) return ERROR;
if (L.length >= L.listsize){
newbase = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (newbase == NULL)
{
printf("realloc failed!\n");
return ERROR;//exit(-1);
}
L.elem = newbase;
L.listsize += LISTINCREMENT;
}

p = L.elem+i-1;
for( q = L.elem + L.length - 1; q>= p; --q )
{
*(q+1) = *q;
}
*p = e;
++L.length;

return OK;
}

Status CrtList(SqList &L)    // 从键盘输入数据生成线性表
{
printf("输入整数，以0结束:\n");
ElemType e;
int i = 1;
scanf("%d", &e);
while (e != 0)
{
if (!ListInsert(L, i, e)) return ERROR;
i++;
scanf("%d", &e);
}
return OK;
}

Status CrtList2(SqList &L, ElemType d[], int n)    // 从数组生成线性表
{
int i;
for (i = 0; i < n; ++i)
{
if (!ListInsert(L, i + 1, d[i])) return ERROR;
}
return OK;
}

Status ListDelet(SqList &L, int i, ElemType &e)    //删除
{
if ((i<1) || (i>L.length)) return ERROR;
ElemType *p, *q;
p = &(L.elem[i - 1]);
e = *p;
q = L.elem + L.length - 1;
for (++p; p <= q; ++p) *(p - 1) = *(p);
--L.length;
return OK;
}

Status GetElem(SqList &L, int i, ElemType &e)    //取值
{
if ((i <= 0) || (i>L.length)) return ERROR;
else
{
e = L.elem[i - 1];
return OK;
}
}

Status compare(ElemType a, ElemType b)    //比较
{
if (a == b) return TRUE;
else return FALSE;
}

int LocateElem(SqList L, ElemType e)    //定位
{
Status compare(ElemType a, ElemType b);
int i;
for (i = 0; i<L.length; i++)
{
if (compare(L.elem[i], e))
return ++i;
}
if (i == L.length) return 0;
}

Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e)    //求直接前驱
{
int LocateElem(SqList L, ElemType e);
int i = LocateElem(L, cur_e);
if ((i == 0) || (i == 1)) return ERROR;
pre_e = L.elem[i - 2];
return OK;
}

int ListLength(SqList L)    //求长度
{
int length = L.length;
return length;
}

void MergeList(SqList La, SqList Lb, SqList &Lc)    //归并
{
Lc.length = La.length + Lb.length;
Lc.listsize = Lc.length;
Lc.elem = (ElemType*)malloc(Lc.length*sizeof(ElemType));
if (Lc.elem == NULL) exit(OVERFLOW);
int i, j, k;
for (i = 0, j = 0, k = 0; (i<La.length) && (j<Lb.length); k++)
{
if (La.elem[i]<Lb.elem[j])
{
Lc.elem[k] = La.elem[i];
i++;
}
else
{
Lc.elem[k] = La.elem[j];
j++;
}
}
while (i<La.length)
{
Lc.elem[k] = La.elem[i];
i++;
k++;
}
while (j<Lb.length)
{
Lc.elem[k] = Lb.elem[j];
j++;
k++;
}
}

void vist(ElemType e)
{
printf("%d ", e);
}

Status ListTraverse(SqList L)    //遍历
{
int i;
if (L.length == 0) printf("无元素");
for (i = 0; i<L.length; i++)
{
vist(L.elem[i]);
}
if (i == L.length)
{
printf("\n");
return OK;
}
else return ERROR;
}

Status ListClear(SqList L)    //清空
{
if (L.elem == NULL) return ERROR;
int i;
for (i = 0; i<L.length; i++) L.elem[i] = 0;
L.length = 0;
return OK;
}

Status DestroyList(SqList &L)    //销毁
{
if (L.elem == NULL) return ERROR;
free(L.elem);
L.length = 0;
L.listsize = 0;
return OK;
}

void PrnList(SqList L)    //打印
{
int i;
for (i = 0; i < L.length; ++i){
printf("%5d", L.elem[i]);
}
printf("\n");
}

int main()
{
int j, l;
ElemType e, e1;
SqList La;
if (InitList(La)) printf("OK\n");
else exit(INFEASIBLE);

CrtList(La);
PrnList(La);

int k;
printf("1:判空\n2:插入\n3:删除\n4:定位\n5:求长度\n6:直接前驱\n");
printf("7:归并\n8:遍历\n9:清空\n10:销毁\n\n0:退出\n");
scanf("%d", &k);
while (k != 0)
{
switch (k)
{
case 1:
if (ListEmpty(La)) printf("empty\n");
else printf("non-empty\n");
break;
case 2:
printf("在第几个位置插入何数：");
scanf("%d%d", &j, &e);
if (ListInsert(La, j, e)) printf("OK\n");
else printf("ERROR\n");
break;
case 3:
printf("删除第几个数：");
scanf("%d", &j);
if (ListDelet(La, j, e))
PrnList(La);
printf("删除数为：%d\n", e);
break;
case 4:
printf("定位数字：");
scanf("%d", &e);
if (LocateElem(La, e) != 0) printf("OK,位序为：%d\n", LocateElem(La, e));
else printf("ERROR\n");
break;
case 5:
l = ListLength(La);
printf("ListLength=%d\n", l);
break;
case 6:
printf("寻找何数直接前驱:");
scanf("%d", &e);
if (PriorElem(La, e, e1)) printf("前驱为:%d\n", e1);
else printf("ERROR\n");
break;
case 7:
SqList Lb, Lc;
if (InitList(Lb)) printf("OK\n");
else printf("ERROR\n");
CrtList(Lb);
MergeList(La, Lb, Lc);
printf("有序归并后：\n");
PrnList(Lc);
break;
case 8:
if (ListTraverse(La)) printf("遍历成功\n");
else printf("遍历失败\n");
break;
case 9:
if (ListClear(La)) printf("清空成功\n");
else printf("清空失败\n");
break;
case 10:
if (DestroyList(La)) printf("销毁完成\n");
else printf("销毁失败\n");
return 0;
default:
printf("ERROR\n");
}
scanf("%d", &k);
}
return 0;
}```

```#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define TRUE 1
#define FALSE 0

#define INIT_LIST_SIZE 100
#define LISTINCREMENT 10

typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList *L)
{
L->elem = (ElemType*)malloc(INIT_LIST_SIZE*sizeof(ElemType));
if (!L->elem) exit(OVERFLOW);
L->length = 0;
L->listsize = INIT_LIST_SIZE;
return OK;
}

Status ListEmpty(SqList L)    //判空
{
if (L.length = 0) return TRUE;
else return FALSE;
}

Status ListInsert(SqList *L, int i, ElemType e)    //插入
{
ElemType *newbase, *q, *p;
if (i<1 || i>L->length + 1) return ERROR;
if (L->length>L->listsize)
{
newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMENT)*sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
q = L->elem + i - 1;    //q为插入位置
for (p = L->elem + L->length - 1; p >= q; p--)
{
*(p + 1) = *p;
}
*q = e;
++L->length;
return OK;
}

Status ListDelete(SqList *L, int i, ElemType * e)    //删除
{
ElemType * p, *q;
if (i<1 || i>L->length) return ERROR;
p = L->elem + i - 1;    //p为被删除元素位置
*e = *p;    //被删除元素的值赋值给e
q = L->elem + L->length - 1;    //表尾元素位置
for (++p; p <= q; ++p)
{
*(p - 1) = *p;
}
L->length--;
return OK;
}

Status GetElem(SqList *L, int i, ElemType * e)    //取值
{
if (i<1 || i>L->length) return ERROR;
*e = *(L->elem + i - 1);    //获取第i个元素的地址
return OK;
}

int LocateElem(SqList L, ElemType e)    //定位
{
int i;
for (i = 0; i<L.length; i++)
{
if (L.elem[i]==e)
return ++i;
}
if (i == L.length) return 0;
}

Status PriorElem(SqList L, ElemType e, ElemType &pre_e)    //求直接前驱
{
int LocateElem(SqList L, ElemType e);
int i = LocateElem(L, e);
if ((i == 0) || (i == 1)) return ERROR;
pre_e = L.elem[i - 2];
return OK;
}

Status GetLength(SqList *L)    //求长度
{
return L->length;
}

void PrnList(SqList *L)    //遍历
{
int i;
for (i = 0; i<(*L).length; i++)
{
if (i == 0)printf("(");
printf(" %d ", L->elem[i]);
if (i == (*L).length - 1)printf(")\n");
}
}

Status ClearList(SqList *L)    //清空
{
L->length = 0;
return OK;
}

Status Destroy(SqList *L)    //销毁
{
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
return OK;
}

int main()
{
int n = 0, rc;
int a, i;
int e, e1;

SqList L;
if (InitList(&L)) printf("OK\n");

FILE *fp = fopen("D:/1.txt", "r");
if (fp == NULL)
{
printf("打开文件失败");
}
printf("从1.txt文件读入几个数：");
scanf("%d", &n);
for (i = 0; i< n; i++)
{
fscanf(fp, "%d", &a);
ListInsert(&L, i+1, a);
}
fclose(fp);
PrnList(&L);

char k;
printf("\n1.插入\n2.删除\n3.取值\n4.定位\n5.直接前驱\n6.求长度\n7.遍历\n8.清空\n9.销毁\n");
while (1){
k = getchar();
switch (k){
case '1':
printf("在第几个位置插入何数：");
scanf("%d%d", &i, &e);
if (ListInsert(&L, i, e))printf("i=%d,e=%d 已经插入\n", i, e);
else printf("插入失败\n");
break;
case '2':
printf("删除第几个数：\n");
scanf("%d", &i);
if (ListDelete(&L, i, &e))printf("i=%d,e=%d 已经删除\n", i, e);
else printf("删除失败\n");
break;
case '3':
printf("取第几个数：\n");
scanf("%d", &i);
if (GetElem(&L, i, &e))printf("第i=%d号,e=%d 被取出！\n", i, e);
else printf("取值失败\n");
break;
case '4':
printf("定位数字：");
scanf("%d", &e);
if (LocateElem(L, e) != 0) printf("OK,位序为：%d\n", LocateElem(L, e));
else printf("ERROR\n");
break;
case '5':
printf("寻找何数直接前驱:");
scanf("%d", &e);
if (PriorElem(L, e, e1)) printf("前驱为:%d\n", e1);
else printf("ERROR\n");
break;
case '6':
printf("表长为%d\n", GetLength(&L));
break;
case  '7':
printf("遍历：\n");
PrnList(&L);
break;
case '8':
if (ClearList(&L)) printf("清空成功\n");
else printf("清空失败\n");
break;
case '9':
printf("销毁\n");
Destroy(&L);
printf("销毁成功\n");
exit(0);
return 0;
}
}
return 0;
}```

```#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define TRUE 1
#define FALSE 0

#define INIT_LIST_SIZE 100
#define LISTINCREMENT 10

typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}Sqlist;

Status InitList(Sqlist *L)
{
L->elem = (ElemType *)malloc(INIT_LIST_SIZE *sizeof(ElemType));
if (!L->elem)exit(OVERFLOW);
L->length = 0;
L->listsize = INIT_LIST_SIZE;
return OK;
}

Status ListEmpty(Sqlist L)
{
if (L.length = 0)return ERROR;
else return FALSE;
}

Status ListInsert(Sqlist *L, int i, ElemType e)
{
ElemType *newbase, *p, *q;
if (i<1 || i>L->length + 1)return ERROR;
if (L->length > L->listsize)
{
newbase = (ElemType *)realloc(L, (L->listsize + LISTINCREMENT)*sizeof(ElemType));
if (!newbase)exit(OVERFLOW);
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
p = L->elem + i - 1;
for (q = L->elem + L->length - 1; q >= p; q--)
{
*(q + 1) = *q;
}
*p = e;
L->length++;
return OK;
}

Status CreateList(Sqlist *L, ElemType element[], int n)    // 从数组生成线性表
{
int i;
for (i = 0; i < n; ++i)
{
if (!ListInsert(L, i + 1, element[i])) return ERROR;
}
return OK;
}

Status ListDelete(Sqlist *L, int i, ElemType *e)
{
ElemType *p, *q;
if (i<1 || i>L->length)return ERROR;
p = L->elem + i - 1;
q = L->elem + L->length - 1;
*e = *p;
for (p++; q >= p; p++)
{
*(p - 1) = *p;
}
L->length--;
return OK;
}

Status GetElem(Sqlist *L, int i, ElemType *e)
{
if (i<1 || i>L->length)return ERROR;
*e = L->elem[i - 1];
return OK;
}

int LocateElem(Sqlist L, ElemType e)
{
int i;
for (i = 0; i < L.length; i++)
if (L.elem[i] == e)return i + 1;
}

Status PriorElem(Sqlist L, ElemType e, ElemType &pr_e)
{
int LocateElem(Sqlist L, ElemType e);
int i = LocateElem(L, e);
if (i<1 || i>L.length)return ERROR;
pr_e = L.elem[i - 2];
return OK;
}

Status GetLength(Sqlist *L)
{
return L->length;
}

void PrnList(Sqlist *L)
{
int i;
for (i = 0; i < L->length; i++)
printf("%d ", L->elem[i]);
printf("\n");
}

Status ClearList(Sqlist *L)
{
L->length = 0;
return OK;
}

Status Destroy(Sqlist *L)
{
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
return OK;
}

int main()
{
int i;
int a, n = 0;
int e, e1;

Sqlist L;
ElemType element[] = { 15, 3, 59, 27, 8, 11, 32 };
if (InitList(&L))printf("OK\n");

CreateList(&L, element, 7);
PrnList(&L);

char k;
printf("\n1.插入\n2.删除\n3.取值\n4.定位\n5.直接前驱\n6.求长度\n7.遍历\n8.清空\n9.销毁\n");
while (1)
{
k = getchar();
switch (k)
{
case '1':
printf("在第几个位置插入何数：");
scanf("%d%d", &i, &e);
if (ListInsert(&L, i, e))printf("i=%d e=%d已经插入\n", i, e);
break;
case '2':
printf("删除第几个数：");
scanf("%d", &i);
if (ListDelete(&L, i, &e))printf("i=%d e=%d已经删除\n", i, e);
break;
case '3':
printf("取第几个数：");
scanf("%d", &i);
if (GetElem(&L, i, &e))printf("第i=%d e=%d已经取出\n", i, e);
break;
case '4':
printf("定位何数：");
scanf("%d", &e);
if (LocateElem(L, e))printf("位序为：%d\n", LocateElem(L, e));
break;
case '5':
printf("寻找何数的直接前驱：");
scanf("%d", &e);
if (PriorElem(L, e, e1))printf("前驱为：%d\n", e1);
break;
case '6':
printf("表长为：%d\n", GetLength(&L));
break;
case '7':
printf("遍历：\n");
PrnList(&L);
break;
case '8':
if (ClearList(&L))printf("清空成功！\n");
break;
case '9':
if (Destroy(&L))printf("销毁成功！\n");
exit(0);
return 0;
}
}
return 0;
}```

## C语言实现顺序表的基本操作（从键盘输入 生成线性表，读txt文件生成线性表和数组生成线性表----三种写法）的更多相关文章

1. C语言学习笔记 (006) - 二维数组传参的三种表现形式

# include <stdio.h> # include <stdlib.h> # define M # define N int getdate(int (*sp)[M]) ...

2. c语言实验报告（四） 从键盘输入字符串a和字符串b，并在a串中的最小元素（不含结束符）后面插入字符串b.

a串中最小元素后的字符被舍弃了. #include<stdio.h>#include<string.h>void main(){  int i,min=0;  char a[2 ...

3. SQL Server三种表连接原理

在SQL Server数据库中,查询优化器在处理表连接时,通常会使用一下三种连接方式: 嵌套循环连接(Nested Loop Join) 合并连接 (Merge Join) Hash连接 (Hash ...

4. css三种样式表写法

css三种样式表:1.内嵌样式表: <head>  <style type="text/css">   样式表写法  </style> < ...

5. 顺序表的基本操作【c语言】【创建、插入、删除、输出】

作为数据结构初学者,上课时对一些知识点掌握得不是很透彻,所以利用课余时间通过微博平台总结所学知识,加深对知识的见解,记录学习历程便于后需要时参考. #include<stdio.h> #i ...

6. C语言实现顺序表（顺序存储结构）

顺序表(顺序存储结构)及初始化过程详解 顺序表,全名顺序存储结构,是线性表的一种.通过<线性表>一节的学习我们知道,线性表用于存储逻辑关系为"一对一"的数据,顺序表自然 ...

7. 顺序栈的基本操作(C语言)

由于现在只学了C语言所以就写这个C语言版的栈的基本操作 这里说一下 :网上和书上都有这种写法 int InitStack(SqStack &p) &p是取地址  但是这种用法好像C并不 ...

8. C语言实现顺序表

C语言实现顺序表代码 文件SeqList.cpp #pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqLi ...

9. C++语言实现顺序表

C++语言实现顺序表 顺序表的定义及其特点 顺序表的定义是:把线性表中的所有表项按照其逻辑顺序依次存储到从计算机存储中指定存储位置开始的一块连续的存储空间中. 这样,线性表中第一个表项的存储位置就是被 ...

## 随机推荐

1. [译]JavaScript:将字符串两边的双引号转换成单引号

原文:http://ariya.ofilabs.com/2012/02/from-double-quotes-to-single-quotes.html 代码的不一致性总是让人发狂,如果每位开发者都能 ...

2. 在matlab2015b中配置vlfeat-0.9.18

参考链接: 1.http://cnyubin.com/?p=85                2.http://www.cnblogs.com/woshitianma/p/3872939.html ...

3. 初试visual studio2012的新型数据库LocalDB

http://www.cnblogs.com/zhangran/archive/2012/08/21/2649200.html 今天在vs2012里面打开以前的mvc3项目,结果弹出警告说在vs201 ...

4. JSON简介以及用法代码汇总

什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: { &q ...

5. crontab的两大坑：百分号和环境变量

今天想给服务器加个自动备份mysql数据库的功能(别怪我这么久才加,阿里云每天全盘备份的,不怕丢数据库),本以为只要5分钟就能搞定的,结果入了两个大坑. 我的crontab是这样写的: * * * m ...

<!--创建需要显示的菜单按钮(munebutton),menu指定的是菜单项--><a href="javascript:void(0)" id="m ...

7. JavaWeb 乱码问题终极解决方案！

经常有读者在公众号上问 JavaWeb 乱码的问题,昨天又有一个小伙伴问及此事,其实这个问题很简单,但是想要说清楚却并不容易,因为每个人乱码的原因都不一样,给每位小伙伴都把乱码的原因讲一遍也挺费时间的 ...

8. ⌈洛谷4735⌋⌈BZOJ3261⌋最大异或和【可持久化01Trie】

题目链接 [BZOJ传送门] [洛谷传送门] 题解 终于学会了可持久化trie树了.感觉并不是特别的难. 因为可持久化,那么我们就考虑动态开点的trie树. 都知道异或操作是有传递性的,那么我们就维护 ...

9. linux:提取匹配含有小数点的数字（grep函数）

学艺不精,一开始用了 “grep -ne '46.5743' file.txt” 提取含有46.5743的小数不成功,后面查资料才知道正则表达式中,小数点代表的是:一定有一个任意字节. 正确的写法应该 ...

10. Linux安装npm并打包前端代码

查看node版本\$ node -v查看npm版本\$ npm -v如果没有安装node及npm,需要先安装node及npm#yum install node# yum install npm安装cnpm ...