线性表的顺序和链式存储

顺序

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;
	}
}
相关推荐
sheeta199811 小时前
LeetCode 每日一题笔记 日期:2025.11.24 题目:1018. 可被5整除的二进制前缀
笔记·算法·leetcode
是小胡嘛13 小时前
C++之Any类的模拟实现
linux·开发语言·c++
Want59516 小时前
C/C++跳动的爱心①
c语言·开发语言·c++
lingggggaaaa16 小时前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
phdsky16 小时前
【设计模式】建造者模式
c++·设计模式·建造者模式
H_-H16 小时前
关于const应用与const中的c++陷阱
c++
coderxiaohan16 小时前
【C++】多态
开发语言·c++
gfdhy17 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
百***060117 小时前
SpringMVC 请求参数接收
前端·javascript·算法
weixin_4577600017 小时前
Python 数据结构
数据结构·windows·python