Table pointer implementation. The second method is to use an access list to simulate pointers.
In my understanding, it is to create an array of nodes, simulate the storage device, and then allocate memory and free memory from it.
But the actual memory is not released ~
The following code attaches directly:
main.cpp//cursorlist////Created by Alps on 14-7-27.//Copyright (c) 2014 Chen. All rights reserved.//#include <iostream> #define Cursorspace 100#define elementtype intusing namespace std; typedef int PTRTONODE;TYPEDEF Ptrtonode list;typedef ptrtonode position;void initializecursorlist (void); List Makeempty (list l), int isEmpty (list l), int islast (list L, Position p), void Insert (list L, Position P, ElementType X), V OID Delete (List L, ElementType X); Position Find (List L, ElementType X); Position findprevious (list L, ElementType x); void DeleteList (list l); struct node{ElementType X; Position Next;}; struct Node cursorlist[cursorspace];int isEmpty (List L) {return cursorlist[l]. Next = = 0;} int Islast (List L, Position P) {return cursorlist[p]. Next = = 0;} void Initializecursorlist (void) {int i = 0; for (i = 0; i < cursorspace; i++) {cursorlist[i]. Next = i + 1; } Cursorlist[cursorspace-1]. Next = 0;} Position Cursoralloc () {Position P; P = CursoRlist[0]. Next; Cursorlist[0]. Next = Cursorlist[p]. Next; CURSORLIST[P]. Next = 0; return P;} void Cursorfree (Position P) {cursorlist[p]. Next = cursorlist[0]. Next; Cursorlist[0]. Next = P;} Position Find (List L, ElementType X) {Position P = cursorlist[l]. Next; while (Cursorlist[p]. X! = x && p) {p = cursorlist[p]. Next; } if (P = = 0) {return false; } return P;} Position findprevious (List L, ElementType X) {Position P = L; Position tmp = Cursorlist[p]. Next; while (Cursorlist[tmp]. X! = x && tmp) {tmp = cursorlist[tmp]. Next; P = Cursorlist[p]. Next; } return P;} void Delete (List l, ElementType X) {Position P = findprevious (l, x); Position tmp = Cursorlist[p]. Next; CURSORLIST[P]. Next = cursorlist[tmp]. Next;} void Insert (List L, Position P, ElementType X) {Position tmp; TMP = Cursoralloc (); CURSORLIST[TMP]. x = x; CURSORLIST[TMP]. Next = Cursorlist[p]. Next; CURSORLIST[P]. Next = tmp;} void DeleteList (List L) {Position P = cursorlist[l]. Next; Position tmp = P; while (tmp! = 0) {P = Cursorlist[p]. Next; Cursorfree (TMP); if (P = = 0) {break; } tmp = P; } Cursorlist[l]. Next = 0;} void Print (List L) {Position P = cursorlist[l]. Next; while (P! = 0) {printf ("%d", cursorlist[p]. X); P = Cursorlist[p]. Next; } printf ("\ n");} int main (int argc, const char * argv[]) {printf ("Start ... \ n"); Initializecursorlist (); List L = Cursoralloc (); Insert (L, L, 1); Insert (L, L, 3); Insert (L, L, 5); Insert (L, L, 4); Print (L); Position P = findprevious (L, 3); printf ("%d\n", P); Delete (L, 3); Print (L); DeleteList (L); Print (L); return 0;}
Algorithm is no problem. After that, I finished every function of the test ~
Have any questions, please leave a message ~
Learning algorithm-table pointer Implementation ~ C + +