双向循环链表的各种操作
相对于单链表多了一个指针prev指向全面的节点
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
int socre;
struct student *prev;
struct student *next;
} LinkList;
LinkList *create(int n)
{
LinkList *head, *node, *end;
head = (LinkList *)malloc(sizeof(LinkList));/*初始化头节点*/
end = head; /*开始时end先指向head,添加节点后向后移动*/
head->socre = 101;
for(int i = 0; i < n; i++){
node = (LinkList *)malloc(sizeof(LinkList));
printf("the %d data input:", i+1);
scanf("%d", &node->socre);
/*下面两句让end节点与node节点双向互指*/
end->next = node;
node->prev = end;
end = node;/*end移动到node上,成为最后一个节点*/
/*下面两句让头节点为未节点双向互指*/
head->prev = end;
end->next = head;
}
return head;
}
/*正向打印节点的值*/
int printlist(LinkList *list)
{
LinkList *t = list;/*t指向头结点list*/
t = t->next;/*t指向头节点的下一个节点,一般头节点好像不放值,有效值在头结点的下一个节点*/
int coun = 0;
while(t != list) {/*当节点t没有重新指回头结点,说明没有遍历完链表,就可以继续执行*/
printf("double list socre: %d\n", t->socre);
coun++;
t=t->next;
}
return coun;
}
/*逆向打印节点的值*/
int printlist1(LinkList *list)
{
LinkList *t = list;/*t指向头结点list*/
int coun = 0;
t = t->prev;/*t指向头节点的上一个节点*/
while(t != list) {
printf("double list reserve socre: %d\n", t->socre);
coun++;
t = t->prev;
}
return coun;
}
/*改变某个节点的函数与单链表一致*/
void change(LinkList *list, int n)
{
LinkList *t = list;
//int i = 0;
int i = 1;//由于下面的t指向了下一个节点,所以i就不能从0开始。从1开始正好满足
t = t->next;//t先指向下一个节点,否则的话下面条件就不满足
while(i<n && t!=list){//判断条件现在要以是否指向头结点list来判断是否结束
t = t->next;
i++;
}
if (t!=list) {
printf("enter modify num:");
scanf("%d",&t->socre);
} else {
printf("node isn't exist\n");
}
}
void delete(LinkList *list, int n)
{
LinkList *pre, *t= list;
nt i = 1;//由于下面的t指向了下一个节点,所以i就不能从0开始。从1开始正好满足
t = t->next;//t先指向下一个节点,否则的话下面条件就不满足
while(i<n&&t!=NULL){
pre = t;
t=t->next;
i++;
}
if(t!=NULL){
pre->next = t->next;/*要删除节点的下一个节点,原来是t->next指向的,现在有由t的前一个节点的pre—>next代替*/
t->next->prev = pre;/*要删除节点的下一个节点,原先前一个节点是t,现在改成pre*/
free(t);
}else{
printf("node isn't exist\n");
}
}
void insert(LinkList *list, int n){
LinkList *t=list, *in;
int i = 1;//由于下面的t指向了下一个节点,所以i就不能从0开始。从1开始正好满足
t = t->next;//t先指向下一个节点,否则的话下面条件就不满足
while(i < n && t != NULL) {
t = t->next;
i++;
}
in = (LinkList *)malloc(sizeof(LinkList));
printf("input insert data:");
scanf("%d",&in->socre);
if (t!=NULL){
/*插入的新节点与t后面的一个节点相互指向*/
in->next = t->next;
t->next->prev = in;
/*插入的新节点与t相互指向*/
t->next = in;
in->prev = t;
}else{
printf("node isn't exist\n");
}
}
int main()
{
int n = 0;
int ret = 0;
LinkList *list, *p;
printf("input the node num:");
scanf("%d",&n);
list = create(n);
ret = printlist(list);
ret = printlist1(list);
printf("the node num:%d\n", ret);
printf("insert node:");
scanf("%d",&n);
insert(list, n);
ret = printlist(list);
ret = printlist1(list);
printf("the node num:%d\n", ret);
printf("change node:");
scanf("%d",&n);
change(list, n);
ret = printlist(list);
ret = printlist1(list);
printf("the node num:%d\n", ret);
printf("delete node:");
scanf("%d",&n);
delete(list, n);
ret = printlist(list);
ret = printlist1(list);
printf("the node num:%d\n", ret);
return 0;
}
结果显示:
input the node num:4
the 1 data input:1
the 2 data input:2
the 3 data input:3
the 4 data input:4
double list socre: 1
double list socre: 2
double list socre: 3
double list socre: 4
double list reserve socre: 4
double list reserve socre: 3
double list reserve socre: 2
double list reserve socre: 1
the node num:4
insert node:4
input insert data:5
double list socre: 1
double list socre: 2
double list socre: 3
double list socre: 4
double list socre: 5
double list reserve socre: 5
double list reserve socre: 4
double list reserve socre: 3
double list reserve socre: 2
double list reserve socre: 1
the node num:5
change node:3
enter modify num:8
double list socre: 1
double list socre: 2
double list socre: 8
double list socre: 4
double list socre: 5
double list reserve socre: 5
double list reserve socre: 4
double list reserve socre: 8
double list reserve socre: 2
double list reserve socre: 1
the node num:5
delete node:2
double list socre: 1
double list socre: 8
double list socre: 4
double list socre: 5
double list reserve socre: 5
double list reserve socre: 4
double list reserve socre: 8
double list reserve socre: 1
the node num:4
例子2,不可控制输入参数
#include<stdio.h>
#include<stdlib.h>
typedef struct student {
unsigned int socre;
struct student *prev;
struct student *next;
}LinkList;
LinkList *create(int n)
{
LinkList *head, *node, *end;
head = (LinkList *)malloc(sizeof(LinkList));
head->socre = 100;
end = head;
end->next = head;
end->prev = head;
while(--n) {
node = (LinkList *)malloc(sizeof(LinkList));
node->socre = 100-n;
end->next = node;
node->prev = end;
end = node;
head->prev = end;
end->next = head;
}
return head;
}
int change(LinkList *list, int n)
{
LinkList *node;
node = list;
if(n != 0)
for(int i = 0; i < n; i++) {
node = node->next;
if(node == list) {
printf("out of range node\n");
return -1;
}
}
node->socre = 999;
return 0;
}
int insert(LinkList *list, int n)
{
LinkList *node, *new;
node = list;
if(n != 0)
for(int i = 0; i < n; i++) {
node = node->next;
if(node == list) {
printf("out of range node\n");
return -1;
}
}
new = (LinkList *)malloc(sizeof(LinkList));
new->socre = 888;
new->next = node->next;
node->next->prev = new;
node->next = new;
new->prev = node;
return 0;
}
LinkList *delete(LinkList *list, int n)
{
LinkList *node, *new;
node = list;
if(n != 0)
for(int i = 0; i < n; i++) {
node = node->next;
if(node == list) {
printf("out of range node\n");
return list;
}
}
new = node->next;
node->prev->next = node->next;
node->next->prev = node->prev;
free(node);
if(n == 0)
return new;
else
return list;
}
void scan(LinkList *list)
{
LinkList *node;
node = list;
#if 1
do {
printf("socre is %d\n", node->socre);
node = node->next;
} while(node != list);
#else
do {
printf("socre is %d\n", node->socre);
node = node->prev;
} while(node != list);
#endif
}
void main()
{
int ret;
LinkList *c_node, *d_node;
c_node = create(5);
scan(c_node);
printf("====change========\n");
ret = change(c_node, 1);
if(ret)
printf("change failed\n");
scan(c_node);
printf("====insert========\n");
ret = insert(c_node, 2);
if(ret)
printf("insert failed\n");
scan(c_node);
printf("====delete========\n");
d_node = delete(c_node, 5);
if(d_node == NULL)
printf("delete failed\n");
scan(d_node);
}
结果
kayshi@ubuntu:~/code/linked_list$ gcc double_linked_list.c && ./a.out
socre is 100
socre is 96
socre is 97
socre is 98
socre is 99
====change========
socre is 100
socre is 999
socre is 97
socre is 98
socre is 99
====insert========
socre is 100
socre is 999
socre is 97
socre is 888
socre is 98
socre is 99
====delete========
socre is 100
socre is 999
socre is 97
socre is 888
socre is 98