博客:http://blog.csdn.net/muyang_ren
关于双向链表的原理很多都有说明,我这只是与前面的内核链表作个对比,同样实现数据的增删差改。
截图:
1、main.c
#include "doublelist.h" int main(void) { int num, i; double_plist list; doublelist_init(&list); //初始化双向链表 printf("\n请输入链表长度:"); scanf("%d",&num); //增 printf("---------------- ① ----------------\n"); printf("----------------增加----------------\n"); for(i=1;i<=num;i++){ printf(">>> 增加 %d\n", i); doublelist_addnum(list,i); //加入数据到双向链表 } //查 printf("---------------- ② ----------------\n"); printf("-----------------显示----------------\n"); doublelist_show(list); //显示双向链表内数据 //删 printf("---------------- ③ ----------------\n"); printf("----------------删除----------------\n"); printf("请输入要删除的数据\n"); scanf("%d",&num); doublelist_del(list,num); //再查 printf("----------------显示----------------\n"); doublelist_show(list); //显示双向链表内数据 //修改 printf("\n---------------- ④ ----------------\n"); printf("----------------增加----------------\n"); printf(">>>: n m ,则将 n 修改为 m\n"); scanf("%d %d", &i, &num); doublelist_revise(list, i, num); //显示双向链表内数据 //再再查 printf("----------------显示----------------\n"); doublelist_show(list); //显示双向链表内数据 return 0; }
2、头文件
doublelist.h
#ifndef __doubleLIST_H__ #define __doubleLIST_H__ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef int datatype; typedef struct doublelist{ datatype data; struct doublelist *next, *prior; }double_list,*double_plist; extern void doublelist_new(double_plist * L); //增 extern void doublelist_init(double_plist * L);//初始化表头 extern void doublelist_show(double_plist l);//查 extern void doublelist_addnum(double_plist, int); extern void doublelist_del(double_plist ,int);//删 extern void doublelist_revise(double_plist ,int , int );//改 #endif
3、双向链表实现函数
doublelist.c
#include "doublelist.h" //开辟新的节点空间 void doublelist_new(double_plist *list){ *list=(double_plist)malloc(sizeof(double_list)); if(NULL == *list){ perror("malloc\n"); exit(-1); } } //双向链表初始化 void doublelist_init(double_plist *list){ *list=(double_plist)malloc(sizeof(double_list)); if(NULL == *list){ perror("malloc\n"); exit(-1); } (*list)->next=(*list)->prior=(*list); } //在双向链表表头添加节点并赋值 void doublelist_addnum(double_plist list,int num) { double_plist new; doublelist_new(&new); //开辟新的空间给new new->data = num; new->prior=list->prior; new->prior->next=new; new->next=list; list->prior=new; } //查 //显示双向链表内数据 void doublelist_show(double_plist list) { int i=1; double_plist p; for(p=list->next; p!=list; p=p->next){ while(i++%6==0){ printf("\n"); } printf("%d\t",p->data); } printf("\n"); } //删 void doublelist_del(double_plist list,int num) { int flag=0; //flag 找到删除点的标志位 double_plist p; for(p=list->next; p!=list; p=p->next){ //如果找到了链表对应的值 if(num == p->data){ p->prior->next=p->next; p->next->prior=p->next; flag=1; //标志位置1; free(p); } } //如果没找到链表对应的值 if(flag==0){ printf("***在链表中没有找到对应的值***\n"); } } //修改 void doublelist_revise(double_plist list,int old_num, int new_num) { int flag=0; //flag 找到删除点的标志位 double_plist p; for(p=list->next; p!=list; p=p->next){ //如果找到了链表对应的值 if(old_num == p->data){ p->data = new_num; flag=1; //标志位置1; } } //如果没修改链表对应的值 if(flag==0){ printf("***在链表中没有找到对应的值***\n"); } }
时间: 2024-10-29 04:21:14