通用链表----系统程序员成长计划 作业(1)
View Code
1 // 通用双链表.cpp : 定义控制台应用程序的入口点。 2 // 3 /********************************************* 4 5 设计者:cslave 6 设计日期:2012.6.21 7 8 免责申明:本代码免费使用用,但是导致的任何问题, 9 本人概不负责。 10 11 通用双链表实现方法。 12 13 *********************************************/ 14 15 16 #include "stdafx.h" 17 #include <iostream> 18 using namespace std; 19 typedef struct _DListNode 20 { 21 struct _DListNode* prev; 22 struct _DListNode* next; 23 void* Data; 24 }DListNode; 25 26 27 template <typename ElemType> 28 DListNode* NewNode(ElemType* e) 29 { 30 DListNode* pNode=(DListNode*)malloc(sizeof(struct _DListNode)); 31 pNode->Data=e; 32 pNode->next=NULL; 33 pNode->prev=NULL; 34 return pNode; 35 } 36 37 template<typename ElemType> 38 void InsertNode(DListNode* Head,ElemType* e) 39 { 40 if(Head==NULL) 41 { 42 Head=NewNode(e); 43 return; 44 } 45 46 47 DListNode* NodeTmp=Head; 48 while(NodeTmp->next!=NULL) 49 { 50 NodeTmp=NodeTmp->next; 51 } 52 DListNode* pNodeTmp=NewNode(e); 53 pNodeTmp->next=NULL; 54 NodeTmp->next=pNodeTmp; 55 pNodeTmp->prev=NodeTmp; 56 return; 57 } 58 //删除第一个=e的值 59 template<typename ElemType> 60 DListNode* DeleteNode(DListNode* Head,ElemType* e) 61 { 62 if(Head==NULL) 63 { 64 cerr<<"List Was Empty!"<<endl; 65 return Head; 66 } 67 if(Head->Data==e) 68 { 69 70 71 Head->next->prev=NULL; 72 Head=Head->next; 73 return Head; 74 } 75 DListNode* NodeTmp=Head; 76 DListNode* NodePrev=NULL; 77 while( NodeTmp->Data!=e&&NodeTmp->next!=NULL) 78 { 79 NodePrev=NodeTmp; 80 NodeTmp=NodeTmp->next; 81 82 } 83 NodePrev->next=NodeTmp->next; 84 if(NodeTmp->next!=NULL) 85 NodeTmp->next->prev=NodePrev; 86 87 delete NodeTmp; 88 return Head; 89 } 90 91 92 template<typename ElemType> 93 int FindElem(DListNode* Head ,ElemType* e) 94 { 95 DListNode *pNode=Head; 96 if(Head==NULL) 97 return -1; 98 int Counter=0; 99 while(pNode!=NULL&& pNode->Data !=e) 100 { 101 Counter++; 102 pNode=pNode->next; 103 } 104 if( pNode!=NULL&&pNode->Data==e) 105 return Counter; 106 else 107 return -1; 108 } 109 110 //删除所有的=e的元素 111 template<typename ElemType> 112 DListNode* DeleteNodeAll(DListNode* Head,ElemType* e) 113 { 114 if(Head==NULL) 115 { 116 cerr<<"List Was Empty!"<<endl; 117 return Head; 118 } 119 if(Head->Data==e) 120 { 121 122 123 Head->next->prev=NULL; 124 Head=Head->next; 125 return Head; 126 } 127 DListNode* Temp=NULL; 128 DListNode* NodeTmp=Head; 129 DListNode* NodePrev=NULL; 130 while(NodeTmp->next!=NULL) 131 { 132 while( NodeTmp->Data!=e&&NodeTmp->next!=NULL) 133 { 134 NodePrev=NodeTmp; 135 NodeTmp=NodeTmp->next; 136 } 137 NodePrev->next=NodeTmp->next; 138 if(NodeTmp->next!=NULL) 139 NodeTmp->next->prev=NodePrev; 140 Temp=NodeTmp; 141 NodeTmp=NodeTmp->next; 142 delete Temp; 143 } 144 return Head; 145 } 146 147 148 template<typename ElemType> 149 ElemType FindMaxValue(DListNode* Head) 150 { 151 DListNode* NodeTmp=Head; 152 ElemType* Max=(ElemType *)Head->Data; 153 while(NodeTmp!=NULL) 154 { 155 ElemType* Temp=(ElemType* )NodeTmp->Data; 156 if(*Temp>*Max) //注意这里要重载 > 157 *Max=*Temp; 158 NodeTmp=NodeTmp->next; 159 } 160 return *Max; 161 } 162 163 int _tmain(int argc, _TCHAR* argv[]) 164 { 165 int arr[5]={1,8,3,4,5}; 166 167 DListNode* Head=NewNode<int>(&arr[0]); 168 int *p=(int *)(Head->Data); 169 for(int i=1;i<5;i++) 170 InsertNode<int>(Head,&arr[i]); 171 Head= DeleteNode<int>(Head,&arr[2]); 172 cout<<" FindElem="<<FindElem<int>(Head , &arr[3])<<endl; 173 cout<<"FindMaxValue="<<FindMaxValue<int>(Head)<<endl; 174 cout<<*p<<endl; 175 return 0; 176 }