双向链表的C++实现 Implement of Doubly Linked List
2014-03-31 16:25
573 查看
实现了双向链表的以下功能
头部插入节点尾部插入节点
n-th位置插入节点
n-th位置删除节点
清空
获取长度
查找某个值
判断是否为空
#include <iostream> using namespace std; struct DoublyLinkedList { int val; DoublyLinkedList *pre; DoublyLinkedList *next; }; void AddAtHead(DoublyLinkedList *&head, int data) { DoublyLinkedList *node = new DoublyLinkedList; node->val = data; node->pre = NULL; node->next = NULL; if(head != NULL) { node->next = head; head->pre = node; } head = node; } //This function is different from the one in the implement of Linked List //can add the node even though head is NULL by using *& instead of * void AddAtTail(DoublyLinkedList *&head, int data) { DoublyLinkedList *node = new DoublyLinkedList; node->val = data; node->pre = NULL; node->next = NULL; if (head == NULL) { head = node; return; } DoublyLinkedList *p = head; //must use another pointer as head is a reference while(p->next != NULL)//move the pointer to the last node p = p->next; p->next = node; node->pre = p; } void BuildDoublyLinkedList(DoublyLinkedList *&head,int a[], int n) { //AddAtHead(head,a[0]); for (int i = 0; i < n; i++) { AddAtTail(head,a[i]); } } void Traverse(DoublyLinkedList *head) { while (head!=NULL) { cout << head->val << " "; head = head->next; } cout << "\n"; } int GetSize(DoublyLinkedList *head) { int size = 0; while (head!=NULL) { head = head->next; size++; } return size; } bool IsEmpty(DoublyLinkedList *head) { if(head == NULL) return true; return false; } void MakeEmpty(DoublyLinkedList *&head) { while(head!=NULL) { DoublyLinkedList *p = head; head = head->next; delete p; p = NULL; } } bool Find(DoublyLinkedList *head, int data) { while (head != NULL) { if(head->val == data) return true; head = head->next; } return false; } void Insert(DoublyLinkedList *&head, int pos, int data) { if(pos < 1) return; DoublyLinkedList *node = new DoublyLinkedList; node->val = data; node->pre = NULL; node->next = NULL; if (pos == 1) { node->next = head; if(head != NULL) head->pre = node; head = node; return; } DoublyLinkedList *p = head; int cnt = 0; while(cnt < pos-2 && p!=NULL) { p = p->next; cnt++; } if(p == NULL) return; node->pre = p; node->next = p->next; p->next = node; } void Remove(DoublyLinkedList *&head, int pos) { if(pos < 1 || head == NULL) return; DoublyLinkedList *p = head; if(pos == 1) { head = p->next; head->pre = NULL; delete p; return; } int cnt = 0; while(cnt < pos-2 && p->next!=NULL) { p = p->next; cnt++; } if(p->next == NULL) return; DoublyLinkedList *tmp = p->next; p->next = p->next->next; p->next->next->pre = p; delete tmp; } int main(void) { int a[] = {4,2,6,1,3,5,7}; DoublyLinkedList *head = NULL; //Insert(head,1,10); Remove(head,1); BuildDoublyLinkedList(head,a,sizeof(a)/sizeof(a[0])); /*Traverse(head); AddAtHead(head,0); AddAtTail(head,8); Remove(head,1);*/ Traverse(head); MakeEmpty(head); return 0; }
相关文章推荐
- 单链表的C++实现 Implement of Linked List
- C语言实现双向链表(DoublyLinkedList)
- Circular Doubly Linked List 双向循环链表 C++ 例子
- C++ List 双向链表 实现 会用也要会写
- 单向链表 (Singly linked list) 的C++实现
- C++实现单链表(Singly-Linked List)追加、遍历、删除
- 【c++】实现双向链表List类及测试
- LinkedList,双向链表的实现
- 49. 3种方法实现复杂链表的复制[clone of complex linked list]
- 数据结构之双向链表(SplDoublyLinkedList)
- LinkedList : 双向链表与实现
- php数据结构之双向链表(SplDoublyLinkedList)
- 容器第四课,JDK源代码分析,自己实现LinkedList,双向链表的概念_节点定义
- 图(有向图)的邻接表表示 C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency list
- [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表
- 逆置双向链表 Reverse a doubly linked list
- 用链表实现带菜单功能的一元多项式的基本操作 (Use linked list to realize the basic operation of a one-dimensional polynom)
- 使用C++实现双向链表List
- C++ List 双向链表 实现 会用也要会写
- Data structure-4 双向链表 DoubleLinkedList--Java语言实现