You are here:

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


The Assignment:
You will implement and test a small class called LinkedList
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:
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.)  


#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),

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

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


#include "linkedlist.h"

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

  // 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
// 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"    ;
         cout<<"\n    Agent Name  : "<< temp->agentName;
         cout<<"\n    Case  No    : "<< temp->caseNo;
         cout<<"\n    Description : "<< temp->caseDesc;    
         cout << "\n\n\n ***********************************************************\n\n\n" ;   

// 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;
     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;
       case 1:
         //  Insert appropriate code here ....
       case 2:
         //  Insert appropriate code here ....
       case 3:
         //  Insert appropriate code here ....
       case 4:
         //  Insert appropriate code here ....        
       case 5:
       default :
         cout<<"\n   Invalid Option, Please try again .....\n";
 }while (true);
 return 0;

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,


All Answers

Answers by Expert:

Ask Experts




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


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.

©2017 All rights reserved.