|
-
August 8th, 2011, 04:16 PM
#1
linked list
Code:
#include <iostream>
using namespace std;
template <class Type>
struct nodeType{
Type info;
nodeType <Type> *link;
};
template <class Type>
class linkedListType {
public:
linkedListType ();
void print ()const;
int length ()const;
void insertFirst (const Type& newitem);
void insertLast (const Type& newitem);
void readIntoLinkedList ();
nodeType <Type> *smallestNode();
nodeType <Type> *findNode (const Type& item);
private:
nodeType <Type> *first;
nodeType <Type> *last;
};
template <class Type>
linkedListType<Type>::linkedListType (){
first=NULL;
last=NULL;
}
template <class Type>
void linkedListType <Type>:: print()const{
nodeType <Type> *current;
current=first;
while (current !=NULL)
{
cout<<current->info<<" ";
current =current->link;
}
cout<<endl;
}
template <class Type>
int linkedListType <Type>::length()const{
int n=0;
nodeType <Type> *current;
current=first;
while(current !=NULL)
{
n++;
current=current->link;
}
return n;
}
template <class Type>
void linkedListType <Type> ::insertFirst (const Type& newitem){
nodeType <Type> *newNode;
newNode=new nodeType <Type>;
newNode->info=newitem;
newNode->link=first;
first=newNode;
if (last==NULL)
last=newNode;
}
template <class Type>
void linkedListType <Type> ::insertLast (const Type& newitem){
nodeType <Type> *newNode;
newNode=new nodeType <Type>;
newNode ->link =NULL;
if (first==NULL)
{
first=newNode;
last=newNode;
}
else{
last->link=newNode;
last=newNode;
}
}
template <class Type>
void linkedListType <Type>::readIntoLinkedList (){
int num;
cin>>num;
while (num!=-999){
insertLast (num);
cin>>num;
}
}
int main()
{
linkedListType <int> l1;
l1.readIntoLinkedList ();
cout<<"The given Linked List"<<endl;
cout<<" ";
l1.print();
return 0;
}
why is not reading the numbers??
this is what im getting
The given Linked List
0 0 0 0 0 0 0 0
im suppose to get
the given linked list
4 5 4 8 45 4
thanks
-
August 8th, 2011, 04:50 PM
#2
Re: linked list
 Originally Posted by ryamjones
why is not reading the numbers??
Your
Code:
void linkedListType <Type> ::insertLast (const Type& newitem){}
is missing
Code:
newNode->info=newitem;
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 8th, 2011, 04:57 PM
#3
Re: linked list
omg thanks would take me long to see that
-
August 8th, 2011, 06:50 PM
#4
Re: linked list
 Originally Posted by ryamjones
omg thanks would take me long to see that
Your linked list also has memory leaks. Where is the linked list destructor that is supposed to clean up all the memory you allocated?
Regards,
Paul McKenzie
-
August 8th, 2011, 09:16 PM
#5
Re: linked list
 Originally Posted by ryamjones
omg thanks would take me long to see that
Would be easy if you step through your code in debugger (like I did)
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 8th, 2011, 11:48 PM
#6
Re: linked list
even the functions
Code:
nodeType <Type> *smallestNode();
nodeType <Type> *findNode (const Type& item);
seems to be not implemented. only prototype is present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|