單鏈表實現的線性表
List
頭文件
#include<iostream>
#include"Node.cpp"
#include"utility.cpp"
using namespace std;
Node
#include<iostream>
template <class Node_entry> struct Node
{
Node_entry entry;//數據域
Node<Node_entry> *next;//指針域
Node();
Node(Node_entry data,Node<Node_entry> *link);
};
template <class Node_entry> Node<Node_entry>::Node()
{
next=NULL;
}
template <class Node_entry> Node<Node_entry>::Node(Node_entry data,Node<Node_entry> *link)
{
entry = data;
next = link;
}
utility
枚舉
enum Error_code
{
success, fail, range_error, underflow, overflow, fatal, not_present, duplicate_error, entry_inserted, entry_found, internal_error
};
成員列表
template <class List_entry>
class List {
public:
List();
int size() const;//元素個數
bool full() const;//判滿,一維數組實現的
bool empty() const;
void clear();//置空
void traverse(void (*visit)(List_entry &));//遍歷,把每個元素的值餵給 visit指針所指的函數
Error_code retrieve(int position, List_entry &x) const;//查某個元素
Error_code replace(int position, const List_entry &x);//改
Error_code remove(int position, List_entry &x);//刪
Error_code insert(int position, const List_entry &x);//插入元素,在指position位置上增加一個元素 x
~List();//析構 ,做完了自動回收
List(const List<List_entry> ©);//深拷貝函數
void operator =(const List<List_entry> ©);// =拷貝
protected:
int count;
Node<List_entry> *head;
Node<List_entry> *set_position(int position) const;//給線性表序號,返回地址//相當於遍歷//自用
};
List()
template <class List_entry>
List<List_entry>::List()
{
count = 0;
head = NULL;
}
size()
template <class List_entry>
int List <List_entry>::size() const
{
return count;
}
full()
template <class List_entry>
bool List <List_entry>::full() const
{
return false;
}
empty()
template <class List_entry>
bool List <List_entry>::empty() const
{
return count<=0;
}
clear()
template <class List_entry> //如果不一個一個刪除,到時候就內存泄露了
void List<List_entry>::clear()
{
Node<List_entry> *p,*q;
for(p=head;p;p=q){
q=p->next;
delete p;
}
count = 0;
head=NULL;
}
traverse(void (*visit)(List_entry &))
template <class List_entry> void List<List_entry>::traverse(void (*visit)(List_entry &))
/*
Post: The action specified by function (*visit) has been performed on every
entry of the List, beginning at position 0 and doing each in turn.
*/
{
Node<List_entry> *p;
for(p=head;p;p=p->next){
(*visit)(p->entry);
}
}
replace(int position, const List_entry &x)
template <class List_entry> Error_code List<List_entry>::replace(int position, const List_entry &x)
{
if(position < 0 || position >= count)
return range_error;
Node<List_entry> *following =set_position(position);
following->entry=x;
return success;
}
remove(int position, List_entry &x)
template <class List_entry> Error_code List<List_entry>::remove(int position, List_entry &x)
{
Node<List_entry> *previous,*following;
if(count == 0 )
return underflow;
if(position < 0 || position >= count)
return range_error;
if(position>0)
{
previous = set_position(position-1) ;
following = set_position(position) ;
previous->next=following->next;
}else{
following = head;
head = head->next;
}
x=following->next;
delete following;
count--;
return success;
}
insert(int position, const List_entry &x)
template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x)
/*
Post:創三個指針, 前一項指針給previous,position指針給following,new_node是待加入Node的地址,
previous.next=new_node
*/
{
if (position < 0 || position > count)
return range_error;
Node<List_entry> *new_node, *previous, *following;
if (position > 0) {
previous = set_position(position - 1);//
following = previous->next;//position的地址
}
else following = head;
new_node = new Node<List_entry>(x, following);//創position地址的x內容的Node
if (new_node == NULL)
return overflow;
if (position == 0)
head = new_node;
else
previous->next = new_node;
count++;
return success;
}
List(const List<List_entry> ©)
template <class List_entry>
List<List_entry>::List(const List<List_entry> ©)
{
count = copy.count;
Node<List_entry> *new_node,*old_node =copy.head;
if(old_node=NULL)
{
head=NULL;
}
else
{
new_node = head = new Node<List_entry>(old_node->entry);
while(old_node->next!=NULL){
old_node = old_node->next;
new_node->next=new Node<List_entry>(old_node->entry);
new_node=new_node->next;
}
}
}
~List()
template <class List_entry>
List<List_entry>::~List()
{
clear();
}
//---
set_position(int position) const
template <class List_entry>
Node<List_entry> *List<List_entry>::set_position(int position) const
/*
Pre: position is a valid position in the List; 0 <= position < count.
Post: Returns a pointer to the Node in position.
*/
{
Node<List_entry> *q = head;
for (int i = 0; i < position; i++)
{
q = q->next;
}
return q;//返回的是地址
}
operator =(const List<List_entry> ©)
template <class List_entry> void List<List_entry>:://List=List
operator =(const List<List_entry> ©)
{
List new_copy(copy);//新開了一個List
clear();
count = new_copy.count;
head = new_copy.head;//把首地址交給head
new_copy.count = 0;
new_copy.head = NULL;//如果不設爲NULL,到時候析構就會消除掉new_copy的head地址,head就白接管了
}
所有代碼
//12 中 單鏈表實現的List 線性表
#include<iostream>
#include"Node.cpp"
#include"utility.cpp"
using namespace std;
template <class List_entry>
class List {
public:
// methods of the List ADT
List();
int size() const;//元素個數
bool full() const;//判滿,一維數組實現的
bool empty() const;
void clear();//置空
void traverse(void (*visit)(List_entry &));//遍歷,把每個元素的值餵給 visit指針所指的函數
Error_code retrieve(int position, List_entry &x) const;//查某個元素
Error_code replace(int position, const List_entry &x);//改
Error_code remove(int position, List_entry &x);//刪
Error_code insert(int position, const List_entry &x);//插入元素,在指position位置上增加一個元素 x
// The following methods replace compiler-generated defaults.
~List();//析構 ,做完了自動回收
List(const List<List_entry> ©);//深拷貝函數
void operator =(const List<List_entry> ©);// =拷貝
protected://子類可以訪問
// Data members for the linked list implementation now follow.
int count;
Node<List_entry> *head;
// The following auxiliary function is used to locate list positions
Node<List_entry> *set_position(int position) const;//給線性表序號,返回地址//相當於遍歷//自用
};
//---------------------------------------------------- List() --------------------------------------------------------
template <class List_entry>
List<List_entry>::List()
{
count = 0;
head = NULL;
}
//---------------------------------------------------- size() --------------------------------------------------------
template <class List_entry>
int List <List_entry>::size() const
{
return count;
}
//---------------------------------------------------- full() --------------------------------------------------------
template <class List_entry>
bool List <List_entry>::full() const
{
return false;
}
//---------------------------------------------------- empty() --------------------------------------------------------
template <class List_entry>
bool List <List_entry>::empty() const
{
return count<=0;
}
//---------------------------------------------------- clear() --------------------------------------------------------
template <class List_entry> //如果不一個一個刪除,到時候就內存泄露了
void List<List_entry>::clear()
{
Node<List_entry> *p,*q;
for(p=head;p;p=q){
q=p->next;
delete p;
}
count = 0;
head=NULL;
}
//------------------------------------ traverse(void (*visit)(List_entry &))-------------------------------------------
template <class List_entry> void List<List_entry>::traverse(void (*visit)(List_entry &))
/*
Post: The action specified by function (*visit) has been performed on every
entry of the List, beginning at position 0 and doing each in turn.
*/
{
Node<List_entry> *p;
for(p=head;p;p=p->next){
(*visit)(p->entry);
}
}
//------------------------------ replace(int position, const List_entry &x) -------------------------------------------
template <class List_entry> Error_code List<List_entry>::replace(int position, const List_entry &x)
{
if(position < 0 || position >= count)
return range_error;
Node<List_entry> *following =set_position(position);
following->entry=x;
return success;
}
//----------------------------------- remove(int position, List_entry &x) -------------------------------------------
template <class List_entry> Error_code List<List_entry>::remove(int position, List_entry &x)
{
Node<List_entry> *previous,*following;
if(count == 0 )
return underflow;
if(position < 0 || position >= count)
return range_error;
if(position>0)
{
previous = set_position(position-1) ;
following = set_position(position) ;
previous->next=following->next;
}else{
following = head;
head = head->next;
}
x=following->next;
delete following;
count--;
return success;
}
//----------------------------------- insert(int position, const List_entry &x)-------------------------------------------
template <class List_entry>
Error_code List<List_entry>::insert(int position, const List_entry &x)
/*
Post:創三個指針, 前一項指針給previous,position指針給following,new_node是待加入Node的地址,
previous.next=new_node
*/
{
if (position < 0 || position > count)
return range_error;
Node<List_entry> *new_node, *previous, *following;
if (position > 0) {
previous = set_position(position - 1);//
following = previous->next;//position的地址
}
else following = head;
new_node = new Node<List_entry>(x, following);//創position地址的x內容的Node
if (new_node == NULL)
return overflow;
if (position == 0)
head = new_node;
else
previous->next = new_node;
count++;
return success;
}
//----------------------------------------- List(const List<List_entry> ©) --------------------------------------------------------
template <class List_entry>
List<List_entry>::List(const List<List_entry> ©)
{
count = copy.count;
Node<List_entry> *new_node,*old_node =copy.head;
if(old_node=NULL)
{
head=NULL;
}
else
{
new_node = head = new Node<List_entry>(old_node->entry);
while(old_node->next!=NULL){
old_node = old_node->next;
new_node->next=new Node<List_entry>(old_node->entry);
new_node=new_node->next;
}
}
}
//---------------------------------------------------- ~List() --------------------------------------------------------
template <class List_entry>
List<List_entry>::~List()
{
clear();
}
//---------------------------------- set_position(int position) const--------------------------------------------------
template <class List_entry>
Node<List_entry> *List<List_entry>::set_position(int position) const
/*
Pre: position is a valid position in the List; 0 <= position < count.
Post: Returns a pointer to the Node in position.
*/
{
Node<List_entry> *q = head;
for (int i = 0; i < position; i++)
{
q = q->next;
}
return q;//返回的是地址
}
template <class List_entry> void List<List_entry>:://List=List
operator =(const List<List_entry> ©)
{
List new_copy(copy);//新開了一個List
clear();
count = new_copy.count;
head = new_copy.head;//把首地址交給head
new_copy.count = 0;
new_copy.head = NULL;//如果不設爲NULL,到時候析構就會消除掉new_copy的head地址,head就白接管了
}