Linked List, I knew you in last August. But now, I wrote a reverse single-chain table. It is not enough to expand.
I wrote it a while ago, but it was not implemented.
This time, the idea was completed last night. I wrote it just now, and there is also the linked list itself. It seems to be faster in 30 minutes, at least compared with the previous one.
Okay, post it! Of course, this ADT is concise.
/* 10.2-7-2011-05-08-19.40.c -- Chapter 10, section 2, Section 7 */<br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p>/* explicit constant definition */</P> <p> # define false (0) <br/> # define true (1) </P> <p>/* data type definition */</P> <p> typedef int bool; <br/> typedef int item; <br/> typedef struct node <br/> {<br/> item; <br/> struct node * Next; <br/>} node; <br/> typedef node * alert list; </P> <p>/* interface function declaration */</P> <p> void initialize_l (alert list * con St PL); <br/> bool isempty_l (const shortlist * const PL); <br/> bool insert_l (shortlist * const PL, const item ); <br/> void traversal_l (const shortlist * const PL, void (* pfun) (const item); <br/> void release_l (shortlist * const PL ); </P> <p>/* interface function definition */</P> <p> void initialize_l (Consumer list * const PL) <br/>{< br/> * PL = NULL; <br/>}</P> <p> bool isempty_l (const shortlist * const PL) <br/>{< br/> If (null = * PL) <br/> return true; <br/> else <br/> return false; <br/>}</P> <p> bool insert_l (partition list * const PL, const item) <br/>{< br/> node * newnode; </P> <p> newnode = (node *) malloc (sizeof (node); <br/> If (null = newnode) <br/> return false; <br/> newnode-> item = item; <br/> If (isempty_l (PL) <br/>{< br/> newnode-> next = NULL; <br/> * PL = newnode; <br/>}< br/> Else <br/> {<br/> newnode-> next = * pl; <br/> * PL = newnode; <br/>}</P> <p> return true; <br/>}</P> <p> void traversal_l (const sort list * const PL, void (* pfun) (const item) <br/>{< br/> node * scan; </P> <p> for (SCAN = * pl; scan! = NULL; scan = scan-> next) <br/> (* pfun) (scan-> item ); <br/>}</P> <p> void release_l (Consumer list * const PL) <br/>{< br/> node * scan, * temp; </P> <p> scan = * pl; <br/> while (scan! = NULL) <br/>{< br/> temp = scan; <br/> scan = scan-> next; <br/> free (temp ); <br/>}< br/> * PL = NULL; <br/>}</P> <p>/* main routine */</P> <p> int Mian (void ); <br/> void printitem (const item); <br/> void reverse (Consumer list * const PL); </P> <p> int main (void) <br/>{< br/> initialize list; <br/> item; </P> <p> initialize_l (& list); <br/> item = 1; <br/> insert_l (& list, item); <br/> item = 2; <br/> in Sert_l (& list, item); <br/> item = 3; <br/> insert_l (& list, item); <br/> item = 4; <br/> insert_l (& list, item); <br/> item = 5; <br/> insert_l (& list, item ); <br/> traversal_l (& list, printitem); <br/> reverse (& list); <br/> traversal_l (& list, printitem ); <br/> release_l (& list); </P> <p> return 0; <br/>}</P> <p> void printitem (const item) <br/>{< br/> printf ("%-3D/N", item); <br/>}</P> <p> V Oid reverse (sorted list * const PL) <br/>{< br/> node * Current, * Next, * temp; </P> <p>/* If your list is empty or only has one node. */<br/> If (isempty_l (PL) | null = (* PL)-> next) <br/> return; <br/> current = * pl; <br/> next = Current-> next; <br/> while (next-> next! = NULL) <br/>{< br/> temp = Next-> next; <br/> next-> next = current; <br/> current = next; <br/> next = temp; <br/>}< br/> next-> next = current; <br/> (* PL)-> next = NULL; <br/> * PL = next; <br/>}