Home > Back-end >  Everyone a great god can help me with a novice, I feel that the main function of don't know how
Everyone a great god can help me with a novice, I feel that the main function of don't know how

Time:09-30

Has a bidirectional circular linked list, each node except with the prior data and next three outside, also added a visit frequency domain freq, before the list is appointed, the frequency domain freq value are initialized to zero, and every time list for a LOCATE (L, x) after operation, the access node (that is, the element value is equal to the node of x) in the frequency domain freq value will increase 1, adjust the order between the linked list of nodes at the same time, make its access frequency of increasing the order of the order, in order to keep nodes are always close to the header is frequently accessed, is written in accordance with the above requirements LOCATE operation algorithm,
#include
#include
#include
Typedef int Elemtype;
Typedef struct DuLNode {
Elemtype data;
The prior DuLNode *;
DuLNode * next;
Int freq.
} DuLNode, * LinkList;
DuLNode * LinkListInit ()
{
DuLNode * L;
L=(DuLNode *) malloc (sizeof (DuLNode));
If (L==NULL)
Return NULL;
L - & gt; The prior=NULL;
L - & gt; Next=NULL;
L - & gt; Freq=0;
Return the L;
}
Int LinkListInsert (DuLNode * L, int I Elemtype e)
{
DuLNode * p, * s;
S=(DuLNode *) malloc (sizeof (DuLNode));
S - & gt; Data=https://bbs.csdn.net/topics/e;
S - & gt; The prior=NULL; S - & gt; Next=NULL;
If (I==1)
{
S - & gt; Next=L;
L - & gt; The prior=s;
L=s;
}
The else
{
P=L;
For (int j=1; J & lt; I - 1; J++)
{
P=p - & gt; next;
}
If (p - & gt; Next==NULL)
{
P - & gt; Next=s; S - & gt; The prior=p;
}
The else
{
P - & gt; Next - & gt; The prior=s;
S - & gt; Next=p - & gt; next;
P - & gt; Next=s;
S - & gt; The prior=p;
}
}
return 1;
}
Void PrintList (DuLNode * L)
{
DuLNode * p=L - & gt; next;
While (p!=NULL)
{
Printf (" % 3 d ", p - & gt; The data);
P=p - & gt; next;
}
printf("\n");
}
Void PrintList_s (DuLNode * L)
{
DuLNode * p=L - & gt; next;
While (p!=NULL)
{
Printf (" % 3 d ", p - & gt; The data);
P=p - & gt; next;
}
printf("\n");
While (p!=NULL)
{
Printf (" freq: % d ", p - & gt; Freq);
P=p - & gt; next;
}
printf("\n");
}
DuLNode * LinkListLocate DuLNode * L, Elemtype (e)
{
DuLNode * p * q;
P=L; Q=p - & gt; next;
While (q!=L & amp; & Q - & gt; The data!=e)
Q=q - & gt; next;
If (q!=L)
{
Q - & gt; Freq++;
P=q - & gt; next;
P - & gt; The prior=q - & gt; The prior; Q - & gt; The prior - & gt; Next=p;
P=L - & gt; next;
While (p!=L & amp; & P - & gt; Freq & gt; Q - & gt; Freq)
P=p - & gt; next;
Q - & gt; Next=p; P - & gt; The prior - & gt; Next=q;
Q - & gt; The prior=p - & gt; The prior; P - & gt; The prior=q;
The return of q;
}
The else
return 0;
}
Int main ()
{
DuLNode * head;
Int x, loc;
int i=1;
The head=LinkListInit ();
If (head==NULL)
{
Printf (" initialization list failed! \n");
return 0;
}
Do {
Printf (" please enter the first % d element values (end input please enter 0) : ", i++);
Scanf_s (" % d ", & amp; X);
LinkListInsert (head, I, x);
} while (x!=0);
PrintList (head);
return 0;
}
  • Related