Estructura de datos: una lista enlazada circular bidireccional con el nodo principal de una tabla lineal

Prefacio

Hay muchos tipos de listas enlazadas (de acuerdo con el encabezado o no, cíclico o no cíclico, etc.), pero nos centramos en solo dos, uno de los cuales tiene una estructura muy simple es un enlace no cíclico unidireccional sin cabeza lista . Ver su funcionamiento

Estructura de datos: lista de tabla lineal vinculada

Básicamente, este tipo de resultado no se utiliza en el desarrollo, porque una estructura simple a menudo significa una operación complicada , como las preguntas de OJ en la búsqueda de empleo. Básicamente, la lista vinculada utiliza esta estructura. Para aquellos interesados ​​en estas preguntas de OJ, consulte

Lista vinculada clásica

La otra es la estructura más compleja de la lista enlazada, una lista enlazada circular bidireccional con un nodo principal.
Inserte la descripción de la imagen aquí
Esta estructura parece ser muy complicada, pero su operación es muy simple, como la operación de inserción de cola más utilizada, y su complejidad temporal es O (1) . Al mismo tiempo, la lista en el STL en C ++ es esta estructura.
Para esta estructura, debido a que tiene un nodo principal, puede hacer que la mayoría de las operaciones usen el mismo código para lograr, como la inserción de encabezados, etc., y porque es cíclico, tan conveniente para la inserción de la cola y otras operaciones

Presta atención al juicio de la lista vacía.

head->prev=head
haed->next=head

lograr

(1) Definición de estructura

typedef struct DListNode
{
    
    
	struct ListNode* next;//指向前一个结点
	struct ListNode* prev;//指向后一个结点
	DLdatatype val;
}DListNode;

(2) Funciones básicas

1: Solicite un nodo ,
Inserte la descripción de la imagen aquí
2: Imprimir
Preste atención a la impresión, no hay NULL en esta estructura, es decir, la condición para juzgar el final no puede ser NULL, pero el cabezal
Inserte la descripción de la imagen aquí

(3) Realización de la operación

1: Inserción de la cola
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
2: Eliminación de la cola
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí
3: Inserción de la cabeza
Inserte la descripción de la imagen aquí
Inserte la descripción de la imagen aquí

Inserte la descripción de la imagen aquí
4: Eliminación de encabezado
Inserte la descripción de la imagen aquí
5: Buscar nodo

Inserte la descripción de la imagen aquí
6: Insertar
Inserte la descripción de la imagen aquí
en cualquier posición ( después de un nodo) 7: Insertar en cualquier posición (antes de un nodo)
Inserte la descripción de la imagen aquí
8: Eliminar en cualquier posición
Inserte la descripción de la imagen aquí
9: Destruir
Inserte la descripción de la imagen aquí

prueba

Inserte la descripción de la imagen aquí

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;

}

prueba.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();
}

Supongo que te gusta

Origin blog.csdn.net/qq_39183034/article/details/113136845
Recomendado
Clasificación