#include
using namespace std;
const int MaxSize=100;
template
struct Node {
DataType data;
Node *next;
};
template
class linklist {
public:
linklist();//建立只有头结点的空链表
linklist (DataType a[],int n); //建立n个元素的单链表
~linklist();//析构函数
int length();//求单链表的长度
DataType Get (int i); //按位查找,查找第i个结点的元素值
int locate (DataType x); //按值查找,查找值为x的元素序号
void insert (int i,DataType x); //插入操作,第i个位置插入值为x的结点
DataType Delete (int i); //删除操作,删除第i个结点
int empty();//判断线性表是否为空
void printlist();//遍历操作,按序号依次输出各元素
private:
Node *first;
DataType data[MaxSize];
};
template
linklist::linklist() {
first=new Node;//生成头结点
first->next=nullptr;//头结点的指针域置空
}
template
linklist::linklist (DataType a[],int n) {
first=new Node;
first->next=nullptr;//初始化一个空链表
for (int i=0; i<n; i++) {
Node *s=nullptr;
s=new Node;
s->data=a[i];
s->next=first->next;
first->next=s;//将结点s插入头结点后
}
}
template
linklist::~linklist() {
Node *p=first;
while (first!=nullptr) {// 释放每一个结点的存储空间
first=first->next;//first指向被释放结点的下一个结点
delete p;
p=first;//工作指针p后移
}
}
template
int linklist::length() {
Node *p=first->next;//工作指针初始化
int count=0;//累加器初始化
while (p!=nullptr) {
p=p->next;
count++;
}
return count;//注意count的初始化和返回值之间的关系
}
template
DataType linklist::Get (int i) {
Node *p=first->next;//工作指针p初始化
int count=1;//累加器count初始化
while (p!=nullptr&&count<i) {
p=p->next;//工作指针p后移
count++;
}
if (pnullptr)
throw"查找位置错误";
else
return p->data;
}
template
int linklist::locate (DataType x) {
Node *p=first->next;//工作指针p初始化
int count=1;//累加器count初始化
while (p!=nullptr) {
if (p->datax)
return count;//查找成功返回序号
p=p->next;//工作指针p后移
count++;
}
return 0;//退出循环表明查找失败
}
template
void linklist::insert (int i,DataType x) {
Node *p=first,*s=nullptr;
int count=0;
while (p!=nullptr&&count<i-1) { //查找第i-1个结点
p=p->next;//工作指针p后移
count++;
}
if (pnullptr)
throw"插入位置错误";//没有找到第i-1个结点
else {
s=new Node;
s->data=x;//申请结点s,数据域为x
s->next=p->next;
p->next=s;//将结点s插入到结点p之后
}
}
template
DataType linklist::Delete (int i) {
DataType x;
Node *p=first,*q=nullptr;
int count=0;
while (p!=nullptr&&count<i-1) { //查找第i-1个结点
p=p->next;
count++;
}
if (pnullptr||p->nextnullptr) //结点p不存在或p的后继结点不存在
throw"删除位置错误";
else {
q=p->next;
x=q->data;//暂存被删结点
p->next=q->next;//摘链
delete q;
return x;
}
}
template
int linklist::empty() {
if (first->nextnullptr)
return 1;
else
return 0;
}
template
void linklist::printlist() {
Node *p=first->next;//工作指针p初始化
while (p!=nullptr) {
cout<data<<"\t";
p=p->next;//工作指针p后移,注意不能写作p++
}
cout<<endl;
}
template
struct PolyNode {//定义多项式单链表的结点
int coef,exp;//coef表示系数,exp表示指数
PolyNode *link;
};
template
class Polynomial {
public:
Polynomial();//构造函数
Polynomial (const Polynomial &B);//拷贝构造函数
~Polynomial();//析构函数,同单链表析构函数
Polynomial operator+ (Polynomial &B);//重载运算符,多项式相加
void Print();//打印一元多项式单链表
private:
PolyNode *first1;//一元多项式单链表的头指针
};
template
Polynomial::Polynomial() {
PolyNode *r=nullptr,*s=nullptr;
int coef,exp;
first1=new PolyNode;//申请头结点
r=first1;//尾插法建立单链表
cout<<“请输入系数和指数:”;
cin>>coef>>exp;//输入第一项的系数和指数
while (coef!=0) {//循环结束的条件是输入系数为0
s=new PolyNode;
s->coef=coef;
s->exp=exp;
r->link=s;//将结点s插入单链表的尾部
r=s;
cout<<“请输入系数和指数:”;
cin>>coef>>exp;
}
r->link=nullptr;
}
template
Polynomial::Polynomial (const Polynomial &B) {
first1=B.first1;
}
template
Polynomial::~Polynomial() {
PolyNode *p=first1;
while (first1!=nullptr) {
first1=first1->link;
delete p;
p=first1;
}
}
template
Polynomial Polynomial::operator+ (Polynomial &B) {
PolyNode *pre=first1,*p=pre->link;//工作指针pre和p初始化
PolyNode *qre=B.first1,*q=qre->link;//工作指针qre和q初始化
PolyNode *qtemp=nullptr;
while (p!=nullptr&&q!=nullptr) {
if (p->expexp) {//第一种情况
pre=p;
p=p->link;
} else if (p->exp>q->exp) {//第二种情况
qtemp=q->link;
pre->link=q;//将结点q插入到结点p之前
q->link=p;
pre=q;
q=qtemp;
qre->link=q;
} else {//第三种情况
p->coef=p->coef+q->coef;
if (p->coef0) {//系数相加为0,则删除结点p
pre->link=p->link;
delete p;
p=pre->link;
} else {//系数不为0
pre=p;
p=p->link;
}
qre->link=q->link;//第三种情况都要删除结点q
delete q;
q=qre->link;
}
}
if (q!=nullptr)
pre->link=q;
return *this;
}
template
void Polynomial::Print() {
PolyNode *p=first1->link;
if (p!=nullptr) {//输出第一项
cout<coef<<“x”<<"^"<exp;
}
p=p->link;
while (p!=nullptr) {
if (p->coef>0) {//输出系数的正号或负号
cout<<"+"<coef<<“x”<<"^"<exp;
} else {
cout<coef<<“x”<<"^"<exp;
}
p=p->link;
}
cout<<endl;
}
int main() {
int a[6],b[6];
cout<<“请输入第一个集合中的元素:”<<endl;
for(int i=0;i<6;i++){
int n;
cin>>n;
a[i]=n;
}
cout<<“请输入第二个集合中的元素:”<<endl;
for(int i=0;i<6;i++){
int n;
cin>>n;
b[i]=n;
}
linklist l,l3,l4,l5;
linklist l1(a,6);
linklist l2(b,6);
cout<<“两个集合的并运算:”<<endl;
int s0=0;
for(int i=0;i<6;i++){
l3.insert(s0,a[i]);
s0++;
}
for(int i=0;i<6;i++){
int m=0;
for(int j=0;j<6;j++){
if(b[i]!=a[j]){
m++;
}
}
if(m6){
s0++;
l3.insert(s0,b[i]);
}
}
l3.printlist();
cout<<“两个集合的交运算:”<<endl;
int s1=0;
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
if(a[i]==b[j]){
l4.insert(s1,b[j]);
s1++;
}
}
}
l4.printlist();
cout<<“两个集合的差运算:”<<endl;
int s2=0;
bool judge=false;
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
if(a[i]==b[j]){
judge=false;
break;
}
else{
judge=true;
}
}
if(judge){
l5.insert(s2,a[i]);
s2++;
}
}
l5.printlist();
Polynomial c{};
c.Print();
Polynomial d{};
d.Print();
Polynomial e=c+d;
cout<<“结果是:”<<endl;
e.Print();
return 0;
}
本文介绍了如何使用C++实现单链表,包括初始化、获取元素、插入、删除、查找等基本操作,并展示了如何创建和操作多项式链表进行加法运算。示例代码详细展示了链表数据结构的实现细节。
5135

被折叠的 条评论
为什么被折叠?



