单链表ADT模板简单应用算法设计:单链表的逆置(WA 4610)(方法不是很好,索性就直接放源码吧)

问题描述

目的:使用C++模板设计单链表的抽象数据类型(ADT)。并在此基础上,使用单链表ADT的基本操作,设计并实现单链表的简单算法设计。

内容:(1)请使用模板设计单链表的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考网盘中的ADT原型文件。)

(2)ADT的简单应用:使用该ADT设计并实现单链表应用场合的一些简单算法设计。

应用:试设计一个算法,将带头结点的单链表A逆置。要求完全利用原表的存储空间,不允许另行定义新链表。

参考函数原型:

template<class ElemType>
void Lk_Reverse( LinkList<ElemType> &A ); //测试数据限定为string。实际应用时,不受其限制

输入说明

第一行:单链表A的长度

第二行:单链表A的数据元素(数据元素之间以空格分隔)

输出说明

第一行:单链表A的遍历结果

第二行:逆置后单链表A的遍历结果

(输入与输出之间以空行分隔)

输入范例

4
1 2 3 4

输出范例

1 2 3 4 

4 3 2 1 

思路分析

  • 问题重点
    • 逆置带头结点的单链表
    • 利用原来的存储空间,不允许新定义链表
  • 思路分析:
尝试一
  • 步骤
    • 如果是双向的指针那就好办了,可以简单的首节点和尾结点换一下就行了
    • 现在是单链表,以为着结点之间的指针顺序是单向的,让他反向不就好了
    • 然后再交换首尾两个结点
  • 结论:这个方法,根本不可行,结点之间的连接时单向的,逆转了某个结点,后面的结点就都丢失了,所以不可取
    在这里插入图片描述
尝试二
  • 步骤
    • 采用强硬的方法,将所有的结点依次向后移动,移到最后的结点之后
    • 标记原来的尾结点为final,当head结点遇到final结点的时候,就停止遍历
    • 标记后来的生成的尾结点为end,方便将元素后置。

在这里插入图片描述

  • 结论:笑死我了,完整的转了一圈。我搞得和真的一样

在这里插入图片描述

尝试三
  • 步骤:
    • 源于对尝试二的改进,将首结点插到final结点的后面
    • end指针取消,不过不会再发生变化了
    • 循环条件不变,仍旧是head碰见了final结点

在这里插入图片描述

伪码



#include <iostream>

using namespace std;

/* 单链表的结点定义 */
template<class ElemType>
struct LinkNode
{
    ElemType data;
    LinkNode<ElemType> *next;
    LinkNode(LinkNode<ElemType> *ptr = NULL){next = ptr;}
    LinkNode(const ElemType &item, LinkNode<ElemType> *ptr = NULL)
    //函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
    {
        next = ptr;
        data = item;
    }
};

//带头结点的单链表
template<class ElemType>
class LinkList
{
   private:
      LinkNode<ElemType> *head;   // 头结点
   public:
      //the constructer without parameters
      LinkList(){head = new LinkNode<ElemType>;}

      //the construtor to construct the first node with value
      LinkList(const ElemType &item){head = new LinkNode<ElemType>(item);}

      //the constructor to copy the linkedlist
      LinkList(LinkList<ElemType> &List);

      //deconstructor of the function
      ~LinkList(){ListDestroy();}

      //the overload of the operator "=" whose function is assignment
      LinkList<ElemType>& operator=(LinkList<ElemType> &List);

      //destory the linkedlist
      void ListDestroy();

      //clear all the effective element of the linklist
      void ListClear();

      //return the length of the linklist
      int ListLength() const;

      //judge whether the linklist is wmpty ort not
      bool ListEmpty() const;

      //insert an element at the start of the linklist
      bool InsFirst( ElemType e );

      //get the head of the linklist
      LinkNode<ElemType>* GetHead() const{ return head;}

      //set the value of head of the LInkList
      void SetHead(LinkNode<ElemType> *p){*head = *p;}

      //return the element whose index is pos
      ElemType GetElem(int pos);

      //insert the element before the element whose index is pos
      bool ListInsert(int pos,ElemType e);

      //delete the first node of the linklist and return the value to e
      bool DelFirst( ElemType &e);

      //construct the linklist by inserting the element at the beginning of linklist
      void CreateList_Head(int n, ElemType *A);

      //construct the linklist by inserting the element at the end of the linklist
      void CreateList_Tail(int n, ElemType *A);

      //delete the element whose index is pos
      ElemType ListDelete(int pos);

      //return the pointer which point to eligible element
      bool LocateElem(const ElemType &e, LinkNode<ElemType> *pos);

      //return the value which is previous to the element you input
      bool PriorElem(ElemType cur_e, ElemType &pri_e);

      //return the value which is next to the element you input
      bool NextElem(LinkNode<ElemType> *p, ElemType &e);

      //traverse the whole linklist
      bool ListTraverse() const;

      //get the index you want
      LinkNode<ElemType> *getIndex(int times);

      //get the tail of the linklist
      LinkNode<ElemType> *getTail();

};

//the constructor to copy the linkedlist
template <class ElemType>
LinkList<ElemType>::LinkList(LinkList<ElemType> &List)
{
    //traverse the whole linkedlist to copy all the elements

    //copy the first node of the linkedlist
    LinkNode<ElemType> *temp = List.head;
    head = new LinkNode<ElemType>(*temp);

    //copy the other nodes apart from the first node
    LinkNode<ElemType> *temp2 = head;
    while(temp)
    {
        temp2->next = new LinkNode<ElemType>(*temp->next);
        temp = temp->next;
        temp2 = temp2->next;
    }

}


/*
    description:the overload of the operator "="
    function:assign the value to other linkedlist
*/
template<class ElemType>
LinkList<ElemType>& LinkList<ElemType>::operator=(LinkList<ElemType> &List)
{

    //case 1:the linkedlist is NULL,just a pointer
    if(!head)
    {
        this = LinkList(List);
    }
    else
    {
    //case 2:the linkedlist is not NULL
        LinkNode<ElemType> *temp1 = List->head;
        LinkNode<ElemType> *temp2 = head;

        //copy the common the element that both have
        while(temp1 && temp2)
        {
            *temp1 = *temp2;
            temp1 = temp1->next;
            temp2 = temp2->next;
        }

        //deal with the left element of the linklist
        //the right linklist is longer and copy the left part of the linklist
        if(temp1)
        {
            while(temp1)
            {
                temp2 = new ElemType(*temp1);
                temp1 = temp1->next;
                temp2 = temp2->next;
            }
            return *this;
        }
        //the left linklist is longer and delete the left part of the linklist
        if(temp2)
        {
            LinkNode<ElemType> temp3 = temp2->next;
            while(temp2)
            {
                temp3 = temp2->next;
                delete temp2;
                temp2 = temp3;

            }
            return *this;
        }

    }
}


/*
    description:delete all the element of the of the linkedlist
*/
template<class ElemType>
void LinkList<ElemType>::ListDestroy()
{
    LinkNode<ElemType> *temp = NULL;

    while(head)
    {
        temp = head->next;
        delete head;
        head = temp;
    }

}

/*
    description:clear all the element of the linklist
*/
template<class ElemType>
void LinkList<ElemType>::ListClear()
{
    //delete other element apart from the first element
    LinkNode<ElemType> *temp = head->next;
    LinkNode<ElemType> *temp2 = NULL;

    while(temp)
    {
        temp2 = temp->next;
        delete temp;
        temp = temp2;
    }

    //reset the value of the first element
    head->next = NULL;
}

/*
    description:return the length of the linklist
*/
template<class ElemType>
int LinkList<ElemType>::ListLength() const
{
    int length = 0;
    LinkNode<ElemType> *temp = head->next;

    while(temp)
    {
        length ++;
        temp = temp->next;
    }

    return length;
}

/*
    description:judge whether the linklist is empty or not
*/
template<class ElemType>
bool LinkList<ElemType>::ListEmpty() const
{
    if(head->next)
    {
        return false;
    }
    else
    {
        return true;
    }
}

/*
    description:insert the element at the start of the linklist
*/
template<class ElemType>
bool LinkList<ElemType>::InsFirst(ElemType e)
{
    //create the new LinkNode
    LinkNode<ElemType> *temp = new LinkNode<ElemType>(e);
    if(!temp)
    {
        return false;
    }
    temp->next = head->next;
    head->next = temp;
    cout<<endl;
    return true;
}

/*
    description:return the value of the element whose index is pos
*/
template<class ElemType>
ElemType LinkList<ElemType>::GetElem(int pos)
{
    //get the index of the element which is previous to the element
    LinkNode<ElemType> *temp = head;
    int index = 0;
    while(temp && index < pos)
    {
        temp = temp->next;
        index ++;
    }

    //judge whether you have found the correct idnex
    if(!temp || index < pos-1)
    {
        cout<<"the index of the element you want is wrong"<<endl;
        return;
    }

    //reutrn the target
    return temp->data;
}

/*
    description:return the value of the element whose index is pos
*/
template<class ElemType>
bool LinkList<ElemType>::ListInsert(int pos,ElemType e)
{
    //get the index of the element which is previous to the element
    LinkNode<ElemType> *temp = head;
    int index = 0;
    while(temp && index < pos - 1)
    {
        temp = temp->next;
        index ++;
    }

    //judge whether you have found the correct idnex
    if(!temp || index < pos-1)
    {
        cout<<"the index of the element you want is wrong"<<endl;
        return false;
    }

    //create the new linknode and insert it in the suitable position
    LinkNode<ElemType> *temp2 = new LinkNode<ElemType>();
    temp2->data = e;
    temp2->next = temp->next;
    temp->next = temp2;
}

/*
    description:delete the first element of the linklist and return it to the e
*/
template<class ElemType>
bool LinkList<ElemType>::DelFirst(ElemType &e)
{
    LinkNode<ElemType> *temp = head->next;
    e = temp->data;
    if(!temp) return false;
    head->next = temp->next;
    delete temp;
    return true;
}

/*
    description:construct the linklist by inserting the element at the beginning of linklist
    parament:A is the pointer of the array of the ElemType
             n is the length of the array

*/
template<class ElemType>
void LinkList<ElemType>::CreateList_Head(int n,ElemType *A)
{
    ElemType *temp = NULL;

    //traverse the whole linklist and create the relevent element to insert at the start of the linklist
    for(int i = 0;i < n;i ++)
    {
        temp = A + i;
        InsFirst(*temp);
    }

}

/*
    description:construct the linklist by inserting the element at the end of linklist
    parament:A is the pointer of the array of the ElemType
             n is the length of the array

*/
template<class ElemType>
void LinkList<ElemType>::CreateList_Tail(int n,ElemType *A)
{
    LinkNode<ElemType> *temp = head;

    //get the element at the end of the linklist
    int length = ListLength(),cont = 0;
    while(temp && cont < length)
    {
        temp = temp->next;
        cont++;
    }
    LinkNode<ElemType> *temp2 = temp;

    //maybe some special situation may happen .in fact the possibility is very low
    if(!temp || cont<length)
    {
        return;
    }

    //traverse the whole linklist and create the relevent element to insert at the start of the linklist
    for(int i = 0;i < n;i ++)
    {
        temp->next = new LinkNode<ElemType>(*(A+i));
        temp = temp->next;
    }
}

/*
    descrition:delete the element whose index is pos
*/
template<class ElemType>
ElemType LinkList<ElemType>::ListDelete(int pos)
{
    //find the element previous to the target
    LinkNode<ElemType> *temp = head;
    int i = 0;
    while(temp && i < pos - 1)
    {
        temp = temp->next;
        i ++;
    }

    //judge whether find
    if(!temp || i < pos - 1)
    {
        cout<<"the position you input is invalid"<<endl;
        return ElemType();
    }

    //delete the revelent
    LinkNode<ElemType> *temp2 = NULL;
    temp2 = temp->next;
    temp->next = temp2->next;
    ElemType result = temp2->data;
    delete temp2;
    return result;

}


/*
    description:return the pointer which point to eligible element
    parament: ElemType &e :the target element you want to search
              pos:the pointer which point to LinkNode
*/
template<class ElemType>
bool LinkList<ElemType>::LocateElem(const ElemType &e, LinkNode<ElemType> *pos)
{
    //cout<<"LOCATEELEM"<<endl;

    //traverse the whole linklist
    LinkNode<ElemType> *temp = head;
    while(temp)
    {
        if(temp->data == e)
        {
            pos->data = temp->data;
            pos->next = temp->next;

            return true;
        }
        temp = temp->next;
    }

    //dont find the revelent element
    return false;
}

/*
    description:return the value which is previous to the element you input
    parameters:cur_e :the current element you input
               &pri_e:the reference to the element privious to the current element
*/
template<class ElemType>
bool LinkList<ElemType>::PriorElem(ElemType cur_e, ElemType &pri_e)
{
    //traverse the whole linklist
    LinkNode<ElemType> *temp = head;
    while(temp->next)
    {
        if(temp->next->data == cur_e)
        {
            pri_e = temp->data;
            return true;
        }
        temp = temp->next;
    }

    //dont find the revelent element
    return false;
}

/*
    description:return pointer which is next to the element whose value is e
    parameters:p:the pointer pointing to the element next to the target
               e:the value of the target element
*/
template<class ElemType>
bool LinkList<ElemType>::NextElem(LinkNode<ElemType> *p, ElemType &e)
{
    //cout<<"NEXTELEMT"<<endl;
    //find the eligible element and return the index
    if(LocateElem(e,p))
    {
        p = p->next;
        return true;
    }
    else
    {
        return false;
    }

}

/*
    description:traverse the whole linklist
*/
template<class ElemType>
bool LinkList<ElemType>::ListTraverse()const
{
    //caution£ºyou have refer the index of the next element of the first
	//so,you should judge if the element does exist

	if(ListEmpty())
    {
        return false;
    }
    LinkNode<ElemType> *temp = head->next;
    while(temp)
    {
        cout<<temp->data<<" ";
        temp = temp->next;
    }
    cout<<endl;
}

template<class ElemType>
void inputLinkList(LinkList<ElemType> &A)
{
    int size;
    cin>>size;
    ElemType temp[size];
    for(int i = 0; i < size;i ++)
    {
        cin>>temp[i];
    }
    A.CreateList_Tail(size,temp);
}

template<class ElemType>
LinkNode<ElemType> *LinkList<ElemType>::getTail()
{
    LinkNode<ElemType> *temp = head;
    while(temp->next)
    {
        temp = temp->next;
    }
    return temp;
}

template<class ElemType>
void Lk_Reverse( LinkList<ElemType> &A )
{
    A.ListTraverse();
    cout<<endl;
    LinkNode<ElemType> *finalNode = A.getTail();
    LinkNode<ElemType> *endTrue = finalNode;
    LinkNode<ElemType> *temp = NULL;
    LinkNode<ElemType> *temp2 = NULL;


    while(A.GetHead()->next != finalNode)
    {
        //get the point of the first node
        temp = A.GetHead()->next;
        //change the first node
        A.GetHead()->next = temp->next;
        //move temp after the finalNode not endNode
        temp2 = finalNode->next;
        finalNode->next = temp;
        temp->next = temp2;


    }

    A.ListTraverse();
}

int main12()
{
    LinkList<string> A;
    inputLinkList(A);
    Lk_Reverse(A);

    return 0;
}

错误现场

  • 异常如下,我重读了一遍题目,并且尝试着去找了一下顺序表的相关的题目,并没有找到任何的样例,盲猜尝试了一下字符串型,发现通过了

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

分析与总结

  • 盲目的想了好多方法,虽然没有一次性解决,但是仍旧在以往思索的基础上找到了一个好的方法

如有疑问或者不妥之处,请在评论区留言,如果很急,请加扣扣651378276,希望我能解答

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页