zoukankan      html  css  js  c++  java
  • 线性表——(1)顺序表

    #include <iostream>
    using namespace std;
    
    template<class T>
    class SqList
    {
    public:
    	SqList(int size = maxSize);	// 初始化顺序表
    	~SqList(); 	// 销毁顺序表
    
    	int Length() const;	// 返回顺序表长度
    	int isEmpty() const;	//判断是否空
    	int isFull() const;	// 判断是否满
    	void Clear();	// 清空顺序表
    	int Prior(int x, T &pri);	// 求前驱
    	T Get(int i); // 获取元素
    	int SetElem(int x, T targ);	// 元素赋值
    	int Locate(T x) const;	// 确定元素位置
    	int Insert(int i, T x);	//	插入元素(前)
    	int Delete(int i);	// 删除元素
    
    protected:
    	const static int maxSize = 100;
    	T* elem;	//	顺序表的基址
    	int last;	//	最后一个元素的index-->length==last+1
    };
    
    template<class T>
    SqList<T>::SqList(int size = 0)
    {
    	if (size > 0) {
    		elem = new T[maxSize];	//分配空间防污染
    		last = -1;
    	}
    	else
    	{
    		cout << "Invalid Size!" << endl;
    	}
    }
    
    template<class T>
    SqList<T>::~SqList()
    {
    	delete[] elem;
    }
    
    template<class T>
    inline int SqList<T>::Length() const
    {
    	return last + 1;
    }
    
    template<class T>
    inline int SqList<T>::isEmpty() const
    {
    	return (last + 1 == 0 ? 1 : 0);
    }
    
    
    template<class T>
    inline int SqList<T>::isFull() const
    {
    	return (last + 1 == maxSize ? 1 : 0);
    }
    
    template<class T>
    void SqList<T>::Clear()
    {
    	last = -1;
    }
    
    template<class T>
    int SqList<T>::Prior(int x, T &pri)
    {
    	if (x > 0 && x <= last) {
    		pri = elem[x - 1];
    		return 1;
    	}
    	else {
    		cout << "Prior not found!" << endl;
    		return 0;
    	}
    }
    
    template<class T>
    T SqList<T>::Get(int i)
    {
    	return ((i < ) || i > last) ? NULL : elem[i]);
    }
    
    template<class T>
    int SqList<T>::SetElem(int x, T targ)
    {
    	if (x >= 0 && x <= last) {
    		elem[x] = targ;
    		return 1;
    	}
    	else {
    		cout << "Index Error!" << endl;
    		return 0;
    	}
    }
    
    
    template<class T>
    int SqList<T>::Locate(T x) const
    {
    	for (int i = 0; i <= last; ++i) {
    		if (elem[i] == x) {
    			return i;
    		}
    		else {
    			return -1;
    		}
    	}
    }
    
    template<class T>
    int SqList<T>::Insert(int i, T x)
    {
    	if (!Full() && i >= 0 && i <= last + 1) {
    		++last;
    		for (int j = last; j > i; --j) {
    			elem[j] = elem[j - 1];
    		}
    		elem[i] = x;
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }
    
    template<class T>
    int SqList<T>::Delete(int i)
    {
    	if (!Empty() && i >= 0 && i <= last) {
    		for (int j = i + 1; j <= last; ++j) {
    			elem[j - 1] = elem[j];
    		}
    		--last;
    		return 1;
    	}
    	else {
    		return 0;
    	}
    }
    
    
    醉 生 梦 死
  • 相关阅读:
    开发servlet三种方式
    puppet 启动失败
    linux 内核 中链表list
    software level
    ubuntu 使用 root “sudo /bin/bash”
    linux 内存管理
    linux kernel "current" macro
    hello.hs haskell
    ubuntu samba
    微信小程序中使用 npm包管理 (保姆式教程)
  • 原文地址:https://www.cnblogs.com/TuerLueur/p/9782744.html
Copyright © 2011-2022 走看看