📄 listcount.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define LAST 0
#define FIRST 1
struct Node{
int data;
struct Node* Next;
};
class List
{
Node *head;
int iMaxsize;
int iCout;
public:
List(){head = NULL; iCout = 0;iMaxsize = 0;cout<<"调用构造函数!"<<endl;}
List(int isize){head = NULL; iCout = 0; iMaxsize = isize;}
List(const List <);
~List();
List& operator=(const List &la);
List operator+(const List &la);
void creatList(int ic);
void insert(int e,int loc);
void delayNode(int loc);
void clearList();
void isListEmty();
void isListFull();
void display();
};
List::List(const List <)
{
Node *ptn1, *ptn2, *ptn3;
iCout = lt.iCout;
iMaxsize = lt.iMaxsize;
ptn2 = NULL;
if (lt.head == NULL)
{
head = NULL;
}
else
{
ptn1 = lt.head;
while (ptn1 != NULL)
{
ptn3 = new Node;
ptn3->data = ptn1->data;
ptn3->Next = NULL;
if (ptn2 == NULL)
{
ptn2 = ptn3;
head = ptn2;
}
else
{
ptn2->Next = ptn3;
ptn2 = ptn2->Next;
}
ptn1 = ptn1->Next;
}
}
cout<<"调用拷贝构造函数!"<<endl;
}
List::~List()
{
Node *ptn;
while (head != NULL)
{
ptn = head;
head = ptn->Next;
delete ptn;
}
cout<<"析构成功!"<<endl;
}
List& List::operator=(const List &la)
{
if (this == &la)
{
return *this;
}
Node *ptn1, *ptn2, *ptn3;
while (head != NULL)
{
ptn1 = head;
head = ptn1->Next;
delete ptn1;
}
iCout = la.iCout;
iMaxsize = la.iMaxsize;
ptn2 = NULL;
ptn1 = la.head;
while (ptn1 != NULL)
{
ptn3 = new Node;
ptn3->data = ptn1->data;
ptn3->Next = NULL;
if (ptn2 == NULL)
{
ptn2 = ptn3;
head = ptn2;
}
else
{
ptn2->Next = ptn3;
ptn2 = ptn2->Next;
}
ptn1 = ptn1->Next;
}
return *this;
}
List List::operator+(const List &lb)
{
List Ltemp;
Node *ptn1, *ptn2, *ptn3;
Ltemp.iCout = iCout + lb.iCout;
Ltemp.iMaxsize = iMaxsize + lb.iMaxsize;
ptn2 = NULL;
if (lb.head == NULL && head == NULL)
{
Ltemp.head = NULL;
}
else
{
ptn1 = head;
while (ptn1 != NULL)
{
ptn3 = new Node;
ptn3->data = ptn1->data;
ptn3->Next = NULL;
if (ptn2 == NULL)
{
ptn2 = ptn3;
Ltemp.head = ptn2;
}
else
{
ptn2->Next = ptn3;
ptn2 = ptn2->Next;
}
ptn1 = ptn1->Next;
}
ptn1 = lb.head;
while (ptn1 != NULL)
{
ptn3 = new Node;
ptn3->data = ptn1->data;
ptn3->Next = NULL;
if (ptn2 == NULL)
{
ptn2 = ptn3;
Ltemp.head = ptn2;
}
else
{
ptn2->Next = ptn3;
ptn2 = ptn2->Next;
}
ptn1 = ptn1->Next;
}
}
return Ltemp;
}
void List::creatList( int ic)
{
int ii,temp;
for(ii = 0; ii < ic; ++ii)
{
temp = rand() % 100;
insert(temp,LAST);
}
}
void List::clearList()
{
Node *ptn;
while (head != NULL)
{
ptn = head;
head = ptn->Next;
delete ptn;
}
iCout = 0;
}
void List::isListEmty()
{
if (head == NULL)
{
cout<<"链表为空!"<<endl;
}
}
void List::isListFull()
{
if (iMaxsize == 0)
{
cout<<"链表长度无限定!"<<endl;
}
else if (iCout == iMaxsize)
{
cout<<"链表已满!"<<endl;
}
}
void List::insert(int e,int loc)
{
int i = 1;
Node *mod,*ptn;
mod = head;
ptn = new Node;
ptn->data = e;
if (iMaxsize == 0 || iCout < iMaxsize)
{
if(loc == LAST)
{
ptn->Next = NULL;
if(head == NULL)
head = ptn;
else
{
while(1)
{
if(mod->Next == NULL)
{
mod->Next = ptn;
break;
}
else
mod = mod->Next;
}
}
}
else
{
if(head==NULL)
i=0;
else
{
while(mod->Next!=NULL)
{
mod=mod->Next;
i++;
}
}
if(loc> (i+2))
cout<<"超出链表范围!"<<endl;
else if(loc == i+1)
{
ptn->Next = NULL;
if(head == NULL)
head = ptn;
else
mod->Next = ptn;
}
else
{
mod=head;
if(loc == 1)
{
ptn->Next=mod;
head = ptn;
}
else
{
for(i=2;i<loc;i++)
mod= mod->Next;
ptn->Next=mod->Next;
mod->Next=ptn;
}
}
}
iCout++;
}
else
cout<<"链满!"<<endl;
}
void List::delayNode(int loc)
{
int i=1,j=1;
Node *ptn,*del;
ptn = head;
if(loc < 1)
{
cout<<"输入错误!"<<endl;
return;
}
if(head == NULL)
i=0;
else
while(ptn->Next!=NULL)
{
ptn=ptn->Next;
i++;
}
if(loc> i)
cout<<"超出链表范围!"<<endl;
else
{
ptn = head;
for(j=2;j<loc;j++)
ptn=ptn->Next;
if(loc==i)
{
if(loc == 1)
head = NULL;
else
ptn->Next=NULL;
}
else
{
if(loc == 1)
{
del = ptn;
head = ptn->Next;
}
else
{
del = ptn->Next;
ptn->Next = (ptn->Next)->Next;
}
}
del->Next = NULL;
delete del;
iCout--;
}
}
void List::display()
{
Node *ptn;
if(head == NULL)
cout<<"链表为空!"<<endl;
else
{
ptn = head;
do
{
cout<<ptn->data;//<<'('<<ptn<<')'
if(ptn->Next != NULL)
cout<<"->";
ptn = ptn->Next;
}while(ptn!= NULL);
}
cout<<endl;
}
void main(void)
{
int flg;
int i = 0, j = 0;
List l1;
time_t t;
srand((unsigned) time(&t));
cout<<"是否限定链表长度?(0/1):";
cin>>flg;
if (flg == 1)
{
cout<<"请输入限定链表长度:";
cin>>i;
l1 = List(i);
}
cout<<"请输入生成第一个链表长度:";
cin>>j;
l1.creatList(j);
l1.display();
/* cout<<"请输入要删除第几个结点:";
cin>>j;
l1.delayNode(j);
l1.display();
l1.isListFull();
// l1.clearList();
l1.isListEmty();*/
cout<<"请输入生成第二个链表长度:";
cin>>j;
List l2;
//
l2.creatList(j);
l2.display();
cout<<"通过第一个链表生成第三个链表:"<<endl;
List l3(l1);
l3.display();
List l4 = l1 + l2 + l3;
cout<<"三个链表之和:"<<endl;
l4.display();
cout<<"将和付给第二个链表:"<<endl;
l2 = l4;
l2.display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -