1个回答
展开全部
你的链表一个节点存一个字符还是一句话呢
追问
储存一段字符串,比如一段ip地址
追答
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Node
{
public:
Node *next;
string s;
};
class List
{
private:
Node *first;
Node *last;
public:
List()
{
first = new Node;
last = first;
}
void get_data(string s)
{
Node *temp = new Node;
temp->s = s;
last->next = temp;
last = temp;
}
void display()
{
Node *temp = first->next;
while (temp->next != NULL)
{
cout << temp->s << endl;
temp = temp->next;
}
cout << temp->s << endl;
}
~List()
{
Node *temp1 = first,*temp2;
while (temp1->next != NULL)
{
temp2 = temp1->next;
delete temp1;
temp1 = temp2;
}
delete temp1;
}
};
int main()
{
List l;
ifstream fin("in.txt", ios::in);
string s;
while (getline(fin,s))
{
l.get_data(s);
}
l.display();
system("pause");
return 0;
}
你自己写个in.txt,把数据放进去
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询