c++中对单链表操作代码

来源:互联网 发布:数据恢复电脑版 编辑:程序博客网 时间:2024/06/06 05:25

      最近在看c++语法、c++基础之类的东西,未雨绸缪一下,为下一步作打算,今天看了一下单链表功能,就参照其他文章及方法,自己在机器上敲入,并整理、修正。明天如果有时间,则看看更复杂些的链表。

      如下:

      链表类的声明,我单独放到mlist.h

    

#include <iostream>using namespace std;typedef struct node{int num;struct node* pNext;}NODE,*PNODE;class MyList{private:PNODE pHead;public:MyList():pHead(NULL){};~MyList();void Init();void Add(int num);//append to link tailint InsertAt(int num, int position);int DeleteAt(int position);int Find(int num);void Travel();void Sort();int GetNodeCnt();};

    实现类,放到mlist.cpp中

#include "stdafx.h"#include "mlist.h"void MyList::Init(){int nVal;char aRet;PNODE pTail, pNew;do {cout <<"Please insert a node value:"<<endl;cin>>nVal;if ( pHead == NULL ){pHead = new NODE;pHead->num = nVal;pHead->pNext = NULL;pTail = pHead;}else{if ( pTail == NULL )pTail = pHead;while ( pTail->pNext != NULL ){pTail = pTail->pNext;}pNew = new NODE;pNew->num = nVal;pNew->pNext = NULL;pTail->pNext = pNew;pTail = pTail->pNext;}cout <<"continue to insert?(Y or N)"<<endl;cin>>aRet;} while ( aRet == 'Y' || aRet =='y' );}void MyList::Add(int num){PNODE pTail, pNew;if ( pHead == NULL ){pHead = new NODE;pHead->num = num;pHead->pNext = NULL;}else{pTail = pHead;while ( pTail->pNext != NULL ){pTail = pTail->pNext;}pNew = new NODE;pNew->num = num;pNew->pNext = NULL;pTail->pNext = pNew;}}int MyList::DeleteAt(int position){PNODE p1, p2, pTmp;if ( pHead == NULL )return -1;if ( position < 0 || position > GetNodeCnt() - 1  )return -1;if ( GetNodeCnt() == 1) //only one node, delete it{delete pHead;pHead = NULL;return 0;}if ( position == 0 )//delete old head{pTmp = pHead;pHead = pTmp->pNext;pTmp->pNext = NULL;delete pTmp;pTmp = NULL;return 0;}if ( position == GetNodeCnt() - 1){PNODE p,pTmp;p = pHead;while ( p->pNext->pNext != NULL ){p = p->pNext;}pTmp = p->pNext;p->pNext = NULL;delete pTmp;return 0;}p1 = pHead;int i = 0;while ( i<position - 1 ){p1 = p1->pNext;i++;}pTmp = p1->pNext;p2 = p1->pNext->pNext;p1->pNext = p2;delete pTmp;return 0;}int MyList::InsertAt(int num, int position){PNODE p1, p2, pNew, pTmp;if ( pHead == NULL )return -1;if ( position < 0 || position > GetNodeCnt() )return -1;if ( position == 0 ){pTmp = pHead;pNew = new NODE;pNew->num = num;pNew->pNext = pTmp;pHead = pNew;//pNew->pNext = pTmp;return 0;}p1 = pHead;int i = 0;while ( i < position - 1 ){p1 = p1->pNext;i++;}p2 = p1->pNext;pNew = new NODE;pNew->num = num;pNew->pNext = p2;p1->pNext = pNew;//pNew->pNext = p2;return 0;}int MyList::Find(int num){PNODE p;p = pHead;int i = 0;while ( p != NULL ){if ( p->num == num )return i;p = p->pNext;i++;}return -1;}void MyList::Travel(){PNODE p;p = pHead;while ( p!= NULL ){cout << p->num << "  ";p = p->pNext;}cout << endl;}//bubble sortvoid MyList::Sort(){int n = GetNodeCnt();PNODE p1, p2;for ( int i=0; i< n -1; i++){p1 = pHead;for ( int j=0; j < n-1-i; j++){p2 = p1->pNext;if ( p1->num > p2->num){int k = p1->num;p1->num = p2->num;p2->num = k;}p1 = p1->pNext;}}}int MyList::GetNodeCnt(){int nCnt=0;PNODE pTmp = pHead;while ( pTmp != NULL ){nCnt++;pTmp = pTmp->pNext;}return nCnt;}//delete all listMyList::~MyList(){while ( pHead != NULL ){PNODE pTmp = pHead->pNext;delete pHead;pHead = pTmp;}}

引用此类,则在工程的含有main函数的类测试,我自己测试了一遍,没发现什么问题,希望对您学习有用。

#include "mlist.h"int main(){MyList list;  for(int i=10; i>0; i--)  {  list.Add(i);  }  //list.Init();list.Travel();  list.DeleteAt(9);list.InsertAt(500,9);//list.Sort();  list.Travel();  }