C language to achieve a simple one-way linked list List

Source: Internet
Author: User
Tags count

Using C language to realize a simple and practical one-way chain list, has a certain practical significance. Especially when we don't want to use the List<...> class in the STL. The list that I implemented, the node stores any type of data (void*) that any caller assigns. This list applies to a few simple situations and consumes very little resources.

Header file:

/*
* List.h
* Generic sequential linked list node structure-can hold any type data.
* Cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#ifndef list_h_included
#define List_h_included

#include "Unistd.h"

typedef struct _LISTNODE_T
{
struct _listnode_t *next;
union{
void* data;
struct _list_t *list;
const char *STR;
Long key;
};
}listnode_t;

typedef struct _LIST_T
{
size_t size; /* Count of nodes * *
listnode_t *head;
listnode_t *tail;
}list_t, *list_p;

/* A prototype of callbacked function called by List_destroy (), NULL for no use. */
typedef void (*pfcb_list_node_free) (listnode_t* node);

/* A example of free node data function implemented by callee:
void My_list_node_free (listnode_t *node)
{
Free (node->data);
}
*/

/* Appends a node to a list * *
extern void
List_append_node (list_t *in_list, listnode_t *in_node);

/* Removes the "a" and returns it * *
extern listnode_t*
List_remove_head (list_t *in_list);

/* Removes all nodes but for list itself * *
extern void
List_remove_all (list_t *in_list, Pfcb_list_node_free pfunc/* NULL for no use or a key node * *);

/* Returns a copy of a list_t from heap * *
extern list_t*
List_copy (list_t in_list);

/* Concatenates two lists into the list. Not freeing the second * *
extern void
List_concat (list_t *first, list_t *second);

/* Allocates a new listnode_t from heap. NO memory allocated for input Node_data * *
extern listnode_t*
List_node_create (void* node_data);

/* Allocates a new listnode_t with a key node type * *
extern listnode_t*
List_key_create (long Node_key);

/* Allocates a empty list_t from heap * *
extern list_t*
List_create ();

/* Frees In_list ' s all nodes and destroys in_list from heap.
* The callee is responsible for freeing node data.
* The node freed-function (Pfunc) is called by List_destroy.
*/
extern void
List_destroy (list_t *in_list, Pfcb_list_node_free pfunc/* NULL for no use or a key node * *);

/* Gets count of nodes in the list * * *
extern size_t
List_size (const list_t* in_list);

/* Gets node by index 0-based. 0 is head */
extern listnode_t*
List_node_at (const list_t* in_list, int index);


#endif/* list_h_included * *

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.