S
Shawn Windle
----begin node.h--------
#ifndef NODE_H
#define NODE_H
#include <iostream> //NULL
using namespace std;
class node
{
public:
node(int new_data = 0, node *new_link = NULL)
{
data = new_data;
link = new_link;
}
private:
int data;
node *link;
friend class list;
};
#endif
----end node.h-------
----begin list.h------
#ifndef LIST_H
#define LIST_H
#include <iostream> //NULL
#include "node.h"
using namespace std;
class list
{
public:
list() { header = NULL; }
void insert(int new_item);
void print();
private:
node *header;
};
#endif
---end list.h-------
---begin list.cpp-----
#include "list.h"
using namespace std;
void list::insert(int new_item)
{
int i = 0;
node *itr = header;
if(itr == NULL)
header = new node(new_item,NULL);
else
{
while(itr->link != NULL)
{
i++;
itr->link = itr->link->link;
}
//cout << i << endl;
itr->link = new node(new_item,NULL);
}
return;
}
void list:rint()
{
node *itr = header;
if(itr == NULL)
cout << "Empty list" << endl;
else
{
while(itr->link != NULL)
{
cout << itr->data << endl;
itr->link = itr->link->link;
}
}
return;
}
----end list.cpp-------
----begin main.cpp-------
#include <iostream>
#include "list.h"
using namespace std;
int main()
{
list a;
a.insert(10);
a.insert(11);
a.insert(123);
a.print();
return 0;
}
----end main.cpp-------
I've been trying to write a Linked List Data Structure and I've run
into some trouble. Could someone please help me out? I can insert one
int, but thats all. I think the problem is in void list::insert(int
new_item), but I can't figure out what it is. Thanks for the help!
Shawn Windle
#ifndef NODE_H
#define NODE_H
#include <iostream> //NULL
using namespace std;
class node
{
public:
node(int new_data = 0, node *new_link = NULL)
{
data = new_data;
link = new_link;
}
private:
int data;
node *link;
friend class list;
};
#endif
----end node.h-------
----begin list.h------
#ifndef LIST_H
#define LIST_H
#include <iostream> //NULL
#include "node.h"
using namespace std;
class list
{
public:
list() { header = NULL; }
void insert(int new_item);
void print();
private:
node *header;
};
#endif
---end list.h-------
---begin list.cpp-----
#include "list.h"
using namespace std;
void list::insert(int new_item)
{
int i = 0;
node *itr = header;
if(itr == NULL)
header = new node(new_item,NULL);
else
{
while(itr->link != NULL)
{
i++;
itr->link = itr->link->link;
}
//cout << i << endl;
itr->link = new node(new_item,NULL);
}
return;
}
void list:rint()
{
node *itr = header;
if(itr == NULL)
cout << "Empty list" << endl;
else
{
while(itr->link != NULL)
{
cout << itr->data << endl;
itr->link = itr->link->link;
}
}
return;
}
----end list.cpp-------
----begin main.cpp-------
#include <iostream>
#include "list.h"
using namespace std;
int main()
{
list a;
a.insert(10);
a.insert(11);
a.insert(123);
a.print();
return 0;
}
----end main.cpp-------
I've been trying to write a Linked List Data Structure and I've run
into some trouble. Could someone please help me out? I can insert one
int, but thats all. I think the problem is in void list::insert(int
new_item), but I can't figure out what it is. Thanks for the help!
Shawn Windle