//.... utili.h
#ifndef UTILI_H
#define UTILI_H
#include<iostream>
using namespace std;
#include<assert.h>
#define bool int
#define true 1
#define false 0
#endif
//..... list.h
#ifndef LIST-H
#define LIST_H
#include"./utili.h"
#define size_t int
//有元函数声明
template <class Type>
class List;
template<class Type>
class ListNode
{
friend class List<Type>;
public:
ListNode():data(Type()),next(NULL)
{}
ListNode(Type d,ListNode<Type> *n=NULL):data(d),next(n)
{}
~ListNode()
{}
private:
Type data;
ListNode<Type> *next;
};
template<class Type>
class List
{
public:
List()
{
first=last=new ListNode<Type>;
size=0;
}
~List()
{
destory();
}
public:
void show_list();
void push_back(Type &x);
void push_front(Type &x);
void pop_back();
void pop_front();
bool insert_val(Type &x);
ListNode<Type> *find(Type &x);
bool delete_val(Type &x);
void clear();
void quit_system(int &x);
void reverse();
void sort();
void destory();
private:
ListNode<Type> *first;
ListNode<Type> *last;
size_t size;
};
//尾插
template<class Type>
void List<Type>::push_back(Type &x)
{
ListNode<Type> *s= new ListNode<Type>(x);
assert(s!=NULL);
last->next=s;
last=s;
size++;
}
//头插
template<class Type>
void List<Type>::push_front(Type &x)
{
int i;
ListNode<Type> *s=new ListNode<Type>(x);
assert(s!=NULL);
s->next=first->next;
first->next=s;
last = s;
for (i = 0; i <size; i++)
{
last = last->next;
}
size++;
}
//打印链表
template<class Type>
void List<Type>::show_list()
{
ListNode<Type> *p=first->next;
while(p!=NULL)
{
cout<<p->data<<"->";
p=p->next;
}
cout<<"over."<<endl;
}
//头删
template<class Type>
void List<Type>::pop_front()
{
int i;
if(size==0)
return ;
ListNode<Type> *p=first->next;
first->next=p->next;
free(p);
size--;
}
//尾删
template<class Type>
void List<Type>::pop_back()
{
if(size==0)
return ;
ListNode<Type> *p=first;
while(p->next!=last)
p=p->next;
p->next=NULL;
free(last);
last=p;
size--;
}
//按值插入
template<class Type>
bool List<Type>::insert_val(Type &x)
{
if(size==0)
{
push_back(x);
return true;
}
ListNode<Type> *p=first;
while(p->next!=NULL&& p->next->data<x)
{
p=p->next;
}
if(p->next==NULL)
{
push_back(x);
return true;
}
ListNode<Type> *s=new ListNode<Type>(x);
if(s==NULL)
return false;
s->next=p->next;
p->next=s;
size++;
return true;
}
//查找
template<class Type>
ListNode<Type>*List<Type>::find(Type &x) //返回节点的地址
{
ListNode<Type> *p=first->next;
while(p!=NULL&&p->data!=x)
p=p->next;
return p;
}
//排序
template<class Type>
void List<Type>::sort()
{
if(size==0)
return ;
ListNode<Type> *p=first->next;
ListNode<Type> *q=p->next;
// p->next=NULL;
last=p;
size_t tmp_size=size;
last->next=NULL;
ListNode<Type> *s;
while(q!=NULL)
{
insert_val(q->data);
s=q;
q=q->next;
delete s;
}
}
//按值删除
template<class Type>
bool List<Type>::delete_val(Type &x)
{
if(size==0)
{
cout<<"未找到该数:"<<endl;
return false;
}
ListNode<Type> *p=find(x);
if(p==NULL)
{
cout<<"未找到该数:"<<endl;
return false;
}
if(p==last)
{
pop_back();
}
else
{
ListNode<Type> *q=p->next ;;
p->data =q->data ;
p->next =q->next ;
free(q);
size--;
}
}
//清空
template<class Type>
void List<Type>::clear()
{
if(size==0)
return ;
ListNode<Type> *p=first->next;
while(p!=NULL)
{
pop_back();
}
}
//逆序
template<class Type>
void List<Type>::reverse()
{
if(size==0)
return ;
ListNode<Type> *p=first->next;
ListNode<Type> *q=p->next;
p->next=NULL;
last=p;
size=1;
ListNode<Type> *s;
while(q!=NULL)
{
push_front(q->data);
s=q;
q=q->next;
free(s);
}
}
//停止
template<class Type>
void List<Type>::quit_system(int &x)
{
clear();
x=0;
}
//摧毁
template<class Type>
void List<Type>::destory()
{
clear();
free(first);
first=last=NULL;
size=0;
}
#endif
//....test.cpp
#include"./utili.h"
#include"./list.h"
int main()
{
List<int>mylist;
int select=1;
int Item;
while(select)
{
cout<<"********************************"<<endl;
cout<<"*[1]show_list [2]push_front*"<<endl;
cout<<"*[3]push__back [4]pop_front *"<<endl;
cout<<"*[5]pop_back [6]insert_val*"<<endl;
cout<<"*[7]find [8]delete_val*"<<endl;
cout<<"*[9]clear [10]quit_system"<<endl;
cout<<"*[11]reverse [12]destory *"<<endl;
cout<<"*[13]sort***********************"<<endl;
cout<<"please select:>";
cin>>select;
switch(select)
{
case 1:
mylist.show_list();
break;
case 2:
cout<<"please input data:>";
while(cin>>Item,Item!=-1)
{
mylist.push_front(Item);
}
case 3:
cout << "请输入要插入的值(-1结束):>";
while (cin >> Item, Item != -1)
{
mylist.push_back(Item);
}
break;
case 4:
mylist.pop_front();
break;
case 5:
mylist.pop_back();
break;
case 6:
cout << "请输入要插入的值:>";
cin >> Item;
mylist.insert_val(Item);
break;
case 7:
cout << "请输入要查找的数:";
cin >> Item;
cout << "该数的地址为:";
cout<<mylist.find(Item)<<endl;
break;
case 8:
cout << "请输入要删除的值:>";
cin >> Item;
mylist.delete_val(Item);
break;
case 9 :
mylist.clear();
break;
case 10:
mylist.quit_system(select);
break;
case 11:
mylist.destory();
case 12:
mylist.reverse();
break;
case 13:
mylist.sort();
break;
default:
break;
}
}
return 0;
}
单链表的创建、插入删除等操作
最新推荐文章于 2022-04-22 21:29:41 发布