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.Everytime我試着和txt給了我一箇中國writing.Teacher也說我需要與字符串合併或我需要給節點字符串,該應用程序可以輕鬆地保存該字符串行。也許是因爲我試圖寫node *temp
。任何人都知道我該如何解決我的問題?其他進程後,它將被複制,剪切,粘貼並替換節點。寫作鏈表txt文件(保存處理)
您的'main()'在哪裏?無法重現您的問題。' –
in main()'teditor sl; start = NULL;我使用switch:case,我可以很容易地調用sl.save();' –
你在哪裏「啓動」你的鏈表「node」? 'start'不應該是NULL,它應該在某個地方是'= * node;' –