双链表 double_list
2016-03-10 17:24
211 查看
.h 文件 double_node.h
//双链表 #define ElemType int typedef struct Node * Pnode;//这里要加冒号 typedef struct Node { int data; struct Node *prev; struct Node *next; } Node,* DoubleList; //函数声明 void print_list(Node *pHead); DoubleList InitLink(); void CreateList1(Pnode L,ElemType e); //头插法建立双链表 void DeleteLink(Pnode L,int i); //删除操作 Pnode double_find(Pnode l, int num); //查找第I 个结点 传进来头结点
.c文件
// 双链表 #include "double_node.h" #include <stdio.h> #include <stdlib.h> int main() { int n; Node *pHead = NULL, *pFoot; printf("双链表测试\n"); Pnode list=InitLink(); //链表初始化 CreateList1(list,56); CreateList1(list,46); print_list(list); DeleteLink(list,2); return 0; } //双链表初始化 //初始化链表 DoubleList InitLink() { Pnode L =(Node *)malloc(sizeof(Node)); if(L == NULL) printf("内存分配失败!\n"); L->next = NULL; L->prev =NULL; return L; } void CreateList1(Pnode L,ElemType e) //头插法 { Pnode s; Pnode newNode=(Pnode)malloc(sizeof(Node)); newNode->data=e; if(L->next == NULL){ L->next=newNode; newNode->prev =L; newNode->next =NULL; } else{ s=L->next; L->next =newNode; newNode->prev=L; newNode->next =s; s->prev=newNode; } } void DeleteLink(Pnode L,int i) //删除操作 { Pnode s =double_find(L,i); Pnode a =s->prev; if(s->next==NULL) { a->next =NULL; }else{ Pnode b =s->next; a->next=b; b->prev=a; } free(s); printf("删除操作!\n"); print_list(L); } void print_list(Node *pHead){ Node *p; printf("打印链表:\n"); for(p=pHead->next;p!=NULL;p=p->next) { printf("%d ",p->data); } } Pnode double_find(Pnode l, int num)//查找第I 个结点 传进来头结点 { Pnode p; int i=-1; for(p=l;p!=NULL;p=p->next) { i++; if(i == num) { return p; break; } } if(p==NULL) printf("没有你要找的位置 "); }
相关文章推荐
- C# 集合
- Spring Hibernate Transaction示例
- phpstrtotime()对于31日求上个月有问题
- Chrome 远程桌面
- 2016/3/10个人总结:当前测试产品(DB)测试用例设计编写总结
- 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
- Java概述和开发环境
- iOS9请求https问题
- [C#] 如何分析stackoverflow等clr错误
- Matlab和java混合编程开发
- [C#] 如何分析stackoverflow等clr错误
- selenium 模拟键盘及鼠标事件
- 2月10日
- 站长们:卢松松还在用的自媒体平台,你知道吗
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- CodeForces 392C Yet Another Number Sequence 矩阵快速幂
- scala中sealed关键字使用指南
- git 使用基础on MAC
- iOS 动画详解
- 文件操作(NSFileManager)