You are here:

C++/linked lists in data structures in Dev C++.

Advertisement


Question
The Assignment:
You will implement and test a small class called LinkedList
Purposes:
Ensure that you can write a small class that meets a precise specification.
Make sure you understand how to write a class that is separated into a header file and an implementation file.
Give you experience in using a test program to track down bugs in a class's implementation.
Before Starting:
 
Know how to compile and run C++ programs on your system using Dev-C++..
Files that you must write:
linkedlist.h: The header file for the linkedlist class declaration. Actually, you don't have to write much of this file. Just start with the version that we have provided and add your name and other information at the top. If some of your member functions are implemented as inline functions, then you may put those implementations in this file too.
linkedlist.cpp: The implementation file for the linkedlist class. You will write all of this file, which will have the implementations of all the LinkedList's member functions.

--------------------------------------------------------------------------------

The LinkedList Class
Discussion of the Assignment


For this assignment, you will be writing a program that does similar tasks -- namely, asks the user to enter items until the user chooses to stop, stores them into list nodes, and chains the nodes together into a linked list.

Your linked list should store information about XFile cases that contain three fields: agent name (string), case number (int), and case description (string).

Instead of simply putting all of your code inside the main source code file, you must implement the members method of linked list library (provided). The files should be named linkedList.h/linkedList.c that contain the following functions:
addToStart()
addToEnd()
printList()
removeNodeAt()
Removes the node at the specified position (integer) in the list

Instead of simply asking the user if they want to add a new node, your main program should offer the user a menu of options:
Add a new node to the end
Add a new node to the beginning
Print out the entire list
Remove a node from the list
Quit the program

You must implement all functions with empty body in linkedlist.cpp  file.
You should free up any memory allocated for the program before quitting. Remember that you need a free corresponding to every malloc! (This isn't done in the sample program, but I want you to have the practice freeing up memory.)  

#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H

#include <string>

struct Node
   {
   string agentName;
   int    caseNo;
    string caseDesc;
  
   Node * next;
  Node (const string& name, const int no , const string& desc, Node * nptr)
      : agentName(name),
        caseNo(no),
        caseDesc(desc),
        next(nptr)       
  {
     
  }
};

class LinkedList
{
 public:
   
   
   LinkedList();         // constructor
   ~LinkedList();        // destructor
   int  size()  const;          
   void addToStart(Node *) ;  
   void addToEnd(Node *) ;
   void printList() ;
   void removeNodeAt(int );
   
 
 private:

   Node * myHead;    // header node
   Node * myTail;    // last node in list, header if list is empty
   int    mySize;    // # nodes in linked list
};

#endif

#include "linkedlist.h"

LinkedList::LinkedList()
   : myHead(new Node("dummy",0,"dummy",0)),
     myTail(myHead),
     mySize(0)
// post: internal list is empty, one header node allocated
{
}

~LinkedList(){
  // write your code for releasing memory here...
}


// post: returns # nodes in list    
int LinkedList::size() const

{
   return mySize;
}


// add the new node at the start of list.
void LinkedList::addToStart(Node *n)
{    
   // Write your code here
   
   mySize++;    
}
// add the new node at the end of list.
void LinkedList::addToEnd(Node * n)
// pre: *n allocated
// post: *n is linked as last node of list, size increases by one        

{
 // Write your code here    mySize++;
}



// this function will print all nods in list.
void LinkedList::printList() {
    Node *temp = myHead->next ;
    cout<<" \n          values are \n"    ;
    while(temp)
    {
         cout<<"\n    Agent Name  : "<< temp->agentName;
         cout<<"\n    Case  No    : "<< temp->caseNo;
         cout<<"\n    Description : "<< temp->caseDesc;    
         cout << "\n\n\n ***********************************************************\n\n\n" ;   
         temp=temp->next;
     }
}

// remove the node at the given index
void LinkedList::removeNodeAt(int index){
    
    // Wtite your code here.
      
}
   

#include <iostream>
#include <stdlib.h>
#include <conio.h>

#include "linkedlist.h"
int main(int argc, char *argv[])
{
 LinkedList myList;
 Node *node  ;
 int choice = 0 , index=0 ;
 string agentName, caseDesc ;
 int caseNo;
 do
 {
     
     cout<< "\n Enter 1 Add a new node to the end \n";
     cout<< " Enter 2 Add a new node to the beginning \n";
     cout<< " Enter 3 Print out the entire list \n";
     cout<< " Enter 4 Remove a node from the list \n";
     cout<< " Enter 5 Quit the program \n";
     cout<< " Enter your choice : ";
     cin>> choice;
     switch(choice){
       case 1:
         //  Insert appropriate code here ....
       break;
      
       case 2:
         //  Insert appropriate code here ....
       break;
       
       case 3:
         //  Insert appropriate code here ....
       break;
       
       case 4:
         //  Insert appropriate code here ....        
       break;
       
       case 5:
         exit(1);
       break;      
       
       default :
         cout<<"\n   Invalid Option, Please try again .....\n";
       break;
     
     }
     
 }while (true);
 
 
  
 
 system("PAUSE");   
 return 0;
}

Answer
Hello,
thankyou for your question. Well you didnt
specify a single word what you want me to do
in it.

If you want to know what the teacher has asked, the
he has given you the outline of the Link List class
and you have to implement it and insert the functionalities
of the functions. For further infornmation, try Google.

Hope this helps,
Professional

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Professional

Expertise

I can answer any question about functions,pointers,structures,object oriented programming basics of classes and data structures.My strong field is structured programming.

Experience

I have got 2 years experiece under C . I am able to answer about the structured concepts pointers to a little extent,OOP concepts. I have also experience in data Strucutres like Linked List, Stacks , Queues, Heaps, B Trees, Red Black Trees. I will try to satisfy with my knowledge. I am the Student of an expert here Martin, what i have learnt today, i just owe my every knowledge to him. He is the greatest.

©2016 About.com. All rights reserved.