仅仅实现了基本的链表操作,如创建、查找、删除、排序等。
//头文件/*there is no head node exist * */#includeusing namespace std;typedef struct Node{ int value; struct Node* next;}Node,*ListNode;bool isEmpty(ListNode );//judge the list statusbool createList(ListNode* ,const int=10);//create new listbool printList(ListNode ,ostream& output=cout);//print the listint size(ListNode);bool insert(ListNode* ,int ,int);//insert in the lastbool insertTail(ListNode* ,int );bool find(ListNode ,int ,int& );//find a node in listbool deleteNode(ListNode* ,int ,int& );bool deleteNode(ListNode* ,int ,bool flag=true);bool getData(ListNode ,int ,int& );bool deleteList(ListNode* );//delete the list entireListNode sort(ListNode );//sort,and return a new listint fmin(ListNode );int fmax(ListNode );
//源文件#include#include "List.h"#include bool isEmpty(ListNode pHead){ if(!pHead) return true; return false;}bool createList(ListNode* pHead,const int N){ if(N<0) return false; if(N==0) return true; static size_t randSeed=100; ListNode p1=new Node(); if(!p1)//apply for memory failed return false; srand(time(0)+randSeed); p1->value=rand()%100+1; *pHead=p1; for(int i=0;i next=NULL; return false; } pNew->value=rand()%100+1; p1->next=pNew; p1=pNew; randSeed+=rand(); } p1->next=NULL;//the last node pointer to NULL return true;}bool printList(ListNode pHead,ostream& output){ if(!pHead) return false; ListNode p=pHead; while(p) { output< value<<" "; p=p->next; } return true; }int size(ListNode pHead){ ListNode p=pHead; int length(0); while(p) { length++; p=p->next; } return length;}bool insert(ListNode* pHead,int pos,int data){//insert before the pos node if(pHead==NULL) return false; ListNode p,k; k=p=*pHead; int j=1; //pos should more than 0 while(p && j next; ++j; } //if the list is empty,insert will be refused if(!p || j>pos)//if pos<1,return; return false; ListNode q; q=new Node(); q->value=data; q->next=p; if(k==p)//在第一个节点前插入 { *pHead=q; } else k->next=q; return true;}bool insertTail(ListNode* pHead,int data){ if(pHead==NULL) return false; ListNode p=*pHead; if(!p) { ListNode q=new Node(); q->value=data; q->next=NULL; *pHead=q; return true; } while(p->next) { p=p->next; } ListNode q=new Node(); q->value=data; q->next=NULL; p->next=q; return true;}bool deleteNode(ListNode* pHead,int pos,int& value){ if(pHead==NULL || *pHead==NULL) return false; ListNode p,q; p=q=*pHead; int j=1; while(p && j next; j++; } if(!p || j>pos) return false; if(p==q) { value=p->value; *pHead=p->next; delete p; p=NULL; return true; } else { value=p->value; q->next=p->next; delete p; p=NULL; return true; }}//delete the node where value=data//if flag=true,delete all node where value=data,or delete the first onebool deleteNode(ListNode* pHead,int data,bool flag){ if(pHead==NULL || *pHead==NULL) return false; ListNode p,q; p=q=*pHead; if(flag) { while(p) { if(p->value==data) { if(p==q) { *pHead=p->next; delete p; p=q=*pHead; } else { q->next=p->next; delete p; p=q->next; } } q=p; p=p->next; } } else { while(p && p->value!=data) { q=p; p=p->next; } if(!p) return false; if(p==q) { *pHead=p->next; delete p; p=NULL; } else { q->next=p->next; delete p; p=NULL; } } return true;}//find data in list,pos is the position in listbool find(ListNode pHead,int data,int& pos){ ListNode p=pHead; pos=0; while(p) { pos++; if(p->value==data) { return true; } p=p->next; } return false;}//read the value in k nodebool getData(ListNode pHead,int k,int& data){ if(pHead==NULL) return false; if(k<=0 || k>size(pHead)) return false; int i=1; ListNode p=pHead; while(p && i next; } if(!p) return false; data=p->value; return true;}bool deleteList(ListNode* pHead){ if(pHead==NULL) return false; ListNode p=*pHead; while(p) { ListNode q=p->next; delete p; p=q; } *pHead=NULL; return true;}//select sortListNode sort(ListNode pHead){ ListNode pHeadNew; pHeadNew=NULL; if(!pHead) return pHeadNew; while(pHead) { int min=fmin(pHead); deleteNode(&pHead,min,false); insertTail(&pHeadNew,min); } return pHeadNew;}int fmax(ListNode pHead){ int max=0x80000000; ListNode p=pHead; while(p) { if(p->value > max) { max=p->value; } p=p->next; } return max; }int fmin(ListNode pHead){ int min=0x7FFFFFFF; ListNode p=pHead; while(p) { if(p->value < min) { min=p->value; } p=p->next; } return min; }
//测试程序#include#include "List.h"using namespace std;const int N=10;int main(){ ListNode pHead; pHead=NULL; if(!createList(&pHead,N)) cout<<"create list failed!"<