Linear tables, fractional groups and linked lists (the official name is called sequential storage and chained storage). The code in the daily use, simple write.
First of all, the array, static, dynamic two, nothing to say, pay attention to the dynamic to manually release memory.
The second is the linked list, still static, dynamic. In class, it is generally said that the dynamic implementation, but there is still a static implementation method. Dynamic implementation of the remaining memory, but the static implementation of the time, the exam is of course, depending on the situation. But I guess, in-class exams should not go to the card this time, so everyone should not worry (just a personal view, really got the data card do not look for me ...) )。
In particular, the static linked list implementation, should be in the beginning to apply enough memory, we try to use when there is no memory for a large number of reuse. For example, if the list has at most 10,000 numbers at the same time, but as the process continues to add a total of 1 billion numbers, then it is clear that 10,000 memory should be reused. So, we have to implement the static, we have to manage the memory, this is simply too troublesome ... And it will greatly increase the program run time, rather than directly using dynamic implementation.
There is nothing to say, the code is very short, see the nature to understand. No problem, is simply write two sentences, directly on the code:
The following definitions are given in the code:
Const int - ; int // example of a linear table with n number
Array implementations:
int a[maxn+1// array static implementation int *a=newint[maxn+1// array Dynamic Implementation delete[] A; A[i]=3// using the same method
Linked list node definition:
structNode//link list node definition with doubly linked list as an example{ intC; Node*l,*R; Node (int_c=0) {C=_c; l=r=0; } Node* Born (node *u)//at the right of this insert point U{node*v=r;//because it is inserted on the right of this, this!=b,v!=0u->r=v;if(v) v->l=u;//but at the beginning, a born B will produce v==0 state.U->l= This; R=u; returnu; } voidDel ()//Delete this right point{node*u=r,*v=r->r;//Since you should have a point on the right, u->r!=b, that is v!=bR=v; V->l= This; Delete u; //static implementation of the time, no need to delete, can not be deleted, this sentence can be omitted }};
Dynamic Linked list:
classList//dynamic implementation of linked list{node*a,*B; Public: Node* Add (intC=0) {returnA->born (Newnode (c)); } voidDelintc) { for(Node *p=a;p->r!=b;p=p->R)if(P->R->C==C) {P->del (); Break; } } voidLook () { for(Node *p=a->r;p!=b;p=p->r) cout<<p->c<<" "; } voidClear () { for(Node *p=a;p->r!=b;) p->del ();} List (intn=0) {A=NewNode b=inch();//Set 2 empty nodes, one end, more symmetrical, but a point, see personal habits. At this point, a->r==b is the standard for determining the empty list. for(intX,i=1; i<=n;++i) {cin>>x;inch(x);} } ~List () {clear ();}};
Static linked list:
classList//linked list static implementation{ intL; Node BOX[MAXN],*a,*B; Node* Make (intC=0) {box[l].c=c;return&Box[L++]; }//When you use it, just change the new node to make. Public: Node*inch(intC=0) {returnA->Born (Make (c));} void out(intc) { for(Node *p=a;p->r!=b;p=p->R)if(P->R->C==C) {P->del (); Break; } } voidLook () { for(Node *p=a->r;p!=b;p=p->r) cout<<p->c<<" "; } voidClear () {a->r=b; B->l=A;} List (intn=0) {A=make (); b=inch(); for(intX,i=1; i<=n;++i) {cin>>x;inch(x);} } ~List () {clear ();}};
"Data Structure" C + + code Linear table