PTA 7-7 单链表的创建及遍历 (30 分)

PTA 7-7 单链表的创建及遍历 (30 分)

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef int T;
struct LinkNode //结点定义
{
      T data;//数据域
      LinkNode* next;//链域
      LinkNode(const T& item, LinkNode* ptr=NULL)
      {
            data=item;
            next=ptr;
      }
      LinkNode(LinkNode* ptr=NULL)
      {
            next=ptr;
      }
};
class List
{
private:
      LinkNode * first;
public:
      List()//构造函数
      {
          first=NULL;
      }
      //List(const T& x);
//      List(int n,);
      List(const List& L)//拷贝构造函数
      {
         first=NULL;
         CopyList(L);
      }
      List& operator=(const List& L)//赋值运算符函数
      {
          if(this==&L)
            return *this;
      MakeEmpty();
      CopyList(L);
      return *this;
      }
      ~List()//析构函数
      {
          MakeEmpty();
      }
      void InputFront(const T& elem)//头插法
      {
          LinkNode *L=new LinkNode(elem);
          L->next=first;
          first=L;
      }
      void InputRear(const T& elem)//尾插法
      {
          LinkNode *L=new LinkNode(elem);
          LinkNode *p=first;
          while(p->next!=NULL)
          {
              p=p->next;
          }
          p->next=L;
      }
      void MakeEmpty()//清空链表
      {
          while(first!=NULL)
          {
              LinkNode *p=first;
              first=first->next;
              delete p;
          }
      }
      int Length() const//返回链表中结点个数
      {
          int c=0;
          LinkNode *p=first;
          while(p!=NULL)
          {
              c++;
              p=p->next;
          }
          return c;
      }
      LinkNode* Search(const T& x)//在链表中查找元素x,找到返回它所在结点的地址,否则返回空
      {
          LinkNode *p=first;
          while(p!=NULL)
          {
              if(p->data==x)
              {
                  break;
              }
              p=p->next;
          }
          return p;
      }
      LinkNode* Locate(int i)//返回链表中第i个结点的地址,i取值不合法则返回空
      {
          LinkNode *p=first;
          int c=0;
          while(c<i-1)
          {
              if(p==NULL) break;
              p=p->next;
          }
          return p;
      }
      bool GetData(int i, T& x)//获取链表第i个元素,将它赋值给x
      {
          if(Locate(i)!=NULL)
          {
              x=Locate(i)->data;
              return true;
          }
          return false;
      }
      void CopyList(const List& L)
      {
          LinkNode *newNode,*iter,*rear;
          if(L.first==NULL)
            return;
      newNode=new LinkNode(L.first->data);
      first=newNode;
      iter=L.first->next;
      rear=newNode;
      while(iter)
      {
            newNode=new LinkNode(iter->data);
            rear->next=newNode;
            iter=iter->next;
            rear=rear->next;
      }
      }
      void SetData(int i, const T& x)//设置链表第i个元素为x
      {
          if(Locate(i)!=NULL)
          {
              Locate(i)->data=x;
          }
      }
      bool Insert(int i, const T& x)//在链表的第i个位置上插入元素x
      {
        LinkNode *pre;
        LinkNode *newNode;
        pre=first;
        if(i==1)
        {
            newNode=new LinkNode(x);
            newNode->next=first;
            first=newNode;
            return true;
        }
        else{
        for(int j=1; j<i-1; j++)
        {
            if(pre==NULL)
                break;
            pre=pre->next;
        }
        if(pre==NULL)
        {
            cerr<<"无效的插入位置"<<endl;
            return false;
        }
        else
        {
            newNode=new LinkNode(x);
            newNode->next=pre->next;
            pre->next=newNode;
        }
        return true;}
      }
      bool Remove(int i, T& x)//删除链表第i个位置的元素,并将它的值赋值给x
      {
          LinkNode *p,*q;
          if(i==1)
          {
              p=first;
              first=first->next;
              delete p;
              return true;
          }
          else if(Locate(i)!=NULL)
          {
              p=first;
              for(int j=0;j<i-2;j++)
              {
                  p=p->next;
              }
              q=p->next;
              p->next=q->next;
              delete q;
              return true;
          }
          return false;
      }
      bool IsEmpty() const//返回链表是否为空
      {
          return first==NULL;
      }
      bool IsFull() const;//返回链表是否为满
      void Sort()//对链表中结点进行排序
      {
           T n,c=0,x;
           LinkNode *p=first,*q;
           if(IsEmpty()==false)
           {
               n=first->data;
               p=p->next;
           }
           while(p!=NULL)
           {
               if(p->data<=n)
               {
                   n=p->data;
                   q=new LinkNode(n);
                   q=q->next;
                   Remove(c,x);
                   continue;
               }
               p=p->next;
               c++;
           }
           MakeEmpty();
           first=q;
      }
      List Combine(List &L)
      {
          List newList;
          LinkNode *p=first,*q=L.first;
          int c=1;
          while(p!=NULL&&q!=NULL)
          {
              if(p->data<=q->data)
              {
                  newList.Insert(c++,p->data);
                  p=p->next;
              }
              else
              {
                  newList.Insert(c++,q->data);
                  q=q->next;
              }
          }
          while(p!=NULL)
          {
              newList.Insert(c++,p->data);
              p=p->next;
          }
          while(q!=NULL)
          {
              newList.Insert(c++,q->data);
              q=q->next;
          }
          return newList;
      }
      void Input(int n)
      {
          int num;
          for(int i=0;i<n;i++)
          {
              cin>>num;
              Insert(i+1,num);
          }
      }
      friend ostream& operator<<(ostream& out, const List& L)//输出流运算符重载
      {
          LinkNode *p=L.first;
          for(int i=0;i<L.Length();i++)
          {
              if(i==L.Length()-1) cout<<p->data;
              else{
              cout<<p->data<<" ";
              p=p->next;}
          }
          return out;
      }
      friend istream& operator>>(istream& in, List& L)//输入流运算符重载
      {
          T n,num;
          in>>n;
          for(int i=0;i<n;i++)
          {
              cin>>num;
              L.Insert(i+1,num);
          }
          return in;
      }
};
int main()
{
      List lst1;
      cin>>lst1;
      cout<<lst1;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值