Need to complete insert function - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: Need to complete insert function

  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,705

    Re: Need to complete insert function

    Well after looking closely at the code, I'm seriously not impressed. If that's what your teacher has provided then (s)he probably needs to attend a c++ class themselves rather than trying to teach it!

    If the teacher hasn't covered the mechanism for inserting into a doubly linked list, how does (s)he expect you to know it? To get info about how to code insertion into a doubly linked list then google for double linked list c++. If we just provide the code to this then you're not really going to learn.

    Also see http://forums.codeguru.com/showthrea...ork-assignment

    Try coding it and post your attempt here and we'll be able to advise further. Cheers.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  2. #17
    Join Date
    Mar 2017
    Posts
    7

    Re: Need to complete insert function

    Quote Originally Posted by 2kaud View Post
    Well after looking closely at the code, I'm seriously not impressed. If that's what your teacher has provided then (s)he probably needs to attend a c++ class themselves rather than trying to teach it!

    If the teacher hasn't covered the mechanism for inserting into a doubly linked list, how does (s)he expect you to know it? To get info about how to code insertion into a doubly linked list then google for double linked list c++. If we just provide the code to this then you're not really going to learn.

    Also see http://forums.codeguru.com/showthrea...ork-assignment

    Try coding it and post your attempt here and we'll be able to advise further. Cheers.
    Yeah its highly frustrating as i believe the better way to make this program would be to use nodes as this would simpify the code and in that way i believe i would be able to complete it, thats why i asked for help on here as i have never seen a linked list created in this way before

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,705

    Re: Need to complete insert function

    The program does use nodes. That is the purpose of ListEntry. ListEntry is a node and List is a doubly linked list of ListEntry - ie List is a doubly linked list of nodes. This is fairly conventional. Have you experience of another programming language as you mention that youhaven't seen a linked list created this way before.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,705

    Re: Need to complete insert function

    For info, I've changed the code from post #1 to make it more like modern c++ code - leaving out the insert code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Represents a node in the linked-list
    class Node
    {
    public:
    	explicit	Node(const string& str = ""s);
    
    	string		getData() const;
    	void		setData(const string& str);
    	Node*		getNext() const;
    	Node*		getPrev() const;
    	void		setNext(Node* next);
    	void		setPrev(Node* prev);
    
    private:
    	string	data;		// entry's string
    	Node*	prev_p;		// pointer to previous entry in the linked-list
    	Node*	next_p;		// pointer to next entry in the linked-list
    };
    
    // Represents the linked-list object
    class List
    {
    public:
    	List() = default;
    	~List();
    
    	bool printForward() const;
    	bool printReverse() const;
    	bool insert(const string& str);
    
    private:
    	int	entryCount = 0;			// number of entries present in the linked-list
    	Node*	head_p = nullptr;		// pointer to the first entry in the list
    	Node*	tail_p = nullptr;		// pointer to the last entry in the list
    };
    
    // Node constructor
    Node::Node(const string& str) : data(str), prev_p(nullptr), next_p(nullptr) {}
    
    // Return the stored string object
    string Node::getData() const
    {
    	return data;
    }
    
    // Set the internal string data from a char*
    void Node::setData(const string& str)
    {
    	data = str;
    }
    
    // Returns pointer to the next entry in the list
    Node* Node::getNext() const
    {
    	return next_p;
    }
    
    // Returns pointer to the previous entry in the list
    Node* Node::getPrev() const
    {
    	return prev_p;
    }
    
    // Sets next entry pointer
    void Node::setNext(Node* next)
    {
    	next_p = next;
    }
    
    // Sets previous entry pointer
    void Node::setPrev(Node* prev)
    {
    	prev_p = prev;
    }
    
    // List destructor
    List::~List()
    {
    	// Delete all entries in the list
    	for (auto entry_p = head_p; entry_p != nullptr;) {
    		auto current_p = entry_p;
    		entry_p = entry_p->getNext();
    		delete current_p;
    	}
    }
    
    // Output linked list in order from head to tail
    // printing out the string data from each list entry
    bool List::printForward() const
    {
    	int count = 0;
    
    	cout << "FORWARD: " << entryCount << " entries\n";
    
    	for (auto entry_p = head_p; entry_p != nullptr; entry_p = entry_p ->getNext()) {
    		cout << entry_p->getData() << " ";
    
    		if (++count % 5 == 0 || entry_p == tail_p)
    			cout << endl;
    	}
    
    	return true;
    }
    
    // Output linked list in reverse order from tail to head
    // printing out the string data from each list entry
    bool List::printReverse() const
    {
    	int count = 0;
    
    	cout << "REVERSE: " << entryCount << " entries\n";
    
    	for (auto entry_p = tail_p; entry_p != nullptr; entry_p = entry_p ->getPrev()) {
    		cout << entry_p->getData() << " ";
    
    		if (++count % 5 == 0 || entry_p == head_p)
    			cout << endl;
    	}
    
    	return true;
    }
    
    // Insert the given string into the linked-list such that the
    // entries in the linked-list are in alphabetical order
    bool List::insert(const string& str)
    {
    	return true;
    }
    
    int main()
    {
    	const string places[] = { "Aberdeen", "Bakewell", "Cocking", "Dogdyke",
    		"Eastbourne", "Farranfadda", "Gaggin", "Hadfield",
    		"Irnham", "Jameston", "Kendal", "Langley",
    		"Malmesbury", "Netherhampton", "Ogmore", "Pant",
    		"Quedgeley", "Ramsbottom", "Sallynoggin",
    		"Talla Bheith Forest", "Upchurch", "Victoria Bridge",
    		"Wacton", "Xeroxillington", "Yelling", "Zetland" };
    
    	const unsigned int indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
    		2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };
    
    	const int no_Places = sizeof(places) / sizeof(places[0]);
    
    	List placesList;
    
    	// Insert every word in the places array into the linked-list.
    	cout << "INSERT:\n";
    
    	for (auto i : indexes) {
    		static int count = 0;
    
    		if (i < no_Places) {
    			cout << places[i] << " ";
    
    			//Add a new line for console readability
    			if ((count++ > 0) && (count % 5 == 0))
    				cout << endl;
    
    			placesList.insert(places[i]);
    		}
    	}
    
    	cout << endl;
    
    	// print out the list in alphabetical order
    	placesList.printForward();
    	cout << endl;
    
    	// print out the list in reverse alphabetical order
    	placesList.printReverse();
    	cout << endl;
    
    	return 0;
    }
    You may find this of interest.

    It's really not that difficult to insert into a list. You just have to work through what needs to be done and how the various pointers need to be changed. Try doing it on paper first with a rectangle representing a node and lines going from one node to the next representing the previous and next pointers. Starting with nothing but head_p and tail_p both nullptr, on paper how would you insert the first element? Then work out on paper how you would insert at the head of the list and insert at the tail of the list. Then try coding these so that you have a working list - though not in order. Once you have that working then go back to the paper and work out how you manually do these whilst inserting new elements in order. Then when you know how to insert in order on paper then code it.
    Last edited by 2kaud; March 31st, 2017 at 06:23 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)