Home > Back-end >  Known to have a double linked list, design an algorithm from the loop double-stranded delete value a
Known to have a double linked list, design an algorithm from the loop double-stranded delete value a

Time:12-30

Known to have a double linked list, design an algorithm from the cyclic double linked list to delete values for specified values of all nodes,

CodePudding user response:

Fyi:
//cycle without header node two-way linked list 
#include
#include
#include
#include
#include
Struct NODE {
The int data;
Struct NODE * next;
Struct NODE * prev.
Q} * head, * p, * and * s * (p1, p2, * q1, * * ta.
Int I, k, n, m, v, t, n=10;
Int main () {
The setlocale (LC_ALL, "CHS");
Srand (time (NULL));

The head=NULL;

Printf (" create % d nodes in the cycle of two-way linked list: ", N);//create the cycle of N nodes two-way chain table
P=the head;
for (i=0; iQ=(struct NODE *) malloc (sizeof (struct NODE));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/rand () % 100;//fill in 0.. 99 the random value of
Q - & gt; Next=NULL;
Q - & gt; Prev=NULL;
{if (NULL==p)
The head=q;
P=the head;
} else {
P - & gt; Next=q;
Q - & gt; Prev=p;
P=q;
}
}
P - & gt; Next=head;
The head - & gt; Prev=p;

//output the entire circulation two-way linked list
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 2 d (=), s - & gt; The data);
If (NULL!=s - & gt; Next & amp; & S - & gt; Next - & gt; Prev!=s) {printf ("!" \n"); break; }
S=s - & gt; Next;
If (s==head) {
Printf (" Loop \ n ");
break;
}
}

K=3;
V=5;
Printf (" the value for % d node is inserted into the loop before the first % d a two-way linked list node: ", v, k);//the value v of two-way linked list node is inserted into the cycle before the first k node
n=0;
P=the head;
While (1) {
{if (NULL==p)
break;
}
n++;
If (k==1) {
Q=(struct NODE *) malloc (sizeof (struct NODE));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/v;
Q - & gt; Next=head;
Q - & gt; Prev=head - & gt; Prev.
The head - & gt; Prev - & gt; Next=q;
The head - & gt; Prev=q;
The head=q;
break;
} else {
If (k - 1==n) {
Q=(struct NODE *) malloc (sizeof (struct NODE));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/v;
Q - & gt; Next=p - & gt; Next;
Q - & gt; Prev=p;
P - & gt; Next - & gt; Prev=q;
P - & gt; Next=q;
break;
}
}
P=p - & gt; Next;
If (p==head) break;
}

//output the entire circulation two-way linked list
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 2 d (=), s - & gt; The data);
If (NULL!=s - & gt; Next & amp; & S - & gt; Next - & gt; Prev!=s) {printf ("!" \n"); break; }
S=s - & gt; Next;
If (s==head) {
Printf (" Loop \ n ");
break;
}
}

K=5;
Printf (" to delete the % d node: ", k);//remove the first k node
n=0;
P=the head;
While (1) {
{if (NULL==p)
break;
}
n++;
If (k==1) {
Q=head;
The head=head - & gt; Next;
The head - & gt; Prev=q - & gt; Prev.
Q - & gt; Prev - & gt; Next=head;
If (q==head) head=NULL;
Free (q);
break;
} else {
If (k - 1==n) {
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
Q - & gt; Next - & gt; Prev=p;
Free (q);
break;
}
}
P=p - & gt; Next;
If (p==head) break;
}

//output the entire circulation two-way linked list
S=the head;
While (1) {
If (NULL==s) {
printf("\n");
break;
}
Printf (" % 2 d (=), s - & gt; The data);
If (NULL!=s - & gt; Next & amp; & S - & gt; Next - & gt; Prev!=s) {printf ("!" \n"); break; }
S=s - & gt; Next;
If (s==head) {
Printf (" Loop \ n ");
break;
}
}

Printf (" smallest to: ");//smallest
For (p=head; p!=NULL;) {
P1=p - & gt; Prev.
For (q=p - & gt; Next, q1=p; q!=NULL;) {
If (p - & gt; The data & gt; Q - & gt; Data) {

//exchange data
//printf (" \ n 02 02 swap % d % d ", p - & gt; The data, the q - & gt; The data);
//t=p - & gt; The data; P - & gt; data=https://bbs.csdn.net/topics/q-> data; Q -> data=t;

//or

//exchange prev and next
//printf (" \ n 02 02 swap % d % d ", p - & gt; The data, the q - & gt; The data);
If (p==head) {//p is head
If (p - & gt; Next==q) {//pq next to
The head=q;
The head - & gt; Prev=p;
P1 - & gt; Next=q;
P - & gt; Next=q - & gt; Next;
P - & gt; Prev=q;
Q - & gt; Next - & gt; Prev=p;
Q - & gt; Next=p;
q=p;
P=the head;
} else {//pq not next to
The head=q;
The head - & gt; Prev=p;
P1 - & gt; Next=q;
The p2=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
P - & gt; Next - & gt; Prev=p;
P - & gt; Prev=q1;
Q - & gt; Next - & gt; Prev=p;
Q - & gt; Next=p2;
The p2 - & gt; Prev=q;
Q1 - & gt; Next=p;
q=p;
P=the head;
}
} else {//p not head
If (p - & gt; Next==q) {//pq next to
P1 - & gt; Next=q;
P - & gt; Next=q - & gt; Next;
P - & gt; Prev=q;
Q - & gt; Next - & gt; Prev=p;
Q - & gt; Next=p;
Q - & gt; Prev=p;
q=p;
P=p1 - & gt; Next;
} else {//pq not next to
P1 - & gt; Next=q;
The p2=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
P - & gt; Prev=q - & gt; Prev.
Q - & gt; Next - & gt; Prev=p;
Q - & gt; Next=p2;
Q - & gt; Prev=p;
The p2 - & gt; Prev=q;
Q1 - & gt; Next=p;
q=p;
P=p1 - & gt; Next;
}
}

//output the entire circulation two-way linked list
//s=head;
//the while (1) {
//if (NULL==s) {
//printf (" \ n ");
//break;
//}
02//printf (" % d "(=), s - & gt; The data);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related