Learning algorithm-table pointer Implementation ~ C + +

Source: Internet
Author: User

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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.