string双向链表

写一个string双向链表模块,具有提取和插入功能。通过建立一个程序语言种类的表来练习这个模块。为这个表提供一个sort()函数。并提供一个函数反转表中字符串的顺序。

环境:vs2005 win32控制台程序

#include <iostream>
#include <assert.h>
#include <string>
#include <algorithm> // for std::swap
using std::string;

namespace StringList
{
// using std::string;

struct Node
{
string string_;
Node *prev_;
Node *next_;
} head_node, tail_node; // Start node and End node

typedef Node *Cursor; // Cursor like a iterator

void init()
{
head_node.prev_ = tail_node.next_ = 0;
head_node.next_ = &tail_node;
tail_node.prev_ = &head_node;
}

inline Cursor next(Cursor c)
{
assert(c);
return c->next_;
}

inline Cursor prev(Cursor c)
{
assert(c);
return c->prev_;
}

inline Cursor head() { return &head_node; }
inline Cursor tail() { return &tail_node; }

string extract(Cursor c)
{
assert(c != head() && c != tail());
string s = c->string_;
c->prev_->next_ = c->next_;
c->next_->prev_ = c->prev_;
delete c;
return s;
}

Cursor insert_after(Cursor c, string s)
{
assert(c != tail());
Node *n = new Node;
n->string_ = s;
n->prev_ = c; // Before n
n->next_ = c->next_; // After n
c->next_ = n; // After c;
n->next_->prev_ = n; // Before n->next_
return n;
}

Cursor insert_before(Cursor c, string s)
{
assert(c != head());
Node *n = new Node;
n->string_ = s;
n->prev_ = c->prev_;
n->next_ = c;
c->prev_ = n;
n->prev_->next_ = n;
return n;
}

void reverse()
{
// First exchange the prev_ and next_ pointers
// inside each internal node:
for (Cursor c = next(head()); c!=tail(); c = c->prev_)
std::swap(c->prev_, c->next_);

// Then exchange the head and tail positions:
tail()->prev_->prev_ = head();
head()->next_->next_ = tail();

std::swap(head()->next_, tail()->prev_);
}

namespace // unnamed namespace
{
void quicksort(Cursor left, Cursor right)
{
Cursor p = left->next_;
Cursor l = left->next_, r = right->prev_;

if (left == right || l == right || l == r) return;

while (true)
{
while (l != r && l->string_ < p->string_)
l = next(l);
while (l != r && p->string_ <= r->string_)
r = prev(r);
if (l == r)
break;
std::swap(l->string_, r->string_);
}

if (l->string_ < p->string_)
{
if (r->next_ != right) l = next(l);
}
else
{
if (left->next_ != r) r = prev(r);
}

quicksort(left, l);
quicksort(r, right);
}
} // End unnamed namespace

void sort()
{
if (head()->next_ != tail() && head()->next_->next_ != tail())
quicksort(head(), tail());
}
} // End namespace StringList

void print_stringlist()
{
StringList::Cursor p = StringList::head();
while ((p = StringList::next(p)) != StringList::tail())
std::cout << p->string_ << std::endl;
}

int main()
{
StringList::init();
StringList::insert_before(StringList::tail(), string("Cobol"));
StringList::insert_before(StringList::tail(), string("Fortran"));
StringList::insert_before(StringList::tail(), string("Lisp"));
StringList::insert_before(StringList::tail(), string("Algol 68"));
StringList::insert_before(StringList::tail(), string("Basic"));
StringList::insert_before(StringList::tail(), string("Pascal"));
StringList::insert_before(StringList::tail(), string("C"));
StringList::insert_before(StringList::tail(), string("Ada"));
StringList::insert_before(StringList::tail(), string("Modula-2"));
StringList::insert_before(StringList::tail(), string("C++"));
std::cout << ">>> Original list of strings: \n";
print_stringlist();
std::cout << "\n>>> Reversed list of strings: \n";
StringList::reverse();
print_stringlist();
std::cout << "\n>>> Sorted list of strings: \n";
StringList::sort();
print_stringlist();
std::cout << "\n>>> Done.\n";

return 0;
}


string双向链表

时间: 2024-10-02 11:16:31

string双向链表的相关文章

Shuffling Machine和双向链表

1. 双向链表 https://github.com/BodhiXing/Data_Structure 2. Shuffling Machine https://pta.patest.cn/pta/test/17/exam/4/question/264 思路: 代码: 1 #include <iostream> 2 using namespace std; 3 4 #define MAXCARD 54 5 6 string int2str(int x) 7 { 8 char ch[4]; 9

数据结构 线性双向链表

//线性双向链表 #ifndef _MY_DLINKLIST_H_ #define _MY_DLINKLIST_H_ typedef void DLinkList; typedef struct _tag_DLinkListNode { struct _tag_DLinkListNode* next; struct _tag_DLinkListNode * pre; }DLinkListNode; //创建双向链表 DLinkList* DLinkList_Create(); //销毁双向链表

研磨数据结构与算法-02双端链表与双向链表

Node节点: /* * 链结点,相当于是车厢 */ public class Node { //数据域 public long data; //指针域 public Node next; public Node previous; public Node(long value) { this.data = value; } /** * 显示方法 */ public void display() { System.out.print(data + " "); } } 双端链表: /*

C++标准模版库(STL)双向链表(list)的使用

双向链表是一个常用的数据结构.它并不复杂,如果我们要自己实现也不是太困难的事情.但既然STL已经给我们提供了一个,不妨直接用.这样做,不但省时省力,而且代码的复用性也好. 头文件与模板类 要想使用STL提供的双向链表,需要包含头文件 #include <list> 这样,便可以使用模板类list<T>. 初始化 初始化一个list很简单,用 std::list<T> L; 便可以初始化一个空的链表.用 std::list<T> L{ t1, t2, t3 }

HDU - 5009 Paint Pearls(dp+双向链表优化)

Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In each operation,

C#双向链表

/// <summary> /// 双向链表节点类 /// </summary> /// <typeparam name="T">节点中的存放的数据类型</typeparam> public class Node<T> where T:IComparable<T> { /// <summary> /// 当前节点的数据 /// </summary> T data; /// <summa

剑指Offer之二叉搜索树与双向链表

题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 基本思路 假设二叉搜索树为{10,6,14,4,8,12,16},按照中序遍历,当我们遍历转换到根节点(值为10的节点)时,它的左子树已经转换成一个排序的链表了,并且处在链表的最后一个节点是当前最大的节点.我们把值为8的节点和根节点链接起来,此时链表中的最后一个节点就是10了.接着我们去遍历转换右子树,并把根节点和右子树的最小的节点链接起来.转换左子树和右子树,使用递归的

05 双向链表

双向链表 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 // 数据结构 6 typedef struct node 7 { 8 int data; 9 struct node *prev; 10 struct node *next; 11 }doubleList; 12 13 doubleList *doubleList_init(void) 14 { 15 doubleList

ArrayList和Vector的区别?HashMap和HashTable的区别?StringBuilder、StringBuffer和String的区别?

ArrayList和Vector的区别?从两个方面 1.同步性:ArrayList是线程不安全的,是非同步的:Vector是线程安全的,是同步的.(Java中线程的同步也就满足了安全性) 2.数值增长:ArrayList每次增长为原来的50%;Vector每次增长为原来的100%; (从内部实现机制来讲,ArrayList和Vector都是使用数组(Array)来控制集合中的对象,当向集合中添加对象时,如果内部数组长度不够用时,长度会自动增长.ArrayList会增长为原来的1.5倍,Vecto