The logic that realizes a database with two-way catenary watch is successive
Tuesday, March 03, 2009 by rain
Of one problem put forward
Information is data,The support that cannot leave a database in order to offer the website that information and information give priority to interactively necessarily.For instance common forum, news releases a system is complete those who be based on a database,What all programs offer user and governor is an operation interface only,User and controller should pass this interface to be able to realize logarithm occupy the data in the library to undertake additive only,Edit and delete wait for an operation.
To every data logging in the database,We can add an only label to it (ID) in order to distinguish the data logging at other.Be examined for what convenient user logarithm occupies record information and browse,The person that supply often is carried to use on the program is OK and direct from this the notes skips to the function that issue or attends to record.And this kind of function is based on ID just about increase and reduce those who will come true.To the data logging with successive ID the implementation of this kind of function does not have problem; however,After to the database medium a few data are deleted or discarding,Corresponding ID is nonexistent,The problem that if still use this kind of method to be able to appear,records for nothing in great quantities.If where,accordingly the circumstance with discontinuous ID is realizing every record to jump freely up and down turn,Be the problem that the article should solve.At present,The method that already a few solve this kind of problem,Commonly used is to appear every time when data logging ID is discontinuous,Undertake to data again sort,This kind of method is when data bulk is very great systematic workload is very breathtaking,The biggest drawback that this also is a method.Used two-way catenary watch to keep away from above the problem with groovy implicit method,And do not importune data logging ID successive,What just make they go up in logic is successive with achieving our goal,The principle that we let see two-way list first below.
The principle that two two-way catenary express
In data structure,What following graphs show the memory structure that two-way catenary expresses:
Graph one
From on graph we can see,There is two fingers region in the node that expresses in two-way catenary,Point to direct succeed firstly,Another point to direct before hasten.Its store the structure is as follows:Typedef Struct DulNode{Struct DulNode *priou;
ElemType Data;Struct DulNode *next;
}DulNode,*DuLinkList;
The D of pressing with a finger that two-way to pointing to catenary expresses aleatoric node,Have the concern below:D-%26gt;next-%26gt;priou=d-%26gt;priou-%26gt;next=d
Namely:Of current node succeed before hasten is oneself,The succeed of the hasten before current node also is oneself.
Two-way catenary expresses delete operation principle: Should search two-way catenary to express first,Search the node X that should delete,Introduce node P for this,Be like Data(P)=X,Delete X,Revise its at the same time the left hand of the right hand of left node and right node,Specific if pursue two:
Graph one
This kind of effective method,Below the case that had a few operations only to data,Those who maintained data catenary list is complete successional,This is we are solved in front problem place hopes.We see the specific implementation method that uses this principle to solve a problem and example below.
Three implementation method and example demonstrate
According to the principle that two-way catenary expresses,We are organizing the data logging in the database (data) when,Can regard every record as a node in two-way catenary watch,The connection between different record is not the size order that relies on their ID,Point to fluctuation to record node respectively through two finger however.Because of this,We must define three attribute in every data logging: ? of closely question ? sincering feeling,NEXT_ID of date of node of a record,On one records node date PRIOU _ID. When such programs call data logging to undertake fluctuating recording switch,Not be simple those who pass itself ID add decrease will come true,The guiding principle that adopts oneself however will come true jump turn.
If need to delete some data logging,The NEXT_ID that should alter a record at the same time only and the PRIOU_ID of a next records,With respect to the link that can continue to maintain all data logging,Such with respect to imitate the function of pressing with a finger that two-way catenary expresses,Realized data logic is successive.Below come with comparing popular MySql database and PHP program language at present the function above specific implementation:
Above all the database that we must build a test to use in MySql database and data are expressed,Its SQL statement defines as follows:
CREATE DATABASE Test;
CREATE TABLE Test (
ID Int(30) DEFAULT '0' NOT NULL Auto_increment,
Next_id Int(30) DEFAULT '0' NOT NULL,
Priou_id Int(30) DEFAULT '0' NOT NULL,
Data_title Varchar(60) NOT NULL,
Data Text NOT NULL,
KEY Id (Id) ,
UNIQUE Id_2 (Id)
) ;
In this data table,Outside the link finger that we define a need,The caption that still defines data logging (Data_title) and real data (Data) . The PHP code that gives out to operate this database actually below paragraph:
%26lt; ?
/ / Date:2001/10/13
/ / Author: Nica@263.net
/ / note: ?h swollen naevus is tranquil the function of the definition in library of function of Nai ? HP
$Dbname="Test";
$Tablename="test";
$my_link=mysql_connect('localhost' ,'root' ,''); // and database server establish link
$db=mysql_select_db($Dbname,$my_link);
/ / the code that inserts data: Insert Data Code
/ **** is carried out insert data manipulation,And get arrive ID ******/
$Insertquery="INSERT INTO $Tablename (Data_title,Data) VALUES('$datatitle' ,'$data')";
$I_result=mysql_query($Insertquery) Or Die(mysql_error());
$id=mysql_insert_id(); // obtains ID
/ the finger that ***** definition records originally links ******/
$Priou_id=$id+1;
$Next_id=$id-1;
$Updatequery="UPDATE $Tablename SET Priou_id=$Priou_id,Next_id=$Next_id WHERE ID=" . $id;
$U_result= Mysql_query($Updatequery) Or Die(mysql_error());
/ / the operation code that deletes data: DEtele Data Code
The ID of some data logging that $id="n"; // is about to delete,Get according to particular case
/ two finger that ***** obtains want to be deleted currently record are worth *****/
$Selectquery2="SELECT * FROM $Tablename WHERE ID=" . $id;
$S_result2= Mysql_query($Selectquery2) Or Die(mysql_error());
$Priou_id=mysql_result($S_result2,0,"Priou_id");
$Next_id=mysql_result($S_result2,0,"Next_id");
/ the right hand ***/ that the right hand that ***** changes a record makes its point to this record
$UpdatePriouquery="UPDATE $Tablename SET Next_id=$Next_id WHERE ID=" . $Priou_id;
$UP_result= Mysql_query($UpdatePriouquery) Or Die(mysql_error());
/ the left hand ***/ that the left hand that ***** changes a record makes its point to this record
$UpdateNextquery="UPDATE $Tablename SET Priou_id=$Priou_id WHERE ID=".Next_id;
$UN_result= Mysql_query($UpdateNextquery) Or Die(mysql_error());
/ ****** is carried out delete operation *******/
$Deletequery="DELETE FROM $Tablename WHERE ID=" . $id;
$D_result= Mysql_query($Deletequery) Or Die(mysql_error());
Mysql_close();
? %26gt;
This code moves below Win98+Apache+Mysql 1.3.14+PHP4.0.1p12 through,Pointing to is function and language,If the reader wants to cite need will insert a function code and delete functional code to cite apart.At the same time,This code is basic function only demonstrate,More and unusual situation is like a record finger problem to need him everybody to handle.
Through the example above,Everybody is OK very clear see as long as before deleting a record every time,the record after its are connected the link rises,As a result of the record jump turn the link finger that destination is minute book body decides,What carried data so is successional,The advantage that this is two-way catenary watch is in.Need only so a few process designing code is managing a large number of time of the system,The author hopes this kind of effective implementation method can receive more actual application.