Data structure and algorithm: linear table ADT implementation

Assuming that the data element type of the linear table ADT is a positive integer, a single-chain storage structure with a lead node is adopted. Most of the code for linear table ADT implementation has been given, please add the two member functions insert and reverse of the class. Note: You only need to submit the function code that needs to be added, other codes cannot be rewritten and modified by themselves.
Insert function: Insert an element into a linear table with element values ​​in order from small to large, and still maintain order.
Reverse function: to achieve the inversion of the linear table elements, that is, to reverse the order of the data elements in the linear table.
When inputting linear table elements, use endTag as the end tag.
For example, input: 3 8 7 2 4 9 1 6 5 0,
then output: 9 8 7 6 5 4 3 2 1 The
preset code is as follows: (The /* */ part is the insert and reverse functions to be added)

#include< iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType; //Data element type class List; //Foresight definition, otherwise friends cannot be defined//Node class definition class LinkNode {friend
class List;
private:
LinkNode *link;
ElemType data; public:
LinkNode (LinkNode *ptr = NULL) {link=ptr;}
LinkNode(const ElemType & item, LinkNode *ptr = NULL){ data=item;link=ptr;}
~LinkNode(){}; }; //Single linked list class definition class List {private:
LinkNode *first; //Pointer to the head node of the linked list public:
List (ElemType x) {first = new LinkNode (x);} //
Take the lead ~List (){ MakeEmpty();} //Destructor
void MakeEmpty (); //
Insert the linear table void insert(ElemType val); //Insert the element in the ordered linear table val
void reverse(); //Inversion of linear table
void output(); //Linear table output);
void List:: MakeEmpty () {LinkNode *q; while (first->link != NULL) {q =first->link; //Point to don’t take down the knot Click
first->link = q->link;//Remove the node from the chain
delete q; //Release the removed node
}
};
void List ::output () { LinkNode p=first->link; while(p!=NULL) {if(p==first->link) cout<< p->data; else cout<<" "<< p->data; p=p->link;} cout<< endl;} / Please write the insert member function Please write the reverse member function */ int main() { List list(0); ElemType endTag=0; ElemType val; //The following inserts in order by continuously reading in elements In the singly linked list, build an ordered singly linked list from small to large cin>>val; while(val!=endTag) {list.insert(val);//Insert an element in the ordered list















cin>>val;
}
list.reverse (); //线性表倒置
cout<<“The result is:”<<endl;
list.output ();
return 0; }

void List::insert(ElemType val)
{
    
    
    LinkNode *p1,*t;
    t=new LinkNode(val);
    if(first->link==NULL)
    {
    
    
        first->link=t;
        return;
    }
    if(first->link->data>=val)
    {
    
    
        t->link=first->link;
        first->link=t;
        return;
    }
    p1=first;
    while(p1->link!=NULL&&p1->data<val)
    {
    
    
        if(p1->link->data<=val)p1=p1->link;
        else break;
    }
    t->link=p1->link;
    p1->link=t;
}
void List::reverse()//严格上来说应该采用链表倒转的方法,但这个题中原来的元素是升序的,顺序反转后一定是降序的,因此只需要实现降序排序就可以
{
    
    
    LinkNode *p,*q;
    p=first->link;
    ElemType t;
    while(p!=NULL)
    {
    
    
        q=p->link;
        while(q!=NULL)
        {
    
    
            if(p->data<q->data)
            {
    
    
                t=p->data;
                p->data=q->data;
                q->data=t;
            }
            q=q->link;
        }
        p=p->link;
    }
}

The correct way to reverse the linked list: Header delete + Header insert

void List::reverse()
{
    
    
    LinkNode*newfirst=new LinkNode();//反转后的链表的头结点
    LinkNode*temp;
    first=first->link;
    while(first!=NULL)
    {
    
    
        temp=first;
        first=first->link;//原链表头删
        temp->link=newfirst->link;
        newfirst->link=temp;//新链表头插
    }
    first=newfirst;//一定别忘记将反转后链表的头结点指针赋给原来的头结点指针
}

Guess you like

Origin blog.csdn.net/upc122/article/details/105838599