struct node
{
string info;
struct node *next;
}*start, *last;
long nodecount=0;
class teditor
{
public:
node* create_node(string);
void insert_pos();
void save();
void display();
void delete_pos();
teditor()
{
start = NULL;
}
};
node *teditor::create_node(string value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
void teditor::save()
{
struct node *info;
ofstream listfile;
listfile.open("example.txt",ios::out|ios::app |ios::binary) ;
node *temp;
temp=start;
if(!listfile){
cout<<"\nError";
}
else{
while(temp!=NULL)
{
listfile.write((char*)(temp),sizeof(nodecount));
temp=temp->next;
}
}
listfile.close();
cout<<"\n\n\n\t\tLink list has been saved in file example.txt in current folder.";
cout<<"\n\n\t\tPress a key to continue ... ";getch();
}
void teditor::insert_pos()
{
string value; int counter;
int pos;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
nodecount++;
int i;
s = start;
while (s != NULL)
{
s = s->next; counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 )
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void teditor::display()
{
/*
Need to merge as a string and show to display just in one line like writing
why cannot save health because of application saving pointers.
*/
node *temp;
temp=start;
cout<<"\n\n\n";
while(temp)
{
cout<<"\t\t\t"<<temp->info;
temp=temp->next;
}
cout<<"\n\n\t\t "<<nodecount<<" records displayed ,Press a key to continue.....";getch();
}
void teditor::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
}cout<<s<<" Element Deleted"<<endl;nodecount--;
cout<<"There is left "<<nodecount<<" nodes"<<endl;
}
嗨,大家好!我试图将链表保存到txt时遇到问题。每次我试着和txt给我一个中文写作。教师还说我需要合并字符串或者我需要给节点字符串应用程序可以轻松保存该字符串。也许是因为我正在尝试编写node *temp
。任何人都知道如何解决我的问题?在其他进程之后,它将被复制,剪切,粘贴和替换为节点。
答案 0 :(得分:0)
更改
listfile.write((char*)(temp),sizeof(nodecount));
到
listfile << temp->info;
答案 1 :(得分:0)
您不想将指针ROCR
写入文件中,您希望temp
内的info
指向node
。那你为什么要写temp
而不是写temp
?你可以通过你的指针info
做到这一点,对吧?
以下将为您做以上事项:
temp
答案 2 :(得分:0)
listfile.write((char*)(temp),sizeof(nodecount));
只写一个节点的原始内存。
问问自己 - 文本文件应该是什么样的?你期望它在编辑器中看起来像什么?你必须编写代码才能做到这一点。
你需要做
listfile << temp->info
您希望如何保存下一个和之前的内容。也许它隐含在排序中。所以这就是你所需要的。也许你需要行号来说next=4, prior=14
答案 3 :(得分:-2)
执行这些步骤后,您的问题将得到解决: