Singly Linked List in C: Create, Insert, Count and Concatenate Nodes
Singly Linked List of Integers in C
The following C program implements a singly linked list of integers. It defines a node structure, several functions to create and manage nodes, and some functions still to be implemented in the practical class.
/* List of integers */
#include <stdio.h>
#include <stdlib.h>
struct nodo{
int valor;
struct nodo *prox;
};
typedef struct nodo *t_ptr_nodo;
// In class
// returns an empty list
t_ptr_nodo lista_vacia();
// returns 0 when list is empty, any other number otherwise
int es_lista_vacia(struct nodo* head);
struct nodo* crea_nodo_1(int info);
// notice that is the same as: t_ptr_nodo crea_nodo_1(int info);
void crea_nodo_2(struct nodo **lista, int info);
// notice that is the same as: void crea_nodo_2(struct nodo **lista, int info);
void crea_nodo_3(t_ptr_nodo *lista, int info);
struct nodo* insertar_inicio_1(int info, struct nodo* head);
int contar_elementos(t_ptr_nodo lista);
void display(struct nodo* head);
// In Practical class
t_ptr_nodo concatenar(t_ptr_nodo lista1, t_ptr_nodo lista2);
t_ptr_nodo insertar_en_orden(t_ptr_nodo lista, int info); // Keep the list ordered in increasing order
t_ptr_nodo borrar_elemento(t_ptr_nodo lista, int info); // Assume that the element to delete is in the list and appears only once
int main()
{
struct nodo* head = crea_nodo_1(10);
display(head);
head = insertar_en_orden(head, 17);
head = insertar_inicio_1(20, head);
display(head);
head = insertar_inicio_1(30, head);
display(head);
printf("Numero de elementos: %d\n\n", contar_elementos(head));
return EXIT_SUCCESS;
}
int contar_elementos(t_ptr_nodo head){
int suma = 0;
struct nodo* ptr;
for(ptr = head; ptr != NULL; ptr = ptr->prox){
suma++;
}
return suma;
}
struct nodo* insertar_inicio_1(int info, struct nodo* head){
struct nodo * nodo_nuevo = crea_nodo_1(info);
nodo_nuevo->prox = head;
head = nodo_nuevo;
return head;
}
void crea_nodo_3(t_ptr_nodo *lista, int info){
struct nodo* nodo_nuevo = (struct nodo *) malloc(sizeof(struct nodo));
nodo_nuevo->valor = info;
nodo_nuevo->prox = NULL;
*lista = nodo_nuevo;
}
// In class
t_ptr_nodo lista_vacia(){
return NULL;
}
int es_lista_vacia(struct nodo* head){ // returns 0 when list is empty, any other number otherwise
if (head == NULL)
return 0;
else
return 1;
}
struct nodo* crea_nodo_1(int info){
struct nodo* nodo_nuevo = (struct nodo *) malloc(sizeof(struct nodo));
nodo_nuevo->valor = info;
nodo_nuevo->prox = NULL;
return nodo_nuevo;
}
void display(struct nodo* head){
struct nodo* ptr;
if (es_lista_vacia(head) == 0) printf("La lista esta vacia\n");
for(ptr = head; ptr != NULL; ptr = ptr->prox){
// escribir el valor del elemento apuntado por ptr: ptr->valor
printf("%d ", ptr->valor);
}
printf("\n");
}
void crea_nodo_2(struct nodo **lista, int info){
/*
*lista = bla bla bl
swap(int *x,*y)
*x = tmp
nodo_nuevo hace las veces del tmp del swap
*/
struct nodo* nodo_nuevo = (struct nodo *) malloc(sizeof(struct nodo));
nodo_nuevo->valor = info;
nodo_nuevo->prox = NULL;
*lista = nodo_nuevo;
}
// In Practical class
t_ptr_nodo concatenar(t_ptr_nodo lista1, t_ptr_nodo lista2){
t_ptr_nodo listaConcatenada = NULL;
if ( (lista1 != NULL) && (lista2 != NULL) )
{
listaConcatenada = lista1;
// while (lista1 != NULL)
while (lista1->prox != NULL)
{
lista1 = lista1->prox;
}
// lista1 colocado en el último elemento.
lista1->prox = lista2;
}
else if (lista1 == NULL)
{
listaConcatenada = lista2;
}
else if (lista2 == NULL)
{
listaConcatenada = lista1;
}
return listaConcatenada;
}
// head = insertar_en_orden(head, 17);
// menor a mayor
t_ptr_nodo insertar_en_orden(t_ptr_nodo lista, int info){
t_ptr_nodo listaNueva = lista;
// lista1 vacía.
// lista1 con un elemento:
// -- Inserto por delante del elemento (info es menor)
// -- Inserto por detrás (info es mayor)
// Dos o más elementos:
// -- Inserto por delante del elemento (info es menor)
// -- Inserto por detrás (info es mayor)
// -- Insertar en el medio
return listaNueva;
}
t_ptr_nodo borrar_elemento(t_ptr_nodo lista, int info){
return NULL;
}