-------------------------------------------------------------------------------------------
前言:
用C语言编写实现创建双向链表、正序遍历、逆序遍历、摧毁链表。
-------------------------------------------------------------------------------------------
双向:顾名思义,比单向多一个返回的方向,单向链表只一个指针指向下一个指针,可以很轻松的访问下一个节点,却很难访问上一个节点,而双向链表很轻松的解决这个问题,因此双向链表更具有实用性;
双向链表:
结构体定义:
typedef struct _double_link_node
{
int data;
struct _double_link_node *prev;//指向前一个节点
struct _double_link_node *next;//指向下一个节点
}Node;
核心思想:
本次使用尾插法创建新的节点进行接入,在不考虑临界状态:
旧节点为plast,新节点pnext:
plast->next = pnext; //旧节点的next指向新节点
pnext->prev = plast; //新节点的前一节点指向原来的plast
plast = plast->next; //跟新节点(plast移动到新的节点)
临界情况 :初始第一个节点,将头节点赋值给旧节点;
plast = head;
附上完整创建链表的代码:
Node *Create_double_link() //返回值为结构体的类型
{
Node *head;
Node *pnext;
Node *plast;
int i,n;
head = (Node *) malloc(sizeof(Node));
assert(NULL != head);
head->prev=NULL; //初始化头节点,将前一个节点设置为NULL
head->next=NULL; //初始化头节点,将后一个节点设置为NULL
printf("please input the length of the double linked list:");
scanf("%d",&n); //键盘输入想要创建的数量
while( n != 0 )
{
plast = head;
for( i = 0; i < n; i++)
{
pnext = (Node *) malloc(sizeof(Node));
printf("Insert Node %d data:",i+1);
scanf("%d",&pnext->data);
plast->next = pnext;
pnext->prev = plast;
plast = plast->next;
}
pnext->next = NULL; //最后一个新节点的下一个节点指向NULL;
break;
}
return head;
}
遍历双向链表:
正序遍历思想:
temp = head->next; //存放下一个节点的容器
while(temp != NULL) //遍历到最后一个节点
{
temp = temp->next; //移动到下一个节点
}
附上完整遍历代码:
void print_link_Positive(Node *head)
{
Node *temp;
int j=0;
temp = head;
while(temp != NULL)
{
j++;
printf("Output %d node data :%d\n",j,temp->data);
printf("Output %d node adress :%p\n",temp->data,temp);
temp = temp->next;
}
}
逆序遍历:
void print_link_Reverse(Node *head)
{
Node *p;
p = head;
while(p->next != NULL) //找到最后一节点
{
p = p->next;
}
while(p != head) //从最后一节点开始找到头节点
{
printf("Output %d node data :%d\n",p->data,p->data);
printf("Output %d node adress :%p\n",p->data,p);
p = p->prev;
} //但是头节点没有打印,所以继续打印
printf("Output %d node data :%d\n",p->data,p->data);
printf("Output %d node adress :%p\n",p->data,p);
}
完整代码:
/*********************************************************************************
* Copyright: (C) 2018 guozhihao
* All rights reserved.
*
* Filename: doublelink.c
* Description: This file
*
* Version: 1.0.0(08/20/2018)
* Author: Guozhihao <810170156@qq.com>
* ChangeLog: 1, Release initial version on "08/20/2018 01:11:35 AM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct _double_link_node
{
int data;
struct _double_link_node *prev;
struct _double_link_node *next;
}Node;
Node *Create_double_link()
{
Node *head;
Node *pnext;
Node *plast;
int i,n;
head = (Node *) malloc(sizeof(Node));
assert(NULL != head);
head->prev=NULL;
head->next=NULL;
printf("please input the length of the double linked list:");
scanf("%d",&n);
while( n != 0 )
{
plast = head;
for( i = 1; i < n || i == n; i++)
{
pnext = (Node *) malloc(sizeof(Node));
printf("Insert Node %d data:",i);
scanf("%d",&pnext->data);
plast->next = pnext;
pnext->prev = plast;
plast = plast->next;
}
pnext->next = NULL;
break;
}
return head;
}
void print_link_Reverse(Node *head)
{
Node *p;
p = head;
while(p->next != NULL)
{
p = p->next;
}
while(p != head)
{
printf("Output %d node data :%d\n",p->data,p->data);
printf("Output %d node adress :%p\n",p->data,p);
p = p->prev;
}
printf("Output %d node data :%d\n",p->data,p->data);
printf("Output %d node adress :%p\n",p->data,p);
}
void print_link_Positive(Node *head)
{
Node *temp;
int j=0;
temp = head;
while(temp != NULL)
{
j++;
printf("Output %d node data :%d\n",j,temp->data);
printf("Output %d node adress :%p\n",temp->data,temp);
temp = temp->next;
}
}
void Delete_list(Node *head)
{
Node *pnext = NULL;
while( head != NULL )
{
pnext = head;
head = head->next;
printf("Now free Node data: %d adress:%p\n",pnext->data,pnext);
free(pnext);
}
}
int main(int argc,char **argv)
{
Node *head;
head = (Node *) Create_double_link();
print_link_Positive(head);
print_link_Reverse(head);
Delete_list(head);
return 0;
}
运行程序效果如下:
[guozhihao@localhost C6]$ ./a.out
please input the length of the double linked list:4
Insert Node 1 data:1
Insert Node 2 data:2
Insert Node 3 data:3
Insert Node 4 data:4
Output 1 node data :0
Output 0 node adress :0xf90010
Output 2 node data :1
Output 1 node adress :0xf90030
Output 3 node data :2
Output 2 node adress :0xf90050
Output 4 node data :3
Output 3 node adress :0xf90070
Output 5 node data :4
Output 4 node adress :0xf90090
Output 4 node data :4
Output 4 node adress :0xf90090
Output 3 node data :3
Output 3 node adress :0xf90070
Output 2 node data :2
Output 2 node adress :0xf90050
Output 1 node data :1
Output 1 node adress :0xf90030
Output 0 node data :0
Output 0 node adress :0xf90010
Now free Node data: 0 adress:0xf90010
Now free Node data: 1 adress:0xf90030
Now free Node data: 2 adress:0xf90050
Now free Node data: 3 adress:0xf90070
Now free Node data: 4 adress:0xf90090