S
SKP
Hi,
I am trying do a basic liked list program, where i am adding nodes at
the end. Adding part is fine, but removing part is not working.
Here is the code:
---------
#include<string>
class List {
struct node {
string *s;
node *next;
} *head;
public:
List() { head = 0;};
~List() {delete head;}
void Add(string *str);
string *Remove();
void print();
};
void List::Add(string *str)
{
cout<< *str <<endl;
if(head) {
struct node* cnode = head;
while(cnode->next) cnode=cnode->next;
struct node *temp = new struct node;
temp->s = str;
temp->next = NULL;
cnode->next = temp;
}
else {
head = new struct node;
head->s = str;
head->next = NULL;
}
}
string *List::Remove()
{
struct node *cnode = head;
while(cnode->next) {
cnode = cnode->next;
}
string *p = cnode->s;
cnode = NULL;
delete cnode;
return p;
}
void List:rint()
{
struct node *cnode = head;
while(cnode) {
cout<< *(cnode->s)<<endl;
cnode = cnode->next;
}
}
int main()
{
string StrObjs[] = {"str1","str2","str3","str4"};
List L;
const int total =sizeof(StrObjs)/sizeof(*StrObjs);
for(int i = 0;i < total;i++)
L.Add(&StrObjs);
L.print();
string *temp = L.Remove();
cout<<"popped element = "<<*temp<<endl;
temp = L.Remove();
cout<<"popped element = "<<*temp<<endl;
temp = L.Remove();
cout<<"popped element = "<<*temp<<endl;
cout<<"popped element = "<<*temp<<endl;
temp = L.Remove();
L.print();
return 0;
}
---------
The output it gives is:
--
str1
str2
str3
str4
str1
str2
str3
str4
popped element = str4
popped element = str4
popped element = str4
popped element = str4
str1
str2
str3
str4
--------
which is not the expected one. Please help in correcting it and any
suggestions on improving this program are appreciated.
Thanks in adv,
SKP.
I am trying do a basic liked list program, where i am adding nodes at
the end. Adding part is fine, but removing part is not working.
Here is the code:
---------
#include<string>
class List {
struct node {
string *s;
node *next;
} *head;
public:
List() { head = 0;};
~List() {delete head;}
void Add(string *str);
string *Remove();
void print();
};
void List::Add(string *str)
{
cout<< *str <<endl;
if(head) {
struct node* cnode = head;
while(cnode->next) cnode=cnode->next;
struct node *temp = new struct node;
temp->s = str;
temp->next = NULL;
cnode->next = temp;
}
else {
head = new struct node;
head->s = str;
head->next = NULL;
}
}
string *List::Remove()
{
struct node *cnode = head;
while(cnode->next) {
cnode = cnode->next;
}
string *p = cnode->s;
cnode = NULL;
delete cnode;
return p;
}
void List:rint()
{
struct node *cnode = head;
while(cnode) {
cout<< *(cnode->s)<<endl;
cnode = cnode->next;
}
}
int main()
{
string StrObjs[] = {"str1","str2","str3","str4"};
List L;
const int total =sizeof(StrObjs)/sizeof(*StrObjs);
for(int i = 0;i < total;i++)
L.Add(&StrObjs);
L.print();
string *temp = L.Remove();
cout<<"popped element = "<<*temp<<endl;
temp = L.Remove();
cout<<"popped element = "<<*temp<<endl;
temp = L.Remove();
cout<<"popped element = "<<*temp<<endl;
cout<<"popped element = "<<*temp<<endl;
temp = L.Remove();
L.print();
return 0;
}
---------
The output it gives is:
--
str1
str2
str3
str4
str1
str2
str3
str4
popped element = str4
popped element = str4
popped element = str4
popped element = str4
str1
str2
str3
str4
--------
which is not the expected one. Please help in correcting it and any
suggestions on improving this program are appreciated.
Thanks in adv,
SKP.