我的理解:
所谓双向,指的是各节点之间的逻辑关系是双向的,但通常头指针只设置一个,除非实际情况需要,可以为最后一个节点再设置一个“头指针”。
总代码:
#include <stdio.h>
#include <malloc.h>
/**
* Double linked list of intergers. The key is char.
*/
typedef struct DoubleLinkedNode {
char data;
struct DoubleLinkedNode *previous;
struct DoubleLinkedNode *next;
}DLNode, *DLNodePtr;
/**
* Initialize the list with a header.
* @return The pointer to the header.
*/
DLNodePtr initLinkList(){
DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(DLNode));
tempHeader->data = '\0';
tempHeader->previous = NULL;
tempHeader->next = NULL;
return tempHeader;
}//of initialize
/**
* Print the list.
* @param paraHaeder The header of the list.
*/
void printList(DLNodePtr paraHeader){
DLNodePtr p = paraHeader->next;
while(p != NULL){
printf("%c", p->data);
p = p->next;
}//Of while.
printf("\r\n");
}//Of printList
/**
* Insert an element to the given position.
* @param paraHeader The header of the list.
* @param paraChar The given char.
* @param paraPosition The given position.
*/
void insertElement(DLNodePtr paraHeader, char paraData, int paraPosition){
DLNodePtr p, q, r;
p = paraHeader;
//step 1.Search for the position
for (int i = 0; i < paraPosition; i++){
p = p->next;
if(p == NULL){
printf("The position %d is beyond the list.", paraPosition);
return;
}//Of if
}//Of for
//step 2.construct a new node
q = (DLNodePtr)malloc(sizeof(DLNode));
q->data = paraData;
//step 3.insert after the target
r = p->next;
q->next = p->next;
q->previous = p;
p->next = q;
if(r != NULL){
r->previous = q;
}
}// Of insertElement
void insertElementTest(){
printf("---- The insertElementTest Begin\n");
DLNodePtr tempHeader = initLinkList();
insertElement(tempHeader, 'A', 0);
insertElement(tempHeader, 'B', 1);
insertElement(tempHeader, 'C', 2);
insertElement(tempHeader, 'D', 3);
insertElement(tempHeader, 'E', 4);
insertElement(tempHeader, 'F', 5);
printList(tempHeader);
printf("---- The insertElementTest End\n");
}//Of insertElement
/**
* Delete an element from the list.
* @param paraHeader The header of the list.
* @param paraChar The given char.
*/
void deleteElement(DLNodePtr paraHeader, char paraChar){
DLNodePtr p, q;
p = paraHeader;
//Step 1.Locate.
while((p->next != NULL) && (p->next->data != paraChar)){
p = p->next;
}//of while
//Step 2.Error check.
if(p->next == NULL){
printf("%c is not in the list", paraChar);
return;
}//of if
//Step 3.Change links.
q = p->next;
p->next = p->next->next;
if(q->next != NULL){
p->next->previous = p;
}
//Step 4.Free
free(q);
}//Of delete
void deleteElementTest(){
printf("--- The deleteElementTest Begin\n");
DLNodePtr tempHeader = initLinkList();
insertElement(tempHeader, 'A', 0);
insertElement(tempHeader, 'B', 1);
insertElement(tempHeader, 'C', 2);
insertElement(tempHeader, 'D', 3);
insertElement(tempHeader, 'E', 4);
insertElement(tempHeader, 'F', 5);
printList(tempHeader);
//delete first
deleteElement(tempHeader, 'A');
printList(tempHeader);
//delete last
deleteElement(tempHeader, 'F');
printList(tempHeader);
//delete middle
deleteElement(tempHeader, 'C');
printList(tempHeader);
printf("--- The deleteElementTest End\n");
}
/**
* Address test: beyond the book;
*/
void basicAdressTest(){
DLNode tempNode1, tempNode2;
tempNode1.data = 4;
tempNode1.next = NULL;
tempNode1.previous = NULL;
tempNode2.data = 6;
tempNode2.next = NULL;
tempNode2.previous = NULL;
printf("The first node:\n p of the Node: %d\n p of the Node.data :%d\n p of the Node.next: %d\n p of the Node.prior: %d\n", &tempNode1, &tempNode1.data, &tempNode1.next, &tempNode1.previous);
printf("The second node:\n p of the Node: %d\n p of the Node.data :%d\n p of the Node.next: %d\n p of the Node.prior: %d\n", &tempNode2, &tempNode2.data, &tempNode2.next, &tempNode1.previous);
}
/**
* Locate an element from the list.
* @param paraHeader The header of the list.
* @param paraChar The given char.
* @return empty DLNodePtr or the nodePtr of the element
*/
DLNodePtr locateElement(DLNodePtr paraHeader, char paraData){
DLNodePtr p;
p = paraHeader;
if(p == NULL){
printf("no date in the list\n");
return NULL;
}
while(p->data != paraData){
p = p->next;
if(p == NULL){
printf("the data not in the list\n");
return NULL;
}
}
return p;
}
void locateElementTest(){
printf("--- The locateElementTest Begin\n");
DLNodePtr tempHeader = initLinkList();
insertElement(tempHeader, 'A', 0);
insertElement(tempHeader, 'B', 1);
insertElement(tempHeader, 'C', 2);
insertElement(tempHeader, 'D', 3);
insertElement(tempHeader, 'E', 4);
insertElement(tempHeader, 'F', 5);
printList(tempHeader);
DLNodePtr p;
p = locateElement(tempHeader, 'O');
p = locateElement(tempHeader, 'B');
printf("the next of p: %c\n", p->next->data);
printf("the prior of p: %c\n", p->previous->data);
printf("--- The locateElementTest End\n");
}
int main (){
insertElementTest();
deleteElementTest();
basicAdressTest();
locateElementTest();
return 0;
}
运行结果: