数据结构(C++)的第一次作业
- 主要是单向链表的内容
#include "iostream"
#include "cstdlib"
using namespace std;
template <class T>
struct LinkNode {
T data;
LinkNode<T> *next;
};
template <class T>
class LinkList
{
protected:
LinkNode<T> *first;
public:
LinkList() { first = new LinkNode<T>;first->next=NULL; first->data = 0; }
LinkList(LinkList<T>& L);
~LinkList(){ }
void makeEmpty();
int Length() const;
LinkNode<T> *Search(T x);
LinkNode<T> *Locate(int i);
bool Reverse(); //逆置函数的声明
void setData(int i, T x);
bool Insert (int i, T x);
bool Remove(int i, T& x);
bool Remove(T x);
bool Increase(); //声明递增的判断函数
bool IsEmpty() const
{ return first->next == NULL ? true : false; }
LinkNode<T> *getFirst() const { return first; }
void setFirst(LinkNode<T> *f ) { first = f; }
void output()
{LinkNode<T> *p=first->next;
while(p!=NULL)
{cout<<p->data<<" "; p=p->next; }
cout<<endl;
}
};
template <class T>
void LinkList<T>::makeEmpty()
{ LinkNode<T> *q;
while (first->next != NULL) {
q = first->next;
first->next = q->next;
delete q;
}
}
template <class T>
int LinkList<T> :: Length ( ) const
{ LinkNode<T> *p = first->next;
int count = 0;
while ( p != NULL )
{p = p->next; count++; }
return count;
}
template <class T>
LinkNode<T> *LinkList<T>::Search(T x)
{ LinkNode<T> *current = first->next;
while ( current != NULL && current->data != x )
current = current->next;
return current;
}
template <class T>
LinkNode<T> *LinkList<T>::Locate ( int i )
{ if (i < 0) return NULL;
LinkNode<T> *current = first; int k = 0;
while ( current != NULL && k < i )
{ current = current->next;
k++; }
return current;
}
template <class T>
bool LinkList<T>::Insert (int i, T x)
{ LinkNode<T> *current = Locate(i);
if (current == NULL) return false;
LinkNode<T> *newNode=new LinkNode<T>;
newNode->data=x;
newNode->next = current->next;
current->next = newNode;
return true;
}
template <class T>
bool LinkList<T>::Remove (int i, T& x )
{ LinkNode<T> *current = Locate(i-1);
if (current==NULL||current->next==NULL)return false;
LinkNode<T> *del = current->next;
current->next = del->next;
x = del->data; delete del;
return true;
}
template <class T>
bool LinkList<T>::Remove (T x )
{ LinkNode<T> *p=first->next,*q=first;
while(p!=NULL&&p->data!=x){q=p;p=p->next;}
if (p==NULL){cout<<"not find x\n";return false;}
q->next =p->next;
delete p;
return true;
}
//作业部分
//1.就地逆置
template <class T>
bool LinkList<T>::Reverse ( ){
LinkNode<T>*p = first -> next,*q;
if(p == NULL)return false;
first->next = NULL;
while(p){
q = p;
p = p -> next;
q -> next = first -> next;
first -> next = q;
}
return true;
}
//2.删除多余元素
template <class T>
void Delete(LinkList<T> & LA){
int n1 = LA.Length();
int i = 1;
int j;
T x,m;
while(i <= n1){
x = LA.Locate(i) -> data;
for(j = i+1;j <= n1;j++){
m = LA.Locate(j) -> data;
if (m == x)
{
LA.Remove(j, x); n1--;j--;
}
}
i++;
}
}
//3.求两个列表的交集
template <class T>
void Intersection(LinkList<T> & LA,LinkList<T>& LB) {
int n1 = LA.Length();
int i = 1;
T x;
while (i <= n1)
{
x = LA.Locate(i) -> data;
LinkNode<T> *k = LB.Search(x);
if (k == NULL)
{
LA.Remove(i, x); n1--; i--;
}
i++;
}
}
//4.判断列表是否递增
template <class T>
bool LinkList<T>:: Increase( ){
LinkNode<T> *p=first->next,*q=first;
while(p->next!=NULL&&((p->data)<(p->next->data))){
q=p;p=p->next;}
if (p -> next == NULL){cout<<"Yes"<<endl;return true;}
q->next =p->next;
delete p;
cout<<"No"<<endl;
return false;
}
//调试部分
int main(){
LinkList<int> LA,LB,LC;
int a[6]={2,2,4,5,5,6};
for(int i=0;i<=10;i++)LA.Insert(i,i);
for(int i=0;i<=5;i++)LB.Insert(i,2*i+2);
for(int i=0;i<=5;i++)LC.Insert(i,a[i]);
//1.逆置的测试
// cout<<"\nLA list is:";
// LA.output();
// cout<<"LA逆置后,LA list is:";
// LA.Reverse();
// LA.output();
//2.删除多余的元素
// cout<<"\nLC list is:";
// LC.output();
// cout<<"删除LC中多余的元素后:";
// Delete(LC);
// LC.output();
//3.求两个列表的交集
// cout<<"\nLA list is:";
// LA.output();
// cout<<"\nLB list is:";
// LB.output();
// cout<<"LA和LB的交集LC: ";
// Intersection(LA, LB);
// LA.output();
//4.判断LA是否递增
// cout<<"\nLA list is:";
// LA.output();
// cout<<"LA是否递增: ";
// LA.Increase();
return 0;
}