C++链表的简单应用

对C++链表的构造,析构,插入,删除,查找进行了简单的应用

#include<iostream>
#define MAXSIZE 15
using namespace std;
struct Node
{
    int data;
    Node *next;
};

class LinkList
{
    public:
        LinkList();
        LinkList(int a[],int n);
        ~LinkList();
        int Length();
        int Get(int index);
        int Locate(int i);
        void Insert(int i,int index);
        int Delete(int index);
        void PrintList();
    private:
        Node *first;
};

LinkList::LinkList()
{
    first=new Node;
    first->next=NULL;
}

LinkList::LinkList(int a[],int n)
{
    if(n>MAXSIZE)
        throw "参数非法";
    first=new Node;
    first->next=NULL;
    Node *s;
    for(int i=0;i<n;i++)
    {
        s=new Node;
        s->data=a[i];
        s->next=first->next;
        first->next=s;
    }
}

LinkList::~LinkList()
{
    Node *q;
    while(first!=NULL)
    {
        q=first;
        first=first->next;
        delete q;
    }
}

int LinkList::Length()
{
    Node *p;
    p=first->next;
    int count=0;
    while(p!=NULL)
    {
        p=p->next;
        count++;
    }
    return count;
}

int LinkList::Get(int index)
{
    Node *p;
    p=first->next;
    int count=1;
    while(p!=NULL&&count<index)
    {
        p=p->next;
        count++;
    }
    if(p==NULL)
        throw "位置错误";
    else
        return p->data;
}

int LinkList::Locate(int i)
{
    Node *p;
    p=first->next;
    int count=1;
    while(p!=NULL)
    {
        if(p->data==i)
            return count;
        p=p->next;
        count++;
    }
    return 0;
}
void LinkList::Insert(int index,int i)
{
    Node *p;
    Node *s;
    p=first;
    int count=0;
    while(p!=NULL&&count<index-1)
    {
        p=p->next;
        count++;
    }
    if(p==NULL)
    {
        throw "位置输入错误!";
    }else{
        s=new Node;
        s->data=i;
        s->next=p->next;
        p->next=s;
    }
}

int LinkList::Delete(int index)
{
    Node *p;
    p=first;
    int count=0;
    int temp=0;
    while(p!=NULL&&count<index-1)
    {
        p=p->next;
        count++;
    }
    if(p==NULL||p->next==NULL)
        throw "输入位置错误";
    else{
        Node *q;
        q=p->next;
        temp=q->data;
        p->next=q->next;
        delete q;
        return temp;
    }
}

void LinkList::PrintList()
{
    Node *p;
    p=first->next;
    while(p!=NULL)
    {
        cout<<p->data<<"   ";
        p=p->next;
    }
    cout<<endl;
}

int main(){
    cout<<"初始化链表"<<endl;
    cout<<"---------------------------------------------------------------"<<endl;
    int a[10]={1,2,3,6,5,4,2,9,6,3};
    LinkList list(a,10);
    while(1){
        cout<<"请选择您要进行的操作:"<<endl;
        cout<<"1:打印全部元素;"<<endl;
        cout<<"2:查找第i个元素;"<<endl;
        cout<<"3:查找指定元素;"<<endl;
        cout<<"4:在指定位置插入元素;"<<endl;
        cout<<"5:删除第i个元素;"<<endl;
        cout<<"6:计算链表的长度;"<<endl;
        cout<<"7:退出."<<endl;
        cout<<"---------------------------------------------------------------"<<endl;
        int operation;
        cin>>operation;
        if(operation==7)
            break;
        else if(operation==1){
            list.PrintList();
            cout<<"---------------------------------------------------------------"<<endl;
        }else if(operation==2){
            int index=-1;
            cout<<"请输入您要查询的位置:"<<endl;
            cin>>index;
            cout<<"第"<<index<<"个元素为"<<list.Get(index)<<endl;
            cout<<"---------------------------------------------------------------"<<endl;
        }else if(operation==3){
            int i=0;
            cout<<"请输入您要查找的值:"<<endl;
            cin>>i;
            int index=0;
            index=list.Locate(i);
            if(index==0)
                cout<<"查找失败!"<<endl;
            else
                cout<<i<<"的位置为:"<<index<<endl;
            cout<<"---------------------------------------------------------------"<<endl;
        }else if(operation==4){
            if(list.Length()>=MAXSIZE)
            {
                cout<<"链表已满!"<<endl;
                break;
            }
            int index=-1;
            int i=0;
            cout<<"请输入插入的位置和插入的数字:"<<endl;
            cin>>index;
            cin>>i;
            list.Insert(index,i);
            cout<<"---------------------------------------------------------------"<<endl;
        }else if(operation==5){
            int index=-1;
            cout<<"请输入删除的值:"<<endl;
            cin>>index;
            int data=0;
            data=list.Delete(index);
            cout<<"删除的值为:"<<data<<endl;
            cout<<"---------------------------------------------------------------"<<endl;
        }else if(operation==6){
            cout<<"链表的长度为:"<<list.Length()<<endl;
            cout<<"---------------------------------------------------------------"<<endl;
        }
    }
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页