《数据结构》实验二 线性表的实验
发布时间:2021-05-26 04:23:25 所属栏目:站长百科 来源:网络整理
导读:副标题#e# 《数据结构》实验二:???? 线性表实验 一..实验目的 ???? 巩固线性表的数据结构,学会线性表的应用。 1.回顾线性表的逻辑结构,线性表的物理存储结构和常见操作。 2.学习运用线性表的知识来解决实际问题。 3.进一步巩固程序调试方法。 4.进一步
|
头文件 template<class T>
struct jied
{
T data;
jied<T> *next;
};
template<class T>
class Linklist
{
public:
Linklist();
Linklist(T a[],int n);
~Linklist();
int Length();
void add(int i,T x);
T shanc(int i);
T chaz1(int i);
int chaz2(T x);
void shuc();
private:
jied<T> *first;
};
#include "dlb1.h"
template<class T>
Linklist<T>::Linklist()
{
first=new jied<T>;
first->next=NULL;
}
template<class T>
Linklist<T>::Linklist(T a[],int n)
{
int i;
jied<T> *w,*j;
first=new jied<T>;
w=first;
for(i=0;i<n;i++)
{
j=new jied<T>;
j->data=a[i];
w->next=j;
w=j;
}
w->next=NULL;
}
template<class T>
Linklist<T>::~Linklist()
{
jied<T> *p;
while(first!=NULL)
{
p=first;
first=first->next;
delete p;
}
}
template<class T>
int Linklist<T>::Length()
{
jied<T> *p; int count;
p=first->next; count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
template<class T>
void Linklist<T>::add(int i,T x)
{
jied<T> *p,*s; int count;
p=first; count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"位置错误";
else
{
s=new jied<T>;
s->data=x;
s->next=p->next;
p->next=s;
}
}
template<class T>
T Linklist<T>::shanc(int i)
{
jied<T> *p,*r; T x; int count;
p=first; count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL || p->next==NULL)throw"位置错误";
else
{
r=p->next;
x=r->data;
p->next=r->next;
delete r;
return x;
}
}
template<class T>
T Linklist<T>::chaz1(int i)
{
jied<T> *p; int count;
p=first->next; count=1;
while(p!=NULL && count<i)
{
p=p->next;
count++;
}
if(p==NULL)throw"查找位置错误";
else
return p->data;
}
template<class T>
int Linklist<T>::chaz2(T x)
{
jied<T> *p; int count;
p=first->next; count=1;
while(p!=NULL)
{
if(p->data==x)
return count;
p=p->next;
count++;
}
return 0;
}
template<class T>
void Linklist<T>::shuc()
{
jied<T> *p;
p=first->next;
while(p!=NULL)
{
cout<<p->data<<setw(5);
p=p->next;
}
cout<<endl;
}
(编辑:网站开发网_盐城站长网 ) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐

