顺序表和链表

本文详细介绍了线性表(包括顺序表和动态扩展的顺序表)以及链表(单链表和双向链表)的概念、物理存储特点和常见操作,如增删查改。通过实例展示了如何实现这些数据结构及其相关函数。
摘要由CSDN通过智能技术生成

一、线性表

逻辑结构线性,物理存储不一定是线性的

常见线性表:顺序表、链表、栈、队列

二、顺序表

顺序表是用一段物理地址连续的存储单元一次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据赠删查改。

1.静态顺序表:使用定长数组存储元素

typedef struct Seqlist
{
      int data[50];//定长数组
      size_t size;//有效数据个数
}Seqlist;

2.动态数据表:使用动态开辟的数组存储

typedef struct Seqlist
{
     int* data;//指向动态开辟的数组
     size_t size;//有效数据个数
     size_t capicity;//空间大小
}Seqlist;

下面实现动态顺序表及相关增删查改功能

#include <assert.h>
#include<stdio.h>
typedef int SLDateType;
typedef struct SL
{
	SLDateType * a ;
	SLDateType size;
	SLDateType capacity;
}SeqList;
void SeqListInit(SeqList* ps)
{
	assert(ps);
	 if (ps->capacity < ps->size + 1)
	{
		 if (ps->capacity == 0)
			 ps->capacity = 1;
		 else
		ps->capacity = 2 * ps->capacity;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, ps->capacity * sizeof(SLDateType));
		assert(tmp);
		ps->a = tmp;
	}
}
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	int i = 0;
	for (i;i <ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	
	ps->a[ps->size] = x;
	ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	int i = ps->size;
	for (i; i >=0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	int i = 0;
	for (i; i<ps->size; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	ps->size--;
}

主函数

#include<stdio.h>
#include "sequence list.h"

int main()
{
	SeqList ps;
	ps.capacity = 0;
	ps.size = 0;
	ps.a = NULL;
	 SeqListInit(&ps);
	 SeqListPushBack(&ps, 5);
	 SeqListInit(&ps);
	 SeqListPushBack(&ps, 5);
	 SeqListInit(&ps);
	 SeqListPushBack(&ps, 5);
	 SeqListInit(&ps);
	 SeqListPushBack(&ps, 5);
	 SeqListInit(&ps);
	 SeqListPushFront(&ps, 6);
	 SeqListPopBack(&ps);
	 SeqListPopFront(&ps);
		 SeqListPrint(&ps);
	 SeqListDestroy(&ps);
	 return 0;
}

三、链表

链表:物理存储空间非连续、非顺序的存储结构、数据元素的逻辑顺序是通过链表的指针链接次序实现的。

单链表:前一个指向后一个,后一个不能访问前一个

双向链表:双向访问,每个节点可以访问前后

下面来实现一下单链表

typedef int SLTDateType;
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);
// 单链表查找




主函数与定义

#include <stdio.h>
#include"simpleList.h"
#include <assert.h>

void SListPushFront(SListNode** pplist, SLTDateType x)
{
	SListNode* p =(SListNode*)malloc(sizeof(SListNode));
	assert(p);
	if (*pplist == NULL)
	{
		*pplist = p;
		p->data = x;
		p->next = NULL;
	}
	else
	{
		SListNode* pp = *pplist;
		p->data = x;
		p->next = *pplist;
	}
	*pplist = p;

}
void SListPrint(SLTDateType x,SListNode* plist)
{
	int i = 0;
	for (i; i < x; i++)
	{
		printf("%d", plist->data);
		plist = plist->next;
	}
}
void SListPopFront(SListNode** pplist)
{
	assert(*pplist);
	SListNode* p = *pplist;
	*pplist = (*pplist)->next;
	free(p);
	p = NULL;
}
void SListPushBack(SListNode* plist,int x)
{
	assert(plist);
	SListNode* p = (SListNode*)malloc(sizeof(SListNode));
	while (plist->next != NULL)
	{
		plist = plist->next;
	}
	plist->next = p;
	p->data = x;
	p->next = NULL;
}
void SListPopBack(SListNode* pplist)
{
	while (pplist->next->next != NULL)
	{
		pplist = pplist->next;
	}
	free(pplist->next);
	pplist->next = NULL;
}

int main()
{
	SListNode* plist = NULL;
	SListPushFront(&plist,1);
	SListPushFront(&plist,2);
	SListPushFront(&plist,3);
	SListPushFront(&plist,4);
	SListPopFront(&plist);
	SListPushBack(plist,6);
	SListPopBack(plist);

	SListPrint(3,plist);

}

下面是双链表实现

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

// 创建返回链表的头结点.
ListNode* ListCreate()
{
	ListNode* head = (ListNode*)malloc(sizeof(ListNode));
	head->_next = head;
	head->_prev = head;
	return head;
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
	ListNode* head = pHead;
	head = head->_next;
	while (head != pHead)
	{
		printf("%d ", head->_data);
		head = head->_next;
	}
	head = head->_prev;
	while (head != pHead)
	{
		printf("%d ", head->_data);
		head = head->_prev;
	}
}
// 双向链表删除
void ListDistroy(ListNode* pHead)
{
	pHead->_next = pHead;
	pHead->_prev= pHead;
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	ListNode* p = pHead;
	while (p->_next != pHead)
	{
		p = p->_next;
	}
	ListNode* new = (ListNode*)malloc(sizeof(ListNode));
	new->_data = x;
	new->_prev = p;
	new->_next = p->_next;
	p->_next = new;
	pHead->_prev = new;

}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	ListNode* p = pHead;
	while (p->_next != pHead)
	{
		p = p->_next;
	}
	ListNode* pre = p->_prev;
	pre->_next = pHead;
	free(p);
	p = NULL;
	pHead->_prev = pre;
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	ListNode* new = (ListNode*)malloc(sizeof(ListNode));
	new->_data = x;
	new->_next = pHead->_next;
	pHead->_next = new;
	pHead->_next->_next->_prev = new;
	new->_prev = pHead;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	ListNode* p = pHead->_next;
	pHead->_next = p->_next;
	p->_next->_prev = pHead;
	free(p);
	p = NULL;
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	ListNode* p = pHead->_next;
	while (p != pHead)
	{
		if (p->_prev == x)
			return p;
		p = p->_next;
	}
	return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
	ListNode* new = (ListNode*)malloc(sizeof(ListNode));
	new->_data = x;
	new->_next = pos;
	new->_prev = pos->_prev;
	pos->_prev->_next = new;
	pos->_prev = new;
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	pos->_prev->_next = pos->_next;
	pos->_next->_prev = pos->_prev;
	free(pos);
}
int main()
{
	ListNode* head=ListCreate();
	ListPushFront(head,1);
	ListPushFront(head,2);
	ListPushFront(head,3);
	ListPushFront(head,4);
	ListPrint(head);
	printf("\n");
	ListPushBack(head, 5);
	ListPushBack(head, 6);
	ListPrint(head);
	ListPopBack(head);
	printf("\n");
	ListPrint(head);
	ListPopFront(head);
	printf("\n");
	ListPrint(head);
	ListNode* p = head->_next->_next;
	ListInsert(p, 8);
	printf("\n");
	ListPrint(head);
	ListErase(p);
	printf("\n");
	ListPrint(head);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值