|
-
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
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
|