📄 link.cpp
字号:
//extern const int LinkArray=10;
#include"iostream"
#include"stdlib.h"
#include"Link.h"
using namespace std;
//Node类的实现
Node::Node(const int & item ,Node * nextptr):data(item),next(nextptr){}
Node::~Node()
{
cout<<"The node has been deleted!"<<endl;
}
Node * Node::NextNode()const
{
return next;
}
void Node::InsertAfter(Node * p)
{
p->next=next;
next=p;
}
Node * Node::DeleteAfter()
{
if(next=NULL)
return NULL;
Node * temp;
temp=next;
next=temp->next;
return temp;
}
//Link类的实现
Node * Link::GetNode(const int & item, Node * nextptr)
{
Node * newnode;
newnode= new Node(item,nextptr);
if(newnode==NULL)
{
cout<<"Memory allocation faliure!"<<endl;
// exit(1);
}
return newnode;
}
Link::Link(int a[])
{
head=rear=front=currptr=prevptr=NULL;
for(int i=volume-1;i>=0;i--)
{
InsertFront(a[i]);
}
/* cout<<"Please input the number of the Link you want to create:"<<endl;
int item;i
cin>>item;
cout<<"Please input the data of the node in the link:"<<endl;
int temp;
cin<<temp;
head=new Node(temp);
for(int i=1;i<item;i++)
{
cin<<temp;
head->InsertAfter(new Node(temp));
}
*/
/* cout<<"Please input the number of the Link you want to create:"<<endl;
cin>>size;
for(int i=0;i<size;i++)
InsertFront();
*/
}
Link::~Link()
{
cout<<"The link has been deleted!"<<endl;
}
void Link::InsertFront(const int & item)
{
// cout<<"Please input the data you want to insert in the head:"<<endl;
// int temp;
// cin>>temp;
head=GetNode(item,head);
}
void Link::InsertRear()
{
cout<<"Please input the number you want to insert in the rear:"<<endl;
int temp;
cin>>temp;
currptr=head;
if(head==NULL)
InsertFront(temp);
else
{
while(currptr->NextNode()!=NULL)
currptr=currptr->NextNode();
currptr->InsertAfter(GetNode(temp));
}
}
void Link::PrintList()
{
int count=0;
currptr=head;
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 + -