理论
8种
经典例题:复杂链表的复制
链表的插入时间复杂度O(1),链表不能随机访问,数组通过下标可以。
代码:
DST.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<stdlib.h>
typedef int DataType;
typedef struct ST
{
struct ST* prev;
struct ST* next;
DataType data;
}LTNode;
void DstInit(LTNode* pc);//初始化
void DstPrint(LTNode* pc);//遍历
void DstPushFront(LTNode* pc,DataType x);//头插
void DstPushBack(LTNode* pc, DataType x);//尾插
void DstPopBack(LTNode* pc); //尾删
void DstPopFront(LTNode* pc); //头删
bool EmptyDst(LTNode* pc); //链表是否为空
int DstSize(LTNode* pc); //表长
void DstInsert(LTNode* pos, DataType x); //在pos位置之前插入x
void DstErase(LTNode* pos); //删除pos位置节点
DST.c
#include"DST.h"
//BuyNewNode(DataType x) 不对外开放
LTNode* BuyNewNode(DataType x)
{
LTNode* newNode = (LTNode*)malloc(sizeof(LTNode));
if (!newNode)
{
perror("BuyNewNode:malloc fail");
return NULL;
}
newNode->data = x;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void DstInit(LTNode* pc)
{
pc->next = pc;
pc->prev = pc;
}
void DstPushFront(LTNode* pc, DataType x)
{
assert(pc);
//LTNode* newNode = BuyNewNode(x);
//if (!newNode)
//{
// perror("DstPushFront");
// return;
//}
//newNode->next = pc->next;
//newNode->prev = pc;
//pc->next->prev = newNode;
//pc->next = newNode;
DstInsert(pc->next, x);
return;
}
void DstPushBack(LTNode* pc, DataType x)
{
assert(pc);
//LTNode* newNode = BuyNewNode(x);
//if (!newNode)
//{
// perror("DstPushBack");
// return;
//}
//newNode->prev = pc->prev;
//newNode->next = pc;
//pc->prev->next = newNode;
//pc->prev = newNode;
DstInsert(pc, x);
return;
}
void DstPrint(LTNode* pc)
{
assert(pc);
if (EmptyDst(pc))
{
printf("链表为空!\n");
return;
}
LTNode* Head = pc->next;
while (pc != Head)
{
printf("%d ", Head->data);
Head = Head->next;
}
}
void DstPopBack(LTNode* pc)
{
assert(pc);
if (EmptyDst(pc))
{
printf("链表为空!\n");
return;
}
//LTNode* pop = pc->prev;
//pc->prev->prev->next = pc;
//pc->prev = pc->prev->prev;
//free(pop);
DstErase(pc->prev);
return;
}
void DstPopFront(LTNode* pc)
{
assert(pc);
if (EmptyDst(pc))
{
printf("链表为空!\n");
return;
}
//LTNode* pop = pc->next;
//pc->next->next->prev = pc;
//pc->next = pc->next->next;
//free(pop);
DstErase(pc->next);
return;
}
bool EmptyDst(LTNode* pc)
{
assert(pc);
return pc == pc->next ? true : false;
}
int DstSize(LTNode* pc)
{
assert(pc);
if (EmptyDst(pc))
{
return 0;
}
int i = 0;
LTNode* Head = pc->next;
while (pc != Head)
{
i++;
Head = Head->next;
}
return i;
}
//在pos位置之前插入x
void DstInsert(LTNode* pos, DataType x)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* newNode = BuyNewNode(x);
prev->next = newNode;
newNode->prev = prev;
newNode->next = pos;
pos->prev = newNode;
}
void DstErase(LTNode* pos)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"DST.h"
void test2()
{
LTNode list;
DstInit(&list);
DstPushFront(&list, 5);
DstPrint(&list);
DstPopFront(&list);
DstPrint(&list);
}
void test1()
{
LTNode list;
DstInit(&list);
DstPushFront(&list, 5);
DstPushFront(&list, 8);
DstPushBack(&list, 1);
DstPushBack(&list, 2);
DstPushBack(&list, 3);
DstPushBack(&list, 4);
DstPushBack(&list, 5);
printf("\n******************");
DstPrint(&list);
printf("\n******************");
DstPushFront(&list, 5);
DstPrint(&list);
printf("\n******************");
DstPopBack(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
printf("\n******************");
DstPopFront(&list);
DstPrint(&list);
return;
}
int main()
{
test2();
test1();
return 0;
}