Two-way linked lists are also called double linked lists, each of which has two pointers in each data node, pointing directly to successors and direct precursors. Therefore, starting from any node in a two-way list, it is easy to access its predecessor nodes and successors. Other people often to construct two-way circular chain list, today we maverick, first to try to construct not the leading node bidirectional non circular chain list. Sample code uploaded to Https://github.com/chenyufeng1991/DoubleLinkedList.
(1) Definition of node type of bidirectional non-cyclic linked list without leading node
typedef struct nodelist{
int element;
struct nodelist *prior;
struct nodelist *next;
} Node;
(2) Initialization of the double linked list
1. Non-cyclic bidirectional linked list
void Initlist (node *pnode) {
Pnode = NULL initialized without the lead node;
printf ("%s function execution, list initialization complete \ n", __function__);
}
(3) The construction of two-way non-cyclic linked list by the tail interpolation method:
Create a non-circular doubly linked list
node *createlist (node *pnode) {
node *pinsert;
Node *pmove;
Pinsert = (node*) malloc (sizeof (Node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pinsert->prior = NULL;
scanf ("%d",& (pinsert->element));
Pmove = Pnode;
if (pinsert->element <= 0) {
printf ("%s function execution, enter data illegal, create list stop \ n", __function__);
return NULL;
}
while (Pinsert->element > 0) {
if (Pnode = = NULL) {
pnode = Pinsert;
Pmove = Pnode;
} else{
pmove->next = Pinsert;
Pinsert->prior = Pmove;
Pmove = pmove->next;
}
Pinsert = (node *) malloc (sizeof (node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pinsert->prior = NULL;
scanf ("%d",& (pinsert->element));
}
printf ("%s function execution, build list succeeded \ n", __function__);
return pnode;
}
(4) Print non-circular two-way linked list
3. Print non-circular doubly linked list
void Printlist (Node *pnode) {
if (Pnode = NULL) {
printf ("%s function execution, linked list is empty, print failed \ \", __function__ );
} else{
while (Pnode!= NULL) {
printf ("%d", pnode->element);
Pnode = pnode->next;
}
printf ("\ n");
}
(5) Clear the list
4. Clears all elements in the linear table L to make it an empty table
node *clearlist (node *pnode) {
if (Pnode = NULL) {
printf ("%s" function executes, the original list is an empty list, No need to implement the method \ n ", __function__);
return NULL;
} else{while
(Pnode->next!= NULL) {
//delete each node once
pnode = pnode->next;
Free (pnode->prior);
Pnode->prior = NULL;
}
Clears the last node free
(pnode);
Pnode = NULL;
printf ("%s function execution, two-way non-cyclic list emptying succeeded \ n", __function__);
Return Pnode
}
}
(6) Calculate the length of the linked list
5. Returns the length
int sizelist (node *pnode) {
int i = 0) of the bidirectional linked list without the lead node;
if (Pnode = NULL) {
printf ("%s function executes, linked list is empty, length is 0\n", __function__);
return 0;
} else{while
(Pnode!= NULL) {
i++;
Pnode = pnode->next;
}
printf ("%s function executes, linked list length is%d\n", __function__,i);
return i;
}
}
(7) to determine whether the linked list is empty
6. Check whether the double link table is empty, if empty then return 1, otherwise return 0
int isemptylist (Node *pnode) {
if (Pnode = NULL) {
printf ("%s function execution, Current list is null \ n ", __function__);
return 1;
}
printf ("%s function execution, current list not NULL \ n", __function__);
return 0;
}
(8) Find elements in a two-linked list
7. Returns the element
int getelement (node *pnode,int pos) {
int i = 1, of the POS position in the bidirectional non-cyclic list of not-leading nodes;
Node *pmove;
Pmove = Pnode;
while (Pmove!= NULL) {
if (i = = pos) {
printf ("%s function execution, element of pos=%d position is%d\n", __function__,pos,pmove->element); return
pmove->element;
}
i++;
Pmove = pmove->next;
}
printf ("%s function execution, get pos position element failed \ \ __function__");
return-1;
}
(9) Returns the memory address of an element in the list
8. Find the first element with a given value x from a doubly linked list that does not lead a node, or return the storage address of the data field of the node if the lookup succeeds, otherwise return null
int *getelemaddr (node *pnode,int x) {
node * Pmove;
Pmove = Pnode;
while (Pmove!= NULL) {
if (pmove->element = x) {
printf ("%s function executes, x=%d element's memory address is: 0x%x\n",__function__,x,& (pmove->element));
Return & (Pmove->element);
}
Pmove = pmove->next;
}
printf ("%s function execution, fetch x=%d memory address failed \ \", __function__,x);
Return & (Pmove->element);
}
(10) Modify the value of a node
9. Change the value of the POS node in the double linked list to the value of x, and if the modification returns 1 successfully, return 0
int Modifyelem (Node *pnode,int pos,int x) {
int i = 1;
Node *pmove;
Pmove = Pnode;
while (Pmove!= NULL) {
if (i = = pos) {
pmove->element = x;
printf ("%s function execution, modify pos=%d position element succeeded \ \", __function__,pos);
return 1;
}
i++;
Pmove = pmove->next;
}
printf ("%s function execution, modifying pos=%d position element failed \ n", __function__,pos);
return 1;
}
(11) Table header inserts a node
10. Insert a node
*insertheadlist (Nodal *pnode,int x) {node *pinsert) to the head of a non-circular doubly linked list that does not lead the node
;
Pinsert = (node*) malloc (sizeof (Node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pinsert->prior = NULL;
pinsert->element = x;
Consider the case where the original list is empty
if (Pnode = = NULL) {
pnode = Pinsert;
printf ("%s function execution, success in header insert node \ n", __function__);
return pnode;
}
Pinsert->next = Pnode;
Pnode->prior = Pinsert;
Pnode = Pinsert;
printf ("%s function execution, success in header insert node \ n", __function__);
return pnode;
}
(12) Insert a node at the end of the table
11. Insert a node nodes
*inserttaillist (node *pnode,int x) {node *pmove to the end of the non-circular doubly linked list that does not lead the node
;
Node *pinsert;
Pinsert = (node*) malloc (sizeof (Node));
memset (pinsert, 0, sizeof (Node));
Pinsert->next = NULL;
Pinsert->prior = NULL;
pinsert->element = x;
Pmove = Pnode;
Consider the case where the original list is empty
if (Pnode = = NULL) {
pnode = Pinsert;
printf ("%s function execution, successful at tail insert node \ \ \ __function__);
" return pnode;
}
while (Pmove->next!= NULL) {
pmove = pmove->next;
}
Pmove->next = Pinsert;
Pinsert->prior = Pmove;
printf ("%s function execution, successful at tail insert node \ \ \ __function__);
" return pnode;
}
(13) Test function
int main (int argc, const char * argv[]) {
Node *plist;
Initlist (plist);
Printlist (plist);
Sizelist (plist);
plist = CreateList (plist);
Printlist (plist);
Sizelist (plist);
Isemptylist (plist);
GetElement (plist, 4);
Getelemaddr (plist, 5);
Modifyelem (plist, 4, 1111);
Printlist (plist);
plist = Insertheadlist (plist, 8888);
Printlist (plist);
plist = Inserttaillist (plist,9999);
Printlist (plist);
plist = Clearlist (plist);
Printlist (plist);
return 0;
}