#include<iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node(int d=0,node *temp=NULL):data(d),next(temp)
{}
};
class nodelist :public node
{
protected:
node *head;
public:
nodelist()
{
head = new node;
}
nodelist(const node &x)
{
head = new node(x);
}
nodelist(const nodelist& x);
void makeempty();
~nodelist()
{
makeempty();
}
int length() const;
node* search(const int x);
node* gethead() const { return head; }
node* lacate(const int x);
void tail_insert();
void head_insert();
bool istempty()
{
return head->next == NULL ? true : false;
}
void insert(int i, node *p);
void remove(const int i,int &t);
void setdata(const int i, const int t);
bool find(const int t,int &x);
void output();
nodelist* operator=(nodelist *t);
};
nodelist::nodelist(const nodelist& x)//拷贝构造
{
node*p = x.gethead();
node *q = head = new node;
while (p->next != NULL)//一个节点一个节点复制
{
int temp;
temp = p->next->data;
q->next = new node(temp);
q = q->next;
p = p->next;
}
q->next = NULL;
}
void nodelist::makeempty()//清空链表
{
node *p;
while (head->next != NULL)
{
p = head->next;
head->next = p->next;//保存被删节点,从链表上摘下该节点(最后仅保留头结点)
delete p;
}
}
int nodelist::length()const//求链表的长度
{
node *p = head->next;
int count = 0;
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
node* nodelist::search(int x)//查找数据为x的节点
{
node *p = head->next;
while (p != NULL)
{
if (p->data == x)
break;
else p = p->next;
}
return p;
}
node* nodelist::lacate(const int x)//查找第i位的节点信息
{
if (x < 0) return NULL;
node *p = head;
int k = 0;
while (k != x && p->next != NULL)
{
p = p->next;
k++;
}
return p;
}
void nodelist::tail_insert()
{
makeempty();
int d;
cin >> d;
node *last = head;
while (d!=0)//约定的结束标志
{
node *p = new node(d);
if (p == NULL) exit(0);
last->next = p;
last = p;
cin >> d;
}
last->next = NULL;//可省略
}
void nodelist::head_insert()//头插法
{
int d;
cin >> d;
node *q=head;
while (d != 0)
{
node *p = new node(d);
if (p == NULL) exit(0);
p->next = q->next;
q->next = p;
cin >> d;
}
}
void nodelist::insert(int i, node *x)//在第i位之后插节点x
{
node *p = lacate(i);
if (p == NULL) exit(0);
node *q=p->next ;
x->next = q;
p->next = x;
}
void nodelist::output()//输出
{
node *p = head->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void nodelist::remove(int i,int &t)//删除
{
node *p = lacate(i-1);//找到要删除节点的上一个节点
if (p != NULL)
{
node *q = p->next;
t = p->next ->data;
p->next = q->next;
delete(q);
}
}
void nodelist::setdata(const int i, const int t)//修改第i位数据data
{
node *p = lacate(i);
if (p == NULL) exit(0);
p->data = t;
}
bool nodelist::find(const int x,int &i)//查找链表中存在x的数据并返回存在的节点的个数
{
int temp = 0;
node *p = head->next ;
while (p != NULL)
{
if (p->data == x)
temp++;
p = p->next;
}
if (temp)
{
i = temp;
return true;
}
else return false;
}
nodelist* nodelist::operator=(nodelist *t)//等号运算符重载
{
node *p = t->gethead();
node *q = head;
while (p->next != NULL)
{
int d = p->next->data;
node *temp = new node(d);
q = q->next;
q = temp;
}
q->next = NULL;
return this;
}
int main()
{
nodelist temp;
temp.tail_insert();//尾插法测试
temp.output();
temp.makeempty();
temp.head_insert();//头插法测试
node *temp3 = new node(2);
temp.insert(3, temp3);//任意位置插入数据
temp.output();
int t;
temp.remove(2, t);//删除第二位的数据
cout << "删除节点的数据为:" << t << endl;
temp.setdata(2, 10);//修改第二位的数据
temp.output();
int t1;
if (temp.find(5,t1)) cout << "数据存在于链表中且出现了" <<t1<<"次"<< endl;//查找链表中有没有节点数据为5
else cout << "无此数据" << endl;
nodelist temp1 = temp;//等号重载
temp1.output();
getchar();
getchar();
}
数据结构:单链表(c++继承和复合的形式实现)
最新推荐文章于 2023-11-10 20:16:06 发布