C++链表(single-linked list)
2016-03-03 16:08
429 查看
单向链表(single-linked list)
直接代码
直接代码
//IntSLList.h #ifndef INTSLLIST_H #define INTSLLIST_H class IntSLLNode { public: IntSLLNode() { data = 0; next = 0; } IntSLLNode(int el, IntSLLNode* ptr = 0) { data = el; next = ptr; } int data; IntSLLNode* next; }; class IntSLList { public: IntSLList(); ~IntSLList(); bool IsEmpty(); void AddToHead(int el); void AddToTail(int el); void DeleteFromHead(); void DeleteFromTail(); void DeleteNode(int el); bool IsInList(int el); void ReverseList(); //反转链表 IntSLLNode* GetListHead(); //获取链表头节点 private: IntSLLNode* head; IntSLLNode* tail; }; #endif // !INTSLLIST_H
//IntSLList.cpp #include "IntSLList.h" #include <iostream> using namespace std; IntSLList::IntSLList() { } IntSLList::~IntSLList() { } bool IntSLList::IsEmpty() { return head == NULL; } void IntSLList::AddToHead(int el) { head = new IntSLLNode(el, head); if (tail == 0) { tail = head; } } void IntSLList::AddToTail(int el) { if (tail == NULL) { head = tail = new IntSLLNode(el); } else { tail->next = new IntSLLNode(el); tail = tail->next; } } void IntSLList::DeleteFromHead() { if (head != NULL) { IntSLLNode* ptemp = head; head = head->next; delete ptemp; ptemp = NULL; } } void IntSLList::DeleteFromTail() { if (tail != NULL) { if (head == tail) { delete tail; head = tail = NULL; } else { IntSLLNode* ptemp = head; while (ptemp->next != tail) { ptemp = ptemp->next; } delete tail; tail = ptemp; tail->next = NULL; } } } void IntSLList::DeleteNode(int el) { if (head != NULL) { if (head == tail && head->data == el) { delete head; head = tail = NULL; } else if (head->data == el) { IntSLLNode* ptemp = head; head = head->next; delete ptemp; ptemp = NULL; } else if (head->next != NULL) { IntSLLNode* pFront = head; IntSLLNode* pBehind = head->next; while (pBehind != 0) { if (pBehind->data == el) { pFront->next = pBehind->next; if (pBehind == tail) tail = pFront; delete pBehind; pBehind = NULL; break; } pFront = pBehind; pBehind = pBehind->next; } } } } bool IntSLList::IsInList(int el) { IntSLLNode* ptemp = head; while (ptemp != NULL) { if (ptemp->data == el) return true; ptemp = ptemp->next; } return false; } void IntSLList::ReverseList() { if (head != tail) { IntSLLNode* p1 = head; IntSLLNode* p2 = head->next; IntSLLNode* p3 = NULL; p1->next = NULL; while (p2 != NULL) { p3 = p2->next; p2->next = p1; p1 = p2; p2 = p3; } swap(head, tail); } } IntSLLNode* IntSLList::GetListHead() { return head; }
//MainProcess.cpp #include "IntSLList.h" #include <iostream> using namespace std; void Print(IntSLLNode* phead) { while (phead != NULL) { cout << phead->data << " "; phead = phead->next; } cout << endl; } int main() { IntSLList listt; listt.AddToTail(1); listt.AddToTail(2); listt.AddToTail(3); listt.AddToTail(4); IntSLLNode* phead = listt.GetListHead(); Print(phead); listt.ReverseList(); phead = listt.GetListHead(); Print(phead); cout << endl; return 0; }
相关文章推荐
- VC++2010工程中加入SplashScreen
- 从汇编层面深度剖析C++虚函数
- C语言(一)
- C++中const用法总结
- 2014华为上机题C++编程
- c++ primer 课后习题12.24// 12.24.cpp : 定义控制台应用程序的入口点。
- C++ typename的起源与用法
- C/C++,关键字typeof的用法
- 12.23.cpp : 定义控制台应用程序的入口点。
- C++学习笔记(1)<unfinshed>
- C++设计模式之工厂方法模式
- 谈谈C++中各种初始化方式
- [C语言] C 语言学习代码
- htonl() htons()及inet_ntoa() inet_addr()的用法
- C++语言-08-命名空间
- C++11,正则表达式应用
- C语言字符判断、大小写互转(五)
- ‘之’字形打印矩阵
- leetcode - Count of Smaller Numbers After Self
- TypeError: No to_python (by-value) converter found for C++ type: