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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
=====================================================
//////////Header file///////
//Chain declaration
//Chain_ChianNode.h
#ifndef CHAIN_H
#define CHAIN_H
#include "stdafx.h"
#include <iostream>
template <typename T> class Chain;
template <typename T> class ChainIterator;
template <typename T>
class ChainNode {
T data;
ChainNode<T> *link;
public:
friend Chain<T>;
friend ChainIterator<T>;
};
template <typename T>
class Chain {
ChainNode<T> *first;
ChainNode<T> *last;
public:
Chain() {first = 0;}
~Chain();
bool IsEmpty() const;
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k, T& x);
Chain<T>& Insert(int k, const T& x);
void Output(std::ostream& out) const;
void Erase();
Chain<T>& Append(const T&x);
friend ChainIterator<T>;
};
template <typename T>
class ChainIterator {
ChainNode<T> *location;
public:
T* Initialize(const Chain<T>& c)
{
location = c.first;
if(location) return &location->data;
return 0;
}
T* Next()
{
if(!location) return 0;
location = location->link;
if(location) return &location->data;
return 0;
}
};
template <typename T> //delete all nodes
Chain<T>::~Chain()
{
ChainNode<T> *next;
while (first)
{
next = first->link;
delete first;
first = next;
}
}
template <typename T>
bool Chain<T>::IsEmpty() const
{
return (first == 0);
}
template <typename T> //Length of the Chain
int Chain<T>::Length() const
{
ChainNode<T> *current = first;
int len = 0;
while(current)
{
len++;
current = current->link;
}
return len;
}
template <typename T>
bool Chain<T>::Find(int k, T& x) const
{
if(k < 1) return false;
ChainNode<T> *current = first;
int index = 1; //current index
while (index < k && current)
{
current = current->link;
index++;
}
if(current) {x = current->data; return true;}
return false;
}
template <typename T>
int Chain<T>::Search(const T& x) const
{
ChainNode<T> *current = first;
int index = 1;
while (current && current->data != x)
{
current = current->link;
index++;
}
if(current) return index;
return 0;
}
template <typename T> //Output
void Chain<T>::Output(ostream& out) const
{
ChainNode<T> *current;
for(current = first; current; current = current->link)
{
out << current->data << " ";
}
}
template <typename T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{
x.Output(out);
return out;
}
template <typename T> //delete one element, delete element k and put it to x
Chain<T>& Chain<T>::Delete(int k, T& x)
{
if(k < 1 || !first) //不存在第K个元素
throw OutOfBounds();
ChainNode<T> *p = first;
if(k == 1)
first = first->link;
else {
ChainNode<T> *q = first;
for(int index = 1; index < k - 1 && q; index++)
q = q->link; //q points to element k-1 address
if(!q || !q->link)
throw OutOfBounds();
p = q->link; // p points element k address
if(p == last) last = q;
q->link = p->link; //q->link was pointing to p, and now points to p->link
x = p->data;
delete p;
}
return *this;
}
template <typename T> //Insert
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
if(k < 0) throw OutOfBounds();
ChainNode<T> *p = first;
for(int index = 1; index < k && p; index++)
p = p->link; //move to element k
if(k > 0 && !p) throw OutOfBounds();
//Insert x after k
ChainNode<T> *y = new ChainNode<T>;
y->data = x;
if(k) {
y->link = p->link;
p->link = y;
}
else {
y->link = first;
first = y;
}
if(!y->link) last = y;
return *this;
}
template <typename T>
void Chain<T>::Erase()
{
ChainNode<T> *next;
while (first)
{
next = first->link;
delete first;
first = next;
}
}
template <typename T> //add an element after the last element
Chain<T>& Chain<T>::Append(const T& x)
{
ChainNode<T> *y;
y = new ChainNode<T>;
y->data = x;
y->link = 0;
if(first) {
last->link = y;
last = y;
}
else
first = last = y;
return *this;
}
=====================================================
// PracticeProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
////using namespace std//// If I put this namespace in this line, the program can work
#include "Chain_ChainNode.h"
using namespace std;
int main ()
{
try {
Chain<int> L;
L.IsEmpty();
cout << "Lenth = " << L.Length() << endl;
L.Insert(0,100).Insert(1,101);
cout << "List is : " << L << endl;
L.Insert(1,102).Insert(1,103);
cout << "List is : " << L << endl;
int z;
L.Find(2,z);
cout << "the second element is : " << z << endl;
L.Delete(1,z);
cout << "The element has been deleted is : " << z << endl;
cout << "List is : " << L << endl;
L.Append(54321).Append(9876);
cout << "List is : " << L << endl;
int *x;
ChainIterator<int> c;
x = c.Initialize(L);
while(x) {
cout << *x << ' ';
x = c.Next();
}
}
catch(...) {
cerr << "An exception has occurred" << endl;
}
}
|