// 请改为循环链表存储结构
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkList
{
public:
LinkList();
LinkList(int a[], int n);
~LinkList();
void PrintList();
void Insert(int i, int x);
private:
Node *first;
};
LinkList::LinkList()
{
first=new Node;
first->next=NULL;
}
LinkList::LinkList(int a[], int n)
{
first=new Node;
first->next=NULL;
for(int i=0;i<n;i++)
{
Node *s=new Node;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}
LinkList::~LinkList()
{
Node *p=first->next;
while(p!=NULL) //first改为NULL,前面的*p需要怎么改或者需不需要改
{
Node *q=p;
p=p->next;
delete q;
}
delete first;
}
void LinkList::PrintList()
{
Node *p;
p=first->next;
while(p!=NULL) //first改为NULL,前面的*p需要怎么改或者需不需要改
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void LinkList::Insert(int i, int x)
{
Node *p;
int j;
p=first; j=0;
while (p!=NULL && j<i-1) //first改为NULL,前面的*p需要怎么改或者需不需要改
{
p=p->next;
j++;
}
if (p==NULL) throw "位置";
else {
Node *s;
s=new Node;
s->data=x;
s->next=p->next;
p->next=s;
}
}
void main()
{
int a[]={1,2,3,4,5};
LinkList test(a,5);
test.PrintList();
try
{
test.Insert(2,6);
}
catch(char *s)
{
cout<<s<<endl;
}
test.PrintList();
getchar();
}
那该怎么改