线性表的顺序和链式存储

顺序

cpp 复制代码
#include<iostream>
#define MaxSize 50
using namespace std;
typedef int elemType;
//初始化线性表
typedef struct
{
	int length;
	elemType date[MaxSize];
}SqList;

//建立顺序表
void CreateList(SqList*& L, elemType a[], int n)
{
	int i = 0, k = 0;
	L =(SqList *) malloc(sizeof(SqList));
	while (i < n)
	{
		L->date[k] = a[i];
		k++, i++;

	}
	L->length = k;
}
//初始化线性表
void InitList(SqList*& L)
{
	L = (SqList*)malloc(sizeof(SqList));
	L->length = 0;

}
//销毁线性表
void DestroyList(SqList*& L)
{
	free(L);
}
//判断线性表是否为空
bool ListEmpty(SqList* L)
{
	return (L->length == 0);
}
//求线性表的长度
int ListLength(SqList* L)
{
	return (L->length);
}
//输出线性表
void DispList(SqList* L)
{
	for (int i = 0; i < L->length; i++)
	{
		cout << L->date[i]<<" ";
	}
}
//按序号求线性表中的元素
bool GetSqList(SqList* L, int i, elemType& e)
{
	if (i<1 || i>L->length)
	{
		return false;
	}
	e = L->date[i - 1];
	return true;

}
//按元素值查找
int LocateSqList(SqList* L, elemType e)
{
	int i = 0;
	for (i = 0; i < L->length; i++)
	{
		if (L->date[i] == e)
		{
			break;
		}
	}
	if (i >= L->length)
		return 0;
	else
		return i + 1;
}
//插入数据元素
bool ListInsert(SqList* L, int i, elemType e)
{
	if (i<1 || i>L->length + 1 || L->length == MaxSize)
	{
		return false;
	}
	i--;
	for (int j = L->length; j > i; j--)
	{
		L->date[j] = L->date[j - 1];
	}
	L->date[i] = e;
	L->length++;
	return true;
}
//删除数据元素
bool ListDelete(SqList* L, int i, elemType & e)
{
	if (i<1 || i>L->length||L->length==0)
	{
		return false;
	}
	i--;
	e = L->date[i];
	for (int j = i; j <L->length-1; j++)
	{
		L->date[j] = L->date[j + 1];
	}
	L->length--;
	return true;
}
int main()
{
	SqList* L;
	int date[10];
	for (int i = 0; i < 10; i++)
	{
		date[i] = i + 1;
	}
	int  n = sizeof(date)/sizeof(date[0]);
	CreateList(L, date, n);
	DispList(L);
	elemType e;
	GetSqList(L, 8, e);

	LocateSqList(L, 8);
	ListInsert(L,4, -1);
	elemType s;
	ListDelete(L, 10, s);
		DispList(L);
	DestroyList(L);
	return 0;
}


//删除所有等于n的元素
void delnode(SqList*& L, int n)
{
	int i = 0;
	for (int k = 0; k < L->length; k++)
	{
		if (L->date[k] != n)
		{
			L->date[i] = L->date[k];
			i++;
		}
	}
	L->length = i;
}

链式

cpp 复制代码
#include<iostream>
using namespace std;
typedef int elemType;
typedef struct LNode
{
	elemType date;
	struct LNode* next;
}LinkNode;
//头插法
void CreateListF(LinkNode*& L, elemType a[], int n)
{
	LinkNode* s;
	L = (LinkNode*)malloc(sizeof(LinkNode));
	L->next = NULL;
	for (int i = 0; i < n; i++)
	{
		s= (LinkNode*)malloc(sizeof(LinkNode));
		s->date = a[i];
		s->next = L->next;
		L->next = s;
	}
}
//尾插法
void CreateListR(LinkNode*& L, elemType a[], int n)
{
	LinkNode* s, * r;//s 负责承载新创建的元素节点,r 负责跟踪链表的尾部位置
	L= (LinkNode*)malloc(sizeof(LinkNode));
	r = L;
	for (int i = 0; i < n; i++)
	{
		s= (LinkNode*)malloc(sizeof(LinkNode));
		s->date = a[i];
		r->next = s;
		r = s;
	}
	r->next = NULL;
}
//初始化线性表
void InitList(LinkNode*& L)
{
	L = (LinkNode*)malloc(sizeof(LinkNode));
	L->next = NULL;
}
//销毁线性表
void DestroyList(LinkNode*& L)
{
	LinkNode* pre = L, * p = L->next;
	while (p != NULL)
	{
		free(pre);
		pre = p;
		p = p->next;
	}
	free(pre);
}
//判断线性表是否为空
bool ListEmpty(LinkNode* L)
{
	return (L->next == NULL);
}
//求线性表的长度
int ListLength(LinkNode*& L)
{
	int n=0;
	LinkNode* p = L;
	while (p->next!= NULL)
	{
		n++;
		p = p->next;
	}
	return (n);
}
//输出线性表
void DispList(LinkNode*& L)
{
	LinkNode* p = L->next;
	while (p != NULL)
	{
		cout << p->date << " ";
		p = p->next;
	}
}
//按序号求线性表中的元素
bool Getelem(LinkNode* L, int i, elemType & e)
{
	int j = 0;
	LinkNode* p = L;
	if (i <= 0)
	{
		return false;
	}
	while (j < i && p != NULL)
	{
		j++;
		p = p->next;
	}
	if (p == NULL)
	{
		return false;
	}
	else
	{
		e = p->date;
		return true;
	}
}
//按元素值查找
int LocateList(LinkNode* L, elemType e)
{
	int i = 1;
	LinkNode* p = L->next;
	while (p != NULL && p->date != e)
	{
		i++;
		p = p->next;
	}
	if (p == NULL)
	{
		return 0;
	}
	else
	{
		return i;
	}
}
//插入数据元素
bool ListInsert(LinkNode*& L, int i, elemType e)
{
	int j = 0;
	LinkNode* p = L,*s;
	if (i <= 0)
	{
		return false;
	}
	while (j < i - 1 && p != NULL)
	{
		j++;
		p = p->next;
	}
	if (p == NULL)
	{
		return false;
	}
	else
	{
		s=(LinkNode*)malloc(sizeof(LinkNode));
		s->date = e;
		s->next = p->next;
		p->next = s;
		return true;
	}
	
}
//删除数据元素
bool ListDelete(LinkNode*& L, int i, elemType& e)
{
	int j = 0;
	LinkNode* p = L, * q;
	if (i <= 0)
	{
		return false;
	}
	while (j < i - 1 && p != NULL)
	{
		j++;
		p = p->next;
	}
	if (p == NULL)
	{
		return false;
	}
	else
	{
		q = p->next;
		if (q == NULL)
		{
			return false;
		}
		e = q->date;
		p->next = q->next;
		free(q);
		return true;
	}
}
相关推荐
小苏兮3 小时前
【C++】stack与queue的使用与模拟实现
开发语言·c++
未知陨落3 小时前
LeetCode:95.编辑距离
算法·leetcode
杨小码不BUG3 小时前
小鱼的数字游戏:C++实现与算法分析(洛谷P1427)
c++·算法·数组·信奥赛·csp-j/s
高山有多高3 小时前
栈:“后进先出” 的艺术,撑起程序世界的底层骨架
c语言·开发语言·数据结构·c++·算法
普罗米修斯3 小时前
C++ 设计模式理论与实战大全【共73课时】
c++·后端
YouEmbedded3 小时前
解码查找算法与哈希表
数据结构·算法·二分查找·散列表·散列查找·线性查找
普罗米修斯3 小时前
C++ 设计模式原理与实战大全-架构师必学课程 | 完结
c++·后端
greentea_20134 小时前
Codeforces Round 65 C. Round Table Knights(71)
c语言·开发语言·算法
小秋学嵌入式-不读研版4 小时前
C61-结构体数组
c语言·开发语言·数据结构·笔记·算法