在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
practice4.h文件 #ifndef PRACTICE4_H_INCLUDED #define PRACTICE4_H_INCLUDED #include<iostream> template <class Type> class List;//申明友元类的前置声明 template<class Type> class ListIterator;//申明友元类的前置声明 template<class Type> class ListNode //节点类 { friend class List<Type>; friend class ListIterator<Type>; private: Type data; ListNode *link; ListNode(Type); //构造函数 }; template<class Type> class List { friend class ListIterator<Type>; public: List(){first=0;}; //构造函数 void Insert(Type); void Show(); void Delete(Type); void Invert(); void Concatenate(List<Type>); private: ListNode<Type> *first; }; template<class Type> class ListIterator { public: ListIterator(const List<Type>& l):list(l),current(l.first){};//构造函数,参数是链表,这个链表的迭代器 初始化链表和指针 bool NotNull(); bool NextNotNull(); Type*First(); Type*Next(); private: const List<Type> &list;//链表 迭代器是哪个链表的 ListNode<Type> *current;//指针,指向链表里的节点 }; template <class Type>//判断非空 bool ListIterator<Type>::NotNull() { if(current) return true; else return false; } template <class Type>//判断下一个非空 bool ListIterator<Type>::NextNotNull() { if(current&¤t->link) return true; else return false; } template <class Type>//取first的值 Type*ListIterator<Type>::First() { if(list.first) return &list.first->data; else return 0; } template <class Type>//判断下一个是否为空 Type *ListIterator<Type>::Next() { if(current) { current=current->link; return ¤t->data; } else return 0; } template<class Type>//构造函数 ListNode<Type>::ListNode(Type element) { data=element; link=0; } template <class Type>//插入 void List<Type>::Insert(Type k) { ListNode<Type> *newnode=new ListNode<Type>(k); newnode->link=first; first=newnode; } template <class Type>//展示 void List<Type>::Show() { for(ListNode<Type> *current=first;current;current=current->link) { std::cout<<current->data; if(current->link) std::cout<<"->"; } std::cout<<std::endl; } template<class Type>//删除节点 void List<Type>::Delete(Type k) { ListNode<Type> *previous=0;//前一个的指针 ListNode<Type> *current; for(current=first;current&¤t->data!=k; previous=current,current=current->link) { ; } if(current) { if(previous) previous->link=current->link; else first=first->link; delete current; } } template <class Type>//反转 void List<Type>::Invert()///////////////尚且不懂 { ListNode<Type>*p=first,*q=0; while(p) { ListNode<Type> *r=q;q=p; p=p->link; q->link=r; } first=q; } template<class Type>//合并两个链表 void List<Type>::Concatenate(List<Type> b) { if(!first){first=b.first;return;} if(b.first) { ListNode<Type> *p; for(p=first;p->link;p=p->link);//空循环 p->link=b.first; } } #endif // PRACTICE4_H_INCLUDED practice3.cpp #include<iostream> #include "practice6.h" #include<list> using namespace std; int main() { cout<<"²âÊÔ"<<endl; List<int> intList; intList.Insert(5); intList.Insert(15); intList.Insert(25); intList.Insert(35); // cout<<"这是标准c++stl中的链表和迭代器"<<endl; //std::list<int> listIntegers; // listIntegers.push_front(5); // listIntegers.push_front(15); // listIntegers.push_front(25); // listIntegers.push_front(35); // std::list<int>::iterator i=listIntegers.begin(); //while(i!=listIntegers.end()) // { // cout<<*i<<"->"; // ++i; // } //cout<<endl; cout<<"这是我的链表和迭代器"<<endl; // if(li.NotNull()) // { // cout<<*li.First(); // while(li.NextNotNull()) // cout<<"->"<<*li.Next(); // cout<<endl; // } cout<<"测试一下循环"<<endl; ListIterator<int> iter(intList); cout<<*iter.First()<<endl; cout<<*iter.Next()<<endl; cout<<*iter.Next()<<endl; cout<<*iter.Next()<<endl; cout<<*iter.Next()<<endl; cout<<*iter.Next()<<endl; cout<<*iter.Next()<<endl; cout<<*iter.Next()<<endl; return 0; }
#ifndef PRACTICE4_H_INCLUDED#define PRACTICE4_H_INCLUDED
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论