Esta página se tradujo automáticamente con la API de traducción de Google Cloud.
Algunas páginas se pueden leer mejor en su totalidad.
Una lista enlazada circular es una variación de la lista enlazada en la que el último nodo apunta al primer nodo (o al nodo principal) en lugar de apuntar a nulo. Es decir, es una lista enlazada cuyo nodo de cola apunta al nodo de cabeza.
Una lista enlazada circular es una estructura de datos que contiene una secuencia de nodos. Cada nodo contiene dos campos:
El último nodo de una lista enlazada circular apunta al primer nodo (o al nodo principal). Esto hace que la lista enlazada circular sea una estructura de datos circular.
Como puede ver en el diagrama anterior, el último nodo (nodo 4) apunta hacia el primer nodo (nodo 1).
Una lista enlazada circular admite las siguientes operaciones:
Las listas enlazadas circulares tienen las siguientes ventajas sobre otras estructuras de datos:
Las listas enlazadas circulares tienen las siguientes desventajas:
Puede insertar un nuevo nodo al principio, al final o en cualquier lugar en el medio de la lista.
Inserción al principio:
void insertAtBeginning(int data) {
// Create a new node
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
// Set the next field of the new node to point to the head node
newNode->next = head;
// Set the next field of the last node to point to the new node
if(head != NULL) {
struct Node* temp = head;
while(temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
} else {
// If the list is empty, set the new node as the head node
newNode->next = newNode;
}
// Set the new node as the head node
head = newNode;
}
Inserción al final:
void insertAtEnd(int data) {
// Create a new node
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
// Set the next field of the new node to point to the head node
newNode->next = head;
// If the list is empty, set the new node as the head node
if(head == NULL) {
head = newNode;
} else {
// Set the next field of the last node to point to the new node
struct Node* temp = head;
while(temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
}
}
Inserción en el medio:
void insertInMiddle(int data, int position) {
// Create a new node
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
// Set the next field of the new node to point to the head node
newNode->next = head;
// If the list is empty, set the new node as the head node
if(head == NULL) {
head = newNode;
} else {
// Find the node at the given position
struct Node* temp = head;
int i;
for(i=0; i<position-1; i++) {
temp = temp->next;
}
// Set the next field of the new node to point to the node at the given position
newNode->next = temp->next;
// Set the next field of the node at the given position to point to the new node
temp->next = newNode;
}
}
Puede eliminar un nodo desde el principio, desde el final o desde cualquier lugar en el medio de la lista.
Eliminación desde el principio:
void deleteFromBeginning() {
// If the list is empty, return
if(head == NULL) {
return;
}
// Find the last node
struct Node* temp = head;
while(temp->next != head) {
temp = temp->next;
}
// Set the next field of the last node to point to the head node
temp->next = head->next;
// Free the memory occupied by the head node
free(head);
// Set the head node to the node at the given position
head = temp->next;
}
Eliminación del final:
void deleteFromEnd() {
// If the list is empty, return
if(head == NULL) {
return;
}
// Find the last node
struct Node* temp = head;
while(temp->next != head) {
temp = temp->next;
}
// Set the next field of the last node to point to the head node
temp->next = head;
// Free the memory occupied by the last node
free(temp);
}
Supresión del medio:
void deleteFromMiddle(int position) {
// If the list is empty, return
if(head == NULL) {
return;
}
// Find the node at the given position
struct Node* temp = head;
int i;
for(i=0; i<position-1; i++) {
temp = temp->next;
}
// Set the next field of the node at the given position to point to the node at the given position
struct Node* toBeDeleted = temp->next;
// Set the next field of the node at the given position to point to the node at the given position
temp->next = toBeDeleted->next;
// Free the memory occupied by the node at the given position
free(toBeDeleted);
}
Puede buscar un nodo en particular en la lista.
void search(int data) {
// If the list is empty, return
if(head == NULL) {
return;
}
// Find the node with the given data
struct Node* temp = head;
int position = 1;
while(temp->next != head) {
if(temp->data == data) {
break;
}
temp = temp->next;
position++;
}
// If the data is not found, return
if(temp->data != data) {
return;
}
// Print the data
printf("%d", temp->data);
}
Puede recorrer la lista para imprimir los datos almacenados en cada nodo.
void traverse() {
// If the list is empty, return
if(head == NULL) {
return;
}
// Print the data in each node
struct Node* temp = head;
do {
printf("%d ", temp->data);
temp = temp->next;
} while(temp != head);
}
Escriba un programa para crear una lista enlazada circular e inserte nodos al principio, al final y en el medio de la lista.
Escriba un programa para eliminar nodos desde el principio, desde el final y desde el medio de la lista.
Escriba un programa para buscar un nodo en particular en la lista.
Escriba un programa para recorrer la lista e imprimir los datos almacenados en cada nodo.