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

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

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://rvyyo.baihongyu.com/

你可能感兴趣的文章
为什么sql里面not in后面的子查询如果有记录为NULL的,主查询就查不到记录
查看>>
Angular7里面实现 debounce search
查看>>
Linux 内核链表
查看>>
git学习------>Git 分支管理最佳实践
查看>>
括号和出栈所有序列问题
查看>>
第一次操刀数据库分表的教训与经验
查看>>
录音声音小
查看>>
Ubuntu 12.04 安装 Chrome浏览器
查看>>
java 反射
查看>>
ORACLE物化视图(物理视图)
查看>>
android 读取json数据(遍历JSONObject和JSONArray)(转)
查看>>
UIScrollView中的手势
查看>>
递归和迭代的差别
查看>>
基于jquery的可拖动div
查看>>
可以简易设置文字内边距的EdgeInsetsLabel
查看>>
[詹兴致矩阵论习题参考解答]习题1.3
查看>>
Android Fragment的使用
查看>>
mysql半同步复制实现
查看>>
沙朗javascript总结一下(一)---基础知识
查看>>
js深入研究之函数内的函数
查看>>