list.h
#include"utili.h"
template<class Type>
class List;
template<class Type>
class ListNode
{
friend class List<Type>;
public:
ListNode():data(Type()),prev(NULL),next(NULL)
{}
ListNode(const Type d,ListNode<Type> *p = NULL,ListNode<Type> *n = NULL):data(d),prev(p),next(n)
{}
~ListNode()
{}
private:
Type data;
ListNode<Type> *prev;
ListNode<Type> *next;
};
template<class Type>
class List
{
public:
List()
{
first = last = new ListNode<Type>;
size = 0;
first->prev = NULL;
last->next = NULL;
}
~List()
{
clear();
delete first;
first = last = NULL;
}
public:
bool Is_Full() //判断是否为空
{
if(first->next == NULL)
return true;
else
return false;
}
void push_back(const ElemType &e) //后插函数
{
ListNode<Type> *p = new ListNode<Type>(e);
last->next = p;
p->prev = last;
last = p;
++size;
}
void show_list() //显示函数
{
ListNode<Type> *p = first->next;
while(p != NULL)
{
cout<<p->data<<"-->";
p = p->next;
}
cout<<"Over!"<<endl;
}
void push_front(const ElemType &e) //前插函数
{
ListNode<Type> *p = new ListNode<Type>(e);
p->next = first->next;
first->next = p;
if(0 == size)
last = p;
++size;
}
ListNode<Type>* find(const ElemType &e) //查找函数
{
ListNode<Type> *p = first->next;
while(p != NULL && p->data != e)
{
p = p->next;
}
return p;
}
void pop_back() //尾删函数
{
if(Is_Full() == true)
return;
else
{
ListNode<Type> *p = last->prev;
p->next = NULL;
delete last;
last = p;
--size;
}
}
void pop_front() //头删函数
{
if(Is_Full() == true)
return;
else
{
ListNode<Type> *p = first->next,*q;
q = p->next;
if(q == NULL)
{
last = first;
first->next = q;
}
else
{
first->next = q;
q->prev = first;
}
--size;
}
}
bool insert(ListNode<Type> *p,const Type &x) //给定位置,插入值.
{
ListNode<Type> *q = first->next;
ListNode<Type> *t = new ListNode<Type>(x);
while(q != p && q != NULL)
{
q = q->next;
}
if(q != NULL)
{
if(q == last)
push_back(x);
else
{
p->next->prev = t;
t->next = p->next;
p->next = t;
t->prev = p;
++size;
}
return true;
}
else
return false;
}
bool delete_val(const Type &x) //按值删除函数
{
if(Is_Full() == true)
return true;
ListNode<Type> *p = first->next;
while(p->data != x && p != NULL)
{
p = p->next;
}
if(p == NULL)
return false;
else
{
if(p == first->next)
pop_front();
else if(p == last)
pop_back();
else
{
p->prev->next = p->next;
p->next->prev = p->prev;
--size;
}
return true;
}
}
void clear() //清空函数
{
ListNode<Type> *p = first->next,*q;
while(p != NULL)
{
q = p->next;
delete p;
p = q;
}
first->next = NULL;
last = first;
size = 0;
}
void insert(ListNode<Type> *p) //插入结点,按结点的值插入,从小到大.
{
ListNode<Type> *t = first;
while(t->next != NULL && t->next->data < p->data)
t = t->next;
if(t->next == NULL)
last = p;
else
t->next->prev = p;
p->next = t->next;
t-next = p;
p->prev = t;
}
void sort() //排序函数
{
ListNode<Type> *p = first->next,*q = p->next;
p->next = NULL;
last = p;
while(q != NULL)
{
p = q;
q = p->next;
insert(p);
}
}
void reverse() //转置函数
{
ListNode<Type> *p = first->next,*q = p->next;
last = p;
p->next = NULL;
while(q != NULL)
{
p = q;
q = p->next;
push_front(p->data);
delete p;
}
}
private:
ListNode<Type> *first;
ListNode<Type> *last;
size_t size;
};
utili.h
#ifndef _UTILI_H
#define _UTILI_H
#include<iostream>
using namespace std;
typedef int ElemType;
#endif
main.cpp
#include"list.h"
int main()
{
List<int> mylist;
int select = 1;
ElemType Item;
int postion,value;
while(select)
{
cout<<"******************************************"<<endl;
cout<<"[1]push_back [2]push_front *"<<endl;
cout<<"[3]show_list [0]quit_system *"<<endl;
cout<<"[4]pop_back [5]pop_front *"<<endl;
cout<<"[6]insert_val [7]delete_val *"<<endl;
cout<<"[8]find [9]clear *"<<endl;
cout<<"[10]sort [11]reverse *"<<endl;
cout<<"[12]Is_Full [13]"<<endl;
cout<<"******************************************"<<endl;
cout<<"input select:->";
cin>>select;
switch(select)
{
case 1:
cout<<"input data(-1 Over):>";
while(cin>>Item,Item != -1)
mylist.push_back(Item);
break;
case 2:
cout<<"input data(-1 Over):>";
while(cin>>Item,Item != -1)
mylist.push_front(Item);
break;
case 3:
mylist.show_list();
break;
case 4:
mylist.pop_back();
break;
case 5:
mylist.pop_front();
break;
case 6:
cout<<"输入要插入位置的值和插入的元素:>";
cin>>Item>>value;
mylist.insert(mylist.find(Item),value);
break;
case 7:
cout<<"请输入要删除的值:";
cin>>Item;
if(false == mylist.delete_val(Item))
cout<<"删除失败"<<endl;
else
cout<<"删除成功"<<endl;
break;
case 8:
cout<<"请输入要查找的值:"<<endl;
cin>>Item;
if(mylist.find(Item) == NULL)
cout<<"没有找到"<<endl;
else
cout<<"找到"<<endl;
case 9:
mylist.clear();
break;
case 10:
mylist.sort();
break;
case 11:
mylist.reverse();
break;
case 12:
if(mylist.Is_Full() == true)
cout<<"空"<<endl;
else
cout<<"不空"<<endl;
default:
break;
}
}
return 0;
}