Announcements | Last Post | |
---|---|---|
SoC Curricula | 09/30/2017 01:08PM | |
Demarcation or scoping of examinations and assessment | 02/13/2017 07:59AM | |
School of Computing Short Learning Programmes | 11/24/2014 08:37AM | |
Unisa contact information | 07/28/2011 01:28PM |
Assignment 01 Semester 2 July 16, 2010 08:44AM |
Registered: 16 years ago Posts: 9 Rating: 0 |
Re: Assignment 01 Semester 2 July 16, 2010 05:47PM |
Registered: 15 years ago Posts: 3,496 Rating: 1 |
Re: Assignment 01 Semester 2 July 17, 2010 10:57PM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |
Re: Assignment 01 Semester 2 July 18, 2010 05:46PM |
Registered: 16 years ago Posts: 9 Rating: 0 |
Language: C++//linkedList.h #include <iostream> template <class Type> struct nodeType { Type info; nodeType <Type> *link; } template <class Type> class linkedListType { public: const linkedListType<Type> & operator= (const linkedListType<Type>&); void initializeList(); bool isEmptyList(); void print() const; int length() const; void destroyList(); Type front() const; Type back() const; Type returnKth (int kth); void deleteKth (int kth); virtual bool search(const Type& searchItem) const =0; virtual void insertFirst (const Type& newItem) =0; virtual void insertLast (const Type& newItem)=0; virtual void deleteNode (const Type& deleteItem) =0; linkedListType(); linkedListType (const linkedListType<Type>& otherList); ~linkedListType (); protected: int count; nodeType<Type> *first; nodeType<Type> *last; private: void copyList (const linkedListType<Type>& otherList); }; template <class Type> bool linkedListType<Type>::isEmptyList() const { return (this->first ==NULL); } template <class Type> linkedListType<Type>::linkedListType() { this->first = NULL; this->last = NULL; this->count = 0; } template <class Type> void linkedListType<Type>::destroyList() { nodeType<Type> *temp; while (this->first != NULL) { temp = this->first; this->first = this->first->link; delete temp; } this->last = NULL; this->count = 0; } template <class Type> void linkedListType<Type>::initializeList() { destroylist(); } template <class Type> void linkedListType<Type>::print() const { nodeType<Type> *current; current=this->first; while (current!=NULL) { cout << current->info<< " "; current=current->link; } } template <class Type> int linkedListType<Type>::length() const { return count; } template <class Type> Type linkedListType<Type>::front() const { assert (this->first !=NULL); return (this->first->info); } template <class Type> Type linkedListType<Type>::back () const { assert (this->last !=NULL); return this->last->info; } template <class Type> linkedListIterator<Type> linkedListType<Type>:: begin { linkedListIterator<Type> temp (this->first); return temp; } template <class Type> linkedListIterator<Type> linkedListType<Type>::end { linkedListIterator<Type> temp(NULL); return temp; } template <class Type> void linkedListType<Type>::copyList (const linkedListType<Type>& otherList) { nodeType<Type> *newNode; nodeType<Type> *current; if (this->first!=NULL) destroyList(); if (otherList.first == NULL) { this->first = NULL; this->last = NULL; this->count = 0; } else { current= otherList.first; count=otherlist.count; this->first = new NodeType<Type>; this->first->info = current->info; this->first->link= NULL; this->last= this->first; current=current->link; while (current!=NULL) { newNode = new nodeType<Type>; newNode->info = current->info; newNode->link = NULL; this->last->link = newNode; current = current->link; } } } template <class Type> linkedListType<Type>::~linkedListType() { destroyList(); } template <class Type> linkedListType<Type>::linkedListType (const linkedListType<Type>& otherList) { this->first=NULL; copyList(otherList); } template <class Type> const linkedListType<Type>& linkedListType<Type>::operator= (const linkedListType<Type>& otherList) { if (this!=&otherList) { copyList(otherList); } return*this; } template <class Type> Type linkedListType<Type>:: returnKth (int kth) { nodeType current= this->first; int cnt = 0; if ((kth>count) || (this->first == NULL)) { err<<"The " << kth << "th element does not exist in this list!"<<endl; break; } else { while (cnt < kth) { current=current->link; cnt ++; } if (cnt == kth) return current->info; } } template <class Type> void linkedListType<Type>:: deleteKth (int kth) { nodeType<Type> *current; nodeType<Type> *trailCurrent; int cnt=1; if ((this->first==NULL)||(kth>count)) cerr<< "The "<< kth << "th does not exist!"<< endl; else { current=this->first; if (kth==1) { this->first = this->first->link; delete current; this->count --; } else { while (cnt < kth) { trailCurrent=current; current = current->link; cnt ++; } if (cnt == kth) { trailCurrent->link = current->link; if (current == this->last) this->last = trailCurrent; delete current; } this->count--; } } }//end linkedList.h
Language: C++//unorderedLinkedList.h #ifndef H_unorderedLinkedList #define H_unorderedLinkedList #include "LinkedListType.h" template <class Type> class unorderedLinkedList: public linkedListType<Type> { public: bool search (const Type& search item) const; void insertFirst (const Type& newItem); void insertLast (const Type& newItem); void deleteNode (const Type& deleteItem); }; template <class Type> bool unorderedLinkedList<Type>:: search(const Type& searchItem) const { nodeType<Type> *current; bool found=false; current = this->first; while (current != NULL && !found) { if (current->info == searchItem) found = true; else current= current->link; return found; } } template<class Type> void unorderedLinkedList<Type> :: insertFirst(const Type& newItem) { nodeType<Type> *newNode; newNode = new nodeType<Type>; newNode->info=newItem; newNode->link= this->first; this->first = newNode; count++; if (this->last==NULL) this->last=newNode; } template <class Type> void unorderedLinkedList<Type> :: insertLast const Type& newItem) { nodeType<Type> *newNode; newNode=new nodeType<Type>; newNode->info=newItem; newNode->link=NULL; if (this->first == NULL) { this->first=newNode; this->last=newNode; this->count++; } else { this->last=newNode; this->count++; } } template <class Type> void unorderedLinkedList<Type>:: deleteNode (const Type& deleteItem) { nodeType<Type> *current; nodeType<Type> *trailCurrent; bool found=false; if (this->first == NULL) cout<< "The list is empty!"<<endl; else { if (this->first->info==deleteItem) { current=this->first; this->first=this->first->link; this->count--; if (this->first==NULL) this->last=NULL; delete current; } else { found = false; trailCurrent=this->first; current=this->first->link; while (current!=NULL && !found) { if (current->info != deleteItem) { trailCurrent=current; current=current->link; } else found = true; } if (found) { trailCurrent->link=current->link; this->count--; if (this->last==current) this->last = trailCurrent; delete current; } else cout<<"The item to be deleted is not in the list!"<< endl; } } } #endif//end unorderedLinkedList.h
Language: C++//LinkedListTest.cpp #include <iostream> #include "unorderedLinkedList.h" using namespace std; int main () { unorderedLinkedList<int> list1; int num, kthOutput, kthDelete; cout << "Enter Numbers ending with ';-1'; " << endl; cin >> num; while (num != -1) { list1.insertLast(num); cin >> num; } cout << endl; cout << "Enter the number of the element you wish to be output: "<<endl; cin >> kthOutput; cout<< list1.returnKth(kthOutput); cout << "Enter the number of the element you wish to be output: "<<endl; cin >> kthDelete; list1.deleteKth(kthDelete); cout << "The list after the " << kthDelete << "th item was deleted is: " <<endl; list1.print(); cout<< endl; return 0; }//end LinkedListTest.cpp
Re: Assignment 01 Semester 2 July 18, 2010 06:29PM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |
Re: Assignment 01 Semester 2 July 22, 2010 03:44AM |
Registered: 16 years ago Posts: 9 Rating: 0 |
Re: Assignment 01 Semester 2 July 22, 2010 10:57AM |
Registered: 15 years ago Posts: 3,496 Rating: 1 |
Re: Assignment 01 Semester 2 July 22, 2010 01:06PM |
Registered: 15 years ago Posts: 34 Rating: 0 |
but it's very resource intensive. I prefer to just putLanguage: C++system("pause");
it waits for a return and doesn't reach out to the system for such a small command. Just add it to the end of your main function and all will be well.Language: C++cin.get();
Re: Assignment 01 Semester 2 July 22, 2010 01:11PM |
Registered: 15 years ago Posts: 34 Rating: 0 |
Re: Assignment 01 Semester 2 July 22, 2010 01:57PM |
Registered: 18 years ago Posts: 560 Rating: 2 |
Re: Assignment 01 Semester 2 July 22, 2010 04:21PM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |
Re: Assignment 01 Semester 2 July 22, 2010 07:14PM |
Registered: 15 years ago Posts: 3,496 Rating: 1 |
Re: Assignment 01 Semester 2 July 22, 2010 07:51PM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |
Re: Assignment 01 Semester 2 July 22, 2010 08:55PM |
Registered: 15 years ago Posts: 3,496 Rating: 1 |
Re: Assignment 01 Semester 2 July 24, 2010 08:29PM |
Registered: 16 years ago Posts: 9 Rating: 0 |
Re: Assignment 01 Semester 2 August 12, 2010 12:00PM |
Registered: 14 years ago Posts: 269 Rating: 0 |
Re: Assignment 01 Semester 2 August 13, 2010 10:20AM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |
Re: Assignment 01 Semester 2 August 13, 2010 02:03PM |
Registered: 14 years ago Posts: 269 Rating: 0 |
Re: Assignment 01 Semester 2 August 17, 2010 10:08AM |
Registered: 13 years ago Posts: 19 Rating: 0 |
Re: Assignment 01 Semester 2 August 17, 2010 10:34AM |
Registered: 13 years ago Posts: 19 Rating: 0 |
Re: Assignment 01 Semester 2 August 17, 2010 11:31AM |
Registered: 18 years ago Posts: 1,424 Rating: 0 |