Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Singly Linked List copy constructor value order?

Reply
Thread Tools

Singly Linked List copy constructor value order?

 
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      07-09-2007
I want to add the nodes of MyString in the same order as in the
original class so that I can print "ch" field in the same order for
both classes. I was thinking of navigating until the end of list of
original class with a trailing pointer and inserting nodes backwards
so that I can print them in the same positions. But since this is a
singly list I can't get back to previous node. Another option I
thought is to traverse the list a second time and relink all nodes but
I don't like this idea because of the extra overhead of another loop.
Any other suggestions?

This is the input and output of the program below:
Input
Enter Char:
a
Enter Char:
b
Enter Char:
c

Output
Printing Values:
c
b
a
Copied Printing Values:
a
b
c

I want both to look like this:
c
b
a
#include <iostream>
#include <string>

using namespace std;

struct Node
{
char ch;
Node *next;
};

class MyString
{
private:
Node *mystr;
public:
MyString();
MyString(MyString& m);
Node* CreateNode(char);
void Insert(Node* nd);
void AddNode(char);
void readStr();
void printStr();
void deleteStr();
};

MyString::MyString()
{
mystr = NULL;
}

MyString::MyString(MyString& m)
{
Node* ptr;
Node* tptr;
mystr=NULL;
ptr= m.mystr;
while (ptr!=NULL)
{
Node *newnode;
newnode=CreateNode(ptr->ch);
Insert(newnode);
tptr=ptr;
ptr=ptr->next;
}

}

void MyString::Insert(Node* nd)
{
if (mystr!=NULL)
{
nd->next = mystr;
}
mystr=nd;
}

Node* MyString:: CreateNode(char c)
{
Node* newnode;
newnode = new Node;
newnode->ch= c;
newnode->next = NULL;
return newnode;
}

void MyString::AddNode(char c)
{
Node *newnode;
newnode = new Node; // alocates space
newnode->ch = c;
newnode->next = mystr;
mystr = newnode;
}


void MyString:: readStr()
{
char c;
cout << "Enter Char:" << endl;
cin >> c;
AddNode(c);
}

void MyString:rintStr()
{
Node *ptr;
ptr = mystr;
cout << "Printing Values: " << endl;
while (ptr!=NULL)
{
cout << ptr->ch << endl;
ptr = ptr->next;
}
}

int main()
{
MyString m1 = MyString();
m1.readStr(); // read 'a'
m1.readStr(); // read 'b'
m1.readStr(); // read 'c'
m1.printStr();
MyString m2 = MyString(m1);
m2.printStr();
return 0;
}

Thanks Rod

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      07-09-2007
On 2007-07-09 05:49, wrote:
> I want to add the nodes of MyString in the same order as in the
> original class so that I can print "ch" field in the same order for
> both classes. I was thinking of navigating until the end of list of
> original class with a trailing pointer and inserting nodes backwards
> so that I can print them in the same positions. But since this is a
> singly list I can't get back to previous node. Another option I
> thought is to traverse the list a second time and relink all nodes but
> I don't like this idea because of the extra overhead of another loop.
> Any other suggestions?


Make it a double linked list? And while your at it use std::list and get
rid of half of your code, which will make the program more readable and
more robust.

--
Erik Wikström
 
Reply With Quote
 
 
 
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      07-09-2007
Thanks. I will try it.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse search in a singly-linked list RAJASEKHAR KONDABALA C Programming 20 02-27-2011 12:53 PM
pruning a linear singly linked list Anando C Programming 59 04-28-2006 11:12 AM
About Ordered singly linked list.. HS-MOON C++ 4 09-24-2004 02:26 PM
Stack & Singly Linked List Data Structures Patrick McCourt Java 2 05-24-2004 10:55 PM
AlphaSort for singly linked list CR C Programming 1 12-15-2003 08:52 AM



Advertisments