Jan 23, 2010 at 6:14am UTC
i'm not really familiar with this one,.. but can anyone help me make a program
somewhat having this algorithm -
1. add record(node)
2. delete record
3. edit record
4. search and display record
5. display list
6. count records
7. delete records
8. delete list
so far here's what ive done:
-DList.h
-DList.cpp (the one that implements the DList.h)
"DList.h" -
/*------------------------------------------------------------------------------
This header file defines the data type List for processing list,
basic operations are:
Constructor
Destructor
Copy operator
Assignment operator
empty: Check if list is empty
insert: Insert an item
erase: remove an item
display: Output the list
<< : Output operator
------------------------------------------------------------------------------*/
#include <iostream>
#ifndef DLIST
#define DLIST
typedef int ElementType;
class List
{
public:
/***** Function Members *****/
/***** Class constructor *****/
List(int maxSize = 1024);
/***** Class distructor *****/
-List();
/***** Copy Constructor *****/
List(const List & origList);
/***** Assignment operator *****/
const List & operator=(const List & rightHandSide);
/***** empty operation *****/
bool empty() const;
/***** insert and erase *****/
void insert(ElementType item, int pos);
void erase(int pos);
/***** output *****/
void display(ostream & out) const;
private:
int mySize;
int myCapacity;
ElementType * myArray;
}; //---end of List class
ostream & operator<< (ostream & out, const List & aList)
#endif
//end of "DList.h"
"DList.cpp" -
/*-- DList.cpp ------------------------------------------------------------
This file implements list member functions.
----------------------------------------------------------------------------*/
#include <cassert>
#include <new>
using namespace std;
#include "DList.h"
List::List(int maxSize)
: mySize(0), myCapacity(maxSize)
{
myArray = new(nothrow) ElementType[maxSize];
assert(myArray != 0);
}
List::-List()
{
delete [] myArray;
}
List::List(const List & origList)
: mySize(origList.mySize), myCapacity(origList.myCapacity)
{
myArray = new(nothrow) ElementType[myCapactiy];
if (myArray != 0)
for(int i = 0; i < mySize; i++_
myArray[i] = origList.myArray[i];
else
{
cerr << "*** Omadeqiate memory to allocate storage for list ***\n";
exit(1);
}
}
const List & aList::operator=(const List & rightHandSide)
{
if (this != &rightHandSide)
{
if (myCapacity != rightHandSide.myCapacity)
{
delete[] myArray;
myCapacity = rightHandSide.myCapacity;
myArray = new(nothrow) ElementType[myCapacity];
if (myArray == 0)
{
cerr << "*Inadequate memory to allocate stack ***\n";
exit(1);
}
}
mySize = rightHandSide.mySize;
for(int i = 0; i < mySize; i++)
myArray[i] = rightHandSide.myArray[i];
}
return *this;
}
bool List::empty() const
{
return mySize == 0;
}
void List::display(ostream & out) const
{
for(int i = 0; i < mySize; i++)
out << myArray[i] << " ";
}
ostream & operator<< (ostream & out, const List & aList)
{
aList.display(out);
return out;
}
void List::insert(ElementType item, int pos)
{
if(mySize == CAPACITY)
{
cerr << "*** No space for list element -- terminating "
"execution ***\n";
exit(1);
}
if(pos < 0 || pos > mySize)
{
cerr << "*** Illegal location to insert -- " << pos
<< ". List unchange. ***\n";
return;
}
// First shift array elements right to make room for item
for(int i = mySize; i > pos; i--)
myArray[i] = myArray[i - 1];
// Now insert item at position pos and increase list size
myArray[pos] = item;
mySize++;
}
void List::erase(int pos)
{
if(mySize == 0)
{
cerr << "*** List is empty ***\n";
return;
}
if(pos < 0 || pos >= mySize)
{
cerr << "Illegal location to delete -- " << pos
<< ". List unchange. ***\n";
return;
}
// Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myArray[i] = myArray[i + 1];
// Decrease list size
mySize--;
}
//end of "DList.cpp"
* as long as the algorithm is concenred,.. i hope anyone can help me with this one.. thanks in advance
Jan 25, 2010 at 7:23am UTC
Do step by step:
(1) you want to create a node , so have a structure that contains your data field and the address of the next node.
(2) Don't set the size.Let the compiler gives u error when it is unable to create the node.
(3) Inside the class whenever you are inserting a node, create a new node and there increase the entries,
and if u want to set the limit of entries , you can have this entry as a check.
(3) Now i felt u r making a single linked list, that means you can traverse in a single direction, so be care ful while deleting a node.
I have changed your code, let me know if you dont understand.
check for effiecincy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream.h>
#include<math.h>
template <class Itemtype> struct Node;
template <class Itemtype>
struct Node
{
Itemtype mItem;
Node<Itemtype> *mNext;
};
template <class itemtype>
class List
{
Node<itemtype> *mFirst;
Node<itemtype> *mLast;
int mEntries;
public :
List();
~List();
Node<itemtype>* GetFirstNode(){return mFirst? mFirst: NULL;}
Node<itemtype>* GetLastNode(){return mLast? mLast: NULL;}
int GetEntries(){ return mEntries;}
void DisplayNode();
void InsertNodeAtFirstPosition(itemtype item);
void InsertNodeAtLastPosition(itemtype item);
void InsertNode(itemtype item,bool bFirst=false );
void DeleteNode(itemtype item);
void DeleteFirstNode();
void DeleteLastNode();
void DeleteNodeAtPosition(int iPosition);
Node<itemtype>* GetNode(itemtype item);
void EditNode(itemtype currentItem,itemtype newItem);
};
template <class itemtype>
List<itemtype>::List():mEntries(0),mFirst(NULL),mLast(NULL)
{
}
template <class itemtype>
void List<itemtype>::DisplayNode()
{
if (mEntries)
{
cout<<"Num of entries=" <<mEntries<<endl;
Node<itemtype> *tempNode;
tempNode = mFirst;
while (tempNode!=NULL)
{
cout<<tempNode->mItem<<endl;
tempNode = tempNode->mNext;
}
}
else
{
cout<<"No nodes to display" <<endl;
}
}
template <class itemtype>
void List<itemtype>::InsertNodeAtFirstPosition(itemtype item)
{
Node<itemtype> *pNode = new Node<itemtype>;
pNode->mItem = item;
//check for no Nodes
if (!mFirst)
{
pNode->mNext = mFirst;
mFirst = pNode;
mLast = pNode;
}
else
{
pNode->mNext = mFirst;
mFirst = pNode;
}
mEntries++;
}
template <class itemtype>
void List<itemtype>::InsertNodeAtLastPosition(itemtype item)
{
Node<itemtype> *pNode = new Node<itemtype>;
pNode->mItem = item;
//check if there are no nodes
if (!mLast)
{
pNode->mNext = mLast;
mLast = pNode;
mFirst = pNode;
}
else
{
mLast->mNext = pNode;
pNode->mNext = NULL;
mLast = pNode;
}
}
/*
* Function : InsertNode
* Details : if the flag bFirst is false then by default the node will be inserted at the last
* else if the flag is set to true, the node will be appended at the front.
*/
template <class itemtype>
void List<itemtype>::InsertNode(itemtype item,bool bFirst/*=false*/ )
{
Node<itemtype> *pNode = new Node<itemtype>;
pNode->mItem = item;
//check if there are no nodes
if (!mLast)
{
pNode->mNext = mLast;
mLast = pNode;
mFirst = pNode;
}
else
{
if (bFirst)
{
pNode->mNext = mFirst;
mFirst = pNode;
}
else
{
mLast->mNext = pNode;
pNode->mNext = NULL;
mLast = pNode;
}
}
mEntries++;
}
template <class itemtype>
List<itemtype>::~List()
{
if (mEntries)
{
Node<itemtype> *currentNode;
Node<itemtype> *aheadNode;
currentNode = mFirst;
while (currentNode!=NULL)
{
aheadNode = currentNode->mNext;
delete currentNode;
currentNode = aheadNode;
}
}
}
template <class itemtype>
void List<itemtype>::DeleteNode(itemtype item)
{
if (mEntries)
{
bool bNodeDeleted = false ;
Node<itemtype> *currentNode=NULL;
Node<itemtype> *previousNode=NULL;
currentNode = mFirst;
while (currentNode!=NULL)
{
if (currentNode->mItem == item)
{
bNodeDeleted = true ;
/*
*
* If deleting the first node, the previous node will be null,
* so assigning the next node to be the first.
*/
if (currentNode == mFirst)
{
mFirst=currentNode->mNext ;
}
else if (currentNode == mLast)
{
/*
* if deleting the last node, then currentNode->mNext will be NULL,
* so assigning the previous node to be last.
*/
mLast=previousNode ;
mLast->mNext = NULL;
}
else
previousNode->mNext = currentNode->mNext;
delete currentNode;
mEntries--;
break ;
}
previousNode = currentNode;
currentNode = currentNode->mNext;
}
if (!bNodeDeleted)
{
cout<<"Node" <<item<<" not found" <<endl;
}
}
else
{
cout<<"There are no items to delete in the list" <<endl;
}
}
template <class itemtype>
void List<itemtype>::DeleteFirstNode()
{
if (mEntries)
{
/*
if the number of entries is one
*/
Node<itemtype> *pCurrentNode = NULL;
pCurrentNode = mFirst;
if (pCurrentNode == mLast)
{
delete pCurrentNode;
mFirst = mLast = NULL;
mEntries = 0;
}
else
{
mFirst = pCurrentNode->mNext;
delete pCurrentNode;
mEntries--;
}
}
else
{
cout<<"No Entries found" <<endl;
}
}
template <class itemtype>
void List<itemtype>::DeleteLastNode()
{
if (mEntries)
{
/*
if the number of entries is one
*/
Node<itemtype> *pCurrentNode = NULL;
Node<itemtype> *pPreviousNode = NULL;
pCurrentNode = mFirst;
if (pCurrentNode == mLast)
{
delete pCurrentNode;
mFirst = mLast = NULL;
mEntries = 0;
}
else
{
while (pCurrentNode !=NULL)
{
pPreviousNode = pCurrentNode;
pCurrentNode = pCurrentNode->mNext;
}
delete pCurrentNode;
mEntries--;
mLast = pPreviousNode;
mLast->mNext = NULL;
}
}
else
{
cout<<"No Entries found" <<endl;
}
}
template <class itemtype>
void List<itemtype>::DeleteNodeAtPosition(int iPosition)
{
if (mEntries)
{
if (abs(iPosition) <= mEntries)
{
int iWalker = 0;
Node<itemtype> *pCurrentNode = NULL;
Node<itemtype> *pPreviousNode = NULL;
pCurrentNode = mFirst;
/*
if we are deleting the first entry
*/
if (pCurrentNode == mLast)
{
DeleteFirstNode();
}
else
{
while (pCurrentNode != NULL)
{
iWalker++;
if (iWalker == iPosition)
{
pPreviousNode->mNext = pCurrentNode->mNext;
delete pCurrentNode;
mEntries--;
break ;
}
pPreviousNode = pCurrentNode;
pCurrentNode = pCurrentNode->mNext;
}
}
}
else
{
cout<<"Position" <<iPosition <<"is not valid" <<endl;
}
}
else
{
cout<<"No Entry found:" <<endl;
}
}
template <class itemtype>
Node<itemtype>* List<itemtype>::GetNode(itemtype item)
{
if (mEntries > 0)
{
Node<itemtype> *pCurrentNode = mFirst;
while (pCurrentNode != NULL)
{
if (pCurrentNode->mItem == item)
return pCurrentNode;
pCurrentNode = pCurrentNode->mNext;
}
return NULL;
}
return NULL;
}
template <class itemtype>
void List<itemtype>::EditNode(itemtype currentItem,itemtype newItem)
{
Node<itemtype> * pRequiredNode = GetNode(currentItem);
if (pRequiredNode != NULL)
{
pRequiredNode->mItem = newItem;
}
else
{
cout<<"Node" <<currentItem<<" not found" <<endl;
}
}
#endif
Last edited on Jan 25, 2010 at 7:25am UTC