数据结构链式描述总结(1)

本文总结了链式数据结构的各种类型,包括链表、循环链表、双向链表、链表栈、链队列、跳表、链式哈希表和链式二叉树。特别讨论了链式二叉树的前序、中序和后序遍历的实现,以及静态成员函数在类中的使用和潜在问题。
摘要由CSDN通过智能技术生成
  • 链表有一个firstnode指针和一个int 指明链表元素各式个数
  • 循环链表将firstnode指针改成了headernode头结点,同有int指示个数
  • 双向链表有一个firstnode指针,一个lastnode指针,每个节点都有两个指针,previous与next,要int指示个数,不能直接用两个指针相减,因为内存不是连续的
  • 链表栈,一个stacktop指针,一个int,每一个节点的指针指向的都是上一个的节点,由stacktop = new chainnode(element,stacktop)决定
  • 链队列,一个queuefront指针,一个queueback指针,一个int
  • 跳表
float cutOff;          // used to decide level number
      int level() const;     // generate a random level number
      int levels;            // max current nonempty chain
      int dSize;             // number of pairs in dictionary
      int maxLevel;          // max permissible chain level
      K tailKey;             // a large key
      skipNode<K,E>* search(const K&) const;
                             // search saving last nodes seen
      skipNode<K,E>* headerNode;  // header node pointer
      skipNode<K,E>* tailNode;    // tail node pointer
      skipNode<K,E>** last;       // last[i] = last node seen on level i
  • 链式哈希表
protected:
      sortedChain<K, E>* table;  // hash table
      hash<K> hash;              // maps type K to nonnegative integer
      int dSize;                 // number of elements in list
      int divisor;               // hash function divisor
  • 链式二叉树
template<class E>
class linkedBinaryTree : public binaryTree<binaryTreeNode<E> >
{
public:
	linkedBinaryTree() { root = NULL; treeSize = 0; }
	~linkedBinaryTree() { erase(); };
	bool empty() const { return treeSize == 0; }
	int size() const { return treeSize; }
	E* rootElement() const;
	void makeTree(const E& element,
		linkedBinaryTree<E>&, linkedBinaryTree<E>&);
	linkedBinaryTree<E>& removeLeftSubtree();
	linkedBinaryTree<E>& removeRightSubtree();
	void preOrder(void(*theVisit)(binaryTreeNode<E>*))
	{
		visit = theVisit; preOrder(root);
	}
	void inOrder(void(*theVisit)(binaryTreeNode<E>*))
	{
		visit = theVisit; inOrder(root);
	}
	void postOrder(void(*theVisit)(binaryTreeNode<E>*))
	{
		visit = theVisit; postOrder(root);
	}
	void levelOrder(void(*)(binaryTreeNode<E> *));
	void preOrderOutput() { preOrder(output); cout << endl; }
	void inOrderOutput() { inOrder(output); cout << endl; }
	void postOrderOutput() { postOrder(output); cout << endl; }
	void levelOrderOutput() { levelOrder(output); cout << endl; }
	void erase()
	{
		postOrder(dispose);
		root = NULL;
		treeSize = 0;
	}
	int height() const { return height(root); }
protected:
	binaryTreeNode<E> *root;                // pointer to root
	int treeSize;                           // number of nodes in tree
	static void(*visit)(binaryTreeNode<E>*);      // visit function
	static int count;         // used to count nodes in a subtree
	static void preOrder(binaryTreeNode<E> *t);
	static void inOrder(binaryTreeNode<E> *t);
	static void postOrder(binaryTreeNode<E> *t);
	static void countNodes(binaryTreeNode<E> *t)
	{
		visit = addToCount;
		count = 0;
		preOrder(t);
	}
	static void dispose(binaryTreeNode<E> *t) { delete t; }
	static void output(binaryTreeNode<E> *t)
	{
		cout << t->element << ' ';
	}
	static void addToCount(binaryTreeNode<E> *t)
	{
		count++;
	}
	static int height(binaryTreeNode<E> *t);
};
  • 这里很有意思,这是黑书作者写的类,可以看见,在链式二叉树类的声明中,在public和protected了都有前序遍历,中序遍历,后序遍历,在protected里被声明为静态成员函数,我个人的理解是这几个函数在这个类中起到工具的作用,是整个类的所有对象共用的,参数类型只有二叉树的头结点指针,而public里的这几个函数的参数除了二叉树的头结点指针外还有一个函数指针,使得前序遍历等,中序遍历,后续遍历,让自身有不同的功能,可以直接用public里定义的inorderoutput,也可以用public里的inorder,通过赋予参数实现,这里我不是很明白黑书作者把inorder声明两遍的意图,在我的眼里,可以只在protected里声明,把参数加上一个函数指针,我觉得黑书作者的写法可能只会允许每个对象自由赋值函数指针,但是这种自由又会允许visit函数指针随意更改,而其为静态成员变量,对下一个对象的使用造成影响,所以我认为更优的做法是可以只在protected里声明,把参数加上一个函数指针,再通过在public里的inorderoutput等进行输出等操作(个人拙见)。头文件里有这么一段代码
// the following should work but gives an internal compiler error
// template <class E> void (*linkedBinaryTree<E>::visit)(binaryTreeNode<E>*);
// so the explicit declarations that follow are used for our purpose instead
void(*linkedBinaryTree<int>::visit)(binaryTreeNode<int>*);
void(*linkedBinaryTree<booster>::visit)(binaryTreeNode<booster>*);
void(*linkedBinaryTree<pair<int, int> >::visit)(binaryTreeNode<pair<int, int> >*);
void(*linkedBinaryTree<pair<const int, char> >::visit)(binaryTreeNode<pair<const int, char> >*);
void(*linkedBinaryTree<pair<const int, int> >::visit)(binaryTreeNode<pair<const int, int> >*);
  • 说是存在编译器内部的错误,给出了显示声明,这个应该是让visit后面的参数类型与visit一致,这个具体我也不太懂。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值