最簡單的c++鏈表

#include <iostream>
#include   <stdlib.h>
using namespace std;
class AD
{
public:
    int num;
    bool use;
    AD* next;
};
typedef AD* point;
void create(point &pHead)
{
    pHead=new AD;
    pHead->num=-1;
    pHead->use=false;
    pHead->next=NULL;
    cout<<"建立成功"<<endl;
    return;
}
void insert(point pHead)
{
    point pCur=pHead;

    while(pCur->next!=NULL)
    {
        pCur=pCur->next;
    }
    point pPro=new AD;

    cout<<"請輸入要插入的值"<<endl;
    cin>>pPro->num;
    pPro->use=false;
    pPro->next=NULL;
    pCur->next=pPro;
    cout<<"插入成功"<<endl;
    return;
}
void print(point pHead)
{
    cout<<"隊列中的狀況:";
    int flg=0;
    while(pHead->next!=NULL)
    {
        flg=1;
        pHead=pHead->next;
        cout<<pHead->num<<" ";
    }
    if(!flg)
        cout<<"啥玩意兒也沒有!!";
    cout<<endl;
}
void delet(point pHead)
{
    cout<<"請輸入要刪除的值"<<endl;
    int rt;
    cin>>rt;
    point pCur=pHead;
    int flg=0;
    while(pCur->next!=NULL)
    {
        if(pCur->next->num==rt)
        {
            flg=1;
            pCur->next=pCur->next->next;
            cout<<"刪除成功!!"<<endl;

        }
        pCur=pCur->next;
    }
    if(!flg)
    {
        cout<<"要刪除的值不存在!!"<<endl;
    }
}

void instead(point pHead)
{
    cout<<"請輸入要替換的值"<<endl;
    int rt;
    cin>>rt;
    cout<<"請輸入要成替換的值"<<endl;
    int dd;
    cin>>dd;
    point pCur=pHead;
    int flg=0;
    while(pCur->next!=NULL)
    {
        if(pCur->next->num==rt)
        {
            flg=1;
            pCur->next->num=dd;
            cout<<"替換成功!!"<<endl;

        }
        pCur=pCur->next;
    }
    if(!flg)
    {
        cout<<"要替換的值不存在!!"<<endl;
    }
}
void destory(point pHead)
{
    point pCur=pHead->next;

    while(pHead->next!=NULL)
    {
        delete pHead;
        pHead =pCur;
        pCur=pCur->next;
    }
    cout<<"刪除鏈表成功!!"<<endl;
    return;
}
int main()
{
    point a;
    create(a);

    int op;
    while(1)
    {
        cout<<"請輸入要進行的操做:0打印,1插入,2刪除,3替換,4退出"<<endl;
        cin>>op;
        switch(op)
        {
        case 1:
            insert(a);
            break;
        case 2:
            delet(a);
            break;
        case 0:
            print(a);
            break;

        case 3:
            instead(a);
            break;
        case 4:
            destory(a);
            exit(0);
        }
    }






    return 0;
}
相關文章
相關標籤/搜索