Singly Linked List copy constructor value order?

Discussion in 'C++' started by rlueneberg@gmail.com, Jul 9, 2007.

  1. Guest

    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::printStr()
    {
    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
    , Jul 9, 2007
    #1
    1. Advertising

  2. 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
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 9, 2007
    #2
    1. Advertising

  3. Guest

    Thanks. I will try it.
    , Jul 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick McCourt

    Stack & Singly Linked List Data Structures

    Patrick McCourt, May 24, 2004, in forum: Java
    Replies:
    2
    Views:
    914
    Kenneth P. Turvey
    May 24, 2004
  2. HS-MOON
    Replies:
    4
    Views:
    592
    Method Man
    Sep 24, 2004
  3. CR

    AlphaSort for singly linked list

    CR, Dec 15, 2003, in forum: C Programming
    Replies:
    1
    Views:
    503
    CBFalconer
    Dec 15, 2003
  4. RAJASEKHAR KONDABALA

    Reverse search in a singly-linked list

    RAJASEKHAR KONDABALA, Dec 24, 2003, in forum: C Programming
    Replies:
    20
    Views:
    5,787
    saadbinsaulat
    Feb 27, 2011
  5. Anando

    pruning a linear singly linked list

    Anando, Apr 23, 2006, in forum: C Programming
    Replies:
    59
    Views:
    1,197
    Richard Bos
    Apr 28, 2006
Loading...

Share This Page