【发布时间】:2015-06-08 11:39:29
【问题描述】:
我的问题是,运行程序后,我得到的唯一东西是列表的头元素和第三个元素,无论我要交换哪些元素。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
class List;
class element{
private:
int number;
element* next;
friend class List;
friend ostream & operator<<(ostream &,const List &);
public:
element(int data){
number=data;
}
int getData(){
return number;
}
};
class List{
private:
element* head;
int size;
friend ostream & operator<<(ostream &,const List &);
public:
List(){
head=NULL;
size=0;
}
List(const List &lista){
//cout << "wyw" << endl;
//fflush(stdout);
if (lista.size==0){
size=0;
head=NULL;
}
else
{
size=0;
head=NULL;
element* tmp=lista.head;
while(tmp!=NULL){
cout << tmp->number << " ";
fflush(stdout);
this->addToList(tmp->number);
tmp=tmp->next;
}
}
}
~List(){
if (head!=NULL){
element* tmp=head;
element *temp;
while(tmp!=NULL){
temp=tmp->next;
delete tmp;
tmp=temp;
}
head=NULL;
}
}
void addToList(int data){
// cout << "Ja sie wywolalem" << endl;
element* newNode=new element(data);
newNode->next=NULL;
element *tmp=head;
if (tmp!=NULL){
while(tmp->next!=NULL){
tmp=tmp->next;
}
tmp->next=newNode;
}
else{
head=newNode;
}
size++;
}
int popSelected(element *deleted){
int value=deleted->number;
element* tmp;
if (head==deleted){
delete head;
head=NULL;
}
else
{
tmp=head;
while(tmp->next!=deleted)
tmp=tmp->next;
tmp->next=deleted->next;
delete deleted;
size--;
}
return value;
}
element* getPrevious(element* lol){
element* tmp;
if (lol==head)
return NULL;
else{
tmp=head;
while(tmp->next!=lol)
tmp=tmp->next;
return tmp;
}
}
void Swap(element** before, element **ahead){
if (head==NULL || (*before)==NULL || (*ahead)==NULL){
cout << "Nothing to swap";
return;
}
element* prev=getPrevious(*before);
element* prev2=getPrevious(*ahead);
element* tmp=(*before)->next;
if(prev!=NULL)
prev->next=(*ahead);
if (prev2!=NULL)
prev2->next=(*before);
(*before)->next=(*ahead)->next;
(*ahead)->next=tmp;
if (head==(*before))
head=(*ahead);
else{
if (head==(*ahead))
head=(*before);
}
}
void SortTest(){
Swap(&(head->next->next), &(head));
}
List& operator=(const List& lista){
if (&lista==this) return *this;
if (this->head!=NULL){
element* tmp=head;
element *temp;
while(tmp!=NULL){
temp=tmp->next;
delete tmp;
tmp=temp;
}
head=NULL;
}
if (lista.size==0){
size=0;
head=NULL;
}
else
{
size=0;
head=NULL;
element* tmp=lista.head;
while(tmp!=NULL){
this->addToList(tmp->number);
tmp=tmp->next;
}
}
return *this;
}
List operator+(const List &lista){
List newList;
element *tmp=this->head;
while (tmp!=NULL){
newList.addToList(tmp->number);
tmp=tmp->next;
}
tmp=lista.head;
while (tmp!=NULL){
newList.addToList(tmp->number);
tmp=tmp->next;
}
return newList;
}
bool operator==(const List &lista){
if (this->size==lista.size)
return true;
else
return false;
}
bool operator>(const List &lista){
if (this->size>lista.size)
return true;
else
return false;
}
bool operator<(const List &lista){
if (this->size<lista.size)
return true;
else
return false;
}
};
istream & operator>>(istream &str, List &lists){
int data;
str >> data;
lists.addToList(data);
return str;
}
ostream & operator<<(ostream &str,const List &lists){
element*tmp=lists.head;
if (tmp==NULL){
str << "List is empty" << endl;
}
else{
while(tmp!=NULL){
str <<"(" << tmp->number << ")" << "--";
tmp=tmp->next;
}
str << "NULL" << endl << "Size of list: " << lists.size << endl;
}
return str;
}
int main(){
List lista;
cin >> lista;
cin >> lista;
List list2=lista;
cout << "Lista nr 1" << endl << lista;
cout << "Lista nr 2" << endl << list2;
cin >> list2;
cout << "Lista nr 2 po zmianie danych" << endl << list2;
cin >> list2;
cin >> list2;
cout << list2 << endl << endl << "Tests!" << endl;
list2.SortTest();
cout << list2 << endl;
//cout << list2;
return 0;
}
请不要因为这段代码评判我,我完全是 C++ 新手。
【问题讨论】:
标签: c++ sorting nodes swap singly-linked-list