About Ordered singly linked list..

Discussion in 'C++' started by HS-MOON, Sep 24, 2004.

  1. HS-MOON

    HS-MOON Guest

    I'm asking you to help me.
    I'm a beginner of studying c++.
    I'm trying to make the Singly Linked List(Ordered).
    Anyway, I've been debugging all day.
    I can't sort it out!!

    As you see, I don't know what this is wrong..
    When I run this pgm, It can't find the Current Variable.

    //
    Node *Current = Head;

    if (!Head) {
    Current = NewNode;
    }
    //

    I think the point Current has Head's Data & Next now.
    But It can't find Current variable..
    I show you my coding.
    If my coding is wrong, please correct it~*

    ----------------------------------------------------------
    // InsertList.cpp
    bool List::InsertList(Node *NewNode) {
    Node *Previous = NULL;
    Node *Current = Head;
    if (!Head) // Empty
    {
    Head = NewNode;
    return true;
    }
    if (NewNode->Data < Head->Data) // The first one.
    {
    NewNode->Next = Head;
    Head = NewNode;
    return true;
    }
    while (Current != NULL && (NewNode->Data > Current->Data)) // Go to Next
    Item.
    {
    Previous = Current;
    Current = Current->Next;
    }
    if (Current->Data == NewNode->Data) {
    cout << "Data Redundancy" << endl;
    return false;
    }
    else // In the middle of the list. // the last one.
    {
    Previous->Next = NewNode;
    NewNode->Next = Current;
    return true;
    }
    }
    ----------------------------------------------------------
    //LinkedList.h
    #include <iostream.h>
    #include <stdlib.h>

    class Node {
    friend class List;
    private:
    Node *Next;
    int Data;
    public:
    Node(int i) {
    Data = i;
    }
    ~Node() {}
    };

    class List {
    private:
    Node *Head;

    public:
    List() {
    Head = NULL;
    }
    ~List() {}
    bool InsertList(Node *NewNode);
    bool DeleteList(int Key);
    void Display();
    };
    ----------------------------------------------------------
    Main.cpp
    #include "linked_list.h"


    void main() {
    int MenuNumber, InsertNumber, DeleteNumber;

    Node *DataItem;
    List A;

    cout << "Linked List" << endl;
    cout << "Exit: 0 " << endl;
    cout << "Insert: 1" << endl;
    cout << "Delete: 2" << endl;
    cout << "Display: 3" << endl;

    cout << "Select Menu : ";
    cin >> MenuNumber;

    while (MenuNumber != 0) {
    switch (MenuNumber) {
    case 1:
    cout << "Insert Number: ";
    cin >> InsertNumber;

    DataItem = new Node(InsertNumber);

    if (DataItem == NULL) {
    cout << "There is no memory" << endl;
    exit(0);
    }

    if (A.InsertList(DataItem))
    cout << "Good Insertion" << endl;
    else
    cout << "Bad Insertion" << endl;
    break;

    case 2:
    cout << "Delete Number: ";
    cin >> DeleteNumber;

    if (A.DeleteList(DeleteNumber))
    cout << "Good Delete" << endl;
    else
    cout << "Bad Delete" << endl;
    break;

    case 3:
    A.Display();
    break;

    default:
    cout << "Insert Again." << endl;
    break;
    }

    cout << endl;
    cout << "Select Menu Again: ";
    cin >> MenuNumber;
    }
    }
    ---------------------------------------------------------
    Display.cpp
    #include "linked_list.h"

    void List::Display() {
    Node *Current = Head;
    cout << "List: ";
    while (Current != NULL) {
    cout << Current->Data << " ";
    Current = Current->Next;
    }
    cout << endl;
    }
    ---------------------------------------------------------
    That's it!!~*
    Please Help you..
    I desperately need your advice & help.. ^^&
    HS-MOON, Sep 24, 2004
    #1
    1. Advertising

  2. "HS-MOON" <> wrote in message
    news:...
    > I'm asking you to help me.
    > I'm a beginner of studying c++.
    > I'm trying to make the Singly Linked List(Ordered).
    > Anyway, I've been debugging all day.
    > I can't sort it out!!


    One error spotted see below.

    >
    > As you see, I don't know what this is wrong..
    > When I run this pgm, It can't find the Current Variable.
    >
    > //
    > Node *Current = Head;
    >
    > if (!Head) {
    > Current = NewNode;
    > }
    > //
    >
    > I think the point Current has Head's Data & Next now.
    > But It can't find Current variable..
    > I show you my coding.
    > If my coding is wrong, please correct it~*
    >
    > ----------------------------------------------------------
    > // InsertList.cpp
    > bool List::InsertList(Node *NewNode) {
    > Node *Previous = NULL;
    > Node *Current = Head;
    > if (!Head) // Empty
    > {
    > Head = NewNode;


    Here you need to set NewNode->Next to NULL

    NewNode->Next = NULL;

    > return true;
    > }


    You might have other errors, I didn't check.

    john
    John Harrison, Sep 24, 2004
    #2
    1. Advertising

  3. > ----------------------------------------------------------
    > // InsertList.cpp
    > bool List::InsertList(Node *NewNode) {
    > Node *Previous = NULL;
    > Node *Current = Head;
    > if (!Head) // Empty
    > {
    > Head = NewNode;


    Previuos=NULL; Next=NULL;

    > return true;
    > }
    > if (NewNode->Data < Head->Data) // The first one.
    > {
    > NewNode->Next = Head;



    NewNode->Previous = Head->Previous;
    Head->Previous = NewNode;

    ....
    Make a diagram where you draw all the pointers after the insertion of
    the objects before and after the one that got inserted.
    Gernot Frisch, Sep 24, 2004
    #3
  4. HS-MOON

    HS-MOON Guest

    Re: Thanks I sorted out!!~.. but Is this logically correct?

    I referenced your tips..
    Thanks a lot!!!!!

    Anyway, at last, I made it!! ㅠ_ㅠ(So impressed)
    Is this right?!
    Even though the result is right,
    sometimes our pgm is not logically right.

    So,I'd like you to answer to me whether this code is logically right or
    not.~
    ------------------------------------------------------
    bool List::InsertList(Node *NewNode) {
    Node *Current;
    Node *Previous;

    if (Head == NULL) { // Nothing
    NewNode->Next = NULL;
    Head = NewNode;
    return true;
    }

    Previous = Head;
    Current = Head->Next;

    if (Head->Data > NewNode->Data) { NewNode->Next = Head;
    Head = NewNode;
    return true;
    }

    if (Head->Data == NewNode->Data) {
    cout << "Data Redundancy: Insertion Again!" << endl;
    return false;
    }

    while (Current != NULL) {
    if (Current->Data > NewNode->Data) { NewNode->Next = Current;
    Previous->Next = NewNode;
    return true;
    }
    if (Current->Data == NewNode->Data) { cout << "Data Redundancy:
    Insert Again!" << endl;
    return false;
    } else {
    Previous = Current;
    Current = Current->Next;
    }
    }
    NewNode->Next = NULL;
    Previous->Next = NewNode;
    return true;
    }
    HS-MOON, Sep 24, 2004
    #4
  5. HS-MOON

    Method Man Guest

    "Gernot Frisch" <> wrote in message
    news:...
    > > ----------------------------------------------------------
    > > // InsertList.cpp
    > > bool List::InsertList(Node *NewNode) {
    > > Node *Previous = NULL;
    > > Node *Current = Head;
    > > if (!Head) // Empty
    > > {
    > > Head = NewNode;

    >
    > Previuos=NULL; Next=NULL;
    >
    > > return true;
    > > }
    > > if (NewNode->Data < Head->Data) // The first one.
    > > {
    > > NewNode->Next = Head;

    >
    >
    > NewNode->Previous = Head->Previous;
    > Head->Previous = NewNode;
    >
    > ...


    No, there is no 'Previous' member. He is implementing a singly linked list.
    Check his Node class.
    Method Man, Sep 24, 2004
    #5
    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:
    923
    Kenneth P. Turvey
    May 24, 2004
  2. CR

    AlphaSort for singly linked list

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

    Reverse search in a singly-linked list

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

    pruning a linear singly linked list

    Anando, Apr 23, 2006, in forum: C Programming
    Replies:
    59
    Views:
    1,225
    Richard Bos
    Apr 28, 2006
  5. DL

    Ordered list inside ordered list

    DL, Nov 9, 2009, in forum: Javascript
    Replies:
    6
    Views:
    322
    Dr J R Stockton
    Nov 21, 2009
Loading...

Share This Page