標籤:成功 增刪改 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語言動態鏈表資料結構