C Language interface and implementation learning notes--linked list

Source: Internet
Author: User

Implements a series of interfaces of a linked list, and realizes the use of pointers

List.h

#ifndef list_include#defineList_include#defineT list_ttypedefstructS MT;structt{T Rest; void*First ;};externt List_append (t List, t tail);externt List_copy (t List);externT List_list (void*x, ...);externT List_pop (t List,void**x);externT List_push (t List,void*x);externt List_reverse (t List);extern intlist_length (T List);extern voidList_free (T *list);extern voidList_map (T list,voidApplyvoid**x,void*CL),void*cl);extern void**list_toarray (T List,void*end);#undefT#endif //List_include

List.c

#include <stdarg.h>#include<stddef.h>#include"assert.h"#include"mem.h"#include"list.h"#defineT list_tt List_push (t List,void*x)    {T p;    NEW (P); P->first =x; P->rest =list; returnp;} T List_list (void*x, ...)    {va_list ap; T list,*p = &list;    Va_start (AP, x);  for(; x; x = Va_arg (AP,void*) {NEW (*p); (*P)->first =x; P= & (*p)rest; }    *p =NULL;    Va_end (AP); returnlist;} T List_append (t List, t tail) {T*p = &list;  while(*p) {p= & (*p)rest; }    *p =tail; returnlist;} T List_copy (t List) {T head,*p = &Head;  for(; list; list = List->rest) {NEW (*p); (*p)->first = list->First ; P= & (*p)rest; }    *p =NULL; returnHead;} T List_pop (t List,void**x) {    if(list) {T head= list->rest; if(x) {*x = list->First ;        } free (list); returnHead; }    Else    {        returnlist; }}t list_reverse (t List) {T head=NULL, Next;  for(; list; list =next) {Next= list->rest; List->rest =Head; Head=list; }    returnhead;}intlist_length (T List) {intN;  for(n =0; List List = List->rest) {N++; }    returnN;}voidList_free (T *list)    {T Next;    ASSERT (list);  for(; *list; *list =next) {Next= (*list)rest; Free (*list); }}voidList_map (T list,voidApplyvoid**x,void*CL),void*cl)    {assert (apply);  for(; list; list = List->rest) {Apply (&list->First , CL); }}void**list_toarray (T List,void*end) {    intI, n =list_length (List); void**array = ALLOC ((n+1)*sizeof(*array));  for(i =0; I < n; ++i) {Array[i]= list->First ; List= list->rest; } Array[i]=end; returnArray;}

C Language interface and implementation learning notes--linked list

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.