#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <cassert>
#ifndef INS_FRONT_OR_REAR
#define INS_FRONT_OR_REAR
enum INS_MOD { INS_FRONT, INS_REAR };
#endif
template<typename T>
struct LinkedNode {
T data;
LinkedNode<T> *link;
LinkedNode(LinkedNode<T> *ptr = nullptr) {
link = ptr;
}
LinkedNode(const T& item, LinkedNode<T> *ptr = nullptr) {
data = item;
link = ptr;
}
};
template<typename T>
class LinkedList {
public:
LinkedList() { first = new LinkedNode<T>; }
LinkedList(const T& x) { first = new LinkedNode<T>(x); }
LinkedList(LinkedList<T>& l);
~LinkedList() {
clear();
delete first;
}
void clear();
int length() const;
LinkedNode<T> *getHead() const { return first; }
LinkedNode<T> *search(const T& x);
LinkedNode<T> *locate(int i);
bool getData(int i ,T& x) const;
void setData(int i, T& x);
bool insert(int i, T& x);
bool remove(int i, T& x);
bool is_empty() const { return first->link == nullptr; }
void sort();
void inverse();
void input(T endtag, INS_MOD mod = INS_REAR);
void output();
LinkedList<T>& operator=(LinkedList<T>& L);
friend ostream& operator<<(ostream& os, LinkedList<T> &L) {
LinkedNode<T>& current = L.first->link;
while (current != nullptr) {
os << current->data << "\t";
current = current->link;
}
os << endl;
return os;
}
friend istream& operator>>(LinkedList<T>& L) {
LinkedNode<T> *newNode, *last;
T value;
L.clear();
last = L.last;
while (!in.eof()) {
in >> value;
newNode = new LinkedNode<T>(value);
assert(newNode != nullptr);
last->link = newNode;
last = newNode;
}
last->link = nullptr;
return in;
}
protected:
LinkedNode<T> *first;
void inputFront(T endtag);
void inputRear(T endtag);
};
templatet<typename T>
LinkedList<T>::LinkedList(LinkedList<T>& L) {
T value;
LinkedNode<T> *src = L.first();
LinkedNode<T> *dest = first = new LinkedNode<T>;
while (src->link != nullptr) {
value = src->link->data;
dest->link = new LinkedNode<T>(value);
dest = dest->link;
src = src->link;
}
dest->link = nullptr;
}
template<typename T>
void LinkedList<T>::clear() {
LinkedNode<T> *p;
while (first->link != nullptr) {
p = first->link;
first->link = p->link;
delete p;
}
}
template<typename T>
int LinkedList<T>::length() const {
LinkedNode<T> *p = first->link;
int count = 0;
while (p) {
p = p->link;
count++;
}
return count;
}
template<typename T>
LinkedNode<T> *LinkedList<T>::search(const T& x) {
LinkedNode<T> *current = first->link;
while (current != nullptr && current->data != x) {
current = current->link;
}
return current;
}
template<typename T>
LinkedNode<T> *LinkedList<T>::locate(int i) {
}
template<typename T>
bool LinkedList<T>::getData(int i, T& x) const {
if (i <= 0)
return nullptr;
LinkedNode<T> *current = locate(i);
if (current == nullptr)
return false;
else {
x = current->data;
return true;
}
}
template<typename T>
void LinkedList<T>::setData(int i, T& x) {
if (i <= 0)
return 0;
LinkedNode<T> *current = locate(i);
if (current == nullptr)
return;
else
return current->data = x;
}
template<typename T>
bool LinkedList<T>::insert(int i, T& x) {
LinkedNode<T> *current = locate(i);
if (current == nullptr)
return false;
LinkedNode<T> *newnode = new LinkedNode<T>(x);
if (newnode == nullptr) {
std::cerr << "mem alloc error !" << std::endl;
exit(1);
}
newnode->link = current->link;
current->link = newnode;
return true;
}
template<typename T>
bool LinkedList<T>::remove(int i, T& x) {
LinkedNode<T> *current = locate(i - 1);
if (current == nullptr || current->link == nullptr)
return false;
LinkedNode<T> *delnode = current->link;
current->link = delnode->link;
x = delnode->data;
delete del;
return true;
}
template<typename T>
void LinkedList<T>::output() {
LinkedNode<T> *current = first->link;
while (current != nullptr) {
std::cout << current->data << endl;
current = current->link;
}
}
template<typename T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList<T>& L) {
T value;
LinkedNode<T> *src = L.first;
LinkedNode<T> *dest = first;
clear();
while (src->link != nullptr) {
value = src->link->data;
dest->link = new LinkedNode<T>(value);
dest = dest->link;
src = src->link;
}
dest->link = nullptr;
return *this;
}
template<typename T>
void LinkedList<T>::input(T endtag, INS_MOD mod) {
if (mod == INS_FRONT)
input_front(endtag);
else
input_rear(endtag);
}
template<typename T>
void LinkedList<T>::input_front(T endtag) {
LinkedNode<T> *newnode;
T value;
while (value != endtag) {
newnode = new LinkedNode<T>(value);
if (newnode == nullptr) {
std::cerr << "mem alloc error !" << std::endl;
exit(1);
}
newnode->link = first->link;
first->link = newnode;
std::cin >> value;
}
}
template<typename T>
void LinkedList<T>::inputrear(T endtag) {
LinkedNode<T> *newnode;
LinkedNode<T> *last = first;
T value;
while (last->link != nullptr)
last = last->link;
std::cin >> value;
while (value != endtag) {
newnode = new LinkedNode<T>(value);
if (newnode == nullptr) {
std::err << " mem alloc error !" << std::endl;
exit(1);
}
last->link = newnode;
last = newnode;
std::cin >> value;
}
last->link == nullptr;
}
template<typename T>
void LinkedList<T>::inverse() {
LinkedNode<T> *t, *p, *q;
t = nullptr;
p = first->link;
while (p != nullptr p->link != nullptr) {
q = t;
t = p;
p = t->link;
t->link = q;
}
first->link = t;
}
#endif // LINKEDLIST_H
LinkedList
最新推荐文章于 2024-08-23 23:43:22 发布