Home > Back-end >  Small white questions C simple list container problem, very urgent!!!!
Small white questions C simple list container problem, very urgent!!!!

Time:09-23

This is a topic, but later ask and say a tail has a problem
Due to too dishes so can't write...
Excuse me, can help the completion code fragments? Probably mean already annotation
Makes the program runs after the following output:
I love c + + very much!
I love c + +
I
Love
C + +

Code now
# include
# include

using namespace std;

//MyList
The template & lt; The class T> Struct ListNode {
ListNode (const T& _data while forming=(https://bbs.csdn.net/topics/T) : the pre (0), next (0), data (_data while forming) {}

ListNode * the pre;
ListNode * next;
T data;
};

The template & lt; Typename T> The class MyList {
Private:
Typedef ListNode The Node;
ListNode * the head;
ListNode * tail;
The class list_iterator {
Private:
Node * PTR.//pointer to an element of mylist container
Public:
List_iterator (Node * p=nullptr) : the PTR (p) {}
//*, + +, -, - & gt; Basic operations such as
T & amp; The operator * () const {
//return it to modify the object reference is convenient by *
The return of PTR - & gt; The data;
}
The Node * operator - & gt; (a) const {
//overload - & gt; The operator
Return PTR.
}
List_iterator & amp; Operator++ () {
//TODO: iterators + +, make the iterator built-in pointer move back a

}
List_iterator operator++ (int) {
//TODO: rear + +

}
Boolean operator==(const list_iterator & amp; T) const {
//TODO: overloading==

}
Boolean operator!=(const list_iterator & amp; T) const {
//TODO: overloading!=

}
};
Public:
Typedef list_iterator iterator.
MyList () : the head (new Node) {
The head - & gt; Next=head;
The head - & gt; The pre=head;
};
~ MyList () {
//the Clear ();
Delete the head;
The head=NULL;
};

Bool empty () {//whether the list is empty
Return the head - & gt; Next==head;
}
Void push_back (const T& Data) {//tail insert data
Newnode Node *=new Node (data);
If (empty ())
{
The head - & gt; Next=newnode;
The head - & gt; The pre=newnode;
Newnode - & gt; Next=head;
Newnode - & gt; The pre=head;
}
The else
{
Node * tail=head - & gt; The pre.
Tail - & gt; Next=newnode;
Newnode - & gt; Next=head;
Newnode - & gt; The pre=tail;
The head - & gt; The pre=newnode;
}
}
Void pop_back () {//remove the tail data
if (! The empty ()) {
Node * del=head - & gt; The pre.
Node * tail=del - & gt; The pre.
Delete the del.
Tail - & gt; Next=head;
The head - & gt; The pre=tail;
}
}
Void the print () {//print the list
if (! The empty ()) {
Node * cur=head - & gt; Next;
While (cur!={the head)
Cout & lt; & lt; Cur - & gt; The data & lt; & lt; "';
Cur=cur - & gt; Next;
}
Cout & lt; & lt; Endl;
}
}
Int size () {//return the list size
Node * cur=head - & gt; Next;
int count=0;
While (cur!={the head)
+ + count;
Cur=cur - & gt; Next;
}
Return the count.
}
//operation method of the iterator
Const iterator the begin () {
//TODO: the returned list head pointer
}
Const iterator end () {
//TODO: return to list the tail pointer
}
//other member functions can attempt to achieve insert/erase
};

Int main () {
MyList Mylist_str;
Mylist_str. Push_back (" I ");
Mylist_str. Push_back (" love ");
Mylist_str. Push_back (c + + "");
Mylist_str. Push_back (" very ");
Mylist_str. Push_back (" much!" );
Mylist_str. Print ();
Mylist_str. Pop_back ();
Mylist_str. Pop_back ();
Mylist_str. Print ();
For (MyList : : iterator it=mylist_str. The begin (); it !=mylist_str. End (); It++) {
Cout & lt; & lt; * it & lt; & lt; Endl;
}

return 0;
}
Thank you very much!

  • Related