Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize cyclic double linked list in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要介绍"C语言怎么实现循环双链表",在日常操作中,相信很多人在C语言怎么实现循环双链表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C语言怎么实现循环双链表"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

具体内容如下

#include#include#include typedef int DataType;typedef struct Node{ DataType data; // 数据域 struct Node * prior; // 前趋指针 struct Node * next; // 后继指针 }LinkList; LinkList* Init_List(); // 初始化循环双链表 bool Creat_List(LinkList * L); // 创建链表 int Length_List(LinkList * L); // 链表长度 bool Empty_List(LinkList * L); // 判空 bool Insert_List(LinkList * L, int pos, DataType x); // 插入 bool Delete_List(LinkList * L, int pos, DataType * x);// 删除 bool Destroy_List(LinkList * L); // 销毁链表bool Traverse_List(LinkList * L); // 遍历链表 int Prior_Value(LinkList * L, int pos); // 前趋结点的值 int main(){ DataType x; int pos; LinkList * L = Init_List(); if(Creat_List(L)) printf("链表构造成功!\n"); else printf("链表构造失败!\n"); printf("遍历链表:"); Traverse_List(L); printf("链表结点个数:%d\n\n", Length_List(L)); printf("输入要求前趋结点的结点:"); scanf("%d",&pos); printf("第%d个结点的前趋结点:%d\n\n",pos,Prior_Value(L, pos)); Insert_List(L, 2, 5); printf("插入结点:第2个结点\n"); printf("插入元素:5\n"); printf("遍历链表:"); Traverse_List(L); Delete_List(L, 3, &x); printf("删除结点:第3个结点\n"); printf("被删除元素:%d\n",x); printf("遍历链表:"); Traverse_List(L); if(Destroy_List(L)) printf("销毁成功!\n"); else printf("销毁失败!\n"); return 0; } LinkList* Init_List(){ LinkList * L = (LinkList *)malloc(sizeof(LinkList)); // 创建头结点 if(!L) { printf("申请空间失败!\n"); exit(-1); } L->next = L->prior = L; // 空表,前趋指针和后继指针均指向其自身 return L; // 返回头结点的地址 } bool Creat_List(LinkList * L){ int i,n,val; LinkList * p = L; // 保证L始终指向头结点 printf("请输入循环双链表的结点个数:"); scanf("%d",&n); for(i=0; idata = val; p->next = q; q->prior = p; p = q; } p->next = L; // 保证最后一个结点的后继指针指向头结点 L->prior = p; // 保证头结点的前趋指针指向最后一个结点 return true;} int Length_List(LinkList * L){ int len = 0; LinkList * p = L->next; while(p!=L) // 最后一个结点也要加上 { len++; p = p->next; } return len;} bool Empty_List(LinkList * L){ if(L->next==L&&L->prior==L) return true; else return false;} bool Insert_List(LinkList * L, int pos, DataType x){ int i = 1; LinkList * p = L->next; if(posLength_List(L)) return false; while(inext; } LinkList * q = (LinkList*)malloc(sizeof(LinkList)); q->data = x; q->next = p->next; q->prior = p; p->next->prior = q; p->next = q; return true;} bool Delete_List(LinkList * L, int pos, DataType * x){ int i = 1; LinkList * p = L->next; if(posLength_List(L)) return false; while(inext; } LinkList * q = p->next; *x = q->data; p->next = q->next; q->next->prior = p; free(q); return true;} bool Destroy_List(LinkList * L){// 将循环双链表变成单链表 LinkList * p = L->next; L->next = NULL; // 空表时, 头结点的前趋指针、后继指针都是指向其自身 L->prior = NULL; // 销毁时,头结点前趋指针、后继指针指向空 while(p) { LinkList * q = p->next; free(p); p = q; } L = p = NULL; return true;} bool Traverse_List(LinkList * L){ if(Empty_List(L)) return false; LinkList * p = L->next; while(p!=L) { printf("%3d",p->data); p = p->next; } printf("\n\n"); } int Prior_Value(LinkList * L, int pos){ int i = 1; LinkList * p = L->next; if(posLength_List(L)) return false; while(inext; } return p->prior->data; }到此,关于"C语言怎么实现循环双链表"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report