【算法和数据结构】线性表(一)线性表(C++实现)

            线性表(List)作为一种简单的数据结构而被广泛使用。首先我们给出线性表的通俗定义:线性表是一个有限、有序的数据序列,其中的数据被称为元素(A list is a finite, ordered sequence of data items called elements.)。一个线性表可以记为:(a0,a1……an)。下面是一个线性表的实际例子:

 

 

【算法和数据结构】线性表(一)线性表(C++实现)_第1张图片

      线性表的实现非常简单,不再赘述(这里用C++实现)。

      一下类封装了对线性表的常用操作,包括删除所有元素,插入元素,追加元素,删除某一元素等等。

      1.Array based list:

       

//Array based list
class ALsit
{
private:
	int* listArray;
	int maxSize;
	int listSize;
	int fence;

public:
	ALsit(int& size)
	{
		maxSize = size;
		listSize = fence = 0;
		listArray = new int[maxSize];
	}
	~ALsit()
	{
		delete[] listArray;
	}

	void clear()
	{
		delete[] listArray;
		listSize = fence = 0;
		listArray = new int[maxSize];
	}
	void setStart()
	{
		fence = 0;
	}
	void setEnd()
	{
		fence = listSize;
	}
	void prevoius()
	{
		if (fence>=0)
		{
			fence--;
		}
	}
	void next()
	{
		if (fence<=listSize-1)
		{
			fence++;
		}
	}

	int leftLength()
	{
		return fence;
	}
	int rightLength()
	{
		return listSize - fence;
	}
	//right length including fence itself

	bool setPosition(const int& position)
	{
		if (position >= 0 && position <= listSize)
			fence = position;
		return (position >= 0 && position <= listSize);
	}
	bool getValue(int &it)
	{
		if (rightLength() == 0)
			return false;
		else
		{
			it = listArray[fence];
			return true;
		}
	}
	//to get the value at fence position and put it in "it"

	bool insert(const int& valueInsert)
	{
		if (listSize == maxSize)
			return false;
		for (int i = listSize;i > fence;i--)
		{
			listArray[i] = listArray[i - 1];
		}
		listArray[fence] = valueInsert;
		listSize++;
		return true;
	}
	//to insert a value behind fence
	bool append(const int& valueAppend)
	{
		if (listSize == maxSize)
			return false;
		listArray[listSize++] = valueAppend;
		return true;
	}
	bool remove(int &valueBeenRemoved)
	{
		if (rightLength() == 0)
			return false;
		valueBeenRemoved = listArray[fence];
		for (int i = fence;i < listSize - 1;i++)
			listArray[i] = listArray[i + 1];
		listSize--;
		return true;
	}
	//to remove the value at fence position and put it in "valueBeenRemoved"

	void print()
	{
		if(listSize==0)
			cout<<"This is an empty list"<

 

 

 

      2.Linked list:

       

//Linked list
class ListNode
{
public:
	int element;  //Value of this node
	ListNode* next;  //Pointer to next node

	ListNode(const int & elementValue, ListNode* nextNode)
	{
		element = elementValue;
		next = nextNode;
	}
};

class LList
{
private:
	ListNode* head;
	ListNode* tail;
	ListNode* fence;
	int firstElement;  //the value of the first element
	ListNode* initialPointerOfNode;
	int leftLength;
	int rightLength;
	//right length including fence itself

public:
	void initialize()
	{
		fence = head = tail = new ListNode(firstElement, initialPointerOfNode);
		leftLength = rightLength = 0;
	}
	void removeAll()
	{
		while (head != NULL)
		{
			fence = head;
			head = head->next;
			delete fence;
		}
	}
	void clear()
	{
		removeAll();
		initialize();
	}
	LList(int firstNodeValue)
	{
		firstElement = firstNodeValue;
		initialPointerOfNode = NULL;
		initialize();
	}
	~LList()
	{
		removeAll();
	}

	int getLeftLength()
	{
		return leftLength;
	}
	int getRightLengh()
	{
		return rightLength;
	}
	bool getValue(int &it)
	{
		if (getRightLengh() == 0)
			return false;
		it = fence->element;
		return true;
	}
	//to get the value at fence position and put it in "it"

	void setStart()
	{
		fence = head;
		rightLength += leftLength;
		leftLength = 0;
	}
	void setEnd()
	{
		fence = tail;
		leftLength += rightLength;
		rightLength = 0;
	}
	void previous()
	{
		ListNode* temp = head;
		if (fence == head)
			return;
		while (temp->next != fence)
		{
			temp = temp->next;
		}
		fence = temp;
		leftLength--;
		rightLength++;
	}
	void next()
	{
		if (fence != tail)
		{
			fence = fence->next;
			rightLength--;
			leftLength++;
		}
	}
	bool setPosition(const int& position)
	{
		if ((position<0) || (position>leftLength + rightLength))
			return false;
		fence = head;
		for (int i = 0;i < position;i++)
			fence = fence->next;
		rightLength = rightLength + leftLength - position;
		leftLength = position;
		return true;
	}
	bool insert(const int& valueInsert)
	{
		fence->next = new ListNode(valueInsert, fence->next);
		if (tail == fence)
			tail = fence->next;
		rightLength++;
		return true;
	}
	//to insert a value behind fence
	bool append(const int& valueAppend)
	{
		tail = tail->next = new ListNode(valueAppend, NULL);
		rightLength++;
		return true;
	}
	bool remove(int &it)
	{
		if (fence->next == NULL)
			return false;
		it = fence->next->element;
		ListNode* temp = fence->next;
		fence->next = temp->next;
		if (tail == temp)
			tail = fence;
		delete temp;
		rightLength--;
		return true;
	}
	//to remove the value behind fence and put it in "it"
	void print()
	{
		if (head == NULL)
			cout << "This an empty list" << endl;
		else
		{
			ListNode* temp = head;
			while (temp != tail)
			{
				cout << temp->element << " ";
				temp = temp->next;
			}
			cout << tail->element << endl;
		}
	}
};

            最后,我们比较一下两种实现方式,即基于数组和基于链表两种方式:

 

        Array-Based Lists:
        Insertion and deletion are θ(n).
        Prev and direct access are θ(1).
        Array must be allocated in advance.
        No overhead if all array positions are full.

        Linked Lists:
        Insertion and deletion are θ(1).
        Prev and direct access are θ(n).
        Space grows with number of elements.
        Every element requires overhead.

  

       也就是说基于数组实现,查找操作时间代价更优;而基于链表实现,则插入和删除操作时间代价更优。当然还有空间方面的代价问题,两者也是不一致的。

      点击这里下载线性表数据结构源码,包括:链表、队列、栈。均封装常用操作到c++类。

      

你可能感兴趣的:(编程语言)