順序表鏈表實現(xiàn)

項目相關(guān)代碼在碼云托管

#include"list.h"
#include<iostream>
using namespace std;
list::list()
{
    m_pList=new Node;
    m_pList->data=0;
    m_pList->next=NULL;
    m_pLength=0;
}

bool list::IsEmpty()
{
    if(m_pLength==0)
        return true;
    return false;
}
int list::listLength()
{
    return m_pLength;
}
void list::ClearList()
{
    Node *currentNode=m_pList->next;
    while(currentNode!=NULL)
    {
        Node *temp=currentNode->next;
        delete currentNode;
        currentNode=temp;
    }
    m_pList->next=NULL;
}
list::~list()
{
    ClearList();
    delete m_pList;
    m_pList=NULL;
}
bool list::ListInsertHead(Node *pNode)
{
    Node *temp=new Node;
    if(temp==NULL) return false;
    temp->data=pNode->data;
    temp->next=m_pList->next;
    m_pList->next=temp;
    m_pLength++;
    return true;
}
bool list::ListInsertTail(Node *pNode)
{
    Node* temp=new Node;
    if(temp==NULL)
        return false;
    temp->data=pNode->data;
    temp->next=NULL;
    Node* currentNode=m_pList;
    while (currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
    }
    currentNode->next=temp;
    m_pLength++;
    return true;
}
bool list::ListInsert(int i,Node* pNode)
{
    if(i<0||i>m_pLength)
        return false;
    Node* temp=new Node;
    if(temp==NULL)
        return false;
    temp->data=pNode->data;
    Node* currentNode=m_pList;
    for (int k = 0; k <i; k++)
    {
        currentNode=currentNode->next;
    }
    temp->next=currentNode->next;
    currentNode->next=temp;
    m_pLength++;
    return true;

}
bool list::ListDelte(int i,Node* pNode)
{
    if(i<0||i>=m_pLength)
        return false;
    Node* currentNode=m_pList;
    for (int k = 0; k <i; k++)
    {
        currentNode=currentNode->next;
    }
    Node* temp=currentNode->next;
    currentNode->next=currentNode->next->next;
    pNode->data=temp->data;
    delete temp;
    temp=NULL;
    m_pLength--;
    return true;
}
bool list::GetElem(int i,Node* pNode)
{
    if(i<0||i>=m_pLength)
        return false;
    Node* currentNode=m_pList;
    for (int k = 0; k <=i; k++)
    {
        currentNode=currentNode->next;
    }
    pNode->data=currentNode->data;
    return true;
}
int list::LocateElem(Node* pNode)
{
    Node* currentNode=m_pList->next;
    int count=0;
    while (currentNode!=NULL)
    {
        if(currentNode->data==pNode->data)
            return count;
        count++;
        currentNode=currentNode->next;

    }
    return -1;
}
bool list::PriorElem(Node* currentNode,Node* preNode)
{
    Node* temp=m_pList;
    while (temp->next!=NULL)
    {
        if(temp->next->data==currentNode->data)
        {
            if(temp==m_pList)
                return false;
            preNode->data=temp->data;
            return true;
        }
        temp=temp->next;
    }
    return false;
}

bool list::NextElem(Node* currentNode,Node* nextNode)
{
    Node* temp=m_pList->next;
    while (temp!=NULL)
    {
        if(temp->data==currentNode->data)
        {
            if(temp->next==NULL) return false;
            nextNode->data=temp->next->data;
            return true;
        }
        temp=temp->next;
    }
    return false;
}
void list::ListTraverse()
{
    Node* currentNode=m_pList;
    while (currentNode->next!=NULL)
    {
        currentNode=currentNode->next;
        currentNode->printNode();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蒸痹,一起剝皮案震驚了整個濱河市历筝,隨后出現(xiàn)的幾起案子肴敛,更是在濱河造成了極大的恐慌恰矩,老刑警劉巖浪谴,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異雕什,居然都是意外死亡裕照,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進(jìn)店門平匈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來框沟,“玉大人,你說我怎么就攤上這事增炭∪淘铮” “怎么了?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵隙姿,是天一觀的道長梅垄。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么队丝? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任靡馁,我火速辦了婚禮,結(jié)果婚禮上机久,老公的妹妹穿的比我還像新娘臭墨。我一直安慰自己,他們只是感情好膘盖,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布胧弛。 她就那樣靜靜地躺著,像睡著了一般侠畔。 火紅的嫁衣襯著肌膚如雪结缚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天软棺,我揣著相機(jī)與錄音红竭,去河邊找鬼。 笑死喘落,一個胖子當(dāng)著我的面吹牛茵宪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瘦棋,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼眉厨,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了兽狭?” 一聲冷哼從身側(cè)響起憾股,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎箕慧,沒想到半個月后服球,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡颠焦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年斩熊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伐庭。...
    茶點(diǎn)故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡粉渠,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出圾另,到底是詐尸還是另有隱情霸株,我是刑警寧澤,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布集乔,位于F島的核電站去件,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜尤溜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一倔叼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宫莱,春花似錦丈攒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绝葡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間腹鹉,已是汗流浹背藏畅。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留功咒,地道東北人愉阎。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像力奋,于是被迫代替她去往敵國和親榜旦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評論 2 361