1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| #include <iostream> #include <iomanip> using namespace std; class Node { public: int Value; Node *pNext; }; class LinkList { private: Node *pHead;
public: int Push(int InValue); LinkList(); LinkList(const LinkList &); LinkList operator=(const LinkList &); ~LinkList(); bool IsEmpty(); void Display(); void Erase(int DeValue); Node *Find(int Value) { Node *p = pHead; while (p->Value != Value && p != NULL) { p = p->pNext; } if (!p) return NULL; else return p; } bool IsOrder(); void ReSort(); };
void LinkList::ReSort() { Node *p1 = pHead, *p2 = pHead->pNext; while (p2 != NULL) { Push(p2->Value); p1->pNext = p2->pNext; Node *temp = p2; p2 = p2->pNext; delete temp; } } bool LinkList::IsOrder() { if (!pHead) { cout << "***********空链表!\n"; return false; }
Node *p1 = pHead, *p2 = pHead->pNext; if (p1->Value > p2->Value) { p1 = p1->pNext; p2 = p2->pNext; while (p2->pNext != NULL) { if (p1->Value < p2->Value) { cout << "无序!\n"; return false; }
p1 = p1->pNext; p2 = p2->pNext; } cout << "降序!\n"; } else { p1 = p1->pNext; p2 = p2->pNext; while (p2->pNext != NULL) { if (p1->Value > p2->Value) { cout << "无序!\n"; return false; } p1 = p1->pNext; p2 = p2->pNext; } cout << "升序!\n"; } return true; }
void LinkList::Display() { Node *pPtr = pHead; while (pPtr != NULL) { cout << setw(4) << pPtr->Value; pPtr = pPtr->pNext; } cout << '\n' << '\n'; } void LinkList::Erase(int DelValue) { int Flag = 0; Node *DelPos, *PrevPos = NULL; DelPos = pHead; while (DelPos != NULL) { if (DelPos->Value == DelValue) { if (PrevPos != NULL) PrevPos->pNext = DelPos->pNext; else pHead = pHead->pNext; delete DelPos; Flag = 1; break; } PrevPos = DelPos; DelPos = DelPos->pNext; } if (!Flag) cout << "链表节点中不存在数据" << DelValue << "!\n\n"; return; } LinkList::LinkList() { pHead = NULL; } LinkList::LinkList(const LinkList &a) { if (!a.pHead) { cout << "***********空链表!\n"; } else { pHead = new Node(); pHead->pNext = NULL; Node *p1 = pHead; Node *p2 = a.pHead->pNext; pHead->Value = a.pHead->Value; while (p2) { p1->pNext = new Node(); p1 = p1->pNext; p1->Value = p2->Value; p1->pNext = NULL; p2 = p2->pNext; } } } LinkList LinkList::operator=(const LinkList &a) { return a; } LinkList::~LinkList() { while (pHead != NULL) { Node *pTmp = pHead->pNext; delete pHead; pHead = pTmp; } } bool LinkList::IsEmpty() { if (!pHead) { cout << "***********空链表!\n"; return true; } else { cout << "***********非空链表!\n"; return false; } } int LinkList::Push(int InValue) { Node *NewNode = new Node; NewNode->Value = InValue; NewNode->pNext = NULL; Node *pTmp = pHead; pHead = NewNode; pHead->pNext = pTmp; return InValue; } int main() { LinkList link1; link1.IsOrder(); for (int i = 1; i < 10; i++) link1.Push(i * i); cout << "插入所有节点后:\n"; link1.Display(); link1.IsOrder(); link1.ReSort(); link1.Display(); link1.IsOrder(); link1.Push(33); link1.Display(); link1.IsOrder(); link1.ReSort(); link1.Display();
return 0; }
|