📄 link.cpp
字号:
#include"iostream"
#include"stdlib.h"
#include"Link.h"
using namespace std;
//私有成员函数
Node * Link::GetNode(const char & item, Node * nextptr)
{
Node * newnode;
newnode= new Node(item,nextptr);
if(newnode==NULL)
{
cout<<"Memory allocation faliure!"<<endl;
exit(1);
}
return newnode;
}
//构造析构函数
Link::Link()
{
front=rear=currptr=prevptr=NULL;
//或者把下面的都不要就可以生成一个空的链表供以后的程序使用
/* int temp;
cout<<"Please input the number of the Link you want to create:"<<endl;
cin>>size;
cout<<"Please input the element you want to insert in the link:"<<endl;
for(int i=0;i<size;i++)
{
cin>>temp;
InsertRear(temp);
}
*/
}
//Link::~Link()
//{
// cout<<"The link has been deleted!"<<endl;
//}
//链表基本操作
void Link::InsertFront(const char & item)
{
front=GetNode(item,front);
}
void Link::InsertRear(const char & item)
{
currptr=front;
if(front==NULL)
InsertFront(item);
else
{
while(currptr->NextNode()!=NULL)
currptr=currptr->NextNode();
currptr->InsertAfter(GetNode(item));
}
}
int Link::DeleteNode(const char & item)
{
currptr=front;
if(front->data==item)
{
front=front->NextNode();
delete currptr;
return 1;
}
else
{
while(currptr->NextNode()!=NULL)
{
if(currptr->NextNode()->data==item)
{
delete currptr->DeleteAfter();
return 1;
}
currptr=currptr->NextNode();
}
}
return 0;
}
//链表遍历
void Link::PrintList()
{
cout<<"The elements in the link are:"<<endl;
int count=0;
currptr=front;
while(currptr!=NULL)
{
cout<<currptr->data<<" ";
count++;
if(count%5==0)
cout<<endl;
currptr=currptr->NextNode();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -