博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单链表
阅读量:6266 次
发布时间:2019-06-22

本文共 3921 字,大约阅读时间需要 13 分钟。

template
class SingleList;template
class ListNode{private: friend typename SingleList
; ListNode():m_pnext(NULL){} ListNode(const Type item,ListNode
*next=NULL):m_data(item),m_pnext(next){} ~ListNode(){ m_pnext=NULL; }public: Type GetData(); friend ostream& operator<<
(ostream& ,ListNode
&);private: Type m_data; ListNode *m_pnext;};template
Type ListNode
::GetData(){ return this->m_data;}template
ostream& operator<<(ostream& os,ListNode
& out){ os<

#include "ListNode.h"template
class SingleList{public: SingleList():head(new ListNode
()){} ~SingleList(){ MakeEmpty(); delete head; }public: void MakeEmpty(); //make the list empty int Length(); //get the length ListNode
*Find(Type value,int n); //find thd nth data which is equal to value ListNode
*Find(int n); //find the nth data bool Insert(Type item,int n=0); //insert the data in the nth position Type Remove(int n=0); //remove the nth data bool RemoveAll(Type item); //remove all the data which is equal to item Type Get(int n); //get the nth data void Print(); //print the listprivate: ListNode
*head;};template
void SingleList
::MakeEmpty(){ ListNode
*pdel; while(head->m_pnext!=NULL){ pdel=head->m_pnext; head->m_pnext=pdel->m_pnext; delete pdel; }}template
int SingleList
::Length(){ ListNode
*pmove=head->m_pnext; int count=0; while(pmove!=NULL){ pmove=pmove->m_pnext; count++; } return count;}template
ListNode
* SingleList
::Find(int n){ if(n<0){ cout<<"The n is out of boundary"<
*pmove=head->m_pnext; for(int i=0;i
m_pnext; } if(pmove==NULL){ cout<<"The n is out of boundary"<
ListNode
* SingleList
::Find(Type value,int n){ if(n<1){ cout<<"The n is illegal"<
*pmove=head; int count=0; while(count!=n&&pmove){ pmove=pmove->m_pnext; if(pmove->m_data==value){ count++; } } if(pmove==NULL){ cout<<"can't find the element"<
bool SingleList
::Insert(Type item, int n){ if(n<0){ cout<<"The n is illegal"<
*pmove=head; ListNode
*pnode=new ListNode
(item); if(pnode==NULL){ cout<<"Application error!"<
m_pnext; } if(pmove==NULL){ cout<<"the n is illegal"<
m_pnext=pmove->m_pnext; pmove->m_pnext=pnode; return 1;}template
bool SingleList
::RemoveAll(Type item){ ListNode
*pmove=head; ListNode
*pdel=head->m_pnext; while(pdel!=NULL){ if(pdel->m_data==item){ pmove->m_pnext=pdel->m_pnext; delete pdel; pdel=pmove->m_pnext; continue; } pmove=pmove->m_pnext; pdel=pdel->m_pnext; } return 1;}template
Type SingleList
::Remove(int n){ if(n<0){ cout<<"can't find the element"<
*pmove=head,*pdel; for(int i=0;i
m_pnext;i++){ pmove=pmove->m_pnext; } if(pmove->m_pnext==NULL){ cout<<"can't find the element"<
m_pnext; pmove->m_pnext=pdel->m_pnext; Type temp=pdel->m_data; delete pdel; return temp;}template
Type SingleList
::Get(int n){ if(n<0){ cout<<"The n is out of boundary"<
*pmove=head->m_pnext; for(int i=0;i
m_pnext; if(NULL==pmove){ cout<<"The n is out of boundary"<
m_data;}template
void SingleList
::Print(){ ListNode
*pmove=head->m_pnext; cout<<"head"; while(pmove){ cout<<"--->"<
m_data; pmove=pmove->m_pnext; } cout<<"--->over"<
<
<

#include 
using namespace std;#include "SingleList.h"int main(){ SingleList
list; for(int i=0;i<20;i++){ list.Insert(i*3,i); } for(int i=0;i<5;i++){ list.Insert(3,i*3); } cout<<"the Length of the list is "<
<
==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/04/08/2438183.html,如需转载请自行联系原作者
你可能感兴趣的文章
数据库分页查询
查看>>
[编程] C语言枚举类型(Enum)
查看>>
[Javascript] Compose multiple functions for new behavior in JavaScript
查看>>
ASP.NET MVC性能优化(实际项目中)
查看>>
ES6里关于类的拓展(一)
查看>>
零元学Expression Blend 4 - Chapter 46 三分钟快速充电-设定Margin的小撇步
查看>>
Format Conditions按条件显示表格记录
查看>>
RichTextBox指定全部文字显示不同颜色及部分文字高亮颜色显示
查看>>
mysql优化----explain的列分析
查看>>
Python正则表达式
查看>>
Java中CAS详解
查看>>
Spring Boot Unregistering JMX-exposed beans on shutdown
查看>>
命令行man的帮助手册
查看>>
Ubuntu 16.04下为Android编译OpenCV 3.2.0 Manager
查看>>
poi 导入导出的api说明(大全)
查看>>
Fix-Mapped Addresses
查看>>
fmt标签如何计算两个日期之间相隔的天数
查看>>
Spark核心技术原理透视一(Spark运行原理)
查看>>
《Gradle权威指南》--Gradle任务
查看>>
IntelliJ IDEA创建文件时自动填入作者时间 定制格式
查看>>