-
2019-01-26 21:02:39
线性表特点
1)存在惟一的被称作“第一个”的数据元素
2)存在惟一的被称作最后一个”的数据元素
3)除第一个外,集合中每个数据元素均只有驱
4)除最后一个外,集合中每个数据元素只有个后继
根据存储方式不同,分为顺序存储结构与链式存储结构。线性表的顺序表示指的是用一组地址连续的存储单元依次存储线性表的数据元素
#ifndef _LIST_H #define _LIST_H #define LIST_INIT_SIZE 10 //线性表初始化存储空间大小 #define LIST_INCREMENT 10 //线性表存储空间递增大小 typedef struct{ ElemType * elem; //线性表首地址 int length; //线性表当前使用长度 int size; //线性表当前最大长度 }LIST; LIST *InitList(); void FreeList(LIST *l); int InsertList(LIST * l,int i,ElemType *e); int DeleteList(LIST *l,int i); #endif //ElemType是实现数据元素类型,根据使用进行定义
线性表删除方法
int ListDelete(LIST *list,int i,ElemType *e){ //在顺序线性表list中删除第i个元素,并用e返回其值 ElemType *p=NULL,*q=NULL; //判断i的合法值为 1<=i<=list->length if(i<1||i>list->length) return 0; p=&list->elem[i-1]; *e=*p; q=list->elem+list->length-1; for(++p;p<q;++p) *(p-1)=*p; --list->length; return 1; }
释放内存方法
void FreeList(LIST *list){ free(list->elem); free(list); }
更多相关内容 -
线性表c语言实现
2020-01-15 17:53:27C语言实现线性表(顺序存储结构) linearTable.h #pragma once //线性表的实现 #define MAXSIZE 20 #define OK -1 #define ERROR -2 typedef struct list { int data[MAXSIZE]; //数组存储 int len; ...C语言实现线性表(顺序存储结构)
linearTable.h
#pragma once //线性表的实现 #define MAXSIZE 20 #define OK -1 #define ERROR -2 typedef struct list { int data[MAXSIZE]; //数组存储 int len; //线性表长度 }LIST_T; //线性表初始化 void initList(LIST_T *list); //根据下表查询数据 int getElem(LIST_T *list, int i); //根据数据查找下标 int getId(LIST_T *list, int e); //获取线性表长度 int getLen(LIST_T *list); //头插法 int addToHead(LIST_T *list, int e); //尾插法 int addToTail(LIST_T *list, int e); //指定位置插入 int addSpecific(LIST_T* list, int i, int e); //删除指定下标元素 int delElembyId(LIST_T *list, int i); //删除指定元素 int delElem(LIST_T *list, int e); //清空线性表 void clearList(LIST_T *list); //打印信息 void show(LIST_T *list);
linearTable.c
#include "linearTable.h" #include <stdio.h> //线性表初始化 void initList(LIST_T* list) { for (int i = 0; i < MAXSIZE; ++i) { list->data[i] = 0; } list->len = 0; } //根据下表查询数据 int getElem(LIST_T* list, int i) { if (i<0 || i>MAXSIZE || i > list->len) { return ERROR; } return list->data[i]; } //根据数据查找下标 int getId(LIST_T* list, int e) { for (int i = 0; i < list->len; ++i) { if (e == list->data[i]) { return i; } } return ERROR; } //获取线性表长度 int getLen(LIST_T* list) { return list->len; } //头插法 int addToHead(LIST_T* list, int e) { if (list->len < MAXSIZE) { for (int i = list->len; i >=0; --i) { list->data[i] = list->data[i-1]; } list->data[0] = e; list->len += 1; return OK; } return ERROR; } //尾插法 int addToTail(LIST_T* list, int e) { if (list->len < MAXSIZE) { list->data[list->len] = e; list->len += 1; return OK; } return ERROR; } //指定位置插入 int addSpecific(LIST_T* list, int i, int e) { if (i<0 || i>MAXSIZE || i > list->len) { return ERROR; } for (int count = list->len; count >= i; --count) { list->data[count] = list->data[count - 1]; } list->data[i] = e; ++list->len; return OK; } //删除指定下标元素 int delElembyId(LIST_T* list, int i) { if (i<0 || i>MAXSIZE || i > list->len) { return ERROR; } for (int count = i; count < list->len; ++count) { list->data[count-1] = list->data[count]; } --list->len; return OK; } //删除指定元素 int delElem(LIST_T* list, int e) { for (int count = 0; count < list->len; ++count) { if (list->data[count] == e) { for (int i = count; i < list->len; ++i) { list->data[i] = list->data[i+1]; } --list->len; break; } } return OK; } //清空线性表 void clearList(LIST_T* list) { for (int i = 0; i < list->len; ++i) { list->data[i] = 0; } list->len = 0; return OK; } //打印信息 void show(LIST_T* list) { for (int i = 0; i < list->len; ++i) { printf("NO%d:%d\n",i,list->data[i]); } printf("\n"); }
-
数据结构线性表c语言实现
2019-05-19 17:36:12线性链表的c语言实现代码具体如下 #include <malloc.h> #include <stdio.h> #include <stdlib.h> //函数结果状态码 #define ERROR 0 #define OK 1 #define INFEASIBLE -1 #define TRUE 1 #define ...线性链表的c语言实现代码具体如下
#include <malloc.h> #include <stdio.h> #include <stdlib.h> //函数结果状态码 #define OK 1 #define ERROR 0 #define false 0 #define true 1 #define INFEASIBLE -3 struct LNode { int data; struct LNode *next; } * LinkList; //初始化链表 LNode *InitList(int n) { LNode *p, *head; LinkList = head = (LNode *)malloc(sizeof(LNode)); for (int i = 0; i < n; i++) { p = (LNode *)malloc(sizeof(LNode)); printf("please input %dth number:", i + 1); scanf("%d", &p->data); LinkList->next = p; LinkList = p; } LinkList->next = NULL; return head; } //销毁一个已经存在的链表 int DestroyList(LNode *L) { LNode *q; while (q) { q = L->next; free(L); L = q; } return 100; } //置空一个链表 void ClearList(LNode *L) { LNode *p, *q; p = L->next; while (p) { q = p->next; free(p); p = q; } L->next = NULL; } //判断链表是否为空 int ListEmpty(LNode *L) { if (L->next) return false; else return true; } //判断链表长度 int ListLength(LNode *L) { int i = 0; LNode *p = L->next; while (p) { i++; p = p->next; } return i; } //用e返回指定顺序为i的值 int GetElem(LNode *L, int i, int e) { LNode *p = L; int j = 1; while (p->next && j < i - 1) { p = p->next; j++; } if (!p || j > i) { printf("error\n"); } e = p->data; return e; } //给定元素返回元素在链表中的位置 int LocatElem(LNode *L,int e) { int i = 0; LNode *p = L->next; while(p) { i++; if(p->data==e) { return i; } p = p->next; } return 0; } //给定当前元素返回前一个元素 int PriorElem(LNode *L,int cur_e) { int pri_e; LNode *q, *p = L->next; while(p->next) { q = p->next; if(q->data==cur_e) { pri_e = p->data; return pri_e; } p = q; } return INFEASIBLE; } //给定当前元素返回后一个元素 int NextElem(LNode *L,int cur_e) { int next_e; LNode *p = L->next; while(p->next) { if(p->data==cur_e) { next_e = p->next->data; return next_e; } p = p->next; } return INFEASIBLE; } //在指定位序前插入数据e int ListInsert(LNode *L, int i, int e) { int j = 0; LNode *s; LNode *p = L; while (p && j < i - 1) { p = p->next; j++; } if (!p || j > i - 1) printf("error\n"); s = (LNode *)malloc(sizeof(LNode)); s->data = e; s->next = p->next; p->next = s; return OK; } //指定位置为i删除数据并用e返回 int ListDelete(LNode *L, int i, int e) { int j = 0; LNode *q; LNode *p = L; while (p->next && j < i - 1) { p = p->next; j++; } if (!p->next || j > i - 1) { return ERROR; } q = p->next; p->next = q->next; e = q->data; free(q); return e; } //输出链表数据 void ListTraverse(LNode *L) { LNode *p = L->next; printf("the list is :"); while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { LNode *L = InitList(6); int e = 0; ListTraverse(L); printf("the length of list is:%d\n", ListLength(L)); ListInsert(L, 2, 88); printf("after insert 88 the result is:\n"); ListTraverse(L); int DeleNum = ListDelete(L, 1, e); printf("after delete the %dth number\nthe result is:", DeleNum); ListTraverse(L); int GetNum = GetElem(L, 5, e); printf("the num is: %d\n", GetNum); printf("the 6 is the %dth number in the list\n", LocatElem(L, 6)); printf("the number prior 4 is:%d(-3:not exit)\n",PriorElem(L, 4)); printf("the number next 5 is:%d\n",NextElem(L, 5)); if (DestroyList(L) == 100) printf("the list is destroyed\n"); system("pause"); }
结果如下:
注意事项:
1、在严蔚敏的数据结构(C语言版)这本书种:初始化一个空表是用 int InitList_Sq(SqList &L)表示。我们特别要注意,引用类型可以将形参变量带回主函数,但是这是C++支持的,C语言时不支持的,所有我在写的时候也掉到这个坑里面去了,代码改了好久才发现一直有问题出在哪里。
具体的说明可以看博客:
c++引用类型–将形参变量值带回主函数
2、在程序种有很多地方用到.和->,这两个的区别就是->前面是指向结构体的指针变量,而.前面需要时结构体变量
具体可以见:c语言中.与->的区别 -
顺序线性表C语言实现
2018-02-01 15:44:28代码是我参考了国嵌嵌入式数据结构教学视频及相关资料整理而成,内含详细的注释,我相信对于产品开发应用帮助很大。 -
线性表(c语言代码)
2018-03-14 22:28:44数据结构: C语言代码 线性表的创建、插入、查找、删除 -
//线性表的顺序存储 typedef Ptr SqListPtr; Status List_Init(SqListPtr L); //初始化线性表 Status List_Retrival(SqListPtr L, int pos, ElemType* elem); Status List_Locate(SqListPtr L, ElemType ...
-
(一)线性表C语言实现
2021-05-21 08:52:26线性表线性表是最简单最常用的一种数据结构C语言实现#include #include #define MAXSIZE 100#define true 1#define false 0typedef int status;typedef int ElemType;typedef struct List{ElemType elem[MAXSIZE+1];...线性表
线性表是最简单最常用的一种数据结构
C语言实现
#include
#include
#define MAXSIZE 100
#define true 1
#define false 0
typedef int status;
typedef int ElemType;
typedef struct List{
ElemType elem[MAXSIZE+1];
int length;
}List,*pList;
//基本操作
//创建长度为len的表并输入信息
pList InitList(int len){
pList L = (pList)malloc(sizeof(List));
L->length=len;
for(int i=1;i<=len;i++){
scanf("%d",&L->elem[i]);
}
return L;
}
//销毁线性表L
status DestroyList(pList L){
return true;
}
//清空表,使L成为一个空表
status ClearList(pList L){
if(L!=NULL){
}
return true;
}
//在第n个位置插入元素elem
ElemType InsertList(pList L,int n,int elem){
L->length++;
if(L->length>=MAXSIZE || n>L->length || n<=0){
printf("插入失败");
exit(-1);
}
for (int i = L->length; i > n; --i)
{
L->elem[i] = L->elem[i-1];
}
L->elem[n]=elem;
return elem;
}
//删除第n个元素
ElemType DeleteElem(pList L,int n){
L->length--;
ElemType elem = L->elem[n];
for (int i = n; i <= L->length; ++i){
L->elem[i]=L->elem[i+1];
}
return elem;
}
//遍历列表
status TraverseList(pList L){
for (int i = 1; i <= L->length; ++i)
{
printf("%d ",L->elem[i]);
}
printf("\n");
return true;
}
//把两个有序的表合起来,然后仍然有序
pList MergeList(pList L1,pList L2){
pList L3= (pList)malloc(sizeof(List));
int i=1;int j=1;int k=1;
while (i<=L1->length && j<=L2->length) {
if(L1->elem[i]elem[j]){
InsertList(L3,k,L1->elem[i]);
i++;
}else{
InsertList(L3,k,L2->elem[j]);
j++;
}
k++;
}
while(i<=L1->length){
InsertList(L3, k, L1->elem[i]);
i++;k++;
}
while(j<=L2->length){
InsertList(L3, k, L2->elem[j]);
j++;k++;
}
return L3;
}
int main(){
pList L1 = InitList(5);
pList L2 = InitList(6);
TraverseList(L1);
//插入10
InsertList(L1,2,10);
TraverseList(L1);
//删除第二个位置上的10
DeleteElem(L1,2);
TraverseList(L1);
pList L3 =MergeList(L1, L2);
TraverseList(L3);
}
实例
image.png
-
C语言实现线性表
2020-10-23 20:01:01#define MAXSIZE 100 //定义线性表最大长度 /*定义顺序表*/ typedef struct { int data[MAXSIZE]; //线性表占用的数组空间 int length; //存储线性表的长度 }SeqList; /*初始化顺序表*/ void InitList(SeqList... -
数据结构线性表用C语言实现
2018-07-06 11:08:49整个线性表的插入,删除,初始化等实现中需要注意的问题有: (1)在严蔚敏老师给出的C语言的数据结构书中:初始化一个空表是用 int InitList_Sq(SqList &L)表示的,但是我们需要注意的是,在C语言中,其实是没有... -
数据结构线性表C语言实现
2020-03-22 10:44:26//构造线性表 Status InitList(SqList *L) { L->elem = (ElemType*) malloc(sizeof(ElemType) * INITSIZE); if (!L->elem) exit(OVERFLOW); L->length = 0; L->listsize = INITSIZE; return OK; } //销毁... -
c语言实现线性表
2022-04-01 18:13:26线性表类型: 线性表的初始化: 线性表的尾插法: 线性表的打印 线性表检查增容 线性表尾删法 线性表的头插法 线性表的头删法 线性表的删除 指定位置插入 查找数据 ... -
我的线性表C语言实现包
2010-03-27 11:07:57俺是初学者 (*^__^*) 嘻嘻…… 大家别笑话 哈 -
线性表的C语言实现
2012-05-23 15:12:59C语言 数据结构 线性表 -
线性表及其应用C语言实现(数据结构复习最全笔记)(期末复习最新版)
2018-10-05 17:19:48一、顺序表的表示与实现 注意我写的代码和课本的差不多,和PPT的风格还是有些区别的,但本质没啥区别其实,你会哪个都成 1.线性表的顺序结构定义 #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量 #... -
线性表的代码实现(C语言)
2020-04-08 21:02:44一 、线性表顺序结构实现 初始化一个空的线性表 int InitList(SqList *L){ L->elem = (ElemType *) malloc(list_init_size * sizeof(ElemType)); if (!L->elem) { exit(OVERFLOW); } L->length = 0;... -
#include "LinkList.h" int main() { /* 链表测试代码 */ LinkList *head = InitLinkList(30); InsertLinkList(head, 35); InsertLinkList(head, 24); InsertLinkList(head, 25);... InsertLinkList(head, 56);...
-
线性表C语言数组实现
2019-12-01 12:44:50// 线性表的数组实现,下标从0开始! // 2019/12/1 // Leonwenbin #include <stdio.h> #include <stdlib.h> #define MAXLEN 100 #define ElementType int #define Position int struct LinearTable {... -
C语言版数据结构-C语言实现线性表,含详细的源代码
2020-12-15 15:02:51数据结构-C语言实现线性表 SqList.h头文件 /*文件名:SqList.h*/ #include <stdio.h> #include <stdlib.h> //函数结果状态代码 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define ... -
纯C语言实现线性表
2021-05-20 17:53:00//遍历线性表24 25 //初始化线性表,返回头指针 26 SqList* InitList(SqList *L){27 intx;28 int index = 0;29 30 L = (SqList *)malloc(sizeof(SqList));31 if(L){32 printf("输入到-1结束\n");33 while(1){34 scanf... -
数据结构 - 顺序线性表(C语言实现)
2019-04-05 09:26:40线性表的顺序表示和实现: 线性表的顺序表示指的是用一组地址连续的存储单元一次存储线性表的数据元素。换句话说,以元素在计算机内”物理位置相邻“来表示线性表中数据元素之间的逻辑关系。每一个数据元素的... -
【数据结构】顺序存储线性表C语言实现
2019-07-06 08:38:51* 文件名:顺序存储的线性表 * 基本操作: * InitList(&L) //构造一个空的线性表L * DestoryList(&L) //删除一个线性表 * ClearList(&L) //将线性表重置为空表 * ListEmpty(L) //线性表的探空操作 ... -
线性表C语言实现&调试
2017-03-17 10:36:15我最近在用C作为入门数据结构的编程语言,使用教材是《大话数据结构》。稍微比清华严蔚敏写的易读。就是有些例子举的过于通俗了,可以略微删减一些(或一大段)。...//叶大叔第一次数据结构:线性表linear // -
C语言实现线性表详解(完整代码有详细注释)
2022-04-10 17:35:04#define MAXSIZE 100//线性表的最大长度 typedef struct { int *elem; int length;//线性表的当前长度 //int maxlength;//线性表的最大长度 }SqList; SqList q; int i,k; //初始化函数 void initSqList... -
C语言实现的顺序线性表
2019-01-16 17:29:38用C语言实现了顺序线性表的基本操作(创建、插入数据、获取数据、删除数据、获取存放的数据量、清空线性表、销毁线性表) -
[简述]数据结构-线性表(c语言实现)
2018-04-22 01:48:45[简述]数据结构-线性表(c语言实现)second60 201804221. 线性表的定义线性表是具有相同特性的数据元素的一个有限序列。 2. 线性表抽象数据类型描述ADT List{数据对象:D = { ai | 1<= i <= n,n&... -
数据结构(c语言实现)——线性表
2020-07-15 10:53:51对线性表的操作:创建、删除、增、删、改、查。 //动态分配内存空间线性表 #include <stdio.h> #include <malloc.h> #include <string.h> #define max 51 //数据项最大存储空间 //线性表最大 #... -
线性表c语言实现----初始化插入删除
2017-03-07 14:38:19重温数据结构基础,记录之,好记性不如烂笔头 #include #include #define maxsize 100 typedef struct node{ int data[maxsize]; int length; ...void SeqListInit (SeqList *p,int n) /* seqlist init */