c++主要程序二
Posted 二白工作室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++主要程序二相关的知识,希望对你有一定的参考价值。
结构定义:
T data;
Node* next;
using namespace std;
template<typename T>
class Node{
public:
T data;
Node* next;
Node* prev;
Node(T in){
data = in;
next = NULL;
prev = NULL;
};
Node(){
next = NULL;
prev = NULL;
}
};
template<typename T>
class List{
public:
Node<T>* head,*tail;
int length;
List<T>();
void print();
void insertHead(T data);
void insertTail(T data);
void insertNext(int i,T data);
void insertPrev(int i,T data);
void remove(int i);
void add(List<T>& list);
void sort();
void reverse();
void save(string path);
};
template<typename T>
List<T>::List(){
head = new Node<T>();
tail = new Node<T>();
head->next = tail;
tail->prev = head;
length = 0;
}
template<typename T>
void List<T>::insertHead(T data){
Node<T>* now = new Node<T>(data);
now->next = head->next;
now->prev = head;
head->next->prev = now;
head->next = now;
length++;
}
template<typename T>
void List<T>::insertTail(T data){
Node<T>* now = new Node<T>(data);
now->next = tail;
now->prev = tail->prev;
tail->prev->next = now;
tail->prev = now;
length++;
}
template<typename T>
void List<T>::insertNext(int loc,T data){
if(loc<0){
cout<<"非法访问"<<endl;
return ;
}
Node<T>* now = new Node<T>(data);
Node<T>* flag = head;
for(int i=0;i<loc;i++){
flag = flag->next;
if(flag == tail){
cout<<"非法访问"<<endl;
return ;
}
}
now->next = flag->next;
now->prev = flag;
flag->next->prev = now;
flag->next = now;
length++;
}
template<typename T>
void List<T>::insertPrev(int loc,T data){
insertNext(loc-1,data);
}
template<typename T>
void List<T>::remove(int loc){
if(loc>length){
cout<<"非法访问"<<endl;
return ;
}
Node<T>* now = head;
for(int i=0;i<loc;i++){
now = now->next;
}
now->prev->next = now->next;
length--;
delete now;
}
template<typename T>
void List<T>::add(List<T>& list){
Node<T>* now = list.head->next;
for(int i=0;i<list.length;i++){
insertTail(now->data);
now = now->next;
}
}
template<typename T>
void List<T>::print(){
Node<T>* now = head->next;
for(int i=0;i<length;i++){
cout<<now->data<<" ";
now = now->next;
}
cout<<endl;
}
template<typename T>
void List<T>::sort(){
for(int i=length;i>=0;i--){
Node<T>* now = head->next;
Node<T>* next = now->next;
for(int j=1;j<i;j++){
if(now->data>next->data){
swap(now->data,next->data);
}
now = now->next;
next = now->next;
}
}
}
template<typename T>
void List<T>::reverse(){
Node<T>* now = head;
Node<T>* next;
for(int i=0;i<=length+1;i++){
next = now->next;
now->next = now->prev;
now->prev = next;
now = next;
}
swap(head,tail);
}
template<typename T>
void List<T>::save(string path){
ofstream fout(path);
Node<T>* now = head->next;
for(int i=0;i<length;i++){
fout<<now->data<<" ";
}
cout<<endl;
fout.close();
}
int main(){
List<int> list,list2;
list.insertHead(1);
list.insertHead(2);
list.insertHead(3);
cout<<"list1头插入1、2、3,尾插入4\n";
list.insertTail(4);
list.print();
cout<<"list1第一个元素前插入1\n";
list.insertPrev(1,1);
list.print();
cout<<"list1第一个元素后插入2\n";
list.insertNext(1,2);
list.print();
cout<<"list1移除第一个元素\n";
list.remove(1);
list.print();
cout<<"list2尾插入0、1、2、3\n";
for(int i=0;i<=3;i++){
list2.insertTail(i);
}
list.add(list2);
list2.print();
cout<<"list1增加集合2\n";
list.print();
cout<<"list1排序\n";
list.sort();
list.print();
cout<<"list1翻转\n";
list.reverse();
list.print();
return 0;
}
以上是关于c++主要程序二的主要内容,如果未能解决你的问题,请参考以下文章