双链表基本操作 doubly linked list
来源:互联网 发布:java实现多线程并发 编辑:程序博客网 时间:2024/06/04 21:08
#include<stdio.h> #include<stdlib.h> typedef struct doubleLink { int data; struct doubleLink *pre; struct doubleLink *next; }dnode; //建立链表 dnode* createDLink() { dnode *head,*p,*s; int x; head = (dnode*)malloc(sizeof(dnode)); p = head; while(1) { printf("please input the data: "); scanf("%d",&x); if(x != 0) { s = (dnode*)malloc(sizeof(dnode)); s ->data = x; s-> pre = p; p->next = s; p=s; } else break; } p->next = NULL; head = head ->next; head->pre = NULL; return head; } //顺序、反序打印链表 void printDLink(dnode *head) { dnode *p,*s; p = head; printf(" left to right: \n"); while(p) { printf("%d ",p->data); s = p; //为了到达链表的末尾 p = p->next; } printf("\n right to left: \n"); while(s) { printf("%d ",s->data); s = s->pre; } printf("\n \n"); } //删除一个结点 dnode* deletedNode(dnode *head,int i) { dnode *p; p = head; if(p->data == i) { head = p->next; head->pre = NULL; free(p); return head; } while(p) { if(p->data == i) { p->pre->next = p->next; p->next->pre = p->pre; free(p); return head; } p = p->next; } printf("data not found \n"); return head; } //插入一个结点 dnode* insertdNode(dnode *head,int i) { dnode *p,*temp; p = head; temp = (dnode*)malloc(sizeof(dnode)); temp ->data = i; if(i < p->data) { head = temp; head->next = p; head->pre = NULL; p->pre = head; return head; } while(p != NULL && i > p->data) { p = p->next; } if(i < p->data) { temp ->next = p; temp ->pre = p->pre; p ->pre->next = temp; p ->pre = temp; return head; }else { p->next = temp; temp ->pre = p; temp ->next = NULL; return head; } } int main() { dnode *head; head = createDLink(); printDLink(head); head = insertdNode(head,4); head = deletedNode(head,3); printDLink(head); return 0; }
转载自http://blog.csdn.net/acm_jl/article/details/50949777
阅读全文
0 0
- 双链表基本操作 doubly linked list
- Doubly linked list
- Doubly Linked List
- Linked List/Singly-Linked List/Doubly-Linked List
- HWK:A doubly linked list using class
- (转)A Memory-Efficient Doubly Linked List
- 【Hackerrank】Reverse a doubly linked list
- A Memory-Efficient Doubly Linked List
- Convert BST to Sorted Doubly-Linked List
- 双向链表(Doubly Linked List)
- 链表ALDS1-3-C:Doubly Linked List
- 第四章 ALDS1_3_C:Doubly Linked List 链表
- XOR Linked List – A Memory Efficient Doubly Linked List
- Linked List {singly linked list -> doubly linked list -> circular linked list}
- Reverse a doubly linked list in O(1) time
- Circular Doubly Linked List 双向循环链表 C++ 例子
- Convert Binary Search Tree (BST) to Sorted Doubly-Linked List
- 【Hackerrank】Insert a node into a sorted doubly linked list
- 垂直居中的方式
- 关于Mybatis框架的个人总结
- javascript获取文本框内的内容
- 9-23 数据库内容学习
- Java输入输出
- 双链表基本操作 doubly linked list
- Python LSB图片隐写技术
- 元组
- C#与Excle
- eclipse安装JAVA反编译插件
- 数论——扩展欧几里得算法
- hihoCoder1584
- MATLAB数字图像处理(二)直方图
- linux进程间通信