顺序表和链表的代码实现

线性表代码实现

1.顺序表代码实现(c语言)

#include <stdio.h>
#include <assert.h>
#include<errno.h>
#include <stdlib.h>
#include<string.h>

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	size_t size;
	size_t capacity; // unsigned int
}SeqList;

// 对数据的管理:增删查改 
void SeqListInit(SeqList* ps);	//初始化	
void SeqListDestroy(SeqList* ps);

void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);

// 检查空间,如果满了进行增容
void CheckCapacity(SeqList* ps);

void SeqListInit(SeqList* ps)
{
	printf("please input number of capacity");
	scanf("%d", &ps->capacity);
	ps->a = (SLDateType *)malloc(sizeof(SLDateType) * ps->capacity);
	ps->size = 0;
	return;
}

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	int i;
	for (i = 0; i < ps->size; ++i)
		printf("%d ", ps->a[i]);
	printf("\n");
}

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	//判断capacity是否足够
	CheckCapacity(ps);
	ps->a[ps->size]=x;
	ps->size++;
	return;
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	CheckCapacity(ps);
	//先将所有数向后移
	for (int i = ps->size-1; i >=0; --i)
		ps->a[i+1] = ps->a[i];
	//将第一个位置加入数
	ps->a[0] = x;
	ps->size++;
	return;
}

void SeqListPopFront(SeqList* ps)
{
	//判断是否表为空
	if (ps->size <= 0)
		return;
	for (int i = 0; i < ps->size-1; ++i)
		ps->a[i] = ps->a[i + 1];
	ps->size--;
}

void SeqListPopBack(SeqList* ps)
{
	//判断是否表为空
	if (ps->size <= 0)
		return;
	ps->size--;
}

// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
	//判断顺序表是否为空
	if (ps->size <= 0)
	{
		printf("此表为空\n");
		return -1;
	}
	for (int i = 0; i < ps->size; ++i)
		if (ps->a[i] == x)
			return i;
	printf("数据未在表中\n");
	return -1;
}

// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
	//先判断表有无足够空间
	CheckCapacity(ps);
	//判断插入位置是否为可插入位置
	if (ps->size < pos)
	{
		printf("插入位置不符合要求\n");
		return;
	}
	//将插入位置让出,其他数据向后移
	for (int i = ps->size-1; i >=pos-1; --i)
		ps->a[i + 1] = ps->a[i];
	ps->a[pos - 1] = x;
	ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
	//判断表是否为空,判断删除位置是否可删除
	if (ps->size <= 0||ps->size<=pos)
	{
		printf("此表为空\n");
		return;
	}
	for (int i = pos - 1; i < ps->size-1; ++i)
		ps->a[i] = ps->a[i + 1];
}

void CheckCapacity(SeqList* ps)
{
	if (ps->capacity == ps->size)
	{
		SLDateType* new_a = (SLDateType*)realloc(ps->a, (2 * ps->capacity) * sizeof(SLDateType));
		//判断是否申请成功
		if (new_a != NULL)
		{
			ps->a = new_a;
			ps->capacity *= 2;
			return;
		}
		else
		{
			printf("%s\n", strerror(errno));
		}
	}
	else
	{
		return;
	}
}
//测试部分
int main()
{
	SeqList SL;
	SeqListInit(&SL);	//初始化	
	//SeqListDestroy(&ps);

	
	SeqListPushBack(&SL,4);
	SeqListPushBack(&SL, 5);
	SeqListPushBack(&SL, 6);
	SeqListPushBack(&SL, 7);
	SeqListPushBack(&SL, 8);
	SeqListPushFront(&SL, 3);
	SeqListPushFront(&SL, 2);
	SeqListPushFront(&SL, 1);
	
	SeqListPrint(&SL);
	
	SeqListPopFront(&SL);
	SeqListPrint(&SL);
	
	SeqListPopBack(&SL);
	SeqListPrint(&SL);
	// 顺序表查找
	SeqListFind(&SL,4);
	// 顺序表在pos位置插入x
	SeqListInsert(&SL,4,9);
	// 顺序表删除pos位置的值
	SeqListErase(&SL,5);
	SeqListPrint(&SL);
	return 0;
}

在这里插入图片描述

2.链表代码实现

单链表(C语言)

#include<stdio.h>
#include<assert.h>
#include<malloc.h>

typedef int SLTDateType;
typedef struct SListNode
{
	SLTDateType data;
	struct SListNode* next;
}SListNode;

// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x);
// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);
// 单链表的尾删
void SListPopBack(SListNode** pplist);
// 单链表头删
void SListPopFront(SListNode** pplist);
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);
// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入?
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?
void SListEraseAfter(SListNode* pos);
// 单链表的销毁
void SListDestroy(SListNode** pplist);

SListNode* BuySListNode(SLTDateType x)
{
	SListNode* new_node = (SListNode*)malloc(sizeof(SListNode));
	new_node->data = x;
	new_node->next = NULL;
	return new_node;
}

void SListPrint(SListNode* plist)
{
	if (plist == NULL)
	{
		printf("该链表为空\n");
		return;
	}
	while (plist != NULL)
	{
		printf("%d ", plist->data);
		plist = plist->next;
	}
	printf("\n");
}

void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	//如果为空链表
	if (*pplist == NULL)
	{
		*pplist = BuySListNode(x);
		return;
	}
	SListNode* p = *pplist;
	while (p->next != NULL)
	{
		p = p->next;
	}
	p->next = BuySListNode(x);
}

void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		*pplist = BuySListNode(x);
		return;
	}
	SListNode* new_node = BuySListNode(x);
	new_node->next = *pplist;
	*pplist = new_node;
}

void SListPopBack(SListNode** pplist)
{
	assert(pplist != NULL);
	if (*pplist == NULL)
		return;
	SListNode* p = *pplist;
	SListNode* temp = NULL;
	while (p->next != NULL)
	{
		temp = p;
		p = p->next;
	}
	temp->next = NULL;
	free(p);
}

void SListPopFront(SListNode** pplist)
{
	assert(pplist != NULL);
	if (*pplist == NULL)
		return;
	SListNode* p = (*pplist)->next;
	free(*pplist);
	*pplist = p;
}

SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	if (plist == NULL)
	{
		printf("该链表为空表\n");
		return plist;
	}
	SListNode* p = plist;
	while (p == NULL || p->data == x)
		p = p->next;
	if (p->data == x)
		return p;
	else
	{
		printf("未找到\n");
		return plist;
	}
}

void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	if (pos == NULL)
	{
		pos->next = BuySListNode(x);
		return;
	}
	SListNode* new_node = BuySListNode(x);
	new_node->next = pos->next;
	pos->next = new_node;
}

void SListEraseAfter(SListNode* pos)
{
	if (pos == NULL)
		return;
	SListNode* p = pos->next;
	pos->next = p->next;
	free(p);
}

void SListDestroy(SListNode** pplist)
{
	assert(pplist);
	if (*pplist == NULL)
		return;
	SListNode* p=*pplist;
	while (*pplist != NULL)
	{
		p = p->next;
		free(*pplist);
		*pplist = p;
	}
}

//测试
int main()
{
	SListNode* p = NULL;
	SListPushBack(&p, 1);
	SListPushBack(&p, 2);
	SListPushBack(&p, 3);
	SListPushBack(&p, 4);
	SListPushBack(&p, 5);
	SListPushBack(&p, 6);
	SListPushBack(&p, 7);
	SListPrint(p);
	// 单链表的头插
	SListPushFront(&p,0);
	// 单链表的尾删
	SListPopBack(&p);
	SListPrint(p);
	// 单链表头删
	SListPopFront(&p);
	SListPrint(p);
	// 单链表查找
	SListNode* p1=SListFind(p, 5);
	// 单链表在pos位置之后插入x
	// 分析思考为什么不在pos位置之前插入?
	SListInsertAfter(p1, 9);
	// 单链表删除pos位置之后的值
	// 分析思考为什么不删除pos位置?
	SListPrint(p);
	SListEraseAfter(p1);
	SListPrint(p);
	// 单链表的销毁
	SListDestroy(&p);
	SListPrint(p);
	return 0;
}

在这里插入图片描述

双向链表(c++)

#include<iostream>
#include<assert.h>

using namespace std;

// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType _data;
	struct ListNode* _next;
	struct ListNode* _prev;
}ListNode;

// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);

ListNode* ListCreate()
{
	ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
	if (new_node == NULL)
	{
		assert(0);
		return NULL;
	}
	new_node->_data = 0;
	new_node->_next = NULL;
	new_node->_prev = NULL;
	return new_node;
}

void ListDestory(ListNode* pHead)
{
	assert(pHead);
	ListNode* p = pHead->_next;
	while (p)
	{
		ListNode* temp = p->_next;
		free(p);
		p = temp;
	}
}

void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* p = pHead->_next;
	while (p)
	{
		cout << p->_data << " ";
		p = p->_next;
	}
	cout << endl;
}

void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* p = pHead->_next;
	ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
	if (new_node == NULL)
	{
		assert(0);
		return;
	}
	new_node->_data = x;
	new_node->_next = NULL;
	new_node->_prev = NULL;
	

	//判断是否为空
	if (p == NULL)//表示链表为空链表
	{
		pHead->_next = new_node;
		new_node->_prev = pHead;
		return;
	}
	while (p->_next)
		p = p->_next;
	p->_next = new_node;
	new_node->_prev = p;
}

void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	ListNode* p = pHead->_next;
	if (p == NULL)//表示没有可删除结点
	{
		cout << "the list is empty" << endl;
		return;
	}
	while (p)
		p = p->_next;
	free(p);
}

void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
	if (new_node == NULL)
	{
		assert(0);
		return;
	}
	new_node->_data = x;
	new_node->_next = NULL;
	new_node->_prev = NULL;

	new_node->_next = pHead->_next;
	pHead->_next->_prev = new_node;
	pHead->_next = new_node;
	new_node->_prev = pHead;
}

void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	if (pHead->_next == NULL)
	{
		cout << "the list is empty" << endl;
		return;
	}
	ListNode* p = pHead->_next;
	pHead->_next = p->_next;
	p->_next->_prev = pHead;
	free(p);
}

ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* p = pHead->_next;
	if (p == NULL)
	{
		cout << "the list is empty" << endl;
		return NULL;
	}
	while (p)
	{
		if (p->_data == x)
			return p;
		p = p->_next;
	}
	cout << "no the data in the list" << endl;
}

void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* new_node = (ListNode*)malloc(sizeof(ListNode));
	if (new_node == NULL)
	{
		assert(0);
		return;
	}
	new_node->_data = x;
	new_node->_next = NULL;
	new_node->_prev = NULL;
	
	new_node->_prev = pos->_prev;
	new_node->_next = pos;
	
	pos->_prev->_next = new_node;
	pos->_prev = new_node;
}

void ListErase(ListNode* pos)
{
	assert(pos);
	pos->_prev->_next = pos->_next;
	pos->_next->_prev = pos->_prev;
}

//测试
void test_list()
{
	ListNode* head = ListCreate();
	ListPushBack(head, 1);
	ListPushBack(head, 2);
	ListPushBack(head, 3);
	ListPrint(head);
	ListPushFront(head, 4);
	ListPushFront(head, 5);
	ListPrint(head);
	ListPopFront(head);
	ListPrint(head);
}

int main()
{
	test_list();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值