关闭

double list

173人阅读 评论(0) 收藏 举报
分类:


#ifndef LIST_H_
#define LIST_H_

struct Node
{
	int data;
	Node *next;
	Node *pre;
	Node(int d){ data = d; }
};

class List
{
public:
	List();
	List(int *arr, int size);    //根据数组构造链表
	~List();
	Node *getHead(){ return head; }
	void setHead(Node *newHead){ head = newHead; tail = newHead; }
	void display();              //从头显示链表
	void display(int);           //从尾显示链表
	Node *find(const int num);   //寻找元素num,返回对应结点,没找到返回NULL
	void transfer(Node *position, Node *first, Node *last);    //将结点[first,last)之间的元素移动到position节点之前,注意是前开后闭
	void merge(List &list);      //两个递增的链表合并成一个递增的链表
	void reverse();              //反转链表
	void swap(List &list);       //交换链表
	void sort();                 //排序
private:
	Node *head;
	Node *tail;
};

#endif


#include "list.h"
#include <iostream>
using namespace std;
List::List()
{
	head = new Node(0);   //哨兵节点
	tail = head;
	head->next = tail;
	tail->pre = head;
	tail->next = head;
	head->pre = tail;
}

List::List(int *arr, int size)
{
	tail = new Node(0);
	head = tail;
	head->pre = tail;
	tail->next = head;
	if (size == 0) return;
	Node *p = NULL, *f = tail;
	for (int i = 0; i < size; i++)
	{
		Node *temp = new Node(arr[i]);
		if (i == 0)	head->next = temp;
		else
		{
			p->next = temp;
			p->pre = f;
			f = p;
		}
		p = temp;
	}
	p->next = tail;
	p->pre = f;
	tail->pre=p;
}

List::~List()
{
	while (head != tail)
	{
		Node *p = head->next;
		delete head;
		head = p;
	}
	delete head;
}

void List::display()
{
	Node *p = head->next;
	while (p != tail)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}


void List::display(int)
{
	Node *p = tail->pre;
	while (p != tail)
	{
		cout << p->data << " ";
		p = p->pre;
	}
	cout << endl;
}

Node *List::find(int num)
{
	Node *p = head->next;
	while (p != tail)
	{
		if (p->data == num) return p;
		p = p->next;
	}
	return NULL;
}

void List::transfer(Node *position, Node *first, Node *last)
{
	if (position != last)
	{
		last->pre->next = position;            //(1)
		position->pre->next = first;           //(2)
		first->pre->next = last;               //(3)
		//此处不能这样:first->pre=position->pre;如果这样,first原来的pre就丢了,所以不需有个temp
		Node *temp = position->pre;            //(4)
		position->pre = last->pre;             //(5)
		last->pre = first->pre;                //(6)
		first->pre = temp;                     //(7)
	}
}

void List::merge(List &list)
{
	Node *tail2 = list.getHead();
	Node *p1 = head->next, *p2 = tail2->next;

	while (p1 != tail && p2 != tail2)
	{
		if (p2->data < p1->data)
		{
			Node *n = p2;
			n = n->next;
			transfer(p1, p2, n);
			p2 = n;
		}
		else p1 = p1->next;
	}
	if (p2 != tail2) transfer(p1, p2, tail2);
}

void List::reverse()
{
	Node *p = head->next;
	while (p->next != tail)	transfer(p, tail->pre, tail);
}

void List::swap(List &list)
{
	Node *temp = head;
	head = list.getHead();
	tail = list.getHead();
	list.setHead(temp);
}

void List::sort()
{
	List carry;
	List counter[64];
	int fill = 0;          
	while (head->next != tail)
	{
		carry.transfer(carry.getHead()->next, head->next, head->next->next);
		int i = 0;
		while (i < fill && counter[i].getHead()->next != counter[i].getHead())
		{
			counter[i].merge(carry);
			carry.swap(counter[i++]);
		}
		carry.swap(counter[i]);
		if (i == fill) ++fill;
	}
	for (int i = 1; i < fill; i++)
		counter[i].merge(counter[i-1]);
	swap(counter[fill - 1]);
}

#include <iostream>
#include "list.h"
using namespace std;


int main()
{
	int arr[] = {1,3,5,7,9};
	List myList1(arr,5);
	int arr2[] = { 2, 4, 6, 8 ,10};
	List myList2(arr2,5);
	myList1.transfer(myList1.find(1),myList2.find(2),myList2.getHead());
	//myList1.merge(myList2);
	//myList1.display();
	//myList1.reverse();
	myList1.sort();
	myList1.display();
	return 0;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Java对List对象进行排序

有时候需要对List对象进行排序,如果每一处都去写一个排序方法,就会产生重复代码的坏味道,而且每一处都写,工作量会很大。 我们知道,Java提供了一个Collections.sort()方法可以对Li...
  • zht666
  • zht666
  • 2013-11-27 17:50
  • 11030

Java集合类库 LinkedList 源码解析

基于JDK 1.7,和ArrayList进行比较分析Java已经有了ArrayList,用来存放元素,对元素的操作都很方便。为什么还会有LinkedList呢?我们都知道ArrayList获取元素很快...
  • u012301841
  • u012301841
  • 2016-07-16 17:16
  • 1796

排序与list.sort()使用

排序与List.sort()
  • u011611902
  • u011611902
  • 2015-10-27 16:46
  • 10411

List control with single / double separator lines带有一个/两

  • 2006-02-23 09:05
  • 10KB
  • 下载

double linked list双向链表

[tags] C++双向链表的实现与单向链表相近 节点结构体 类声明 总结双向链表的实现(与单向链表相近)1. 节点结构体struct DouListNode { int elem; DouL...
  • lichen_yun
  • lichen_yun
  • 2016-04-27 21:28
  • 582

双链表(double_linked_list):(增、删、改、查、逆置)的C++的例子,稍微改一下,就成C。

1 #include   2 #include   3 #include   4 using namespace std;   5   6 struct dl_node   7 {   8  ...
  • CIT_RWC
  • CIT_RWC
  • 2013-07-17 22:31
  • 932

leetcode facebook面试 convert bst to double-linked list

题意: convert  bst to double-linked list 代码: struct BSTree { BSTree(int v) :val(v), left(NULL), ri...
  • shiyang6017
  • shiyang6017
  • 2016-10-19 22:15
  • 358

Convert Binary Search Tree to an Ordered Double Linked List

输入: 一棵二叉查找树输出: 与二叉查找树对应的有序双向链表要求:不允许额外生成节点,只允许调整指针指向。例如:     10   / /   6   1   / / / /    4  8...
  • FlyingIceCS
  • FlyingIceCS
  • 2011-05-20 00:01
  • 863

No. 01 - Binary Search Tree and Double-linked List

No. 01 - Binary Search Tree and Double-linked List Question: Convert a binary search tree to a ...
  • zldeng_scir
  • zldeng_scir
  • 2012-07-13 19:36
  • 519

double_linked_list in Python

# -*- coding: utf-8 -*- class Frob(object): def __init__(self, name): self.name = name ...
  • u013805817
  • u013805817
  • 2014-05-01 21:33
  • 1718
    欢迎订阅公众号:IT程序员开发
    欢迎订阅公众号:IT程序员开发
    个人资料
    • 访问:76485次
    • 积分:2842
    • 等级:
    • 排名:第14443名
    • 原创:217篇
    • 转载:4篇
    • 译文:0篇
    • 评论:27条
    最新评论