C++数据结构实验04--线性表操作

本文深入探讨了链表的基本概念、操作方法及遍历技巧,包括链表的构造、查找、删除、插入、输出等核心功能,以及如何使用链表遍历器进行高效数据处理。
#include"stdafx.h"
#include <iostream>  
#include<new.h>  


using namespace std;    
  
//节点类,定义了每个节点的存储类型和指针名称  
template<class T>    
class ChainNode{    
    public:    
        T data;    
        ChainNode<T> *link;    
};    
  
//链表类,封装了链表操作的相应方法  
template<class T>    
class Chain{    
    public:    
        Chain(){first=0;}                        //构造函数,头结点指向空值  
        ~Chain();                                //析构函数  
        bool IsEmpty()const{return first==0;}    //判断是否为空  
        int Length()const;                        //返回该链表的长度  
        bool Find(int k,T&x)const;                //返回第k个元素到x中  
        int Search(const T&x)const;                //返回x所在的位置    
        Chain<T>& Delete(int k,T& x);            //删除第k个元素并把它返回到x中  
        Chain<T>& Insert(int k,const T&x);        //在第k个元素之后插入x  
        void Output(ostream& out) const;        //重载操作符的输出函数  
        ChainNode<T> *first;                    //指向第一个节点的指针  
        ChainNode<T> *last;                        //指向最后一个节点的指针  
  
        void Erase();  
        void Zero(){first=0;};  
        Chain<T>& Append(const T&x);  
  
};  
  
//链表遍历器类实现对链表的遍历  
template<class T>  
class ChainIterator{  
    public:  
        T* Initialize(const Chain<T>&c){  
            location = c.first;  
            if(location){  
                return &location->data;  
            }  
            return 0;  
        }  
  
        T* Next(){  
            if(!location)  
                return 0;  
            location = location->link;  
            if(location)  
                return &location->data;  
            return 0;  
        }  
    private:  
        ChainNode<T>*location;  
};  
  
  
  
//链表的析构函数,用于删除所有的链表中的节点  
template<class T>  
Chain<T>::~Chain(){  
    Erase();  
}  
  
  
//清除掉链表中的所有元素并且释放内存  
template<class T>  
void Chain<T>::Erase(){  
    ChainNode<T>*next;  
    //指向下一个节点  
    while(first){  
        next=first->link;  
        delete first;  
        first = next;  
    }  
}  
  
  
//输出链表  
template<class T>  
void Chain<T>::Output(ostream& out)const{  
    ChainNode<T>*current;  
    for(current=first;current;current=current->link){  
        out<<current->data;  
        if(!current->link){  
            out<<""<<endl;  
        }else{  
            out<<",";  
        }  
    }  
}  
//重载操作符  
template<class T>  
ostream& operator<<(ostream& out,const Chain<T>&x){  
    x.Output(out);  
    return out;  
}  
  
class OutOfBounds{    
    public:    
        OutOfBounds(){    
        cout<<"Out Of Bounds!"<<endl;    
        }    
};  
  
   
//内存不足的异常类    
class NoMem{    
    public:    
        NoMem(){    
            cout<<"No Memory!"<<endl;    
        }    
};    
//使new引发NoMem异常而不是xalloc异常    
//如果要恢复原始行为可以做以下调用    
//_set_new_handler(Old_Handler);    
int my_new_handler(size_t size){    
    throw NoMem();    
}  
  
  
//确认链表的长度  
template<class T>  
int Chain<T>::Length()const{  
    ChainNode<T>*current = first;  
    int length = 0;  
    while(current){  
        length++;  
        current = current->link;  
    }  
    return length;  
}  
  
//在链表中查找第k个元素  
//存在就储存到x中  
//不存在则返回false,否则返回true  
template<class T>  
bool Chain<T>::Find(int k,T&x)const{  
    if(k<1)  
        return false;  
    ChainNode<T>*current = first;  
    int index = 1;  
    while(index<k&& current){  
        current = current->link;  
        index++;  
    }  
    if(current){  
        x = current->data;  
        return true;  
    }  
    return false;  
}  
  
  
//在链表中搜索  
//查找x,如果发现则返回x的下标  
//如果x不存在则返回0  
template<class T>  
int Chain<T>::Search(const T&x)const{  
    ChainNode<T>*current = first;  
    int index = 1;  
    while(current&& current->data!=x){  
        current = current->link;  
        index++;  
    }  
    if(current){  
        return index;  
    }  
    return 0;  
}  
  
  
  
//从链表中删除一个元素  
//将第k个元素取至x  
//然后从链表中删除第k个元素  
//如果不存在则引发异常OutOfBounds  
template<class T>  
Chain<T>& Chain<T>::Delete(int k,T& x){  
    if(k<1||!first){  
        throw OutOfBounds();  
    }      
    ChainNode<T>*p = first;  
    if(k==1){  
        first = first->link;  
    }else{  
        ChainNode<T>*q = first;  
        for(int index = 1;index<k-1&&q;index++){  
            q = q->link;  
            //此时q指向要删除的前一个节点  
        }  
        if(!q||!q->link){  
            throw OutOfBounds();  
        }  
        p = q->link;  
        if(p==last)  
            last=q;  
        q->link=p->link;  
        //从链表中删除该节点  
        x = p->data;  
        delete p;  
        return *this;  
    }  
}  
  
  
//在第k个位置之后插入元素  
//不存在则报OutOfBounds异常  
//没有足够内存则报NoMem异常  
template<class T>  
Chain<T>& Chain<T>::Insert(int k,const T&x){  
    if(k<0){  
        throw OutOfBounds();  
    }      
    ChainNode<T>*p = first;  
  
    for(int index = 1;index<k && p;index++){  
        p = p->link;  
    }  
    if(k>0 && !p){  
        throw OutOfBounds();  
    }  
    ChainNode<T>*y = new ChainNode<T>;  
    y->data = x;  
    if(k){  
        y->link=p->link;  
        p->link=y;  
    }else{  
        //作为第一个元素插入  
        y->link = first;  
        first = y;  
    }  
    if(!y->link)  
        last=y;  
    return *this;  
}  
  
  
//在链表右端添加一个数据  
template<class T>  
Chain<T>& Chain<T>::Append(const T&x){  
    ChainNode<T>*y;  
    y = new ChainNode<T>;  
    y->data = x;  
    y->link = 0;  
    if(first){  
        last->link = y;  
        last = y;  
    }else{  
        first = last = y;  
    }  
    return *this;  
}  
  
template <class T>  
void InsertSort(T& array, int length){  
    int i, j, key;  
  
    for (i = 1; i < length; i++){  
        key = array[i];  
        //把i之前大于array[i]的数据向后移动  
        for (j = i - 1; j >= 0 && array[j] > key; j--){  
            array[j + 1] = array[j];  
        }  
        //在合适位置安放当前元素  
        array[j + 1] = key;  

    }  

  
  
int _tmain(int argc, _TCHAR* argv[]) 
{  
    int count = 0;  
  
    //收集输入的内容至链表a当中  
    Chain<int> a;  
      
    cout<<"创建链表并输出链表"<<endl; 
int inputNumber[10];  
    int count1 = 0;  
    cout<<"Input"<<endl;  
    for(int i =0;i<10;i++){  
        int a;  
        cin>>a;  
        if(a==0){  
            break;  
        }  
        inputNumber[i]=a;  
        count1++;  
    }  
InsertSort(inputNumber[i],count1);
for (int i = 0; i < count1; i++)
{
a.Insert(i,*b[i]);

        


        count++;  
    }  
      
    cout<<"Output1"<<endl;  
    cout<<a<<endl;  
     
cout<<"搜索链表的某个数据,若没有返回0"<<endl; 
    cout<<"Input2"<<endl;  
    int q;  
    cin>>q;  
    cout<<"Output2"<<endl;  
    cout<<a.Search(q)<<endl;  
      






  //创建链表遍历器,并反序输出
cout<<"创建链表遍历器,并反序输出"<<endl; 
cout<<"Input3"<<endl;  
    while(true)  
    {  
        int b;  
        cin>>b;  
        if(b==0)break;  
        a.Append(b);  
        count++;  
    }  
int *x;
int *d[100];
int i=0;
ChainIterator<int> c;
x=c.Initialize(a);


while (x&&i<a.Length())
{
d[i]=x;
x=c.Next();
i++;
}
    cout<<"Output3"<<endl; 


for (int i =a.Length()-1; i >=0; i--)
{
cout<<*d[i]<<endl;
}  
   










//创建两个有序链表,通过链表遍历器合并这两个有序链表
cout<<"创建两个有序链表,通过链表遍历器合并这两个有序链表"<<endl;
    Chain<int> w;  
    count = 0;  
    cout<<"Input4"<<endl;  
     cout<<a<<endl; 
    while(true){  
        int b;  
        cin>>b;  
        if(b==0)break;  
        w.Append(b);  
        count++;  
    }  
cout<<w<<endl;
    //直接将w链表遍历加在a链表的尾部  
    cout<<"Output4"<<endl;  
    ChainNode<int>*current = w.first;  
    while (current)  
    {  
        a.Append(current->data);  
        current= current->link;  
    }  
    cout<<a<<endl; 
system("pause");
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值