c++ 文本文件 双向链表

C++语言 码拜 9年前 (2016-04-20) 1424次浏览
输入字符串到文本文件,然后读取文件中的字符串,奖励双向链表,删除最长和最短的字符串,输出删除后的双向链表,假如有相同长度的字符串,输出长度。急求啊!#include <iostream>
#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

outoutBidirList的while循环里少了一句curNode = curNode->next;
导致无限循环。

#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);
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c++ 文本文件 双向链表
喜欢 (0)
[1034331897@qq.com]
分享 (0)