Introduction to algorithms tenth chapter data structure--doubly linked list

Source: Internet
Author: User

Look at the concept is very hazy, there is no clear double-linked list exactly what needs to be done, in fact, for this structure should be able to write a lot of methods, and there is no specific criteria.

But I really don't know how to do it. All kinds of mistakes, all kinds of overflow

#include <iostream>


using namespace Std;
Template<class t> struct Node
{
T value;
node<t>* Pre;
Node<t>* Next;


};
Template<class T> class Flist
{
Private
Node<t>* Front;
Node<t>* end; Each write a function that iterates over the variables in the class which is to be maintained
int count;

Public
Flist (); Defaut Constructor
~flist ();
Flist (const flist& c_list);//support this, no write support = can write an empty inline function to vent this behavior, otherwise the compiler will automatically generate
inline void deeply_copy (const flist& copylist);
BOOL Isempty () const;
int listsize () const;
t& Listfront () const;
t& listend () const; Returns whether the reference can change the key value of the node.
void Push_front (T N);
void Push_back (T N);
void Del_front ();
void Del_back ();
node<t>* Listfind (T x); Search for a node with a key value X
T ShowKey (int n); Output nth Value

};
Template<class t> flist<t>::flist (): Count (0), front (0), end (0)//initialization order related to declaration in class is irrelevant
{

}
Template<class t> flist<t>::~flist ()
{
node<t>* temp;
while (front! = 0)
{
temp = Front;
Delete temp;
Front = front->next;
}
temp = NULL;
}
Template<class t> BOOL Flist<t>::isempty () const
{
return 0 = = count;
}
Template<class t> int flist<t>::listsize () const{
return count;
}
Template<class t> flist<t>::flist (const flist& c_list)
{
Deeply_copy (c_list);

}
Template<class t> t& flist<t>::listfront () const
{
Return front->value;
}
Template<class t> t& flist<t>::listend () const
{
Return end->value;
}
Template<class t> void Flist<t>::p ush_front (T N)
{
node<t>* p = new node<t>;
P->value = N;
P->next = Front;
if (front! = 0)
Front->pre = p;
Front = p;
if (end = = 0)
end = P;
P->pre = 0;
count++;
}
Template<class t> void Flist<t>::p ush_back (T N)
{
node<t>* p = new node<t>;
P->value = N;
P->pre = end;
if (end! = 0)//The start of this crash is because end is not initialized 0 is used
End->next = p;
end = P;
End->next = 0;
count++;
}
Template<class t> void Flist<t>::d El_front ()
{
node<t>* temp = front;
Front = front->next;
count--;
Front->pre = 0;
Delete temp;
}
Template<class t> void Flist<t>::d El_back ()
{
node<t>* temp = end;
End = end->pre;
End->next = 0;
count--;
Delete temp;
}

Template<class t> T flist<t>::showkey (int n)
{

/*
if (front = = 0)
{
cout << "There is no element is the list:" << Endl;
return; Not resolved, size is 0 o'clock, how to prompt
}*/
if ((front!=0) && (n <=count) && (n >= 1))
{
node<t>* temp = front;
while (--N)//here if the while (n--) overflow crashes, n use after minus, here is to judge, do not think just parentheses can be used before and after
{
temp = temp->next; n Greater than size also overflows
}
Return temp->value;
Here temp points to a node that also has other pointers, do not delete
}

}
Template<class t> void Flist<t>::D eeply_copy (const flist& copylist)
{
Front = end = 0;
if (Copylist.front = = 0)
return; This is where the End function works.
Front = new node<t>;
node<t>* CP = Copylist.front;
node<t>* NP = front;
Np->value = cp->value;
Count = 1;
Np->pre = 0;
CP = cp->next;
while (cp! = 0)
{
Np->next = new node<t>;
count++;
(np->next)->pre = NP;
NP = np->next;
Np->value = cp->value;
CP = cp->next;
}
end = NP;
}
int main ()
{
Flist<int> LISTF;
int a[5] = {1,2,3,4,5};
for (int i = 0; i < 5; i++)
Listf.push_front (A[i]);
cout << "lisrfsize:" << listf. Listsize () << Endl;
for (int i = 0; i < 5; i++)
Listf.push_back (A[i]);
cout << "lisrfsize:" << listf. Listsize () << Endl;
cout << "Listf is empty?:" << listf. Isempty () << Endl;
Flist<int> LISTF2 (LISTF);
cout << "listf2size:" << listf2. Listsize () << Endl;
Listf2.del_front ();
Listf2.del_back ();
cout << "LISTF2 front:" << LISTF2. Listfront () << Endl;
cout << "LISTF2 end:" << LISTF2. Listend () << Endl;
cout << "LISTF2 size:" << listf2. Listsize () << Endl;
for (int i = 1; I <= LISTF2. Listsize (); i++)
cout << LISTF2. ShowKey (i) << Endl;


return 0;
}

Introduction to algorithms tenth chapter data structure--doubly linked list

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.