C語言動態鏈表資料結構

來源:互聯網
上載者:User

標籤:成功   增刪改   lse   變數   printf   col   new   lis   typedef   

鏈表的操作增刪改查

typedef int DATA;struct SNode{    DATA data;    SNode* pNext;};SNode* g_head=NULL;//全域變數//從頭部添加void AddHead(DATA nNum){    SNode* p = (SNode*)malloc(sizeof(SNode));//C語言的方式    //SNode* p = new SNode;//C++ 方式    p->data = nNum;    p->pNext = g_pHead;    g_pHead = p;}//從尾部添加void AddTail(DATA nNum){    SNode* p = (SNode*)malloc(sizeof(SNode));//C語言的方式    pNew->data = nNum;    pNew->pNext = NULL;    if (!g_pHead)    {        g_pHead = pNew;        return;    }    SNode* p = g_pHead;    SNode* p1 = NULL;    while (p)    {        p1 = p;        p= p->pNext;    }    //跳出迴圈時,p1記錄的時最後一個節點,讓最後一個節點的pNext指向新建立的節點    p = p1;    p->pNext = pNew;    //另一種寫法    //while(p->pNext != NULL)//迴圈遍曆,當下一個節點為空白說明到尾部了    //    p=p->pNext;        //p->pNext = pNew;}//找到,返回找節點位置;失敗返回-1int FindNodeIndex(DATA nNum){    SNode* p = g_pHead;    int i =0;    while(p)    {        if (p->data == nNum)        {            return i;        }                    p = p->pNext;        ++i;    }    return -1;}//刪除成功返回-1;失敗返回0int DeleteNode(DATA nNum){    SNode* p = g_pHead;    SNode* p1 = NULL;//記錄前一個節點    if (p == NULL)//當前鏈表為空白    {        return -1;    }    //前端節點沒有前一個節點,要特殊處理    if (p->data == nNum)    {        g_pHead = p->pNext;        delete p;        return 0;    }    while(p)    {        if (p->data == nNum)        {            //刪除當前節點時,讓當前節點的前一個節點的pNext指向當前節點的後一個節點            p1->pNext = p->pNext;            delete p;            return 0;        }        p1= p;//記錄前節點        p = p->pNext;    }    return -1;}//修改指定節點的值void ModifyNode(DATA oldNum,DATA newNum){    SNode* p = g_pHead;    while(p)//    {        if (p->data == oldNum)        {            p->data = newNum;        }            p = p->pNext;    }}//列印所有節點void PrintALL(){    SNode* p = g_pHead;    if (p == NULL)    {        printf("當前鏈表為空白\n");        return;    }    while(p)//    {        printf("%d\n",p->data);        p = p->pNext;    }}//成功返回0;失敗返回-1int FindNode(DATA nNum){    SNode* p = g_pHead;    while (p)    {        if (p->data == nNum)        {            return 0;        }        p=p->pNext;    }    return -1;}//在某個節點之後插入新節點;成功返回0;失敗返回-1int InsertNode(DATA npos,DATA nNum){    SNode* pNew = new SNode;//C++ 方式    pNew->data = nNum;    pNew->pNext = NULL;       SNode* p = g_pHead;    //if (p->data == npos)//頭結點    //{    //    pNew->pNext=p->pNext;//前端節點指向的下一個節點指標放到新節點的pNext    //    p->pNext=pNew;//前端節點pNext指向新節點    //    return 0;    //}    while(p)    {        if (p->data == npos)        {            pNew->pNext = p->pNext;            p->pNext=pNew;            return 0;        }            p = p->pNext;    }    return -1;}void DeleteAll(){    SNode* p = g_pHead;    SNode* p1 = NULL;    while(p)    {        p1=p;        p= p->pNext;        delete p1;    }    g_pHead = NULL;}int main(){    DeleteNode(999);//鏈表為空白的時候,刪除要加判斷    lAddHead(1);    lAddHead(2);    AddHead(3);    puts("修改前");    PrintALL();    ModifyNode(2,-88);    puts("修改後");    lPrintALL();    int i = FindNodeIndex(-88);    if (i >= 0)    {        cout << "第" << i+1 << "個節點找到" <<endl;    }    //刪除節點    i = DeleteNode(3);//頭部刪除要特殊處理    if (i == 1)    {        cout << "刪除節點成功!" << endl;    }    else    {        cout << "刪除節點失敗!" << endl;    }    PrintALL();    puts("尾部添加");    AddTail(4);    PrintALL();    //修改節點    ModifyNode(-88,0);    puts("插入節點3");    //插入節點    i = list.InsertNode(4,3);    list.PrintALL();    puts("清空鏈表");    DeleteAll();    PrintALL();  return 0;}

 

C語言動態鏈表資料結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.