C chain list reconstruction

xiaoxiao2021-03-06  55

/ * This rewriting is mainly in order to increase the versatility of the structure. In order to take care of beginners, I will leave the original code, that looks easier to be understood by beginners. The MIAN function in the code * is just a simple test, everyone You can modify it as needed. * /

---------------- Lis.H --------------

#ifndef list_h

#define list_h

/ * Define function status * /

#ifndef ok

#define err -1

#define ok 1

#ENDIF

Typedef int status; / * function status * /

Typedef struct ListItem {

Void * Date; / * Node data * /

Int size;

Struct ListItem * next; / * points to the next node * /

} list_node; // Link table node

Void * Copy_Node (int, void *);

Status destroy_node (void *);

Typedef struct {

Struct ListItem * PTR; / * Link table head pointer * /

INT size; / * Link table length * /

} list; // Link list

List * list_init (void); / * Initialization * /

Status List_Destroy (list *); / * Destroy * /

Status add_node (list *, void *, int); / * Add a node * /

STATUS DELETE_ALL (List *); // Clear

Status delete_node (list *, list_node *); / * Delete a node * /

Status INSERT_NODE (List *, const list_node *, void *, int);

/ * Insert a node * /

List_node * find_node (const list *,

BOOL (*) (const void *, const void *),

Void *); / * Find * /

Status List_Print (const list *,

Void (*) (void *, const char *),

Const char *); / * Print * /

#ENDIF

_______________________________________________________________

#include

#include "list.h"

Void * Copy_Node (int size, void * date)

{

Void * p = malloc (size);

IF (size == sizeof (int))

* (int *) p = * (int *) Date;

Else IF (size == sizeof (char))

* (char *) p = * (char *) Date;

Else IF (size == sizeof (float))

* (float *) p = * (float *) Date;

Else IF (size == sizeof (double))

* (double *) p = * (double *) Date;

Return P;

}

Status destroy_node (void * p)

{

IF (p! = 0)

Free (p);

Return OK;

}

____________________________________________________________

#include #include

#include "list.h"

List * list_init (void)

{

List * p = (list *) malloc (sizeof (list));

IF (p == 0)

Return 0;

P-> PTR = 0;

P-> size = 0;

Return P;

}

Status List_Destroy (List * PEV)

{

IF (PEV == 0)

Return ERR;

Delete_all (PEV);

Free (pev);

Return OK;

}

Status Add_Node (List * P, Void * Date, int size)

{

List_node * pev =

(List_node *) Malloc (sizeof (list_node));

IF (PEV == 0)

Return ERR;

PEV-> DATE = COPY_NODE (SIZE, DATE);

PEV-> Next = P-> PTR;

P-> PTR = pev;

P-> Size ;

Return OK;

}

Status delete_node (List * p, list_node * pev)

{

List_node * temp = pev;

IF (PEV == 0)

Return ERR;

PEV = TEMP-> NEXT;

Destroy_Node (TEMP-> DATE);

Free (TEMP);

P-> size -

Return OK;

}

Status Delete_all (List * PEV)

{

IX;

IF (PEV == 0)

Return ERR;

IF (pev-> size == 0)

Return ERR;

For (ix = 0; ix size; ix, pev-> PTR)

Delete_node (pev, pev-> ptr);

Return OK;

}

Status INSERT_NODE (List * P, List_Node * PEV,

Void * Date, int size)

{

IF (p == 0)

Return ERR;

IF (PEV == 0)

Add_node (p, date, size);

Else

{

List_node * pv =

(List_node *) Malloc (sizeof (list_node));

IF (PEV == 0)

Return ERR;

PV-> DATE = Copy_Node (size, date);

PV-> Next = pev-> next;

PEV-> Next = PV;

P-> Size ;

}

Return OK;

}

List_node * find_node (const list * pev, bool (* f) (const void *, const void *),

Void * Date)

{

IX;

List_node * p = pev-> PTR;

For (ix = 0; ix size; ix)

IF ((* f) (P-> Date, Date))

Return P;

Else

P = P-> next;

Return 0;

}

STATUS LIST_PRINT (Const List * PEV, Void (* Pprint) (Void *, Const Char *), Const Char * CH)

{

IX;

List_node * p = pev-> PTR;

IF (PEV == 0)

Return ERR;

IF (pev-> size == 0)

Return OK;

For (ix = 0; ix size; ix)

{

(* pprint) (P-> Date, CH);

Printf ("/ t");

P = P-> next;

}

Printf ("/ n");

Return OK;

}

___________________________ os.h ___________________________

#ifndef iOS_H

#define iOS_H

Void Output (void *, const char *);

Void INPUT (Void *, const char *);

#ENDIF

_________________________ os.c _________________________________

#include

#include

#include "ios.h"

Void Output (Void * Date, Const Char * CNT)

{

IF (strCMP (CNT, "% D")))

Printf (cnt, * (int *) date);

ELSE IF (Strcmp (CNT, "% C")))

Printf (CNT, * (Char *) Date);

ELSE IF (Strcmp (CNT, "% S")))

Printf (CNT, * (Char **) Date);

ELSE IF (Strcmp (CNT, "% F")))

Printf (CNT, * (Double *) Date;

}

Void Input (Void * Date, Const Char * CNT)

{

IF (strCMP (CNT, "% D")))

Scanf (CNT, (INT *) DATE);

ELSE IF (Strcmp (CNT, "% C")))

Printf (CNT, (CHAR *) DATE;

ELSE IF (Strcmp (CNT, "% S")))

Scanf (CNT, (Char **) Date;

ELSE IF (Strcmp (CNT, "% F")))

Scanf (CNT, (Double *) Date);

}

____________________ maim.c _____________________________________

#include

#include

#include "list.h"

#include "ios.h"

int main ()

{

List * mylistp = list_init ();

For (int i = 0; i <10; i)

{

CHAR A = 'a' i; add_node (MyListp, & A, SIZEOF (A));

}

List_print (MyListp, Output, "% C");

delete_all (myListp);

System ("pause");

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-116564.html

New Post(0)