线性表
线性表是一种常见的数据结构,用于存储一组按照顺序排列的元素。线性表中的元素之间存在一对一的关系,即每个元素都有唯一的前驱和后继元素(除了第一个元素和最后一个元素)。
常见的线性表:顺序表、链表、栈、队列、字符串等,线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。
顺序表(Array List):
静态顺序表:使用定长数组存储元素。
动态顺序表:使用动态开辟的数组存储。
- 基本原理:顺序表是一种基于数组实现的数据结构,存储在内存中的一块连续的存储空间。
- 特点:
- 随机访问:可以通过索引快速访问任意位置的元素。
- 内存占用:是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
- 插入和删除:在中间插入或删除元素时,需要移动其他元素,时间复杂度较高。
- 适用场景:适合对数据的随机访问较多,元素数量相对固定的情况。
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define N 1000
typedef int SLDataType;
//静态顺序表
//typedef struct SeqList
//{
// SLDataType a[N];
// int size;//表示数组中存储了多少个数据
//}SL;
//动态顺序表
typedef struct SeqList
{
SLDataType* a;// 指向动态开辟的数组
int size;//表示数组中存储了多少个数据
int capacity;//数据实际能存数据空间容量
}SL;
void SeqListPrint(const SL* ps);
void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListCheckCapacity(SL* ps);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFron(SL* ps);
int SeqListFind(const SL* ps, SLDataType x);
void SeqListInsert(SL* ps, int pos, SLDataType x);
void SeqListErase(SL* ps, int pos);
#include "SeqList.h"
void SeqListInit(SL* ps)
{
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SeqListPrint(const SL* ps)
{
for (int i = 0; i < ps->size; ++i)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListCheckCapacity(SL* ps)
{
//若没有空间或空间不足,就扩容
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
void SeqListDestory(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
void SeqListPushBack(SL* ps, SLDataType x)
{
SeqListCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPopBack(SL* ps)
{
if (ps->size > 0)
{
//ps->a[ps->size - 1] = 0;
ps->size--;
}
}
void SeqListPushFront(SL* ps, SLDataType x)
{
SeqListCheckCapacity(ps);
//从后往前挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
void SeqListPopFron(SL* ps)
{
//从前往前挪动数据
if (ps->size > 0)
{
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;
}
}
int SeqListFind(const SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
SeqListCheckCapacity(ps);
if (pos < ps->capacity && pos >= 0)
{
for (int i = ps->size - 1; i >= pos; i--)
{
ps->a[i + 1] = ps->a[i];
}
ps->a[pos] = x;
ps->size++;
}
}
void SeqListErase(SL* ps, int pos)
{
if (pos < ps->size)
{
int c = ps->size - pos;
while (c--)
{
ps->a[pos++] = ps->a[pos + 1];
}
ps->size--;
}
}
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void TestSeqList1()
{
SL s1;
SeqListInit(&s1);
SeqListPushBack(&s1, 1);
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
SeqListPushBack(&s1, 4);
SeqListPushBack(&s1, 5);
SeqListPushBack(&s1, 6);
SeqListPrint(&s1);
SeqListPopBack(&s1);
SeqListPrint(&s1);
SeqListDestory(&s1);
}
void TestSeqList2()
{
SL s1;
SeqListInit(&s1);
SeqListPushBack(&s1, 1);
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
SeqListPushFront(&s1, 6);
SeqListPrint(&s1);
SeqListPopFron(&s1);
SeqListPrint(&s1);
printf("%d\n", SeqListFind(&s1, 3));
SeqListInsert(&s1, 1, 66);
SeqListPrint(&s1);
SeqListErase(&s1, 2);
SeqListPrint(&s1);
SeqListDestory(&s1);
}
void TestSeqList3()
{
SL s1;
SeqListInit(&s1);
SeqListPushBack(&s1, 1);
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
SeqListPushFront(&s1, 1);
SeqListPushFront(&s1, 2);
SeqListPushFront(&s1, 3);
SeqListPrint(&s1);
SeqListDestory(&s1);
}
void Menu()
{
printf("*************************\n");
printf("请选择你的操作:>\n");
printf("1.头插 2.头删\n");
printf("3.尾插 4.尾删\n");
printf("5.查找 6.查增\n");
printf("7.查删 0.退出\n");
printf("*************************\n");
}
int MenuTest()
{
SL s1;
SeqListInit(&s1);
int input = 1;
int data = 0;
int x = 0;
while (input)
{
Menu();
scanf("%d", &input);
if (input == 0)
{
printf("退出\n");
return 0;
}
if (input != 4 && input != 2 && !(input > 7))
{
printf("请输入数值:>");
scanf("%d", &data);
}
switch (input)
{
case 1:
SeqListPushFront(&s1, data);
break;
case 2:
SeqListPopFron(&s1, data);
break;
case 3:
SeqListPushBack(&s1, data);
break;
case 4:
SeqListPopBack(&s1, data);
break;
case 5:
printf("找到下标为:%d\n", SeqListFind(&s1, data));
goto end;
break;
case 6:
printf("请输入要插入的下标:>");
scanf("%d", &x);
SeqListInsert(&s1, x, data);
break;
case 7:
SeqListErase(&s1, data);
break;
default:
printf("输入错误,请重新输入!\n");
}
SeqListPrint(&s1);
end:;
}
SeqListDestory(&s1);
}
int main()
{
//TestSeqList1();
//TestSeqList2();
//TestSeqList3();
MenuTest();
return 0;
}
链表(Linked List):
- 基本原理:链表是一种由节点组成的数据结构,每个节点包含数据和指向下一个节点的指针。
- 特点:
- 随机访问:无法直接随机访问元素需要从头节点开始逐个遍历。
- 内存占用:灵活使用内存,节点在内存中可以不连续存储。
- 插入和删除:插入和删除操作效率高不需要移动大量元素。
- 适用场景:适合频繁进行插入和删除操作不需要随机访问元素的情况。
#pragma once
#include <stdio.h>
#include <stdlib.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);
void SListPushBack(SLTNode** phead, SLTDataType x);
void SListPushFront(SLTNode** phead, SLTDataType x);
void SListPopBack(SLTNode** phead);
void SListPopFront(SLTNode** phead);
SLTNode* SListFind(const SLTNode* phead, SLTDataType x);
void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x);
void SListInsertAfter(SLTNode* pos, SLTDataType x);
void SListErase(SLTNode** phead, SLTNode* pos);
void SListEraseAfter(SLTNode* pos);
void SListDestory(SLTNode** phead);
#include "SList.h"
SLTNode* BuyListNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL");
}
void SListPushBack(SLTNode** phead, SLTDataType x)
{
SLTNode* newnode = BuyListNode(x);
if (*phead == NULL)
{
*phead = newnode;
}
else
{
//找到尾结点
SLTNode* tail = *phead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SLTNode** phead, SLTDataType x)
{
SLTNode* newnode = BuyListNode(x);
newnode->next = *phead;
*phead = newnode;
}
void SListPopBack(SLTNode** phead)
{
if (*phead == NULL)
{
return;
}
if ((*phead)->next == NULL)
{
free(*phead);
*phead = NULL;
}
else
{
//找到尾结点
SLTNode* tail = *phead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void SListPopFront(SLTNode** phead)
{
if (*phead)
{
SLTNode* next = (*phead)->next;
free(*phead);
*phead = next;
}
}
SLTNode* SListFind(const SLTNode* phead, SLTDataType x)
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}
void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x)
{
SLTNode* newnode = BuyListNode(x);
if (*phead == pos)
{
newnode->next = *phead;
*phead = newnode;
}
else
{
SLTNode* posPrev = *phead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newnode;
newnode->next = pos;
}
}
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
SLTNode* newnode = BuyListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListErase(SLTNode** phead, SLTNode* pos)
{
if (*phead == pos)
{
*phead = pos->next;
free(pos);
}
else
{
SLTNode* prev = *phead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SListEraseAfter(SLTNode* pos)
{
SLTNode* next = pos->next;
pos->next = next->next;
free(next);
}
void SListDestory(SLTNode** phead)
{
SLTNode* cur = *phead;
while (cur)
{
SLTNode* next = cur->next;
free(cur);
cur = next;
}
*phead = NULL;
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}LTNode;
LTNode* ListInit();
void ListPrint(const LTNode* phead);
void ListPushBack(LTNode* phead, LTDataType x);
void ListPopBack(LTNode* phead);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopFront(LTNode* phead);
LTNode* ListFind(const LTNode* phead, LTDataType x);
void ListInsert(LTNode* pos, LTDataType x);
void ListErase(LTNode* pos);
void ListDestroy(LTNode* phead);
#include "List.h"
LTNode* ListInit()
{
LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPrint(const LTNode* phead)
{
printf("\n");
if (phead)
{
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL");
}
}
void ListPushBack(LTNode* phead, LTDataType x)
{
ListInsert(phead,x);
}
void ListPopBack(LTNode* phead)
{
if (phead->next != phead)
{
LTNode* tail = phead->prev;
phead->prev = tail->prev;
tail->prev->next = phead;
free(tail);
tail = NULL;
}
}
void ListPushFront(LTNode* phead, LTDataType x)
{
ListInsert(phead->next, x);
}
void ListPopFront(LTNode* phead)
{
if (phead->next != phead)
{
LTNode* next = phead->next;
phead->next = next->next;
next->next->prev = phead;
free(next);
next = NULL;
}
}
LTNode* ListFind(const LTNode* phead, LTDataType x)
{
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void ListInsert(LTNode* pos, LTDataType x)
{
LTNode* posPrev = pos->prev;
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
newnode->data = x;
newnode->next = pos;
newnode->prev = posPrev;
posPrev->next = newnode;
pos->prev = newnode;
}
void ListErase(LTNode* pos)
{
if (pos)
{
LTNode* posPrev = pos->prev;
posPrev->next = pos->next;
pos->next->prev = posPrev;
free(pos);
pos = NULL;
}
}
void ListDestroy(LTNode* phead)
{
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
链表的中间结点
要找到链表的中间节点,可以使用快慢指针技术。这种方法使用两个指针,一个指针每次移动一个节点,另一个指针每次移动两个节点。当快指针到达链表末尾时,慢指针将指向链表的中间节点。
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构
struct Node {
int data;
struct Node* next;
};
// 寻找链表的中间节点
struct Node* findMiddleNode(struct Node* head) {
struct Node *slow_ptr = head;
struct Node *fast_ptr = head;
while (fast_ptr != NULL && fast_ptr->next != NULL) {
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next->next;
}
return slow_ptr;
}
// 创建新节点
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
// 创建链表:1 -> 2 -> 3 -> 4 -> 5
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
// 寻找中间节点
struct Node* middleNode = findMiddleNode(head);
// 输出中间节点的值
printf("Middle node value: %d\n", middleNode->data);
return 0;
}
如果链表中节点数为偶数,则此方法将返回中间两个节点中的第二个节点。
环形队列
环形队列是一种特殊形式的队列,它通过在队列的尾部到达数组的末尾时将其连接到数组的开头,形成一个环来实现循环的效果。这样可以更有效地利用存储空间,避免出现队列满时依然有空闲空间无法利用的情况。
下面是一个简单的环形队列的实现示例,使用数组来存储队列元素:
#include <stdio.h>
#define MAX_SIZE 5
// 定义环形队列结构
typedef struct {
int front, rear;
int data[MAX_SIZE];
} CircularQueue;
// 初始化环形队列
void initQueue(CircularQueue *queue) {
queue->front = 0;
queue->rear = 0;
}
// 判断队列是否为空
int isEmpty(CircularQueue *queue) {
return queue->front == queue->rear;
}
// 判断队列是否已满
int isFull(CircularQueue *queue) {
return (queue->rear + 1) % MAX_SIZE == queue->front;
}
// 入队操作
void enqueue(CircularQueue *queue, int value) {
if (isFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->data[queue->rear] = value;
}
}
// 出队操作
int dequeue(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
return queue->data[queue->front];
}
}
int main() {
CircularQueue queue;
initQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
printf("Dequeued element: %d\n", dequeue(&queue));
printf("Dequeued element: %d\n", dequeue(&queue));
enqueue(&queue, 4);
enqueue(&queue, 5);
enqueue(&queue, 6);
printf("Dequeued element: %d\n", dequeue(&queue));
printf("Dequeued element: %d\n", dequeue(&queue));
printf("Dequeued element: %d\n", dequeue(&queue));
return 0;
}