【数据结构】用C++实现双链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)

//【数据结构】用C++实现双链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)

//头文件

#ifndef _LIST_H
#define _LIST_H

#include<iostream>
using namespace std;

template<class Type>
class DList;

template<class Type>
class ListNode
{
	friend class DList<Type>;
public:
	ListNode() :data(Type()), next(NULL), prio(NULL)
	{}
	ListNode(Type d, ListNode<Type> *n = NULL,ListNode<Type> *m=NULL)
		:data(d), next(n), prio(m)
	{}
	~ListNode()
	{}

private:
	Type data;
	ListNode<Type> *next;
	ListNode<Type> *prio;
};

template<class Type>
class DList
{
public:
	DList()
	{
		first = last = Buynode();
	}
	~DList()
	{
		destroy();
	}
public:

	void push_back(const Type &x)            //尾插``
	{
		ListNode<Type> *s = Buynode(x);
		last->next = s;
		s->prio = last;
		last = s;
		first->data++;
	}

	void push_front(const Type &x)          //头插``
	{
		if (first->data == 0)
		{
			push_back(x);
			return;
		}
		ListNode<Type> *s = Buynode(x);
		s->next = first->next;
		first->next->prio = s;
		first->next = s;
		s->prio = first;
		last = s;
		for (int i = 0; i < first->data; i++)
		{
			last = last->next;
		}
		first->data++;
	}

	void pop_back()                 //尾删``
	{
		if (first->data == 0)
			return;
		ListNode<Type> *p = last;
		last = last->prio;
		last->next = NULL;
		delete p;
		first->data--;

	}

	void pop_front()                //头删``
	{
		if (first->data == 0)
		{
			return;
		}
		else
		{
			ListNode<Type> *s = first->next;
			if (first->data == 1)
			{
				delete s;
				last = first;
				last->next = NULL;
			}
			else
			{
				first->next = s->next;
				s->next->prio = first;
				delete s;
			}
			first->data--;
		}
	}

	void insert_val(const Type &x)          //按值插入``
	{
		ListNode <Type> *p = Buynode(x);
		ListNode <Type> *s = first;
		if (first->data == 0)
		{
			push_back(x);
			return;
		}
		while (s->next->data<x&&s->next->next != NULL)
		{
			s = s->next;
		}
		if (s->next->next == NULL)
		{
			if (s->next->data>x)
			{
				p->next = s->next;
				s->next->prio = p;
				s->next = p;
				p->prio = s;
				first->data++;
			}
			else
				push_back(x);
		}
		else
		{
			p->next = s->next;
			s->next->prio = p;
			s->next = p;
			p->prio = s;
			first->data++;
		}

	}

	void show_list()                //显示``
	{
		if (first->data == 0)
		{
			cout << "NULL" << endl;
			return;
		}
		ListNode<Type> *q = first->next;
		while (q != NULL)
		{
			cout << q->data << "->";
			q = q->next;
		}
		cout << "NULL  first->data=" << first->data <<" last->data="<<last->data<< " last->next=" << last->next <<" "<< endl;
	}

	void sort()                     //排序``
	{
		if (first->data == 0)
		{
			return;
		}
		ListNode<Type> *p = first->next;
		ListNode<Type> *q = p->next;
		p->next = NULL;
		last = p;
		first->data = 1;
		ListNode<Type> *r;
		while (q != NULL)
		{
			insert_val(q->data);
			r = q;
			q = q->next;
			delete r;
		}
	}

	ListNode<Type>* find(const Type &x)              //查找``
	{
		ListNode<Type> *s;
		for (s = first->next; s != NULL; s = s->next)
		{
			if (s->data == x)
			{
				return s;
			}
		}
		return NULL;
	}

	int length()                          //求长度``
	{
		return(first->data);
	}

	void delete_val(const Type &x)       //按值删除``
	{
		if (first->data == 0)
		{
			cout << "未找到该数:" << endl;
			return;
		}
		else
		{

			ListNode<Type> *p = find(x);
			if (p == NULL)
			{
				cout << "未找到该数:" << endl;
				return;
			}
			ListNode<Type> *q = first;
			while (q->next != p)
			{
				q = q->next;
			}
			if (q->next->next == NULL)
			{
				pop_back();
			}
			else
			{
				q->next = p->next;
				p->next->prio = q;
				delete p;
				first->data--;
			}
		}
	}

	void clear()                       //清除``
	{
		if (first->data == 0)
			return;
		while (first->next != NULL)
		{
			pop_back();
		}
	}

	void resver()                       //逆序``
	{
		if (first->data == 0)
		{
			return;
		}
		ListNode<Type> *p = first->next;
		ListNode<Type> *q = p->next;
		p->next = NULL;
		last = p;
		first->data = 1;
		ListNode<Type> *r;
		while (q != NULL)
		{
			push_front(q->data);
			r = q;
			q = q->next;
			delete r;
		}
	}

	void quit_system(int &a)               //退出``
	{
		clear();
		a = 0;
	}

	void destroy()              //摧毁``
	{
		clear();
		delete first;
	}
protected:
	ListNode<Type>* Buynode(Type x = Type())
	{
		ListNode<Type> *p = new ListNode<Type>(x);
		return p;
	}
private:
	ListNode<Type> *first;
	ListNode<Type> *last;
};

//主函数

#endif

#include"DList.h"

void main()
{
	DList<int> mylist;
	int select = 1;
	int Item;
	//int pos;
	while (select)
	{
		cout << "**************************************" << endl;
		cout << "* [1] show_list       [2] push_front *" << endl;
		cout << "* [3] push_back       [4] pop_front  *" << endl;
		cout << "* [5] pop_back        [6] insert_val *" << endl;
		cout << "* [7] find            [8] delete_val *" << endl;
		cout << "* [9]length           [10] clear     *" << endl;
		cout << "* [11] quit_system    [12] destroy   *" << endl;
		cout << "* [13] resver         [14] sort      *" << endl;
		cout << "**************************************" << endl;
		cout << "请选择:>";
		cin >> select;
		switch (select)
		{
		case 1:
			mylist.show_list();
			break;
		case 2:
			cout << "请输入要插入的值(-1结束):>";
			while (cin >> Item, Item != -1)
			{
				mylist.push_front(Item);
			}
			break;
		case 3:
			cout << "请输入要插入的值(-1结束):>";
			while (cin >> Item, Item != -1)
			{
				mylist.push_back(Item);
			}
			break;
		case 4:
			mylist.pop_front();
			;
			break;
		case 5:
			mylist.pop_back();
			break;
		case 6:
			cout << "请输入要插入的值:>";
			cin >> Item;
			mylist.insert_val(Item);
			break;
		case 7:
			cout << "请输入要查找的数:";
			cin >> Item;
			cout << "该数的地址为:";
			cout << mylist.find(Item) << endl;
			break;
		case 8:
			cout << "请输入要删除的值:>";
			cin >> Item;
			mylist.delete_val(Item);
			break;

		case 9:
			cout << "该顺序表长度为:" << mylist.length() << endl;
			break;
		case 10:
			mylist.clear();
			break;
		case 11:
			mylist.quit_system(select);
			break;
		case 12:
			mylist.destroy();
		case 13:
			mylist.resver();
			break;
		case 14:
			mylist.sort();
			break;
		default:
			break;
		}
	}
	return;
}
<img src="http://img.blog.csdn.net/20150531224355335?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

时间: 2024-11-05 18:40:26

【数据结构】用C++实现双链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)的相关文章

java数据结构与算法之双链表设计与实现

转载请注明出处(万分感谢!): http://blog.csdn.net/javazejian/article/details/53047590 出自[zejian的博客] 关联文章: 关联文章: java数据结构与算法之顺序表与链表设计与实现分析 java数据结构与算法之双链表设计与实现 java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制) ??上一篇文章分析顺序表和单链表,本篇就接着上篇继续聊链表,在单链表

【c++版数据结构】之双链表的实现(带头结点以及尾节点)

所实现的双链表的结构如下图所示: 双链表的实现,和第一篇文章单链表的实现大致相同点击打开链接 本篇文章在构建节点的同时,初始化构建节点的前驱和后继,具体细节参考下列代码 头文件:DList.h #ifndef DLIST_H_ #define DLIST_H_ typedef enum{FALSE,TRUE}Status; #include<iostream> #include<cassert> using namespace std; template<class Type

【数据结构】两个单循环链表的连接操作

如果在单链表或头指针表示的链表上操作这个比较消耗性能,因为都需要遍历第一个链表,找到an,然后将b1链接到an的后面,时间复杂度是:O(n).若在尾指针表示的单循环链表上实现,则只需改变指针,无需遍历,时间复杂度是:O(1) 现在看算法实现,需要4个主要步骤,都注释了: LinkList Connect(LinkList A,LinkList B){ //假设A,B为非空循环链表的尾指针 LinkList p = A->next;//1.保存A链表的头结点的位置 A->next = B->

双链表内部迁移操作

说明:本文仅供学习交流,转载请标明出处,欢迎转载! 所谓双链表的迁移操作指的是将双链表中一部分连续的结点迁移到另外一个结点之前,为了说明这个定义,我们从下图来解释. 我们将[first,last)对应的结点迁移到position所指向的位置之前?那该怎么做呢?这不是一个算法设计题,因为不需要什么特别有效率的技巧,我只是想理清这个思路而已,因为双链表的增删操作如果不理清思路会很容易产生混乱. 下面我们给出该双链表结点的定义: template<class T> struct node { <

数据结构-编程实现一个双链表的建立,双链表的打印,双链表的测长

1:双链表的建立,打印,代码如下: // ConsoleApplication24.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<malloc.h> #include <iostream> #include <assert.h> using namespace std; typedef struct DbNode //双向链表结构体 { int data;//节点数据 DbNode *lef

C语言实现非循环双链表节点的删除(带头结点尾结点)

我在之前一篇博客<C语言实现非循环双链表节点的删除(不带头结点)>中详细讲解了不含头尾节点的双链表中删除一个节点,处理过程还是稍显麻烦.自从我们学习使用头尾节点来处理双链表后,删除过程就非常方便.代码上传至 https://github.com/chenyufeng1991/DeleteNodeDoubleLinkedList_HeadList . 核心代码如下: //删除pos位置的节点 int DeletePosList(Node *pHead,Node *pTail,int pos){

【c++版数据结构】之循环单链表的实现(带头结点以及尾节点)

所实现的循环单链表的结构如下图所示: 循环单链表的实现,和上一篇文章单链表的实现大致相同点击打开链接,略有区别: 1:循环判断的条件不再是s == NULL或者s->next == NULL,而是他们是否等于头指针.2: 断开链表时的处理,尾节点的next不是NULL,而是指向头结点 具体细节参考上一篇文章 头文件:SCList.h #ifndef SCLIST_H #define SCLIST_H #include<iostream> #include<cassert> u

【数据结构作业—02】双链表

2.实现下述要求的Locate运算的函数 问题描述 设有一个带表头结点的双向链表L,每个结点有4个数据成员:指向前驱结点的指针prior.指向后继结点的指针next.存放数据的成员data和访问频度freq.所有结点的freq初始时都为0.每当在链表上进行一次Locate (L, x)操作时,令元素值为x的结点的访问频度freq加1,并将该结点前移,链接到与它的访问频度相等的结点后面(如果该结点没有找到与它访问频度相等的结点,链接到表头后面结点),使得链表中所有结点,保持按访问频度递减的顺序排列

java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制)

转载请注明出处(请尊重原创!谢谢~): http://blog.csdn.net/javazejian/article/details/53073995 出自[zejian的博客] 关联文章: java数据结构与算法之顺序表与链表设计与实现分析 java数据结构与算法之双链表设计与实现 java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制) ??这篇是数据结构与算法的第3篇,通过前两篇的介绍,对应顺序表和链表已有