常用顺序结构c++实现

将定义和声明放在了同一个头文件中方便阅读。但是通常是要分开的。

SeqList.h

/*
SeqList
14 06 2014
@author: zhegnwu
*/

const int DefaultSize = 100;
template<typename T>
class SeqList
{
private:
	int maxSize;
	int length;
	T *data;
public:
	SeqList();
	SeqList(int m);
	SeqList(T a[], int n, int m);
	~SeqList();
	void Print();		
	int Length();           //链表长度
	T Get(int i);           //获得第i个元素
	int Locate(T x);       //x出现的第一个位置
	void Insert(int i ,T x);
	T Delete(int i);
	void reverse();

};
//空链表
template<typename T>
SeqList<T>::SeqList()
{
	data = new T[DefaultSize];
	maxSize = DefaultSize;
	length = 0;
}

//构造长度为m的空链表
template<typename T>
SeqList<T>::SeqList(int m)
{
	if (m < 0) throw "参数错误";
	maxSize = m;
	length = 0;
	data = new T[m];
}

//长度为m并从a中获得n个元素的构造函数
template<typename T>
SeqList<T>::SeqList(T a[], int n, int m)
{
	if (m < n || m < 0 || n < 0) throw "参数错误";
	maxSize = m;
	length = n;
	data = new T[m];
	for (int i = 0; i < n; i++)
	{
		data[i] = a[i];
	}
}


//析构
template<typename T>
SeqList<T>::~SeqList()
{
	delete[] data;
}


//输出
template<typename T>
void SeqList<T>::Print()
{
	for (int i = 0; i < length; i++)
	{
		cout << data[i] << " ";
	}
	cout << endl;
}
//长度

template<typename T>
int SeqList<T>::Length()
{
	return length; 
}

//按位置查找
template<typename T>
T SeqList<T>::Get(int i)
{
	if (i > length || i < 1) throw "参数错误";
	return data[i-1];
}

//按值查找
template<typename T>
int SeqList<T>::Locate(T x)
{
	for (int i = 0; i < length; i++)
	{
		if (data[i] == x)
		{
			return i+1;
		}
	}
	return 0;
}
//插入元素

template<typename T>
void SeqList<T>::Insert(int i, T x)
{
	if (length >= maxSize) throw "上溢";
	if (i<1 || i>length + 1) throw "位置错误";
	for (int j = length; j>=i; j--)
	{
		data[j] = data[j - 1];
	}
	data[i - 1] = x;
	length++;
		
}

//删除元素
template<typename T>
T SeqList<T>::Delete(int i)
{
	if (length == 0)throw "下溢";
	if (i<1 || i>length)throw "位置错误";
	
	
	T ret = data[i - 1];
	for (int j = i; j < length; j++)
	{
		
		data[j - 1] = data[j];
	}
	length--;
	return ret;
	
}

//就地逆序
template<typename T>
void SeqList<T>::reverse()
{
	if (length == 0) throw "链表为空";
	T temp;
	for (int i = 0; i < length / 2; i++)
	{
		temp = data[i];
		data[i] = data[length - i - 1];
		data[length - i - 1] = temp;
	}
}
LinkList.h

/*单链表
14 06 2014
*/
#ifndef LINKLIST_H
#define LINKLIST_H
#include"Node.h"
#include <numeric>
#endif
template<typename T>
class LinkList
{
private:
	Node<T> *first;
public:
	LinkList();
	LinkList(T a[], int n);
	~LinkList();
	void Print();
	int Length();
	T Get(int i);
	Node<T>* Locate(T x);
	void Insert(int i, T x);
	T Delete(int i);
	void reverse();
};

template<typename T>
LinkList<T>::LinkList()
{
	first = new Node<T>;
	first->next = NULL;
}

template<typename T>
LinkList<T>::LinkList(T a[], int n)
{
	first = new Node<T>;
	Node<T> *p ,*s;
	p = first;
	for (int i = 0; i < n; i++)
	{
		s = new Node<T>;
		s->data = a[i]; s->next = NULL;
		p->next = s;
		p = s;
	}
}

template<typename T>
LinkList<T>::~LinkList()
{
	Node<T> *p;
	while (first)
	{
		p = first;
		first = first->next;
		delete p;
	}
}

template<typename T>
void LinkList<T>::Print()
{
	Node<T> *p=first->next;
	while (p!=NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}

template<typename T>
int LinkList<T>::Length()
{
	Node<T> *p=first;
	int num = 0;
	while (p->next != NULL)
	{
		num++;
		p = p->next;
	}
	return num;
}

template<typename T>
T LinkList<T>::Get(int i)
{
	if (i < 0||i>Length())throw "位置小于0";
	Node<T> *p = first;
	int count = 0;
	while (p->next != NULL)
	{
		p = p->next;
		count++;
		if (count == i)
		{
			return p->data;
		}
	}
}

//按值查找
template<typename T>
Node<T>* LinkList<T>::Locate(T x)
{
	Node<T> *p = first;
	while (p->next != NULL)
	{
		if (p->data == x)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

template<typename T>
void LinkList<T>::Insert(int i, T x)
{
	int count = 1;
	Node<T>* p = first;
	while (p&&count<i)
	{
		p = p->next;
		count++;
	}

	if (!p)throw "位置异常";

	else
	{
		Node<T> *s = new Node<T>;
		s->data = x;
		s->next = p->next;
		p->next = s;
	}
}

template<typename T>
T LinkList<T>::Delete(int i)
{
	int count = 1;
	Node<T> *p = first;
	while (p&&count < i)
	{
		count++;
		p = p->next;
	}
	if (!p||p->next==NULL) throw "位置异常";
	else
	{
		Node<T> *s = p->next;
		p->next = s->next;
		T x = s->data;
		delete s;
		return x;
	}
}

template<typename T>
void LinkList<T>::reverse()
{
	Node<T> *p,*q, *s;
	p=q = first->next;
	if (!p || p->next == NULL)throw "there is no need to reverse";

	s = p->next;
	p->next = NULL;
	while (q!= NULL)
	{
		first->next = s;
		q = s->next;
		s->next = p;
		p = s;
		s = q;
	}
	
}

SeqQueue.h

/*顺序对*/
template<typename T>
class SeqQueue
{
private:
	int front, rear, QueueSize;
	T *data;
public :
	SeqQueue();
	SeqQueue(int n);
	~SeqQueue();
	void EnQueue(T x);
	T DeQueue();
	T GetQueue();
	bool Empty();
};

template<typename T>
SeqQueue<T>::SeqQueue()
{
	QueueSize = 0;
	front = rear = 0;
}

template<typename T>
SeqQueue<T>::SeqQueue(int n)
{
	data = new T[n];
	QueueSize = n;
	front = 0;
	rear =0;
}

template<typename T>
SeqQueue<T>::~SeqQueue()
{
	QueueSize = 0;
	front = rear = 0;
	delete data;
}

template<typename T>
void SeqQueue<T>::EnQueue(T x)
{
	if (front == (rear + 1) % QueueSize) throw "上溢";
	rear = (rear + 1) % QueueSize;
	data[rear] = x;
}

template<typename T>
T SeqQueue<T>::DeQueue()
{
	if (front == rear) throw "下溢";
	front = (front + 1) % QueueSize;
	return data[front];
}


template<typename T>
T SeqQueue<T>::GetQueue()
{
	if (front == rear)throw "空队";
	int i = front + 1;
	return data[i];
}

LinkQueue.h

#ifndef LINKLIST_H
#define LINKLIST_H
#include"Node.h"
#include<numeric>
#endif

template<typename T>
class LinkQueue
{
private:
	Node<T> *front, *rear;
public:
	LinkQueue();
	~LinkQueue();
	void Enqueue(T x);
	T DeQueue();
	T GetQueue();
	bool Empty();
};

template<typename T>
LinkQueue<T>::LinkQueue()
{
	front=rear = new Node<T>;
	rear->next = NULL;
}

template<typename T>
LinkQueue<T>::~LinkQueue()
{
}

template<typename T>
void LinkQueue<T>::Enqueue(T x)
{
	rear->next = new Node<T>;
	rear = rear->next;
	rear->next = NULL;
	rear->data = x;
}

template<typename T>
T LinkQueue<T>::DeQueue()
{
	if (front == rear) throw "下溢";
	Node<T> *s;
	s = front->next;
	front->next = s->next;
	T x = s->data;
	delete s;
	return x;
}


template<typename T>
T LinkQueue<T>::GetQueue()
{
	if (front == rear) throw "下溢";
	return front->next->data;
}

template<typename T>
bool LinkQueue<T>::Empty()
{
	if (front == rear)
		return true;
	else
		return false;
}

SeqStack.h

template<typename T>
class SeqStack
{
private:
	int stackSize;
	int top;
	T *data;
public:
	SeqStack();
	SeqStack(int n);
	~SeqStack();
	void Push(T x);
	T Pop();
	T GetTop();
	bool Empty();
};

template<typename T>
SeqStack<T>::SeqStack()
{
	stackSize = 0;
	top = -1;
}

template<typename T>
SeqStack<T>::SeqStack(int n)
{
	data = new T[n];
	stackSize = n;
	top = -1;
}

template<typename T>
SeqStack<T>::~SeqStack()
{

}

template<typename T>
void SeqStack<T>::Push(T x)
{
	if (top == stackSize - 1) throw "stack 已满";
	top++;
	data[top] = x;
	
}

template<typename T>
T SeqStack<T>::Pop()
{
	if (top == -1) throw "下溢";
	T x = data[top--];
	return x;
}


template<typename T>
T SeqStack<T>::GetTop()
{
	if (top == -1) throw "空栈";
	return data[top];
}

template<typename T>
bool SeqStack<T>::Empty()
{
	if (top == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

DulNode.h

/*双链表节点*/
template<typename T>
struct DulNode
{
	T data;
	DulNode<T> *prior;
	DulNode<T> *next;
};

DulList.h

/*双链表*/
#ifndef DulLinkList_H
#define DulLinkList_h
#include"DulNode.h"
#include<numeric>
#endif

template<typename T>
class DulLinkList
{
private:
	DulNode<T> *first;
public:
	DulLinkList();
	DulLinkList(T a[], int n);
	~DulLinkList();
	void Print();
	void Insert(int i, T x);
	T Delete(int i);
};

template<typename T>
DulLinkList<T>::DulLinkList()
{
	first = new DulNode<T>;
	first->prior = first;
	first->next = first;
}

template<typename T>
DulLinkList<T>::DulLinkList(T a[], int n)
{
	first = new DulNode<T>;
	DulNode<T> *p=first;
	for (int i = 0; i < n; i++)
	{
		DulNode<T> *s = new DulNode<T>;
		s->data = a[i];
		s->next = first;
		p->next = s;
		s->prior = p;
		p = s;
		first->prior = s;
	}
}

template<typename T>
DulLinkList<T>::~DulLinkList()
{

}

template<typename T>
void DulLinkList<T>::Print()
{
	DulNode<T> *p=first->next;
	while (p!= first)
	{
		cout << p->data<<" ";
		p = p->next;

	}
	cout << endl;
}

template<typename T>
void DulLinkList<T>::Insert(int i, T x)
{
	DulNode<T> *p=first->next;
	int count = 1;
	while (p != first && count < i)
	{
		p = p->next;
		count++;
			
	}

	if (p == first) throw "位置错位";
	else
	{
		DulNode<T> *s = new DulNode<T>;
		s->data = x;
		s->next = p->next;
		p->next->prior = s;

		s->prior = p;
		p->next = s;

	}
}

template<typename T>
T DulLinkList<T>::Delete(int i)
{
	DulNode<T> *p = first->next;
	int count = 1;
	while (p != first&&count < i)
	{
		p = p->next;
		count++;
	}
	if (p == first)throw "位置异常";
	else
	{
		DulNode<T> *s = p->next;
		T x = s->data;
		s->next->prior = p;
		p->next = s->next;
		delete s;
		return x;
	}
}



Node.h

template<typename T>
struct Node
{
	T data;
	Node *next;
};

CirList.h

/*循环链表*/

#ifndef LINKLIST_H
#define LINKLIST_H
#include<numeric>
#include"Node.h"
#endif
template<typename T>
class CirLinkList
{
private:
	
	Node<T> *first;

public:
	CirLinkList();
	CirLinkList(T a[],int n);
	~CirLinkList();
	void Print();
	void Insert(int i, T x);
	T Delete(int i);
};

template<typename T>
CirLinkList<T>::CirLinkList()
{
	first= new Node<T>;
	first->next = first;
	
}

template<typename T>
CirLinkList<T>::CirLinkList(T a[], int n)
{
	first = new Node<T>;
	Node<T> *p = first;
	for (int i = 0; i < n; i++)
	{
		Node<T> *s = new Node<T>;
		s->data = a[i];
		s->next = first;
		p->next = s;
		p = s;
	}
}

template<typename T>
CirLinkList<T>::~CirLinkList()
{

}
template<typename T>
void CirLinkList<T>::Print()
{
	Node<T> *p = first->next;
	while (p != first)
	{
		cout << p->data<<" ";
		p = p->next;
	}
	cout << endl;
}

template<typename T>
void CirLinkList<T>::Insert(int i, T x)
{
	Node<T> *p = first;
	int count = 1;
	while (p->next != first&&count < i)
	{
		p = p->next;
		count++;
	}
	if (p->next == first) throw "位置错位";
	else
	{
		Node<T> *s = new Node<T>;
		s->data = x;
		s->next = p->next;
		p->next = s;
	}
}


template<typename T>
T CirLinkList<T>::Delete(int i)
{
	Node<T> *p = first;
	int count = 1;
	while (p->next != first&&count < i)
	{
		p = p->next;
		count++;
	}
	if (p->next == first) throw "位置错误";
	else
	{
		
		Node<T>* s = p->next;
		T x = s->data;
		p->next = s->next;
		delete s;
		return x;
	}
	
}

main.cpp

#include<iostream>
#include<string>
#include"SeqList.h"
#include"LinkList.h"
#include"CirlinkList.h"
#include"DulLinkList.h"
#include"SeqStack.h"
#include"SeqQueue.h"
#include"LinkQueue.h"
#include"Array.h"


using namespace std;

int main()
{
	//test SeqList

	/*
	int teststr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
	SeqList<int> testSeqList(teststr, 8, 10);
	cout << "初始链表:";
	testSeqList.Print();

	try
	{

		testSeqList.Insert(3, 888);
		cout << "插入元素:";
		testSeqList.Print();
		cout << "插入后长度:" << testSeqList.Length() << endl;;

		int dx = testSeqList.Delete(3);
		cout << "删除结果:";
		testSeqList.Print();

		cout << "第3个元素是:" << testSeqList.Get(3) << endl;
		cout << "6是第" << testSeqList.Locate(6) << "个元素" << endl;

		testSeqList.reverse();
		cout << "就地逆置";
		testSeqList.Print();

	}
	catch (char *Error)
	{
		cout << *Error;
	}
	
	*/

	//test Linklist
	/*
	string testStr[] = { "zheng", "wu", "you", "are", "good" };
	LinkList<string> testLinkList(testStr, 5);
	cout << "原始LinkList:";
	testLinkList.Print();
	cout << "链表长度:" << testLinkList.Length() << endl;

	testLinkList.Insert(5, "so");
	cout << "插入:";
	testLinkList.Print();
	testLinkList.Delete(5);
	cout << "删除";
	testLinkList.Print();

	Node<string> *testN = testLinkList.Locate("you");
	cout << testN->data<<endl;

	string str2 = testLinkList.Get(2);
	cout << str2<<endl;

	
	
	cout << "逆置:";
	testLinkList.reverse();
	testLinkList.Print();
	testLinkList.~LinkList();
	*/

	//CirLinList
	/*
	int a[] = { 1, 2, 3, 4, 5, 6 };
	CirLinkList<int> CirList(a, 6);
	CirList.Print();
	CirList.Insert(3, 88);
	cout << "插入88:";
	CirList.Print();
	CirList.Delete(3);
	cout << "删除88:";
	CirList.Print();
	*/

	//DulLinkList
	/*
	int a[] = { 1, 2, 3, 4, 5, 6, 7 };

	DulLinkList<int> testDul(a, 7);
	testDul.Print();
	testDul.Insert(2, 888);
	testDul.Print();

	testDul.Delete(2);
	testDul.Print();
	*/

	//SeqStack
	/*
	int a[] = { 1, 2, 3, 4 };
	SeqStack<int> testSeqStack(4);
	for (int i = 0; i < 4; i++)
	{
		testSeqStack.Push(a[i]);
	}
	cout << "stack top is " << testSeqStack.GetTop() << endl;
	cout << "test Pop function:" << testSeqStack.Pop() << endl;
	*/
    //SeqQueue
    /*

    SeqQueue<int> testSQueue(10);
	int ina[] = { 1, 2, 3, 4, 5 };
	for (int j = 0; j < 5; j++)
	{
		testSQueue.EnQueue(ina[j]);
	}
	cout << testSQueue.GetQueue() << endl;
	cout << testSQueue.DeQueue()<<endl;
	*/
	//LinkQueue
	/*
	LinkQueue<int> testLinkQueue;
	int testinLQ[] = { 1, 2, 3, 4 };
	cout <<"queue is empty:"<< testLinkQueue.Empty();
	for (int t = 0; t < 4; t++)
	{
		testLinkQueue.Enqueue(testinLQ[t]);
	}
	cout << testLinkQueue.GetQueue()<<endl;
	cout << testLinkQueue.DeQueue() << endl;
	if (!testLinkQueue.Empty())
		cout << "Queue is not empty" << endl;
	*/

    //Array
/*
    int ArrayData[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
	Array<int> testArray(ArrayData, 4, 4);
	testArray.Print();

	int d34 = testArray.GetData(3, 3);
	cout << "第3,3为:" << endl;
	cout << d34 << endl;

	Array<int> testRow = testArray.GetRow(2);
	cout << "第2行为:" << endl;
	testRow.Print();
	cout<<"0元素的个数:"<<testArray.GetNonzeroNum()<<endl;


	int testSparse[] = { 1, 0, 0, 0, 2, 0, 0, 0, 4 };
	Array<int> sparseArray(testSparse, 3, 3);
	sparseArray.Print();
	
	*/

	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值