Today using C to implement a doubly linked list, the list has a sorting function, by default in ascending order, the accepted parameters can be a number, or it can be a string. Now write your own code and share it.
Header file:
#define Double_link_h_included
Doubly linked list node
typedef struct DOUBLE_NODE
{
struct double_node* next;
struct double_node* pre;
Const void* value;//Note that this is declared as const, which is considered a character constant
} node;
Determine if the linked list is empty
extern bool Dlink_is_empty ();
The size of the linked list
extern int dlink_size ();
Compare the size of node values, only numbers and strings
extern int compare (const void* a,const void* B);
Inserts "value" into the linked list, returns 0 successfully, otherwise returns-1
extern int Dlink_insert (const void *pval);
Returns the first element of a linked list
extern node* Peek ();
Remove the first element of the list, free operation, after the poll method, cannot forget, otherwise, memory leaks
extern node* poll ();
#endif//double_link_h_included
#include <cstdio> #include <cstdlib> #include <malloc.h> #include <iostream> #include < cctype> #include <cstring> #include "double_link.h" using namespace std;
Linked list head static node* head = NULL; static node* tail = NULL; Number of nodes static int count = 0;
/** * Create node, return node pointer after success * \param void* value incoming pointer * \return node* */static node* CreateNode (const void* value) {n ode* n = (node*) malloc (sizeof (node)); if (!n) {cout << "failed in createnode!" << Endl; return NULL; }//Assign value to node N->value = value; Maintain its own pointer n->next = N->pre = N; return n; }
/** * Determines whether the linked list is empty * \return */bool Dlink_is_empty () { return count = = 0;}
/** * Returns the size of the linked list * \return count */int dlink_size () { return count;}/** * Compare node values Size, limited to numbers and strings */int compare (const void* A,const void* b) { if (isdigit (* (int*) a)) &NBSP;&NBSP;&N Bsp { return (* (int*) a)-(* (int*) b); } else { int i = 0,j = 0; & nbsp; while (((char*) a) [I] && ((char*) b) [j]) { & nbsp; if (((char*) a) [I]-((char*) b) [j]! = 0) { return ((char*) a) [I]-((char*) b) [j]; } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; i++,j++; } if (strlen ((char*) a) = = I && strlen ((char*) b) = = j) { return 0; } else if (strlen ((char*) a) = = i) { return 1; } Else { return-1; }
}
}
/** * Inserts "value" into the linked list, returns 0 successfully, otherwise 1 */int dlink_insert (const void *pval) { node* n; // 1. The list is empty if (count = = 0) { n = createnode (PVal); head = tail = n; count + +; return 1; } //2. If the linked list is not empty, make a comparison, determine the location if (compare (Head->value,pval) > 0) //in front of head { n = createnode (pval); & nbsp; n->next = head; head->pre = n; head = n; count++; return 1; } else if (compare (Tail->value,pval) &Lt 0) //tail back { n = createnode (pval); nbsp; tail->next = n; n->pre = tail; tail = n; count++; return 1; } else //At a location between head and tail { node* index = tail; while (compare (Index->value,pval) > 0) { index = index->pre; } n = CreateNode (pval);
Index->next = n; N->pre = index; N->next = index->next; Index->next->pre = n; count++; return 1; } return-1; }/** * Returns the first element of the list */node* peek () {return head = = NULL? Null:head; }/** * Take out the first element of the list, the free operation, after the poll method, cannot forget, otherwise, memory leaks */node* poll () {node* n = head; Head = head->next; return n; }
Test:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include "Double_link.h"
using namespace Std;
void Testnum ();
void Teststr ();
int main () {
Teststr ();
}
void Testnum () {
int num = 12;
void* value = #
Dlink_insert (value);
int NUM1 = 3;
void* value1 = &num1;
Dlink_insert (value1);
node* n = poll ();
cout << * ((int*) (n->value)) << Endl;
Free (n);
node* no = Peek ();
cout << * ((int*) (no->value)) << Endl;
}
void Teststr () {
Const void* str = "ABC";
Dlink_insert (str);
Const void* STR1 = "ABCD";
Dlink_insert (STR1);
node* n = poll ();
cout << (char*) (n->value) << Endl;
Free (n);
node* no = Peek ();
cout << (char*) (no->value) << Endl;
}
Output Result:
3 12
ABCD ABC
A sortable, doubly linked list of C implementations