单链表实现的线性表

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> &copy);//深拷贝函数 
   void operator =(const List<List_entry> &copy);// =拷贝 
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> &copy)

template <class List_entry> 
List<List_entry>::List(const List<List_entry> &copy)
{
	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> &copy)

template <class List_entry> void List<List_entry>:://List=List
operator =(const List<List_entry> &copy)
{
	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> &copy);//深拷贝函数 
   void operator =(const List<List_entry> &copy);// =拷贝 
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> &copy) --------------------------------------------------------
template <class List_entry> 
List<List_entry>::List(const List<List_entry> &copy)
{
	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> &copy)
{
	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就白接管了 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值