Implementing doubly linked lists with multiple arrays of objects

Source: Internet
Author: User
Tags arrays prev

In some languages, such as Fortran, where pointers and object data types are not available, how do you implement a doubly linked list? We will introduce the use of arrays and array subscripts to construct a doubly linked list logically, so that it behaves just as naturally as a pointer implements.


multiple array representations of objects

The following figure is a logical diagram of a doubly linked list implemented with an array:

For a set of objects with the same domain, each field can be represented by an array. The figure above illustrates how to implement a doubly linked list with three arrays. The keywords for dynamic geometry are stored in the array key, and the pointer fields are stored in prev and next. For a given subscript x,key[x],prev[x],next[x] long together to form an object in a linked list, that is, a node, in this interpretation, a pointer x is a common subscript pointing to the array Key,prev and next.

In the list above, the object of keyword 4 is behind the object with the keyword 16. Correspondingly, the keyword appears on the key[2], the keyword 16 appears in the key[5], so there is next[5] = 2;prev[2] = 5. Although Changshu nil (/) appears in the next field of the footer and in the Prev domain of the table header, But we usually use an integer that does not point to any of the locations in the array (in our implementation code, take Noexist-1). In addition, the variable L stores the subscript of the header element.


allocating and releasing objects to insert an object into a dynamic collection represented by a doubly linked list, you need to assign a pointer to an object that is currently not being used in the list representation, which is the subscript.    Then, we need to manage the unused space in the list so that it can be easily allocated. Here, we use the pointer field to organize the free objects into a single linked list, which is a free list. The free list is used only in the next field, which holds the subscript for the next free object, which is stored in the. When the list L is non-empty, the free list and the doubly linked list L will be staggered together, as shown in the following figure.     However, a space exists either in a free list or in a doubly linked list, and cannot exist in both. A) for the start of the list, b) for the result after inserting the keyword 25; c) for deleting the result of the keyword 16.
Freedom is similar to a stack, where each allocation takes place from the free-link header, which is the recently released one, and the following is the function of allocating and reclaiming free objects.
Get a Free object

Template <typename t>
inline size_t list<t>::getfree ()
{
	if (free = = noexist)
	{//If the list is empty, You need to add space
		size_t *old_next = Next, *old_prev = prev;
		T *old_key = key;
		size_t old_list_size = list_size;
		List_size *= 2;
		NewList (list_size);//Allocate new space
		copylist (Old_prev, Old_key, Old_next, old_list_size);//copy content to new space
		DeleteList ( Old_prev,old_key,old_next);//release old space
		Setfree (old_list_size);
	}
	size_t index = free;
	free = Next[index];
	return index;
}

Releasing a node
void Addfree (size_t index)
	{//Add free space to free-link list
		next[index] = freely;
		free = index;
	}

When space is sufficient, the time cost of the above two processes is O (1), so it is useful.
the implementation of doubly linked list with multi-array representation      As we all know, ordinary arrays have a flaw in the ability to dynamically change size, and each time must allocate enough space in advance. For the use of multiple arrays to achieve the double-linked list, and to behave as if the pointer to achieve the same natural, the primary solution is the dynamic allocation and reclaim space problems.       In the above allocation process Getfree we have seen the adoption of the strategy is that once the space is not enough, immediately allocate two times the original size, and copy the original content into the new space, and then release the old space. Solve this problem, then the next implementation is very simple, logically we can change the two linked lists free and l to the familiar look to understand the insertion and deletion process.
Implementing the Source code:
#include <iostream> #define Noexist 0x7fffffff using namespace std;
Template <typename t> class list;

Template <typename t> ostream& operator<< (ostream&, const list<t> &);
	Template <typename t> class List {private:size_t *next;
	T *key;
	size_t *prev; size_t free;//Manage free node list, single linked list can size_t head;//manage used nodes linked list, double linked list size_t list_size;//list total size, including free link list friend ostream& operator&l
	t;< <T> (ostream&, const list &);
		size_t getfree ();//Get free node void Addfree (size_t index) {//Add free space to free-link list next[index] = freely;
	free = index;
		} void NewList (size_t n) {//Allocated space key = new T[n];
		Next = new Size_t[n];
	prev = new Size_t[n];
		} void DeleteList (size_t *p, T *k, size_t *n) {//Reclaim space delete[] p;
		Delete[] k;
	Delete[] n;
		} void Copylist (size_t *p, T *k, size_t *n, size_t s) {//Copy linked list copy (p, p + S, prev);
		Copy (n, n + S, next);
	Copy (k, k + S, key); } void Setfree (size_t start_index) {//Set free-link list next[list_size-1] = NoexisT
		for (size_t i = start_index; I! = list_size-1; ++i) Next[i] = i + 1;
	free = Start_index;
		} public:list (): Free (noexist), Head (noexist), List_size (8)//-1 indicates NULL, initially linked list has 8 spaces {//initialized, setting free list newlist (list_size);
	Setfree (0);
	} list (t *beg, T *end): list () {insert (beg, end);}
	void SetData (size_t index, const t &t) {Key[index] = t;}
	T getData (size_t index) const {return key[index];}
	void Insert (const t&);
	void Insert (t*, t*);
	size_t Locate (const t&);
	void Erase (const t&);
	void Erase (size_t);
	void edit (const t&, const t&); bool Empty () {return head = = noexist;}
	Whether the linked list is empty ~list () {deletelist (prev, key, next);

}//bool Full () {return free = =-1;}}; Template <typename t> inline size_t list<t>::getfree () {if (free = = noexist) {///if the freely linked list is empty, you need to add space size_t *ol
		D_next = Next, *old_prev = prev;
		T *old_key = key;
		size_t old_list_size = list_size;
		List_size *= 2;
		NewList (list_size); Copylist (Old_prev, Old_key, Old_nexT, Old_list_size);
		DeleteList (Old_prev,old_key,old_next);
	Setfree (old_list_size);
	} size_t index = free;
	free = Next[index];
return index;
	} template <typename t> void List<t>::insert (const T &t) {size_t index = Getfree ();
	Key[index] = t;
		if (head = = noexist) {//Insert the first node next[index] = noexist;
		Prev[index] = noexist;
	Head = index;
		} else {//otherwise next[index] = head;
		Prev[head] = index;
		Prev[index] = noexist;
	Head = index; 

}} template <typename t> void List<t>::insert (t *beg, T *end) {for (; Beg! = end; ++beg) insert (*beg);}
	Template <typename t> size_t list<t>::locate (const T &t) {size_t p = head;
	while (P! = noexist && key[p]! = t) p = next[p];
return p; } template <typename t> void list<t>::erase (size_t index) {if (index = = head)//delete head node head = Next[index]
	;
		else {Next[prev[index]] = Next[index]; if (Next[index]! = noexist)//If the last node is not deleted Prev[next[index]] = prev[iNdex];
} addfree (index);
	} template <typename t> void list<t>::erase (const T &t) {size_t index = locate (T);
if (Index! = noexist) erase (index); } template <typename t> void list<t>::edit (const t &old_key, const T &new_key) {size_t index = loc
	Ate (Old_key);
		if (index = = noexist) {cout << old_key << "isn ' t exist!" << Endl;
	Return
} Key[index] = New_key;  } template <typename t> ostream& operator<< (ostream &out, const list<t> &lst) {size_t p =
	Lst.head;
		while (P! = noexist) {out << lst.key[p];
		if (lst.next[p]! = noexist) out << ';
	p = lst.next[p];
} return out;
	} int main () {int a[] = {1, 2, 3, 4, 5, 6};
	list<int> LST (a,a + 6);
	cout << lst << endl << Endl;
	for (int i = ten; I! =); ++i) Lst.insert (i);
	cout << lst << endl << Endl;
		for (int i = 1; I! =), ++i) {size_t f = lst.locate (i); if (f! = NOEXIST) lst.erase (f);
	else Lst.insert (-i);
	} cout << lst << Endl;
	GetChar ();
return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.