一个简单的链表结构

来源:互联网 发布:网络通讯设备 编辑:程序博客网 时间:2024/05/20 00:13

         仅仅实现了基本的链表操作,如创建、查找、删除、排序等。

//头文件/*there is no head node exist * */#include <iostream>using 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 <time.h>#include "List.h"#include <stdlib.h>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 failedreturn false;srand(time(0)+randSeed);p1->value=rand()%100+1;*pHead=p1;for(int i=0;i<N-1;++i){srand(time(0)+randSeed);ListNode pNew=new Node();if(!pNew){p1->next=NULL;return false;}pNew->value=rand()%100+1;p1->next=pNew;p1=pNew;randSeed+=rand();}p1->next=NULL;//the last node pointer to NULLreturn true;}bool printList(ListNode pHead,ostream& output){if(!pHead)return false;ListNode p=pHead;while(p){output<<p->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 nodeif(pHead==NULL)return false;ListNode p,k;k=p=*pHead;int j=1;//pos should more than 0while(p && j<pos){k=p;p=p->next;++j;}//if the list is empty,insert will be refusedif(!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;}elsek->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<pos){q=p;p=p->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<k){i++;p=p->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 <iostream>#include "List.h"using namespace std;const int N=10;int main(){ListNode pHead;pHead=NULL;if(!createList(&pHead,N))cout<<"create list failed!"<<endl;cout<<"The initial length:"<<size(pHead)<<endl;cout<<endl<<"print list:"<<endl;if(!isEmpty(pHead))printList(pHead);insert(&pHead,1,45);insert(&pHead,3,30);cout<<endl<<"print list:"<<endl;if(!isEmpty(pHead))printList(pHead);insertTail(&pHead,18);cout<<endl<<"print list:"<<endl;if(!isEmpty(pHead))printList(pHead);int valueToBeDeleted;deleteNode(&pHead,1,valueToBeDeleted);cout<<endl<<"print list:"<<endl;if(!isEmpty(pHead))printList(pHead);valueToBeDeleted=2;deleteNode(&pHead,valueToBeDeleted);cout<<endl<<"print list:"<<endl;if(!isEmpty(pHead))printList(pHead);int value;cout<<endl<<"find result:"<<find(pHead,18,value)<<endl;cout<<"position:"<<value<<endl;cout<<"read data:"<<getData(pHead,11,value)<<endl;cout<<"data:"<<value<<endl;cout<<"min:"<<fmin(pHead)<<endl;ListNode newList=sort(pHead);if(!isEmpty(newList))printList(newList);elsecout<<"list is empty..."<<endl;deleteList(&pHead);if(!isEmpty(pHead))printList(pHead);elsecout<<endl<<"list is empty..."<<endl;return 0;}


0 0