用c语言描述双链表的数据类型,数据结构--双链表(C语言)

双链表:

双链表与单链表不同的是,其结构体中存在两个指针,“Next”指向直接后继,“Prior”指向直接前驱,链表的访问不再是单链表的单向访问(只能访问后继结点),可以利用next和prior指针方便的进行双向访问(既可以访问后继结点也可以访问前驱结点)。

实现:

//结构体

typedef struct node{

int data;

struct node *next;

struct node *prior;

}Node;

#include "Link2.h"

//初始化链表头结点

//return:头结点地址

Node *init_list()

{

Node *head;

head = (Node *)malloc(sizeof(Node));

if(!head)

{

printf("Error!\n");

exit(1);

}

head->data = 0;

head->next = NULL;

head->prior = NULL;//将头结点前驱结点置为NULL

return head;

}

//队尾插入新结点

//@num:新结点个数

//@head:头结点

void add_node(int num,Node *head)

{

if(!head)

{//判断链表是否存在

printf("The list is empty!\n");

return ;

}

Node *p,*q;

p = head;

q = head;

while(q->next)//循环q至链表队尾

q=q->next;

while(num--)

{

p = (Node *)malloc(sizeof(Node));

printf("input data to add:");

scanf("%d",&p->data);

p->next = NULL;//新结点的next指针为NULL(即将成为尾结点)

p->prior = q;//新结点的前驱结点指向尾结点q

q->next = p;//将p结点"挂"到q上,使p成为新的尾结点

q = p;//q重新指向队尾

}

}

//根据结点位置删除结点

//@no:待删除结点位置

//@head:头指针

//return:成功返回删除结点的值,失败返回-1(默认没有结点的数据域为-1)

int delete_node(int no,Node *head)

{

if(!head)

{

printf("The list is empty!\n");

return -1;

}

Node *p,*q;

int value;

p = head;

while(no-- && p->next)

{

p = p->next;

if(!p->next && no > 0)

{//当p为队尾且no还大于0时,链表长度不够

printf("The length is less than \'no\'\n");

return -1;

}

}

q = p->prior;//q为p的前驱结点

q->next = p->next;//q的next指针指向p的下一个节点,将p从链表中脱离

if(p->next)//若p不是队尾结点,则将p下一结点的prior指针指向q

p->next->prior = q;

value = p->data;

free(p);

p = NULL;

return value;

}

//根据结点位置修改结点的数据域

//@no:待修改值的结点位置

//@value:新值

//@head:头结点

//return:成功返回结点旧值,失败返回-1

int change_node(int no,int value,Node *head)

{

if(!head)

{

printf("The list is empty!\n");

return -1;

}

int oldvalue;

Node *p = head;

while(p->next && no--)

{

p = p->next;

if(!p->next && no > 0)

{

printf("The list is less than no\n");

return -1;

}

}

oldvalue = p->data;

p->data = value;

return oldvalue;

}

//根据结点位置查找结点

//@no:待查找结点位置

//@head:头结点

//return:成功返回结点值,失败返回-1

int search_node(int no,Node *head)

{

if(!head)

{

printf("The list is empty!\n");

return -1;

}

Node *p = head;

while(p->next && no--)

{

p = p->next;

if(!p->next && no > 0)

{

printf("The list is less than no!\n");

return -1;

}

}

return p->data;

}

//销毁链表

//@head:头结点地址

void destroy_list(Node **head)

{

if(!*head)

{

printf("The list is empty!\n");

return ;

}

Node *p = *head;

while(p->next)

{//循环删除head的下一个结点,直到只剩head

p = p->next;

(*head)->next = p->next;

p->next = NULL;

p->prior = NULL;

free(p);

p = *head;

}

free(p);//释放头结点

p = NULL;

*head = NULL;

}

//打印链表全部值

//@head:头结点

void print_list(Node *head)

{

if(!head)

{

printf("The list is empty!\n");

return ;

}

Node *p = head;

while(p->next)

{

p = p->next;

printf("%d\n",p->data);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值