Data structure of this thing, it is not difficult to understand, but the implementation of the difficulty is not small, although the idea is clear, but do not know where to start and the details of the language has been the main obstacle to prevent beginners (such as me). Today it took an afternoon to finish the list operation independently.
Find online code, mostly using the structure, and some are not suitable for just learning C + + or data structure of the people to see, so I am writing with the class, the code is more in line with the students ' habits and level.
First look at the class definition
class node{public: int data; *next;}; class linklist{ *h;
...//Some functions}
Two classes, node is used to represent nodes, node *next, which means that next is a pointer to the node type (some students do not understand this sentence, will be confused with the constructor), Linklist class is to hold the head pointer and define the function of the operation.
First, the creation of the whole table
The whole table is created in two ways, head interpolation (flashback) and tail interpolation (order), here only to say the head plug.
voidHead (linklist &l,intN) {node*p; P=Newnode; L.h=p;//defines the head node and the cast pointer p.->data=The data field of the n;//head pointer is the number of nodes P->next=the successor of the null;//last node must be empty. for(intI=0; i<n;i++)//Create n new Node {node*q=Newnode; CIN>>q->data; Q->next=p->Next; P->next=q;//Each new node is placed behind the head node}} .
Second, single node insertion
void Insert (linklist &l,int n,int num) { *p=l.h; for (int i=0; i<n;i++) { P=p->next; } Locate the insertion position *q=new node; Q->next=p->next; P->next=q; Q->data=num; }
Third, single node deletion
void del (linklist &l,int N) { *p=l.h; for (int i=0; i<n-1; i++) { P=p->next; } Locate the deleted location *q=p; Q=q->next; P->next=q->next; Delete q;//free Space }
Iv. Finding nodes
void Search (linklist &l,int N) { *p=l.h; for (int i=0; i<n;i++) { P=p->next; } cout<<p->data<<endl; }
Five, inverted
Because the head is a flashback output, I want to invert, many of the online code is a new linked list, or use the tail pointer doubly linked list and so on, I think it will not be so troublesome and then think of this algorithm
void reverse (linklist l) { *p=l.h; *Q; P=p->next; while (p->Next) { Q=p->next; P->next=q->next; // q->next=p; If you replace the following two sentences with this sentence, it will be tragic. q->next=l.h->next; L.h->next=q; } }
One afternoon time is mainly delayed here, I began to write the comments that sentence, and then always lose the data of the node, carefully observed that the original is the head pointer followed by the head node to the last face, and then this problem through the following two sentences to ensure that the head pointer is always in the table head.
Vi. Conclusion
Write here, finally understand why the online include a lot of code on the book is easy to read, because the book or blog author will not put the mistakes he made, some simple code also do not have a lot of comments, their own time to understand them, want to write down the reasons for each step is too difficult, can not express things too much , so want to master a thing or hands-on practice, many mistakes can progress, because the book or online difficult to find the details of the mistakes, thought they will be the time to write out, in the end will find or not ... Loop a few times, it's almost there.
[Original] using C + + class to implement the list of one-way list additions and deletions and reverse operation