算法与数据结构【C++】:双向链表

双向链表相对于单向链表的优点:


    1、单向链表在需要对列表尾部进行操作时,需要通过遍历的方式获取尾部的结点,很浪费时间
    2、对于需要频繁使用deleteFromTail()和addToTail()方法的程序,程序效率会非常低
    3、双向链表存储了每个结点前面的结点,在对尾部进行操作时,能非常方便的获取尾部的结点信息,方便对尾部进行操作,大大提高了程序效率
    4、但是,双向链表比单向链表多存储了一个对象的引用,空间复杂度更高
C++代码:

#include
#include
#include
using namespace std;

//双向链表结点类
class Node{
	public:	
	int value;	//结点数据	 
	Node* pre;	//前一个结点的指针
	Node* next;	//后一个结点的指针
	
	//构造函数
	Node(int aValue, Node* aPre=0, Node* aNext=0){
		value = aValue;
		pre = aPre;
		next = aNext;
	}
};

//双向链表类
class DoublyLinkedList{
	public:
	Node* head;		//链表头
	Node* tail;		//链表尾
	int length;		//链表长度
	
	//构造函数
	DoublyLinkedList(){
		head = tail = 0;
		length = 0;
	}
	
	//判断链表是否为空
	int isEmpty(){
		return head == 0;
	}

	//判断链表是否包含某个值
	int contains(int value){
		//遍历链表查找值
		for (Node* now=head; now!=0; now=now->next)
			if (now->value == value)
				return true;
		return false;
	}
	
	//向链表头添加结点
	void addToHead(int value){
		//如果链表为空,直接添加
		if (head == 0)
			head = tail = new Node(value, 0, 0);
		else 
			//链表不为空,添加结点,更新head
			head = head->pre = new Node(value, 0, head);
		length++;
	}

	//向链表尾添加结点
	void addToTail(int value){
		//如果链表为空,直接添加
		if (head == 0)
			head = tail = new Node(value, 0, 0);
		//链表不为空,添加结点,更新tail
		else
			tail = tail->next = new Node(value, tail, 0);
		length++;	
	}

	//向指定下标添加结点
	void addNode(int value, int index){
		//如果指定下标不合法,直接返回
		if (index > length+1 || index <= 0)	return ;
		//如果链表为空,且指定下标不是1,直接返回
		if (head==0 && index!=1)
			return ;
		//如果链表为空,需要新加入结点
		if (head==0 && index==1){
			head = tail = new Node(value, 0, 0);
			//printf("Wrong.....\n");
		}
		//如果链表不为空,需要在头部加入结点
		else if (index==1)
			head = head->pre = new Node(value, 0, head);
		else {
			//链表不为空,在链表中加入结点
			int cnt = 0;
			Node* aheadOfAdd=head;
			//找到要加入结点的前一个结点
			for(cnt=1; cnt+1next;
			//如果在尾部之后加入结点,更新tail
			if (aheadOfAdd == tail)
				tail = tail->next = new Node(value, tail, 0);
			//否则直接加入
			else
				aheadOfAdd->next = aheadOfAdd->next->pre = new Node(value, aheadOfAdd, aheadOfAdd->next);
		}
		length++;
	}
	
	//删除链表的第一个结点
	int deleteFromHead(){
		Node* deletedNode = 0;
		int deletedValue = -1;
		//如果链表为空,直接返回
		if (head == 0)
			return -1;
		//如果链表只有一个结点,删除这个结点,更新head和tail
		if (head == tail)
			deletedNode = head;
		//正常删除
		else{
			deletedNode = head;
			head = head->next;
			head->pre = 0;
		}
		length--;
		
		deletedValue = deletedNode->value;
		delete deletedNode;
		return deletedValue;
	}

	//删除链表的最后一个结点
	int deleteFromTail(){
		Node* deletedNode = 0;
		int deletedValue = -1;
		//如果链表为空,直接返回
		if (head == 0)
			return -1;
		//如果链表只有一个结点,删除这个结点,更新head和tail
		if (head == tail){
			deletedNode = head;
			head = tail = 0;
		}		
		//其他情况
		else{
			deletedNode = tail;
			tail = tail->pre;
			tail->next = 0;
		}
		length--;
		
		deletedValue = deletedNode->value;
		delete deletedNode;
		return deletedValue;
		
		
		
	}

	//按照给定下标删除结点
	int deleteNode(int index){
		//如果给定index明显不合法,直接返回
		if (index<=0 || index>length)
			return -1;
		//要删除的结点和被删除的值
		Node* deletedNode = head;
		int deletedValue = -1;
		//如果链表为空,直接返回
		if (head == 0) return -1;
		//如果链表只有一个元素且要求删除其他的元素,直接返回
		if (head == tail && index != 1)
			return -1;
		//如果链表只有一个元素且要求删除该元素,更新head和tail
		if (head == tail && index == 1){
			deletedNode = head;
			head = tail = 0;
		}
		//如果需要删除链表头结点
		else if(index == 1){
			deletedNode = head;
			head = head->next;
			head->pre = 0;
		}
		//如果需要删除链表尾结点
		else if(index == length){
			deletedNode = tail;
			tail = tail->pre;
			tail->next = 0;
		}
		//其他情况
		else{
			int cnt;
			for(deletedNode=head,cnt=1; cntnext);
			deletedNode->pre->next = deletedNode->next;
			deletedNode->next->pre = deletedNode->pre;		
		}
		length--;
		deletedValue = deletedNode->value;
		delete deletedNode;
		return deletedValue;
	}
	
	//打印链表
	//从前向后和从后向前打印,确保前向和后向指针完全正确
	void printSelf(){
		printf("DoublyLinkedList: [");
		for (Node* now=head; now!=0; now=now->next)	
			printf("%d, ", now->value);
		printf("]	");
		printf("Backside front: [");
		for (Node* now=tail; now!=0; now=now->pre)	
			printf("%d, ", now->value);
		printf("]\n\t\t");
		printf("Head: %d\t\ttail: %d\t\tLength: %d\n", head->value, tail->value, length);
		
	}
};
//测试程序
int main(){
	DoublyLinkedList* list = new DoublyLinkedList();
	list->addToHead(100);list->printSelf();
	list->addToHead(200);list->printSelf();
	list->addToTail(1);list->printSelf();
	list->addToTail(2);list->printSelf();
	list->addToTail(3);list->printSelf();
	list->deleteFromTail();list->printSelf();
	list->deleteFromTail();list->printSelf();
	list->addNode(10000, 1);list->printSelf();
	printf("\n\n");
	list->deleteNode(5);list->printSelf();
	printf("%d \n", list->isEmpty());
	printf("123456--- %d \n", list->contains(123456));
	printf("10000--- %d \n", list->contains(10000));
	
	//list->deleteFromHead();list->printSelf();
	//list->deleteFromHead();list->printSelf();
	//list->printSelf();
	
}

 

你可能感兴趣的:(算法与数据结构c++&Java,WilliamCode算法大师,Data,structure,c++,算法与数据结构)