473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Linked list program question..

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::print()
{
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[i]);
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.

Mar 2 '06 #1
1 2586
SKP wrote:
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:
---------
I'll add some style comments, too.
#include<string>
class List {

struct node {
string *s;
Why do you use pointers to strings?
Also, it should be std::string.
node *next;
} *head;
public:
List() { head = 0;};
List() : head(0) {}
~List() {delete head;}
void Add(string *str);
Again, I wouldn't use a pointer here. The string should also be const. A
reference would be best, so it would be:

void Add(const std::string& str);
string *Remove();
void print();
This function doesn't modify your object. Such functions should be made
const, like:

void print() const;
};
void List::Add(string *str)
{
cout<< *str <<endl;
if(head) {
struct node* cnode = head;
You can leave out the struct keyword here (and in all the following uses of
your node struct). It's superfluous.
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;
Well, here's your problem. First, you overwrite your cnode pointer with a
null pointer, then _afterwards_, you try to delete it. So in fact, you
delete a null pointer, which does nothing. The object that cnode was
pointing to before is still existing.
What you need to do is remember the pointer to the previous node, because
that one's next pointer needs to be deleted and then set to null. And that
means you can't just exchange the cnode = NULL and the delete line, because
cnode is not the last node's next pointer, but a copy of it. I don't give
you a solution to save you the learning experience.
return p;
}
void List::print()
{
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[i]);
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;
}
---------


Mar 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
7
by: alternativa | last post by:
Hello, I'm a beginner in C programming and I have a problem that probably will seem trivial to most of you, however I can't find a solution... So, I have to write a data base - program should ask...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
6
by: Fazana | last post by:
I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........ Here is the question for the program Question. Part 1. Add the function...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.