🌱 Digital Garden

Search

Search IconIcon to open search

Last updated Jul 20, 2023 Edit Source

Quizas ver primero:


# Implementacion de una Single LinkedList en C

Para crear una SingleLinkedList primero debemos crear la estructura de sus nodos:

1
2
3
4
5
6
typedef struct node
{
	int value; //data
	struct node *next; //pointer to next node
}
node;

Ahora, veamos las implementaciones de las operaciones descritas en Single Linked-List.

Creacion:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
node* createList(int initialValue)
{
	node* linkedList = malloc(sizeof(node));
	if (linkedList == NULL)
	{
		return NULL;
	}

	linkedList->value = initialValue;
	linkedList->next = NULL;
	return linkedList;
}

Busqueda:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
bool find(node* head, int searchValue)
{
	if (head->value == searchValue)
	{
		return true;
	} 
	else if (head->next == NULL)
	{
		return false;
	}
	else
	{
		return find(head->next);
	}
}

Insertar:

1
2
3
4
5
6
7
node* insert(node* head, int newValue)
{
	node* newNode = malloc(sizeof(node));
	newNode->value = newValue;
	newNode->next = head;
	return newNode;
}

Eliminar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void delete(node* head)
{
	if (head->next == NULL)
	{
		return;
	}

	delete(head->next);
	free(head);
}

Siguiente: Double Linked-List