adt之链表

#include <iostream>
#include <string>
template <class T>
struct MyNode{
T element;
MyNode * next;
};
template <class T>
class MyList{
typedef struct MyNode<T> Node;
typedef struct MyNode<T> * Position, *List;
List mylist;
int length;
public:
MyList(){
mylist = new Node[1];
mylist->next = NULL;
length =0;
}
~MyList(){
deletelist();
delete mylist;
}
const List getheader()const {
return mylist;
}
int getlength(){
return length;
}
void setlength(int l){
length =l;
}
void deletelist(){
List p (mylist->next);
mylist->next = NULL;
length =0;
while(p!=NULL){
List tmp = p->next;
delete p;
p = tmp;
}
}
Position findprevious(T x){
List tmp = mylist->next;
while (tmp->next!=NULL&&tmp->next->element!=x)
{
tmp = tmp->next;
}
return tmp;
}
void insertlist(T x){
List tmpn = mylist;
while (tmpn->next !=NULL)
{
tmpn = tmpn->next;
}
List tmp = new Node [1];
tmp->next = tmpn->next;
tmp->element = x ;
tmpn->next = tmp;
length++;
}
void insertlist(T x, Position P){
List tmp = new Node [1];
tmp->next = P->next;
tmp->element =x;
P->next = tmp;
length++;
}
void deletelist(T x){
if (Position p = findprevious(x))
{
Position tmp = p->next;
delete tmp;
p->next =p->next->next;

else{
std::cout<<"error,none this element!";
}
}
void display(){
List p = mylist->next;
while (p!=NULL)
{
std::cout<<p->element<<std::endl;
p=p->next;
}
}
void vectorLinkList(){
List P,C,N;
P = NULL;
C = mylist->next;
N= C->next;
while (N!=NULL)
{
C->next = P;
P = C;
C = N;
N = N->next;
}
C->next = P;
mylist->next = C;
}
};


template <class T>
class cycleList:public MyList<T>{
typedef struct MyNode<T> Node;
typedef struct MyNode<T> * Position, *List;
Position privious;
T *returnElements;
public:
cycleList ():MyList(){
List tmp = MyList::getheader();
tmp ->next = tmp;
}
~cycleList(){
MyList::~MyList();
}
void insertlist(T x){
List tmpn = MyList::getheader();
while (tmpn->next !=MyList::getheader())
{
tmpn = tmpn->next;
}
List tmp = new Node [1];
tmp->next = tmpn->next;
tmp->element = x ;
tmpn->next = tmp;
MyList::setlength(MyList::getlength()+1);
}
void display(){
List p = MyList::getheader()->next;
while (p!=MyList::getheader())
{
std::cout<<p->element<<std::endl;
p=p->next;
}
}
Position findPrevious(const Position p){
privious = MyList::getheader();
while (privious->next != p&&privious->next!=MyList::getheader())
{
privious= privious->next;
}
return privious;
}
T * josephus(const int &M){
List headNode = MyList::getheader();
List cntNode = MyList::getheader()->next;
List priviousCntNode ;
int cntN=1;
returnElements = new T [MyList::getlength()];
while (cntN< MyList::getlength())
{
int cntM=0;
while (cntM!=M)
{
priviousCntNode = cntNode;
cntNode = cntNode->next;
if (cntNode!=headNode)
{
cntM++;

}
//删除
returnElements[cntN-1] = cntNode->element;
List tmp = cntNode;
priviousCntNode->next = cntNode->next;
cntNode = cntNode->next;
if (cntNode==MyList::getheader())
{
cntNode = cntNode->next;

delete tmp;
cntN++;
}
return returnElements;
}
};


template <class T>
class TwoLists{
typedef struct MyNode<T> Node;
typedef struct MyNode<T> * Position, *List;
MyList <T> L,P;
public:
TwoLists (const MyList<T> &l, const MyList<T> &p){
List tmp = l.getheader()->next;
List mylisttemp = L.getheader();
int lenth=0;
while (tmp!=NULL)
{
List tt = new Node [1];
tt->element = tmp->element;
tt->next = NULL;
mylisttemp->next  = tt;
mylisttemp = mylisttemp->next;
tmp = tmp->next;
lenth++;
}
L.setlength(lenth);
tmp = p.getheader()->next;
mylisttemp = P.getheader();
lenth=0;
while (tmp!=NULL)
{
List tt = new Node [1];
tt->element = tmp->element;
tt->next = NULL;
mylisttemp->next  = tt;
mylisttemp = mylisttemp->next;
tmp = tmp->next;
lenth++;
}
P.setlength(lenth);
}
~TwoLists(){
L.deletelist();
P.deletelist();
}
void PrintLots(){
List tmpL(L.getheader()->next);
List tmpP(P.getheader()->next);
int listcnt=1;
while (tmpL!=NULL &&tmpP!=NULL)
{
if (listcnt == tmpP->element)
{
std::cout<<tmpL->element<<std::endl;
tmpP = tmpP->next;
}
tmpL = tmpL->next;
listcnt++;
}
}
void PrintSingal(){
List ttl1 = L.getheader()->next;
List ttl2 = P.getheader()->next;
while (ttl1!=NULL||ttl2!=NULL )
{
if (ttl1!=NULL&&ttl2!=NULL&&ttl1->element==ttl2->element)
{
std::cout<<ttl1->element<<std::endl;
ttl1 = ttl1->next;
ttl2 = ttl2->next;

else if(ttl1!=NULL){
ttl1 = ttl1->next;
}
else if (ttl2!=NULL)
{
ttl2 = ttl2->next;
}
}
}
void PrintAllSingal(){
List ttl1 = L.getheader()->next;
List ttl2 = P.getheader()->next;
while (ttl1!=NULL||ttl2!=NULL )
{
if (ttl1!=NULL&&ttl2!=NULL&&ttl1->element<ttl2->element)
{
std::cout<<ttl1->element<<std::endl;
ttl1 = ttl1->next;

else if (ttl1!=NULL&&ttl2!=NULL&&ttl1->element>ttl2->element)
{
std::cout<<ttl2->element<<std::endl;
ttl2 = ttl2->next;
}
else if(ttl1!=NULL&&ttl2!=NULL&&ttl1->element==ttl2->element){
std::cout<<ttl2->element<<std::endl;
ttl1 = ttl1->next;
ttl2 = ttl2->next;
}
else if(ttl1!=NULL){
std::cout<<ttl1->element<<std::endl;
ttl1 = ttl1->next;
}
else if (ttl2!=NULL)
{
std::cout<<ttl2->element<<std::endl;
ttl2 = ttl2->next;
}
}
}
};




void main(){
MyList <int>L2;
const int M=3, N=5;


for (int i=1;i<N+1;i++)
{
L2.insertlist(i);
}
L2.display();
std::cout<<std::endl;
L2.vectorLinkList();
L2.display();
std::cout<<std::endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值