C语言之双链表

2 篇文章 0 订阅
1 篇文章 0 订阅

/*D_list.h头文件*/

#ifndef __D_LIST_H__

#define __D_LIST_H__
typedef int DataType;
typedef struct node{
DataType data;
struct node *next;
struct node *prev;
}D_node;
typedef struct list{
D_node *head;
}D_list;


void Judge(char *ch);
void Print_D_list(D_list *L);
int Length_D_list(D_list *L);
bool Init_D_list(D_list *L);
void Create_D_list(D_list *L);
bool Insert_node(D_list *L,DataType d,int pos);
bool Delate_node(D_list *L,int pos);
#endif


/*D_list_func.c */

/*
内容:双链表操作函数:初始化,结点添加,打印,表长,取数据,修改数据,插入,删除;
作者:Carre
日期:2015.9.17
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>


//定义结点数据类型,可根据实际情况替换
typedef int DataType;


//定义结点结构体
typedef struct node{
DataType data;
struct node *prev;
struct node *next;
}D_node;


//定义双链表结构体
typedef struct list{
D_node *head;
}D_list;


//******************************************************
//判断标志为y/n
void Judge(char *ch){
scanf("%*[^\n]");
scanf("%*c");
scanf("%c",ch);
}
//*****************************************************
//打印双链表
void Print_D_list(D_list *L){
D_node *current=L->head->next;
if(current==NULL){
printf("空链表!");
}
else{
while(current!=NULL){
printf("%d->",current->data);
current=current->next;
}
printf("NULL");
printf("\n");
}
}
//*****************************************************
//计算链表长度
int Length_D_list(D_list *L){
D_node *current=L->head->next;
int length=0;
while(current){
length++;
current=current->next;
}
return length;
}
//******************************************************
//双链表的初始化
bool Init_D_list(D_list *L){
L->head=(D_node *)malloc(sizeof(D_node));
if(L->head){
L->head->next=NULL;
L->head->prev=NULL;
return true;
}
else 
return false;
}
//*********************************************************
//创建双链表依次添加链表结点
void Create_D_list(D_list *L){
D_node *current=L->head,*tmp;
char flag;
while(current->next!=NULL){
current=current->next;
}
while(1){
tmp=(D_node *)malloc(sizeof(D_node));
tmp->next=NULL;
tmp->prev=NULL;
printf("请输入结点数据(int):");
scanf("%d",&tmp->data);
current->next=tmp;
tmp->prev=current;
current=tmp;
printf("是否继续录入结点y/n:");
Judge(&flag);
if(flag=='y');
else break;
}
}
//****************************************************
//双链表在指定位置后插入结点
bool Insert_node(D_list *L,DataType d,int pos){
D_node *current=L->head,*tmp,*p;
int num=0;
tmp=(D_node *)malloc(sizeof(D_node));
tmp->next=NULL;
tmp->prev=NULL;
tmp->data=d;
if(current->next==NULL||pos==0){
p=current->next;
current->next=tmp;
tmp->prev=current;
tmp->next=p;
return true;
}
while(current->next!=NULL&&num<pos){
num++;
current=current->next;
}
if(num<=pos){
if(current->next==NULL){
current->next=tmp;
tmp->prev=current;
return true;
}
p=current->next;
current->next=tmp;
tmp->next=p;
p->prev=tmp;
tmp->prev=current;
return true;
}
}
//*****************************************************
//删除指定位置结点
bool Delate_node(D_list *L,int pos){
D_node *current=L->head;
int num=0;
if(current->next==NULL||pos==0) 
return false;
while(current->next!=NULL&&num<pos){
num++;
current=current->next;
}
if(num<=pos){
if(current->next==NULL&&num!=pos){
return false;
}
if(current->next==NULL&&num==pos){
current->prev->next=NULL;
free(current);
return true;
}
current->prev->next=current->next;
current->next->prev=current->prev;
free(current);
return true;
}
}
//****************************************************
/*
int main(){
D_list list;
//初始化双链表
Init_D_list(&list);
//创建双链表
Create_D_list(&list);
//打印链表
Print_D_list(&list);
//计算链表长度(不含头指针)
printf("length=%d\n",Length_D_list(&list));
int pos=0;//操作位置
DataType d=0;//结点数据
char flag;//操作判断位
while(1){
//指定位置后插入结点
printf("请输入插入的数据和位置:");
scanf("%d%d",&d,&pos);
Insert_node(&list,d,pos);
//打印链表
Print_D_list(&list);
//计算链表长度(不含头指针)
printf("length=%d\n",Length_D_list(&list));
//删除指定位置结点
printf("请输入删除结点的位置:");
scanf("%d",&pos);
Delate_node(&list,pos);
//打印链表
Print_D_list(&list);
//计算链表长度(不含头指针)
printf("length=%d\n",Length_D_list(&list));
printf("是否继续操作y/n:");
Judge(&flag);
if(flag=='y');
else break;
}
printf("\n");
return 0;
}
*/


/*D_list_main.c*/
内容:双链表演示
作者:Carre
日期:2015.9.17
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include"D_list.h"
//*****************************************************8888
int main(){
D_list *list;
//初始化链表
Init_D_list(&list);
//创建链表
printf("创建双链表:\n");
Create_D_list(&list);
//打印链表
Print_D_list(&list);
//计算链表的长度(不含头指针)
printf("length=%d\d",Length_D_list(&list));
int pos=0;//操作结点位置
DataType d=0;//结点数据
char flag;//操作判断位
while(1){
//指定位置后插入结点
printf("请输入插入结点的数据和位置:");
scanf("%d%d",&d,&pos);
Insert_node(&list,d,pos);
//打印链表
Print_D_list(&list);
//计算链表的长度(不含头指针)
printf("length=%d\d",Length_D_list(&list));
//删除指定位置结点
printf("请输入删除结点的位置:");
scanf("%d",&pos);
Delate_node(&list,pos);
//打印链表
Print_D_list(&list);
//计算链表的长度(不含头指针)
printf("length=%d\d",Length_D_list(&list));
printf("是否继续操作y/n:");
Judge(&flag);
if(flag=='y');
else break;
}
printf("\n");
return 0;
}
















































































































































































*/



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值