C语言数据结构第二章—— 双向链表
get 到的东西:
没什么好说的,就是比单向列表多了一个向前指的指针,参考量更多,操作起来更方便,但是占的内存更大了。
用来练手,练熟练度的数据结构。
- 结束的时候指针p=p->next就是0,判断牌是否为0即可
- 结构体内外同名也行~
- 在用结构体传的时候,主函数这边的结构体是正常的,用点就行
- 函数那边是结构体的指针,用箭头就行
- 包含node指针的list就好,不用指向list的指针,然后传地址到函数里√
- 在函数的接收端定义指向它的指针
- 中间插入节点时,需要改前面的next和后面的prior
实现流程
- 初始化
- 结构体
typedef struct dnode{ //Dliat是double link list 的简写,即双向链表
int data;
struct dnode *prior;
struct dnode *next;
}DNode;
- 一些参数
- 头地址,头结点,头结点等于申请到的内存地址
- 前为NULL,后为NULL,data为0
- 输入参数,用来找最后一位地址的p
每次到一个新的节点的时候都要把几个参数都设置一下
C语言代码——实现部分操作
// 2020/2/13 双向列表
#include <stdio.h>
#include <stdlib.h>
typedef struct dnode{ //double link node abridge
int data;
struct dnode *prior;
struct dnode *next;
}DNode;
typedef struct dlist{ //double link list abridge
struct dnode *phead;
}DList;
void add(DList *list);
void Put_list(DList *list);
void Change_list(DList *list);
void Insert_list(DList *list);
int main()
{
DList list;
add(&list);
Put_list(&list);
// Change_list(&list);
// Put_list(&list);
Insert_list(&list); // point head may be changed, should use structure list to get point head
Put_list(&list);
return 0;
}
/* add double link list from scan*/
void add(DList *list)
{
int input_number;
DNode *pcreate = (DNode*)malloc(sizeof(DNode));
DNode *p =NULL ;
//first node's set
list->phead = pcreate;
DNode *phead = list->phead;
phead->data = 0;
phead->prior = phead->next = NULL;
for( p = phead ; ; p = p->next)
{
p->next = NULL;
scanf("%d", &input_number);
if( input_number != -1 ){
p->next = (DNode*)malloc(sizeof(DNode));
p->data = input_number;
if(p==phead){ p->prior = NULL; }
(p->next)->prior = p;
}
else {
(p->prior)->next = NULL;
free((p->prior)->next);
break;
}
}
}
/* output double link list data by using print*/
void Put_list(DList *list)
{
DNode *phead = list->phead;
DNode *p = NULL;
for( p = phead; p; p = p->next)
{
if(!p->data) { printf("no data in double_link_list\n"); break; }
printf("%d\t", p->data) ;
}
}
//change data from screen
void Change_list(DList *list)
{
int i = 1;
DNode *phead = list->phead;
DNode *p = NULL;
int insert_data, insert_location;
printf(" Input change data and location like this :48 2 \n");
scanf("%d %d", &insert_data, &insert_location);
for(p = phead; p; p = p->next)
{
if(i==insert_location){
p->data = insert_data;
break;
printf(" data has been change \n");
}
i++;
}
if( !p->next ){printf(" list don't have data \n");}
}
// insert a data in list
void Insert_list(DList *list)
{
int i = 1;
DNode *phead = list->phead;
DNode *p = NULL;
DNode *insert_address = NULL;
int insert_data, insert_location;
printf(" Input change data and location like this :48 2 \n");
scanf("%d %d", &insert_data, &insert_location);
for(p = NULL; (p||( i==1 )); )
{
if(i==insert_location){
insert_address = (DNode*)malloc(sizeof(DNode));
insert_address->data = insert_data;
insert_address->prior = p;
if(i!=1)
{
insert_address->next = p->next;
(insert_address->next)->prior = insert_address;
(insert_address->prior)->next = insert_address;
}
else{
insert_address->next = phead;
phead->prior = insert_address;
list->phead = insert_address;
}
printf(" data has been insert \n");
break;
}
if(i==1) {p = phead;}
i++;
if(i>2) p = p->next;
}
if( (!p)&&(i!=1) ){printf(" change location isn't in this list \n");}
}