Thread: Totally confused on assigment using linked lists

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    Totally confused on assigment using linked lists

    I am currently enrolled in a c++ course and have been given an assignment to complete a project that includes using a linked list. I do not know how to do this and i am having trouble contacting my instructor. its an online course here are the direction for the assignment.

    Graded Project 4: Catalog Your CD Collection
    Create a program that allows users to enter information about a music CD collection and save
    that information so it can be retrieved later.
    • Your program should have a menu system that allows you to enter information about a
    new CD, save the list to a file on your hard drive, load a list from the hard drive, or print
    your list to the screen.
    • Create a CD class to store information about the CDs. Store the CD Name, Artist Name,
    and Year Released. These should be private members. Use Class functions to modify
    the data
    • You should use dynamic data to store CDs. Store the information in an array and
    dynamically create a new CD entry every time the user wants to enter new CD
    information.
    • If the user wants to save the file, ask them what they want the name of the file to be and
    save the file to the hard drive.
    • If the user wants to load a file, ask them the name of the file they want to load from the
    hard drive and let them know if it doesn’t exist.
    • If the user selects to print the list, the system should print out a list of all the CDs in the
    collection.
    When you are finished:
    • Submit the .cpp source file, .exe file and code file to your instructor in a zipped folder.
    • Put your name in a comment at the top of the file, followed by the name of the program.
    Evaluation Criteria
    1. The program compiles/builds successfully, runs normally (no runtime errors or unnecessary
    delays), and exits normally.
    2. The program uses a menu with the following choices: enter information about a new CD, save
    the list to a file on the hard drive, load a list from the hard drive, or print the list to the screen.
    3. The program goes into Enter New Data mode at the user’s command. It performs the
    following Jobs (in the order that they are given) and then goes to the main menu.
    Job A. display the following text on the screen: CD Name:
    Job B. receive input
    Job C. display the following text on the screen: Artist Name:
    Job D. receive input
    Job E. display the following text on the screen: Year Released:
    Job F. receive input
    4. The program goes into Save to File mode at the user’s command. It performs the following
    Jobs (in the order that they are given) and then goes to the main menu.
    Job G. ask the user to enter a name for the new file
    Job H. receive input
    Job I. place a new file on the hard drive
    Job J. use the information from Job H to name the file
    Job K. write the entire CD list to the file
    Job L. close the file
    5. The program goes into Load From File mode at the user’s command. Before it can load a file,
    it must first go into Attempt to Open File mode. If the attempt is not successful, the program
    informs the user that the file does not exist and goes to the main menu. If the attempt is
    successful, the program goes to the main menu after performing the following Jobs (in the order
    that they are given):
    Job P. input the entire CD list from the file
    Job Q. close the file
    6. The program goes into Attempt to Open File mode from Load From File mode. It performs the
    following Jobs (in the order that they are given) and then returns to Load From File mode.
    Job M. ask the user to enter the name of the file
    Job N. receive input
    Job O. try to open a file for reading, using the information from Job N
    7. The program goes into Print the List mode at the user’s command. It performs the following
    Jobs (in the order that they are given) for each CD and then goes to the main menu.
    Job R. on a separate line, display “CD Name: ”, followed by the current CD’s Job B
    information
    Job S. on a separate line, display “Artist Name: ”, followed by the current CD’s Job D
    information
    Job T. on a separate line, display “Year Released: ”, followed by the current CD’s Job F
    information
    Job U. separate this CD’s information from the next CD’s information (to assist the
    user/reader) by displaying a blank line or by other means
    8. The program uses a CD class to store information about each CD. The class stores the CD
    Name, Artist Name, and Year Released in private members. Class functions are used to modify
    the data.
    9. The program dynamically creates a new CD entry every time keyboard/file input is received for
    a new CD
    10. The program is easy to use.
    11. The program does not perform unnecessary tasks.
    12. The source code is contained in one file and is not unnecessarily long.
    13. The source code utilizes whitespace properly and consistently to enhance legibility.
    14. The variable/function/struct/class/object names are chosen and typed/written in a way that
    clearly explains their purposes. The naming style is consistent throughout the whole program.
    15. The source contains effective, consistent comments, especially near any complex or obscure
    sections of code.

    This contains all the information to put it together but im stumped on how to create a linked list for this program. help and examples are appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Could you (for example), tackle this problem if it said use "arrays" rather than "linked lists"?

    In other words, we need to know where you're really stuck, not just be given the whole assignment without any effort on your part.
    As specs go, it's very detailed, so you should be able to do some of it (in fact, most of it).

    http://cboard.cprogramming.com/annou...t.php?f=3&a=51
    http://cboard.cprogramming.com/annou...t.php?f=3&a=39
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    I guess the reason that you need a linked list is because of this:
    • You should use dynamic data to store CDs. Store the information in an array and
    dynamically create a new CD entry every time the user wants to enter new CD
    information.
    Am I right? And are you allowed to use the STL, and hence the std::list class (IIRC it's a linked list)?

    If you really do want to create your own linked list it works roughly like this (this is a stack, but it uses the same principle as a linked list)
    Code:
    #ifndef __STACK__HPP__INCLUDED
    #define __STACK__HPP__INCLUDED
    
    template <typename _T> 
    class StackItem
    {
    public:
    	StackItem()
    	{
    		m_Prev = 0;
    	}
    	StackItem<_T>* m_Prev; //Add a similar for next item...
    	_T m_Data;
    };
    
    template <typename _T>
    class Stack
    {
    public:
    	Stack()
    	{
    		m_Count = 0;
    		m_Last = 0;
    	}
    	inline void Pop()
    	{
    		StackItem<_T>* pPrev = m_Last->m_Prev;
    		_T data;
    		data = m_Last->m_Data;
    		delete m_Last;
    		m_Last = pPrev;
    		m_Count--;
    	}
    	inline void Push(_T arg)
    	{
    		StackItem<_T>* pItem = new StackItem<_T>();
    		pItem->m_Prev = m_Last;
    		pItem->m_Data = arg;
    		m_Last = pItem;
    		m_Count++;
    	}
    	inline _T Top() const
    	{
    		return m_Last->m_Data;
    	}
    	inline unsigned int Count() const
    	{
    		return m_Count;
    	}
    	inline void Clear()
    	{
    		while (m_Count || m_Last)
    		{
    			Pop();
    		}
    	}
    	inline bool Empty()
    	{
    		return (m_Count == 0);
    	}
    
    	~Stack()
    	{
    		Clear();
    	}
    protected:
    	StackItem<_T>* m_Last; //Add a similar for first item
    	unsigned int m_Count;
    };
    
    #endif /*__STACK__HPP__INCLUDED*/
    In plain words (this is for linked list)
    You have a base node in the list class.

    When someone pushes an object into your list you add a node that contains the data and a pointer to the next node, which is a NULL pointer at first, and to the previous node. The first node you add in the list has a NULL "previous" pointer.

    When another object is added you make it into a node and take the last node in your list and sets the "next" pointer of it to the new node.

    A bit confusing explanation perhaps, but it's basically the same thing as the linked stack - just with a pointer to the next node in the StackItem and a pointer to the first node in the Stack.



    Hope this helped a bit at least
    Daniel

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    thanks that right there gives me a start

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who says you need a linked list? I grade that unnecessary complexity. The assignment does not explicitly say you need a linked list.
    Since it's just supposed to store data, use an array. For C++ that would be std::vector.
    Remember to ask question about what you have problems with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Yeah I know is unnecessarily complex but my instructor asked that i do that way with a linked list as practice.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you understand the principle of linked lists then?
    Basically, it should be a class or a structure which points the next structure/class in the list and, optionally, the previous.
    So, create a class/struct with the information the program must store, add a next pointer and a prev pointer.
    You'll also have to have a head, which keeps track of the very first element in the list.
    Then you simply point the next/previous members of the structs/classes to the the correct node when you create a new one or delete one.

    That should help a little.
    Oh, and always make sure the pointers are initialized to NULL. When a pointer is NULL, you know there's no next node or no previous node.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Thanks that helps clarify it a bit more

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so some examples:
    Code:
    class CCD
    {
    public:
    	CCD() : pNext(NULL), pPrev(NULL) { }
    	// Data here
    	CCD* pNext;
    	CCD* pPrev;
    };
    
    void AddNode(CCD* pInsertAfter, /* Arguments here*/)
    {
    	CCD* pNew = new CCD;
    	// Store information in the new class here
    	assert(pInsertAfter); // pInsertAfter must be valid; if no linked list exists, we must first create a head and pass as argument here
    	pNew->pPrev = pInsertAfter;
    	pInsertAfter->pNext = pNew;
    }
    
    void DeleteNode(CCD*& pNodeToDelete)
    {
    	assert(pNodeToDelete); // Node to delete must be valid
    	if (pNodeToDelete->pPrev) pNodeToDelete->pPrev->pNext = pNodeToDelete->pNext;
    	if (pNodeToDelete->pNext) pNodeToDelete->pNext->pPrev = pNodeToDelete->pPrev;
    	delete pNodeToDelete;
    	pNodeToDelete = NULL;
    }
    
    int main()
    {
    	CCD Head;
    	AddNode(&Head);
    	// Head->pNext is now valid.
    	RemoveNode(Head.pNext);
    	// Head->pNext should be NULL now.
    	return 0;
    }
    OK, so maybe not the best example, but it demonstrates how it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. about linked lists
    By armin_miewes in forum C Programming
    Replies: 4
    Last Post: 01-08-2004, 11:17 AM
  3. Questions on Linked Lists
    By Weng in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2003, 01:17 AM
  4. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  5. Linked Lists -- Again!!! Help!!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2002, 08:27 PM