Implement an algorithm, lazy handwriting linked list, so with C + + Forward_list, no Next () method feel very bad, such as a one-way list of the simplest function requirements:
Input
1 2 5) 3 4
Output
1->2->5->3->4
The equivalent of just inserting, traversing 2 functions (of course, the traversal function is slightly modified is to destroy the list)
Write a test code in pure C
/*definition of the basic data structure and declaration of the function * /typedefintElemtype;typedefstructnode{elemtype Elem; structnode*Next;} Node,* Nodeptr, * *forwardlist; Nodeptr CreateNode (Elemtype x);voidshowlist (forwardlist lst);voiddestroylist (forwardlist lst);//creates a node of element x and inserts it into the node where it is after//if where is null, insert the header of the LST into the list as the first node//returns a pointer to a new nodeNodeptr Insertafternode (nodeptrwhere, Elemtype x, forwardlist lst);
/*concrete implementation of linked list related functions*/nodeptr CreateNode (Elemtype x) {nodeptr Pnode= (nodeptr)malloc(sizeof(Node)); if(Pnode = =NULL) {Perror ("malloc"); Exit (1); } Pnode->elem =x; Pnode->next =NULL; returnPnode;} Nodeptr Insertafternode (ConstNodeptrwhere, Elemtype x, forwardlist LST) {nodeptr Pnode=CreateNode (x); if(where==NULL) { *lst =Pnode; } Else{Pnode->next =where-Next; where->next =Pnode; } returnPnode;}voidshowlist (forwardlist lst) {printf ("Show linked list:"); Nodeptr Curr= *lst; while(Curr->next! =NULL) {printf ("%d->", curr->elem); Curr= curr->Next; } printf ("%d\n", curr->elem);}voiddestroylist (forwardlist lst) {printf ("destroy linked list:"); Nodeptr Curr= *lst; while(Curr! =NULL) {Nodeptr Next= curr->Next; printf ("%d", curr->elem); Free(Curr); Curr=Next; } printf ("\ n");}
/**/int Main ( ) {= NULL; Initlistfromstdin (&head); Showlist (&head); Destroylist (&head); return 0 ;}
Three parts are written in a code (FORWARD_LIST.C), the test results are as follows
$lsdata.inchforward_list.c$CatData.inch 1 2 5 3 4$ GCCForward_list.c-std=C99 $./a.out <data.inchShow linked list:1-2-5-3-4destroy linked list:1 2 5 3 4
Because it is not necessary to consider a comprehensive C code, so a lot of C + + engineering skills do not need to consider, such as template, const, said before the C code is encapsulated into a function has caused the head node of the list has been modified, the last time the list is destroyed, traverse the back node directly pointing to the last node, Causes the first 4 nodes to be destroyed. If you use const properly, you can check it at compile time.
Well, in fact, such a write down, C + + version of the Forward_list is written, after all, my list insertion function is to imitate forward_list, but write a bit awkward. Because you need to traverse to the bottom 2nd node to stop, the final code is as follows
#include <cstdio> #include <forward_list>using namespace std;//gets the next iterator template for the forward iterator it <typename Fwiter>fwiter Nextiter (Fwiter it) { return ++it;} int main () { forward_list<int> lst; int x; for (Auto it = Lst.before_begin (); FSCANF (stdin, "%d", &x) = = 1; ) { it = Lst.emplace_after (it, x); } Output Auto it = Lst.begin () according to A0->a1->...->an format; while (Nextiter (IT)! = Lst.end ()) { printf ("%d->", *it++); } printf ("%d\n", *it); return 0;}
Since C + + does not provide the next () function, it is only handwritten, because the iterator copies a copy of the argument, so Nextiter () returns ++it without modifying the original iterator, but instead modifies the copy of the original iterator.
Note that it is necessary to record the last node of the list in order to insert the linked list, which is consistent with my C code implementation style (well, I'm actually doing the STL implementation).
Then the initial value is Before_begin () instead of begin (), because the empty list does not have a begin (), and the exact initial node of the empty list is null.
Test the code, where _m_node is the underlying implementation of Glibc++ 's forward_list iterator, not cross-platform code. An iterator is equivalent to encapsulating a node address in a layer, while _m_node is the node address.
#include <forward_list> <stdio.h> int main () { Std::forward_ List<int> lst; printf ("begin () address: %p\n", Lst.begin (). _m_node); printf ("before_begin () Address:%p\n", Lst.before_begin (). _m_node); return 0 ;}
The results are as follows:
$ g++ Test. cc -std=c++$. /A.outbegin () Address: 0x7fffb0896b60
C Language implementation Simple one-way list (create, insert, delete) and equivalent STL implementation code