复习下C 链表操作(双向链表)
双向链表 创建、删除、反转、插入
//struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /**********************双向链表************************************/ typedef struct Student_Double
{
char name[];
int point;
struct Student_Double *preStu;
struct Student_Double *nextStu;
} StudentDouble; //创建双向链表
StudentDouble *CreateDoubleLinkTable(void){
int i = ;
StudentDouble *head = NULL;
head = (StudentDouble *)malloc(sizeof(StudentDouble));
head->name[]='\0';
head->point = ;
head->preStu = NULL;
head->nextStu = NULL; StudentDouble *temp,*currentNode;
currentNode=head;
while (i<=) {
temp = (StudentDouble *)malloc(sizeof(StudentDouble));
strncpy(temp->name,"Node",sizeof(temp->name));
temp->point = i;
temp->preStu = currentNode;
temp->nextStu = NULL; currentNode->nextStu = temp; currentNode = temp;
i++;
} return head;
}
//正序查询
StudentDouble * SelectDoubleLinkTableFormHead(StudentDouble *head){
StudentDouble *endNode = NULL;
StudentDouble *next = head->nextStu;
if (next == NULL) {
printf("查询失败,不是链式结构\n");
exit();
} int i =;
while (next) {
printf("index %d; studentName is %s; point is %d\n",i+,next->name,next->point);
if (next->nextStu == NULL) {
endNode = next;
printf("双向链表尾节点:%d \n",endNode->point);
}
next=next->nextStu;
i++;
}
return endNode;
}
//反序查询
void SelectDoubleLinkTableFormEnd(StudentDouble *end){ StudentDouble *pre = end->preStu;
if (pre==NULL) {
printf("查询失败,不是链式结构\n");
exit();
}
pre = end;
int i = ;
while (pre) {
if (pre->name[]=='\0') {
//头链
break;
}
printf("index %d; studentName is %s; point is %d\n",i+,pre->name,pre->point);
pre = pre->preStu;
i++;
}
} //双向链表 插入节点 上一节点序号|插入节点名
StudentDouble *insertStudentDoubleLinkTable(StudentDouble *head,char *content){
if (!content) {
exit();
}
//分割字符串
char *token;
char nodeName[];
int nodeIndex;
token = strtok(content,"|");
int i = ;
while (token != NULL) {
if (i>) {
strncpy(nodeName,token,sizeof(char[]));
}else{
nodeIndex = atoi(token);
}
token = strtok(NULL,"|");
i++;
} //查找节点
StudentDouble *next = head->nextStu;
if (next==NULL) {
printf("不是链式结构");
exit();
}
int search = ;
StudentDouble *searchNode = NULL;
while (next) {
if (next->point == nodeIndex) {
search = ;
searchNode = next;
}
next = next->nextStu;
}
if (search==) {
printf("没有找到节点序号为%d 的节点",nodeIndex);
exit();
}
//插入节点
StudentDouble *nextLinkTable = searchNode->nextStu;
//创建新节点
StudentDouble *new = (StudentDouble *)malloc(sizeof(StudentDouble));
strncpy(new->name,nodeName,sizeof(new->name));
new->point = ;//这两随机去的数。
new->preStu = searchNode;
new->nextStu = nextLinkTable; searchNode->nextStu = new; return head;
} //双向链表 删除节点
StudentDouble *DelegateDoubleLinkTable(StudentDouble *head,int nodeIndex){
//查找节点
StudentDouble *next = head->nextStu;
if (next==NULL) {
printf("不是链式结构");
exit();
}
int search = ;
StudentDouble *searchNode = NULL;
while (next) {
if (next->point == nodeIndex) {
search = ;
searchNode = next;
}
next = next->nextStu;
}
if (search==) {
printf("没有找到节点序号为%d 的节点",nodeIndex);
exit();
} StudentDouble *preNode = searchNode->preStu;
StudentDouble *nextNode = searchNode->nextStu; preNode->nextStu = nextNode;
nextNode->preStu = preNode; //释放
free(searchNode);
return head;
} int main(void){
char sf[];
int num;
/**
* 创建双向循环链表
*/
StudentDouble *head = NULL;
StudentDouble *end = NULL;
printf("开始创建双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
head = CreateDoubleLinkTable();
}
printf("正序查询双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
end = SelectDoubleLinkTableFormHead(head);
} printf("反序查询双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
printf("双向链表尾节点:%d \n",end->point);
if (end->point == ) {
printf("双向链表反向查询\n");
SelectDoubleLinkTableFormEnd(end);
}
} printf("插入双向链表 如:4(上一个节点序号)|name(要插入的节点名)\n");
scanf("%s",sf);
head = insertStudentDoubleLinkTable(head,sf);
SelectDoubleLinkTableFormHead(head); printf("删除双向链表节点 输入要删除节点序号\n");
scanf("%d",&num);
head = DelegateDoubleLinkTable(head,num);
SelectDoubleLinkTableFormHead(head); return ;
}
双向链表反转: 和前面 单向链表反转 同样思路。
遍历链表 , 游标指针为当前遍历到的节点currentNode。 后续节点 nextNode。新链表 newLinkTable 初始为NULL;
思路如下:
//反转 双向链表
StudentDouble * ReversionDoubleLinkTable(StudentDouble *head){
StudentDouble *next = head->nextStu;
if (next==NULL) {
printf("不是链式结构");
exit();
} StudentDouble *currentNode,*nextNode,*newLinkTable;
currentNode = next;
newLinkTable = NULL;
while (currentNode) { //保持后续节点
nextNode = currentNode->nextStu;
//断开
currentNode->nextStu = newLinkTable;
currentNode->preStu = NULL;
if (newLinkTable !=NULL) {
newLinkTable->preStu = currentNode;
}
//保存游标节点
newLinkTable = currentNode;
//重置游标节点
currentNode = nextNode;
} StudentDouble *newHead = (StudentDouble*)malloc(sizeof(StudentDouble));
newHead->name[]='\0';
newHead->point = ;
newHead->nextStu = newLinkTable;
newHead->preStu = NULL;
newLinkTable->preStu = newHead; return newHead;
}
int main(void){
char sf[];
int num;
/**
* 创建双向循环链表
*/
StudentDouble *head = NULL;
StudentDouble *end = NULL;
printf("开始创建双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
head = CreateDoubleLinkTable();
}
printf("正序查询双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
end = SelectDoubleLinkTableFormHead(head);
}
printf("反序查询双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
printf("双向链表尾节点:%d \n",end->point);
if (end->point == ) {
printf("双向链表反向查询\n");
SelectDoubleLinkTableFormEnd(end);
}
}
printf("反转双向链表 Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
head = ReversionDoubleLinkTable(head);
printf("正序查询双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
end = SelectDoubleLinkTableFormHead(head);
}
printf("反序查询双向链表Y|N \n");
scanf("%s",sf);
if (strcmp(sf,"Y")==) {
printf("双向链表尾节点:%d \n",end->point);
if (end->point == ) {
printf("双向链表反向查询\n");
SelectDoubleLinkTableFormEnd(end);
}
}
}
return ;
}
复习下C 链表操作(双向链表)的更多相关文章
- 复习下C 链表操作(单向链表)
Object-C 作为C 的包装语言(运行时.消息机制).如果不熟悉C 的话实在玩得太肤浅. 随便深入oc 内部都会接触到C. runtime .GCD.Block.消息机制... 所有强大的功能无不 ...
- 复习下C 链表操作(双向循环链表,查找循环节点)
双向循环链表 和 单向循环链表 查找循环节点 思路都是一样. 快慢指针查找法. 理论可参考 c 链表之 快慢指针 查找循环节点 typedef struct Student_Double { ]; ...
- 复习下C 链表操作(单向循环链表、查找循环节点)
循环链表 稍复杂点. 肯能会有0 或 6 字型的单向循环链表. 接下来创建 单向循环链表 并 查找单向循环链表中的循环节点. 这里已6字型单向循环链表为例. //创建 循环链表 Student * ...
- linux 内核的链表操作(好文不得不转)
以下全部来自于http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 无任何个人意见. 本文详细分析了 2.6.x 内 ...
- js数据结构之链表(单链表、双向链表、循环链表)
首先,链表有以下特点: 1. 存储空间不固定,可灵活扩充 2.方便多次的插入和删除,效率较高 单链表 单链表是最常用的链表,其对数据的操作均为单项的,向后查找的. /* 链表(基于对象) 此处为单链表 ...
- 【 C# 】(一) ------------- 泛型带头节点的单链表,双向链表实现
在编程领域,数据结构与算法向来都是提升编程能力的重点.而一般常见的数据结构是链表,栈,队列,树等.事实上C#也已经封装好了这些数据结构,在头文件 System.Collections.Generic ...
- SWUSTOJ 960A题总结,又完成一个讨厌的题,内含链表操作启发
今天debug了一个nice代码,先码在这里,SWUST OJ960 双向链表的操作问题 1000(ms) 10000(kb) 2994 / 8244 建立一个长度为n的带头结点的双向链表,使得该链表 ...
- C# 链表操作
关于链表操作,在C#当中微软已经提供了一个LinkedList<T>的数据结构,通过这个类提供的一系列方法就能够实现链表操作. 这里我提供一段代码,这是在论坛里面有人提问时给出的代码,它实 ...
- 线性链表的双向链表——java实现
.线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...
随机推荐
- 如何根据搜索页面内容得到的结果生成该元素的xpath路径
如何根据搜索页面内容得到的结果生成该元素的xpath路径?
- J2EE用监听器实现同一用户只能有一个在线
这里我们讨论的是已登陆或将要登陆的用户,游客不在讨论的范围之内.这一点大家应该很容易就能理解的吧. 那么我们应该怎样去实现同一用户只能有一个在线这样的一个小功能呢? 有人 ...
- Java并发容器之非阻塞队列ConcurrentLinkedQueue
参考资料:http://blog.csdn.net/chenchaofuck1/article/details/51660521 实现一个线程安全的队列有两种实现方式:一种是使用阻塞算法,阻塞队列就是 ...
- Centos6.5卸载图形化
问题描述: Centos6.5想运行在非图形化状态,减小系统资源的开销!提升服务器性能....... 问题解决: 01.切换运行模式,变相实现非图形化运行 多用户模式 init 图形化模式 init ...
- Ubuntu 16.04 编译安装 ss
在网上没有找到合适的适合ubuntu的ss客户端, 考虑到ss的编译安装其实就带了ss-local这样的客户端, 于是在Ubuntu下编译安装了ss. 首先去github上下载最新的安装包 https ...
- linux shell 脚本攻略学习9--rename命令详解
rename命令详解: 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosli@amosli-pc:~/learn/example$ ls abc.txt amosli@a ...
- find -exec 与xargs 区别
find . -name "*.txt" -exec rm {} \;find . -name "*.txt" | xargs rm {} -exec 1 ...
- 关于ARP协议
什么是arp协议: arp协议是地址解析协议,英文是address resolution protocol 通过IP地址可以获得mac地址 两个主机的通信归根到底是MAC地址之间的通信 在TCP/IP ...
- 修改linux 最大文件限制数 ulimit
1)修改当前交互终端的limit值 查询当前终端的文件句柄数: ulimit -n 回车,一般的系统默认的1024. 修改文件句柄数为65535,ulimit -n 65535.此时系统的文件句柄数为 ...
- “医疗信息化行业之中的联发科”- 我们在医疗行业中的定位及目标 想做一个面对中小企业的专业上游软件供应商 台湾联发科技颠覆掉的是一个封闭的手机产业系统 解决方案,即AgileHIS.NET数字化医院基础方案
“医疗信息化行业之中的联发科”- 我们在医疗行业中的定位及目标 我们做中国医疗信息化行业之中的联发科 ---我们在医疗行业中的定位及目标 从我个人来讲,我从2001年到现在这10年之间基本上一直在 ...