c++ 用模板类创建一个单向链表 实现链表节点的增加、删除、输入 怎么写啊;我邮箱787121752@qq.com 5

最好带解释,不要抄袭求高手啊,三天内啊,可以加分... 最好带解释,不要抄袭
求高手 啊,三天内啊,可以加分
展开
 我来答
逆乱天地
2012-06-26 · TA获得超过127个赞
知道答主
回答量:173
采纳率:0%
帮助的人:100万
展开全部
sgi stl slist 源代码
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
锐道
2025-09-24 广告
URule Pro Java 规则引擎,一款给业务人员使用的可视化商业决策规则引擎系统,打开浏览器即可开始设计业务规则;URule Pro是一款自主研发纯Java规则引擎,亦是一款国产智能风控决策引擎,可以运行在Windows、Linux、... 点击进入详情页
本回答由锐道提供
宁不诉离殇
2012-06-26
知道答主
回答量:1
采纳率:0%
帮助的人:1715
展开全部
//head.h
#include<iostream.h>template<class T>class link{public: T element; link * next; link(const T& elemval, link * nextval=NULL); //带两个参数的构造函数 link(link* nextval=NULL); //带一个参数的构造函数 ~link(); //析构函数};
template<class T>class list{private: link<T> * head; link<T> * tail; link<T> * curr;public: list(); ~list(); void clear(); //清空链表 void insert(const T &); //插入一个节点,插入到当前节点的后面, void append(const T&); //在链表的尾部追加一个节点 ,传递的参数是节点的数据 T remove(); //移除当前节点后面一个节点,并返回删除节点的数据 void setFirst(); //当前指针作为了头指针 void next(); //让当前指针指向下一个 void prev(); //使得当前指针 移向前一个节点 int length(); //求链表的长度 void setPos(const int); //给当前指针,确定位置。传递的参数是所确定的位置 void setValue(const T&); //改变当前指针的数据值 T currValue(); //得到当前指针的值 bool isEmpty(); //判断链表是否为空 bool find(const T&); //查找是否有所确定的节点,参数是节点的数据 void display(); };template <class T>link<T>::link(const T & elemval,link * nextval){ element=elemval; next=nextval;}template <class T>link<T>::link(link* nextval){ next=nextval;}template <class T>link<T>::~link(){}template <class T>list<T>::list(){ tail=head=curr=new link<T>(); cout<<"执行构造函数"<<endl;}template <class T>list<T>::~list()//析构函数{ while(head!=NULL) { curr=head; head=head->next; delete curr; } cout<<"执行析构函数"<<endl;}template <class T>void list<T>::clear()//清空链表{ if(head->next==NULL) cout<<"清空链表失败!"<<endl; while(head->next!=NULL) { curr=head; head=head->next; delete curr; } curr=tail=head; }template <class T>void list<T>::insert(const T &elem)//插入到当前节点,{
curr->next=new link<T>(elem,curr->next); if(curr==tail) tail=curr->next;}template <class T>void list<T>::append(const T& item)//在链表的尾部追加一个节点 ,传递的参数是节点的数据{ tail=tail->next=new link<T>(item,NULL);}template <class T>T list<T>::remove()//移除当前节点后面一个节点,并返回删除节点的数据{ T temp=curr->next->element; link<T>* ltemp=curr->next; curr->next=ltemp->next; if(tail==ltemp) tail=curr; delete ltemp; return temp;}template <class T>void list<T>::setFirst()//当前指针作为了头指针 {
curr=head;}template <class T>void list<T>::next() //让当前指针指向下一个{ if(curr->next!=tail) curr=curr->next;}template <class T>void list<T>::prev()//使得当前指针 移向前一个节点{ link<T>* temp=head; if((curr==NULL)||curr==head) { curr=NULL; return ; } while((temp!=NULL)&&(temp->next!=curr))//应该遍历链表 temp=temp->next; curr=temp;
}template <class T>int list<T>::length() //求链表的长度{ int len=0; link<T> *temp=head; for( temp=head->next; temp!=NULL;temp=temp->next) len++; return len;}template <class T>void list<T>::setPos(const int pos )//给当前指针确定位置,传递的参数是所确定的位置 { curr=head; for(int i=0;(curr!=NULL)&&(i<pos);i++) curr=curr->next;}template <class T>void list<T>::setValue(const T& val) //改变当前指针的数据值{ if(curr!=NULL) curr->element=val;}template<class T>T list<T>::currValue(){ return curr->element;}template <class T>bool list<T>::isEmpty() //判断链表是否为空{ if(head->next==NULL) return true; else return false;}template <class T>bool list<T>::find(const T& eleval) //查找是否有所确定的节点,参数是节点的数据{ bool flag=false; link<T> *temp=head; while(temp!=NULL) { if(temp->element==eleval) { flag=true; break; } temp=temp->next; } return flag;}template <class T>void list<T>::display()//链表的输出{ link<T> *p=head; if(p->next==NULL) cout<<"The list is empty."; while(p->next!=NULL) { cout<<p->next->element<<" "; p=p->next; } cout<<endl;}//main.cpp
#include<iostream.h>#include"head.h"int main(){ list<int> s1; int k; cout<<"输入k的值:"<<endl; cin>>k; int i,m; cout<<"输入"<<k<<"个数"<<endl; for(i=0;i<k;i++) { cin>>m; s1.append(m); } s1.display(); int length=s1.length(); cout<<"长度为:"<<length<<endl; cout<<"判断链表是否为空的操作"<<endl; if(s1.isEmpty()==true) cout<<"The list is empty."<<endl; else cout<<"The list is not empty."<<endl; cout<<"插入数据的操作"<<endl; int a; cout<<"输入要插入的数据: "; cin>>a; s1.insert(a); s1.display(); cout<<"删除链表中的当前数据"<<endl; cout<<s1.remove()<<endl; s1.display(); cout<<"设置当前指针的位置"<<endl; s1.setPos(2); cout<<s1.currValue()<<endl; cout<<"设置当前指针的值"<<endl; s1.setValue(100); s1.display(); cout<<"指针向后移动一位"<<endl; s1.next(); cout<<s1.currValue()<<endl; cout<<"指针向前移动一位"<<endl; s1.prev(); cout<<s1.currValue()<<endl; s1.setFirst(); //指针指向头指针 s1.display(); cout<<"查找数据的操作"<<endl; int n; cout<<"输入想要查找的数据"<<endl; cin>>n; if(s1.find(n)==true) cout<<"能够找到这个数据 ."<<endl; else cout<<"不能找到这个数据."<<endl; cout<<"清空链表的操作"<<endl; s1.clear(); s1.display(); return 0;}

希望对你会有帮助
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式