c list sample code. The list interface may varies depends on use case. It seems the last one is more popular. However, i sometimes use the first one.
#include <stdio.h>
#include <stdlib.h>
/* single list */
struct snode {
struct snode *next;
int n;
};
int sinserth(struct snode **h, struct snode *n)
{
if (!n)
return -1;
if (*h) {
n->next = *h;
}
*h = n;
return 0;
}
int sdel(struct snode **h, struct snode *n)
{
if (!n || !*h)
return 0;
if (*h == n) {
*h = n->next;
}
else {
struct snode *prev = *h, *cur = (*h)->next;
while (cur) {
if (cur == n) {
prev->next = cur->next;
break;
}
prev = cur;
cur = cur->next;
}
}
return 0;
}
void sdump(struct snode *h)
{
printf("dump:\n");
while (h) {
printf("%d->", h->n);
h = h->next;
}
printf("\n");
}
int main0()
{
struct snode *head = NULL, *p;
struct snode n1 = {n: 1}, n2 = {n: 2}, n3 = {n: 3};
printf("insert n1, n2, n3\n");
p = &n2;
sinserth(&head, &n1);
sinserth(&head, p);
sinserth(&head, &n3);
sdump(head);
printf("rm n2\n");
p = &n2;
sdel(&head, p);
sdump(head);
printf("rm n3\n");
p = &n3;
sdel(&head, p);
sdump(head);
printf("rm n1\n");
p = &n1;
sdel(&head, p);
sdump(head);
}
/* double list */
struct dnode {
struct dnode *p, *n;
int c;
};
int dinserth(struct dnode **head, struct dnode *n)
{
if (!n)
return -1;
if (!*head) {
*head = n;
n->p = NULL;
n->n = NULL;
}
else {
n->n = *head;
n->p = (*head)->p;
if ((*head)->n) {
(*head)->n->p = n;
}
if ((*head)->p) {
(*head)->p->n = n;
}
*head = n;
}
return 0;
}
int ddel(struct dnode **head, struct dnode *n)
{
if (!*head || !n)
return 0;
if (*head == n) {
if ((*head)->p) {
(*head)->p->n = n->n;
}
if ((*head)->n) {
(*head)->n->p = n->p;
}
*head = n->n;
}
else {
struct dnode *p = (*head)->p;
while (p) {
if (p == n) {
p->p->n = n->n;
if (p->n) {
p->n->p = n->p;
}
break;
}
p = p->n;
}
}
}
void ddump(struct dnode *h)
{
printf("dump:\n");
while (h) {
printf("%d->", h->c);
h = h->n;
}
printf("\n");
}
int main1()
{
struct dnode *head = NULL, *p;
struct dnode n1 = {c: 1}, n2 = {c: 2}, n3 = {c: 3};
dinserth(&head, &n1);
p = &n2;
dinserth(&head, p);
dinserth(&head, &n3);
ddump(head);
printf("del n3\n");
p = &n3;
ddel(&head, p);
ddump(head);
printf("del n2\n");
p = &n2;
ddel(&head, p);
ddump(head);
printf("del n1\n");
p = &n1;
ddel(&head, p);
ddump(head);
}
/* single list 2 */
typedef struct node {
struct node *n;
int d;
} node_t;
int linsert(node_t **h, int d)
{
node_t *p = calloc(1, sizeof(node_t));
p->d = d;
p->n = *h;
*h = p;
return 0;
}
int ldel(node_t **h, int d)
{
node_t *p, *c;
if (!*h)
return 0;
if ((*h)->d == d) {
p = *h;
*h = (*h)->n;
free(p);
} else {
p = *h;
c = p->n;
while (c) {
if (c->d == d) {
p->n = c->n;
free(c);
break;
}
p = c;
c = c->n;
}
}
return 0;
}
node_t *lget(node_t *h, int d)
{
while (h) {
if (h->d == d)
return h;
h = h->n;
}
return NULL;
}
void ldump(node_t *h)
{
printf("dump:\n");
while (h) {
printf("%d->", h->d);
h = h->n;
}
printf("\n");
}
int main()
{
node_t *head = NULL, *p;
linsert(&head, 1);
linsert(&head, 2);
linsert(&head, 3);
ldump(head);
printf("get 2\n");
p = lget(head, 2);
printf("get node: %d\n", p->d);
printf("del 1\n");
ldel(&head, 1);
ldump(head);
printf("del 3\n");
ldel(&head, 3);
ldump(head);
printf("del 2\n");
ldel(&head, 2);
ldump(head);
}