一、双向链表的创建
双向链表的结点结构体有两个指针,一个指向前向结点,一个指向后继节点,创建步骤和单向链表差不多
typedef struct DNode
{
int data;
struct DNode *front;
struct DNode *next;
} DNode;
DNode *create_DNode_list(int n)
{
int a, i;
DNode *head, *pioneer, *previous;
head = (DNode *)malloc(sizeof(DNode));
head->data = 0;
head->next = NULL;
head->front = NULL;
pioneer = head;
previous = head;
for(i = 0; i < n; i++){
scanf("%d", &a);
// 1:malloc space for the new Node and let the pioneer point to it
pioneer = (DNode *)malloc(sizeof(DNode));
// 2:initiate the new Node
pioneer->data = a;
pioneer->next = NULL;
pioneer->front = previous;
// 3:use the previous to link the previous to the new Node
previous->next = pioneer;
// 4:update the previous
previous = pioneer;
}
return head;
}