动态链表类linearList

对类的要求:
定义一个由int型元素所构成的动态链表类LinearList,成员函数包括:
bool insert(int x, int pos); //在位置pos之后插入一个元素x;
//pos为0时,在第一个元素之前插入。
//操作成功时返回true,否则返回false。
bool remove(int &x, int pos); //删除位置pos处的元素。
//操作成功时返回true,否则返回false。
int element(int pos) const; //返回位置pos处的元素。
int search(int x) const; //查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0。
int length() const; //返回元素个数。
!!!编译环境 DevC++ 5.11 TDM-GCC 4.9.2 64-Bit Release
头文件 iquery.h

    #ifndef  _IQUERY_H
    #define _IQUERY_H 1
    using std::string;
    struct Node 
    {
        int element;
        Node *next;
    };
    class LinerList{
        private: 
            Node *head;
            int len ;
        public:
            LinerList(){len=0;head = NULL;}
            bool insert(int x,int pos);    //add a new element to any position 
            bool remove(int &x,int pos);   //remove an element
            int element(int pos) const;    //cout this element at positon pos
            int search(int x) const;       //find this element and return this position 
    };

#endif
//  类成员函数中const的使用
//  一般放在函数体后,形如:void   fun()   const;     
//    任何不会修改数据成员的函数都因该声明为const类型。

成员函数实现文件 iquery.cpp


#include <iostream>
#include "string"
#include "iquery.h"
#include "iomanip"
using namespace std; 
bool LinerList::insert(int x,int pos){
    if (pos<0){
        cout<<"请输入正确的pos值(需要>=0)"<<endl;
        return false;
    }else if (pos == 0){
        Node *p = new Node;
        p->element = x;
        if (head !=NULL){
            p->next  = head;
            head = p;
            p->next = NULL;
            len++;
            return true;
        }else{
        head = p;
        p->next=NULL;
        len++;
        return true;
        }

    }else if (pos>0&&pos<len){
        if (head !=NULL){
            Node *p   = new Node;
            p->element = x;
            Node *p1 = head,*p2=head;
            for (int i = 0;i<pos;i++){
                p2 = p1;
                p1 = p1->next;
            }
            p2->next = p;
            p->next = p1;
            len++;
               return true;
            }else{
            Node *p   = new Node;
            p->element = x;
            head = p;
            p->next=NULL;
            len++;
            return true;
        }

    }else {
        Node *p = new Node;
        p->element = x;
        Node *p1 = head;
        while (p1->next!=NULL){
            p1 = p1->next;
        }
        p1->next = p;
        p->next = NULL;
        len ++;
        return true;
    }
}


    bool LinerList::remove(int &x,int pos){
        if (pos <0||pos>=len){
            cout<<"请输入正确的pos值!"<<endl;
            return false;
        }else if (pos ==0){
            Node *p = head;
            x = p->element;
            head = p->next;
            len--;
            return true;
        }else if(pos>0&&pos<len-1){
            Node *p1 = head,*p2= head;
            for (int i = 0;i<pos;i++){
                p2 = p1;
                p1 = p1->next;
            }
            p2->next = NULL;
            x = p1->element;
            p1 = p1->next;
            p2->next = p1;
            len--;
            return true;
        }else {
            Node *p1 = head,*p2 = head;
            for (int i = 0;i<pos;i++){
                p2 = p1;
                p1 = p1->next;
            }
            p2->next = NULL;
            len --;
            return true;
        }
    }


int LinerList::element(int pos) const{

    if (pos>=len||pos<0){
        cout<<"请输入正确的pos值"<<endl;
        return -1;
    }else{
        Node *p1 = head;
        for (int i = 0;i<pos;i++){
            p1 = p1->next;
        }
        return p1->element;
    }
}


int LinerList::search(int x)const{
    Node *p = head;
    for (int i = 0;i<len;i++){
        if (p->element ==x){
            return i+1;
        }
        p = p->next;
    }
    return 0;
}

下面是使用范例 main.cpp

#include <iostream>
#include "iquery.h"
#include "iomanip"
using namespace std;
int main(int argc, char** argv) {
    int ele=0;
    char sel;
    int pos;
        LinerList list;
    do{
        cout<<"请输入element:"<<endl;
        cin>>ele;
        if(list.insert(ele,pos)){
            pos++;
        }else {
            cout<<"element加入不成功!!!"; 
        } 
        cout<<"继续?(Y/N)"<<endl;
        cin>>sel;
    }while(sel=='Y');
    //数据打印 
    for(int idx=0;idx<pos;idx++){
        ele=list.element(idx);
        cout<<ele<<"\t";
    }
    //delete 
//  if(list.remove(&pos,0)) {
//      ele=list.element(0);
//      cout<<endl<<ele<<"\t";
//  }
//  else cout<<"delete不成功"<<endl; 
//  
    ele=list.search(8);
    if(ele!=0){
        cout<<"element 8 位置为"<<ele;
    }else cout<<"该element 8 不存在.";
}

注:笔者使用prj文件自动链接代码。

猜你喜欢

转载自blog.csdn.net/qq_40724028/article/details/79874525