#include <fstream>
using namespace std;
struct node{
string data;
node *next;
node *pre;
};
node *createBidirList(int n)
{
node *temp,*tail = NULL,*head = NULL;
string str;
head = new node;
if(head ==NULL)
{
cout <<” No memory available!”;
return NULL;
}
else
{
head->data = str;
head->next =NULL;
head->pre = NULL;
tail = head;
}
for(int i=0;i<n-1;i++)
{
temp = new node;
if(temp==NULL)
{
cout << “No memory available!”;
return head;
}
else
{
temp->data = str;
temp->next = NULL;
temp->pre =tail;
tail->next =temp;
tail = temp;
}
}
return head;
}
void outoutBidirList(node * head)
{
cout <<“List: “;
node *curNode = head;
while(curNode)
{
if(curNode->pre)
cout << ” <- “;
cout <<curNode->data;
if(curNode->next)
cout << ” -> “;
cout <<curNode->next;
}
cout <<endl;
return;
}
int main()
{
node *listhead;
int k = 0;
int n = 0;
cout << “Please enter a number of string: “;
cin >> n;
cout << “Please enter a string:\n”;
string *s = new string[n];
string *s1 = new string[n];
int number[20];
for (int i = 0; i < n; i++)
{
cin >> *(s + i);
}
cout << s;
int a = s->size();
//cout << a;
ofstream Instr(“textF.txt”);
if (Instr.is_open()){
cout << “Instr is open” << endl;
}
else{
cout << “you meassed up(Instr)” << endl;
exit(1);
}
//Instr << “Initial string:\n”;
for (int i = 0; i < n; i++)
{
Instr << *(s + i) << endl;
cout<< *(s + i) << endl;
}
Instr.close();
ifstream Afstr(“textF.txt”);
if (Afstr.is_open()){
cout << “Afstr is open” << endl;
}
else{ cout << “you meassed up(Afstr)” << endl; exit(1); }
while (!Afstr.eof())
{
Afstr >>*s;
listhead=createBidirList(n );}
Afstr.close();
outoutBidirList(listhead);
}
本人都不知道本人在写什么。
80
导致无限循环。
#include <fstream> #include<iostream> #include<string> using namespace std; struct node{ string data; node *next; node *pre; }; node *createBidirList(int n) { node *temp,*tail = NULL,*head = NULL; string str; head = new node; if(head ==NULL) { cout <<" No memory available!"; return NULL; } else { head->data = str; head->next =NULL; head->pre = NULL; tail = head; } for(int i=0;i<n-1;i++) { temp = new node; if(temp==NULL) { cout << "No memory available!"; return head; } else { temp->data = str; temp->next = NULL; temp->pre =tail; tail->next =temp; tail = temp; } } return head; } void outoutBidirList(node * head) { cout <<"List: "; node *curNode = head; while(curNode) { if(curNode->pre) cout << " <- "; cout <<curNode->data; if(curNode->next) cout << " -> "; cout <<curNode->next; curNode = curNode->next; } cout <<endl; return; } int main() { node *listhead; int k = 0; int n = 0; cout << "Please enter a number of string: "; cin >> n; cout << "Please enter a string:\n"; string *s = new string[n]; string *s1 = new string[n]; int number[20]; for (int i = 0; i < n; i++) { cin >> *(s + i); } cout << s; int a = s->size(); //cout << a; ofstream Instr("textF.txt"); if (Instr.is_open()){ cout << "Instr is open" << endl; } else{ cout << "you meassed up(Instr)" << endl; exit(1); } //Instr << "Initial string:\n"; for (int i = 0; i < n; i++) { Instr << *(s + i) << endl; cout<< *(s + i) << endl; } Instr.close(); ifstream Afstr("textF.txt"); if (Afstr.is_open()){ cout << "Afstr is open" << endl; } else{ cout << "you meassed up(Afstr)" << endl; exit(1); } while (!Afstr.eof()) { Afstr >>*s; listhead=createBidirList(n );} Afstr.close(); outoutBidirList(listhead); }