Artigo Diretório
Prefácio
Existem muitos tipos de listas vinculadas (de acordo com a liderança ou não, cíclica ou não cíclica, etc.), mas nos concentramos em apenas duas, uma das quais tem uma estrutura muito simples é uma ligação unilateral não cíclica sem cabeça lista . Veja seu funcionamento
Lista vinculada de estrutura de dados individualmente da tabela linear
Basicamente, esse tipo de resultado não é usado no desenvolvimento, porque uma estrutura simples geralmente significa uma operação complicada , como as perguntas do OJ na busca de empregos. Basicamente, a lista vinculada usa essa estrutura. Para os interessados nessas perguntas do OJ, consulte
A outra é a estrutura mais complexa da lista encadeada - uma lista encadeada circular bidirecional com um nó principal.
Esta estrutura parece ser muito complicada, mas sua operação é muito simples, como a operação de inserção de cauda mais comumente usada, e sua complexidade de tempo É O (1) . Ao mesmo tempo, a lista no STL em C ++ é esta estrutura.
Para esta estrutura, por ter um nó principal, ela pode fazer com que a maioria das operações use o mesmo código para atingir, como inserção de cabeçalho, etc. é cíclico, portanto conveniente para inserção de cauda e outras operações
Preste atenção ao julgamento da lista vazia
head->prev=head
haed->next=head
alcançar
(1) Definição da estrutura
typedef struct DListNode
{
struct ListNode* next;//指向前一个结点
struct ListNode* prev;//指向后一个结点
DLdatatype val;
}DListNode;
(2) Funções básicas
1: Solicitar um nó ,
2: Imprimir
Preste atenção na impressão, não há NULL nesta estrutura, ou seja, a condição para julgar o final não pode ser NULL, mas a cabeça
(3) Realização da operação
1: inserção da cauda
2: exclusão da cauda
3: inserção da cabeça
4: Exclusão do cabeçalho
5: Localizar nó
6: Inserir
em qualquer posição ( após um nó) 7: Inserir em qualquer posição (antes de um nó)
8: Excluir em qualquer posição
9: Destruir
teste
Código
Dlist.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int DLdatatype;
typedef struct DListNode
{
struct DListNode* next;
struct DListNode* prev;
DLdatatype val;
}DListNode;
void printlist(DListNode* head);//打印
DListNode* CreatNode(DLdatatype x);//申请结点
void ListPushBack(DListNode* head, DLdatatype x);//尾插
void ListPopBack(DListNode* head);//尾删
void listPushFront(DListNode* head, DLdatatype x);//头插
void listPopFront(DListNode* head);//头删
DListNode* find(DListNode* head, DLdatatype x);//找某个元素
void listinseret_behind(DListNode* head,DLdatatype pos, DLdatatype x);//任意位置插入(后面)
void listinseret_front(DListNode* head,DLdatatype pos, DLdatatype x);//任意位置插入(前面)
void listdelete(DListNode* head,DLdatatype pos);//任意位置删除
void listdestory(DListNode* head);//链表销毁
Dlist.c
#include "DList.h"
DListNode* CreatNode(DLdatatype x)
{
DListNode* NewNode = (DListNode*)malloc(sizeof(DListNode));
NewNode->val = x;
NewNode->next = NULL;
NewNode->prev = NULL;
return NewNode;
}
void printlist(DListNode* head)
{
assert(head);
DListNode* cur = head->next;
while (cur!=head)
{
printf("%d->", cur->val);
cur = cur->next;
}
if (head->next == head)
printf("NULL");
}
void ListPushBack(DListNode* head, DLdatatype x)
{
assert(head);
DListNode* NewNode=CreatNode(x);
NewNode->next = head;
(head->prev)->next = NewNode;
NewNode->prev = head->prev;
head->prev = NewNode;
}
void ListPopBack(DListNode* head)
{
assert(head);
assert(head->next != head);//如果链表为空,断言
DListNode* delelte = head->prev;
head->prev = delelte->prev;
delelte->prev->next = head;
free(delelte);
}
void listPushFront(DListNode* head, DLdatatype x)
{
assert(head);
DListNode* NewNode = CreatNode(x);
NewNode->next = head->next;
NewNode->prev = head;
head->next->prev = NewNode;
head->next = NewNode;
}
void listPopFront(DListNode* head)
{
assert(head);
assert(head->next != head);
DListNode* first = head->next;
first->next->prev = head;
head->next = first->next;
free(first);
}
DListNode* find(DListNode* head, DLdatatype x)
{
assert(head);
DListNode* cur = head->next;
while (cur->next != head && cur->val != x)
{
cur = cur->next;
}
if (cur->next == head && cur->val!=x)
{
return NULL;//未找到
}
else
{
return cur;//否则返回
}
}
void listinseret_behind(DListNode* head, DLdatatype pos, DLdatatype x)
{
assert(head);
DListNode* insert = find(head, pos);
if (insert == NULL)
{
printf("%d不存在\n", pos);
}
else
{
DListNode* NewNode = CreatNode(x);
NewNode->next = insert->next;
NewNode->prev = insert;
insert->next->prev = NewNode;
insert->next = NewNode;
}
}
void listinseret_front(DListNode* head, DLdatatype pos, DLdatatype x)
{
assert(head);
DListNode* insert = find(head, pos);
if (insert == NULL)
{
printf("%d不存在\n", pos);
}
{
DListNode* NewNode = CreatNode(x);
NewNode->next = insert;
NewNode->prev = insert->prev;
NewNode->prev->next = NewNode;
insert->prev = NewNode;
}
}
void listdelete(DListNode* head, DLdatatype pos)
{
assert(head);
assert(pos != head);
DListNode* delete = find(head, pos);
if (delete == NULL)
{
printf("%d不存在\n", pos);
}
else
{
delete->prev->next = delete->next;
delete->next->prev = delete->prev;
free(delete);
}
}
void listdestory(DListNode* head)
{
assert(head);
DListNode* cur = head->next;
DListNode* pre = NULL;
while (cur->next != head)
{
pre = cur;
cur = cur->next;
free(pre);
}
head->next =head;
head->prev = head;
}
test.c
#include "DList.h"
void test()
{
DListNode* head=(DListNode*)malloc(sizeof(DListNode));
head->prev = head;
head->next = head;
printf("尾插4个结点\n");
ListPushBack(head, 1);
ListPushBack(head, 2);
ListPushBack(head, 3);
ListPushBack(head, 4);
printlist(head);
printf("\n");
printf("\n");
printf("尾删2个结点\n");
ListPopBack(head);
ListPopBack(head);
printlist(head);
printf("\n");
printf("\n");
printf("头插4个结点\n");
listPushFront(head, 5);
listPushFront(head, 6);
listPushFront(head, 7);
listPushFront(head, 8);
printlist(head);
printf("\n");
printf("\n");
printf("头删2个结点\n");
listPopFront(head);
listPopFront(head);
printlist(head);
printf("\n");
printf("\n");
printf("在5后面插入7\n");
listinseret_behind(head, 5, 7);
printlist(head);
printf("\n");
printf("\n");
printf("删除1\n");
listdelete(head, 1);
printlist(head);
printf("\n");
printf("\n");
printf("销毁\n");
listdestory(head);
printlist(head);
printf("\n");
printf("\n");
}
int main()
{
test();
}