x******a 发帖数: 6336 | 1 //main()
#include
#include
#include "linkedlist.h"
int main()
{
LinkedList myList(5, "luke");
Iterator myIterator=myList.begin();
myList.insert(myIterator, "leia");
myIterator.forward();
myList.erase(myIterator);
myList.insert(myIterator, "chewbca"); 《======出问题。
myList[2]="han";
for (int i=0; i
std::cout<
}
myList.insert(myIterator, "chewbca"); 《======出问题。
这一句运行起来take forever:test了一下,好像前面erase一个元素对insert有影响?
//linkedlist.h
#ifndef __linkedlist__linkedlist__
#define __linkedlist__linkedlist__
#include
template class LinkedList;
template class Iterator;
template class Node;
template
class Node{
public:
Node(T newData);
friend class LinkedList;
friend class Iterator;
private:
T data;
Node* next;
Node* prev;
};
template
Node::Node(T newData){
data= newData;
next= NULL;
prev= NULL;
}
template
class Iterator{
public:
Iterator();
T get() const;//return data value we point to
void forward();
void backward();
bool isNull();
friend class LinkedList;
private:
Node* position;
};
template
Iterator::Iterator(){
position=NULL;
}
template
T Iterator::get() const{
return position->data;
}
template
void Iterator::forward(){
position=position->next;
}
template
void Iterator::backward(){
position=position->prev;
}
template
bool Iterator::isNull(){
return(position==NULL);
}
template
class LinkedList{
public:
LinkedList();
LinkedList(int numNodes, T defaultValue);
LinkedList(const LinkedList& right);
LinkedList& operator=(const LinkedList& right);
~LinkedList();
void erase(Iterator iter);
void insert(Iterator iter, T value);
Iterator begin() const;
Iterator end() const;
unsigned int size() const;
T& operator[](int index);
private:
Node* first;
Node* last;
};
template< typename T>
LinkedList::LinkedList(){
first=NULL;
}
template
Iterator LinkedList::begin() const{
Iterator iter;
iter.position=first;
return iter;
}
template
void LinkedList::erase(Iterator iter){
Node* pos=iter.position;
iter.forward();
if (pos==first)
first=pos->next;
else
(pos->prev)->next= pos->next;
if (pos==last)
last=pos->prev;
else
(pos->next)->prev=pos->prev;
delete pos;
return;
}
template
LinkedList::~LinkedList(){
while(first!=NULL)
erase(begin());
}
template
void LinkedList::insert(Iterator iter, T value){
Node* pos=iter.position;
Node* newNode=new Node(value);
if(first==NULL){
first=newNode;
last=newNode;
return;
}
newNode->prev=pos->prev;
newNode->next=pos;
if (newNode->prev==NULL){
first=newNode;
}else{
(newNode->prev)->next=newNode;
}
pos->prev=newNode;
return;
}
template
LinkedList::LinkedList(int numNodes, T defaultValue){
first=NULL;
last =NULL;
for(int i=0;i
insert(begin(), defaultValue);
}
template
LinkedList::LinkedList(const LinkedList& right){
first=NULL;
last=NULL;
Iterator iter_right=right.end();
while (!iter_right.isNull) {
insert(begin(), iter_right.get());
iter_right.backward();
}
}
//LinkedList operator=(const LinkedList& right);
template
LinkedList& LinkedList::operator=(const LinkedList& right){
if(this!=&right){
Iterator iter_left=begin();
while (iter_left.position!=NULL) {
erase(iter_left);
iter_left=begin();
}
Iterator iter_right= right.end();
while (!iter_right.isNull) {
insert(begin(), iter_right.get());
iter_right.backward();
}
}
return *this;
}
template
unsigned int LinkedList::size() const{
Iterator iter=begin();
int length=0;
while (!iter.isNull()) {
iter.forward();
length++;
}
return length;
}
template
T& LinkedList::operator[](int index){
Node* pos=first;
for(int i=0; i
pos=pos->next;
}
return pos->data;
}
#endif /* defined(__linkedlist__linkedlist__) */ |
l*********8 发帖数: 4642 | |
x******a 发帖数: 6336 | 3 发现问题出现在这两行,
myList.erase(myIterator);
myList.insert(myIterator, "chewbca");
我先通过iterator删除一个元素,然后再通过这个interator插入一个元素好象不work
。 如果iterator往前再往后挪一下就work了,是不是删除元素影响到iterator了?
【在 l*********8 的大作中提到】 : 这么长。。。
|
c********t 发帖数: 5706 | 4 you are not supposed to use iterator to insert or delete element;
work
【在 x******a 的大作中提到】 : 发现问题出现在这两行, : myList.erase(myIterator); : myList.insert(myIterator, "chewbca"); : 我先通过iterator删除一个元素,然后再通过这个interator插入一个元素好象不work : 。 如果iterator往前再往后挪一下就work了,是不是删除元素影响到iterator了?
|
l*********8 发帖数: 4642 | 5 myIterator = myList.erase(myIterator);
work
【在 x******a 的大作中提到】 : 发现问题出现在这两行, : myList.erase(myIterator); : myList.insert(myIterator, "chewbca"); : 我先通过iterator删除一个元素,然后再通过这个interator插入一个元素好象不work : 。 如果iterator往前再往后挪一下就work了,是不是删除元素影响到iterator了?
|
x******a 发帖数: 6336 | 6 Thanks a lot! it worked.
再请教一个问题:我想历遍一个通过frontinsert生成的linkedlist,为什么看不到第
一个insert的元素?谢谢
#include
#include "singlylinkedlist.h"
int main(int argc, const char * argv[])
{
SingleLinkedList aList;
int i=0;
while(i<10){
aList.firstInsert(i);
++i;
}
aList.traverse();
}
输出是
9 8 7 6 5 4 3 2 1
没有0
template< typename T> class ListElement;
template< typename T> class SingleLinkedList;
template
class ListElement{
public:
ListElement(const T& value): data(value), next(NULL) {}
~ListElement();
friend class SingleLinkedList;
friend class myIterator;
private:
T data;
ListElement* next;
};
template
class SingleLinkedList{
public:
SingleLinkedList(): head(NULL) {}
~SingleLinkedList(){}
bool isEmpty(){ return (head==NULL);}
void firstInsert(const T& val);
void traverse() const;
private:
ListElement* head;
};
template
void SingleLinkedList::firstInsert(const T& val) {
ListElement* newData= new ListElement(val);
newData->next= head;
head=newData;
}
template
void SingleLinkedList::traverse() const{
ListElement* pt=head;
while (pt->next!=NULL) {
std::cout<data<<"\t";
pt=pt->next;
}
std::cout<< std::endl;
}
【在 l*********8 的大作中提到】 : myIterator = myList.erase(myIterator); : : work
|
l*********8 发帖数: 4642 | 7 traverse函数里
while (pt!=NULL) {
【在 x******a 的大作中提到】 : Thanks a lot! it worked. : 再请教一个问题:我想历遍一个通过frontinsert生成的linkedlist,为什么看不到第 : 一个insert的元素?谢谢 : #include : #include "singlylinkedlist.h" : int main(int argc, const char * argv[]) : { : SingleLinkedList aList; : int i=0; : while(i<10){
|