题目
利用《数据结构》知识,定义一元素为整数的单链表类,并在主函数中调用各个功能:
数据成员:指向头结点的指针、链表中当前元素的个数;
成员函数:初始化、在尾部增加一元素、查询指定数据的元素、在指定元素前插入一新元素、删除指定元素、遍历链表中的元素、输出所有链表中的元素、将一个单链表逆序(选作)。
原理
类的成员可以是其他类的对象,称为类的组合。不能以类自身的对象作为本类的成员。但是可以是本身的指针或者引用。
#include<iostream>
using namespace std;
class Link
{
private:
Link* next;
Link* head;
int x;
int n;
public:
void init();
void setLink(int a[], int n);
int getNum();
void insertLast(int data);
void findByValue(int value);
void findByLocation(int location);
void insertValue(int i, int data);
void deleteByValue(int value);
void deleteByLocation(int location);
int traverse();
void print();
void reverseLink();
};
void Link::init()
{
head=new Link;
head->next=NULL;
}
void Link::setLink(int a[], int n)
{
Link* r, * s;
head = new Link;
r = head;
for (int i = 0; i < n; i++)
{
s = new Link;
s->x = a[i];
r->next = s;
r = s;
}
r->next = NULL;
this->n=n;
}
int Link::getNum()
{
return n;
}
void Link::insertLast(int data)
{
Link* r, * s, * p;
p = head;
while (p->next != NULL)
{
p = p->next;
}
r = p;
s = new Link;
s->x = data;
r->next = s;
r = s;
r->next = NULL;
n++;
}
void Link::insertValue(int i, int data)
{
Link* p;
int j;
p = head;
j = 0;
while (p && j < i - 1)
{
p = p->next;
j++;
}
if (!p)
cout << "error";
else
{
Link* s;
s = new Link;
s->x = data;
s->next = p->next;
p->next = s;
}
n++;
}
void Link::findByValue(int value)
{
Link* p, * q;
p = head->next;
int k = 1;
while (p != NULL && value != p->x)
{
p = p->next;
k++;
}
if (p == NULL )
cout << "error";
else
{
cout << k<<" ";
cout << endl;
}
}
void Link::findByLocation(int location)
{
Link* p, * q;
p = head;
int j = 0;
while (p != NULL && j < location - 1)
{
p = p->next;
j++;
}
if (location <= 0 || p == NULL)
cout << "error";
else
{
cout << p->next->x<<" ";
cout << endl;
}
}
void Link::deleteByLocation(int location)
{
Link* p, * q;
p = head;
int j = 0;
while (p != NULL && j < location - 1)
{
p = p->next;
j++;
}
if (location <= 0 || p == NULL)
cout << "error";
else
{
q = p->next;
p->next = q->next;
delete q;
n--;
print();
}
}
void Link::deleteByValue(int value)
{
Link* p, * q;
p = head->next;
int k = 1;
while (p != NULL && value != p->x)
{
p = p->next;
k++;
}
if (p == NULL || p->next == NULL)
cout << "error";
else
{
int j = 0;
p = head;
while (p != NULL && j < k - 1)
{
p = p->next;
j++;
}
q = p->next;
p->next = q->next;
delete q;
n--;
print();
}
}
int Link::traverse()
{
Link* p;
int count = 0;
p = head->next;
while (p)
{
count++;
p = p->next;
}
n=count;
}
void Link::print()
{
Link* p;
p = head->next;
while (p)
{
cout << p->x << " ";
p = p->next;
}
cout << endl;
}
void Link::reverseLink()
{
Link* p, * q;
p = head->next;
if (!p)
cout<<"error";
else
{
head->next = NULL;
while (p)
{
q = p->next;
p->next =head->next;
head->next = p;
p = q;
}
}
}
int main()
{
int array[10];
for (int i = 0; i < 10; i++)
{
array[i] = 2 * i - 1;
}
Link* l = new Link;
cout<<"初始化链表,输出元素个数"<<endl;
l->init();
cout<<l->getNum()<<endl;
cout<<"利用自己生成的数组来给链表赋值"<<endl;
l->setLink(array, 10);
l->print();
cout<<"在链表的最后插入100"<<endl;
l->insertLast(100);
l->print();
cout<<"按位置查找,查询第四个元素,返回值"<<endl;
l->findByLocation(4);
l->print();
cout<<"按值查找,查询100元素的位置,返回位置"<<endl;
l->findByValue(100);
l->print();
cout<<"在第五个位置插入200"<<endl;
l->insertValue(5,200);
l->print();
cout<<"输出当前数据成员n的值"<<endl;
cout<<l->getNum()<<endl;
cout<<"按位置删除,删除第二个元素"<<endl;
l->deleteByLocation(2);
cout<<"按值删除,删除值为200的元素"<<endl;
l->deleteByValue(200);
cout<<"利用get函数获取数据成员"<<endl;
cout<<l->getNum()<<endl;
cout << "------逆序------"<<endl;
l->reverseLink();
cout<<"遍历链表,输出整个链表的元素个数"<<endl;
cout<<l->getNum()<<endl;
l->print();
}