#include "iostream"
#include "stddef.h"
using namespace std;
template <class type>
class List;
template <class type>
class Node {
public:
type data;
Node * next;
Node (type d);
};
template <class type>
Node<type>::Node(type d){
data = d;
next = NULL;
}
template <class type>
class List{
Node<type> * root;
public:
List();
bool isFull();
bool isEmpty();
void print();
bool insert(type info);
void makeEmpty();
bool Delete(type info);
~List();
};
template <class type>
List<type>::List(){
root = NULL;
}
template <class type>
List<type>::~List(){
makeEmpty();
}
template <class type>
bool List<type>::Delete(type info){
if(!isEmpty()){
Node<type> * previous = NULL;
Node<type> * current;
current = root;
while(current && current->data != info){
previous = current;
current= current->next;
}
if(!previous){
root = current->next;
}
else
{
if(current){
previous->next = current->next;
}
else
return false;
}
delete current;
return true;
}
return false;
}
template <class type>
bool List<type>::insert(type info){
if(!isFull()){
Node<type> * newNode = new Node<type>(info);
Node<type> * previous = NULL;
Node<type> * current;
current = root;
while(current && current->data < info){
previous = current;
current= current->next;
}
if(!previous){
newNode->next = root;
root = newNode;
}
else
{
newNode -> next = current;
previous -> next = newNode;
}
return true;
}
return false;
}
template <class type>
bool List<type>::isFull(){
Node <type> * temp = new Node<type>(1);
if(temp){
delete temp;
return false;
}
else
return true;
}
template <class type>
bool List<type>::isEmpty(){
if(root)
return false;
else
return true;
}
template <class type>
void List<type>::print(){
Node <type>* current = root;
while(current){
cout<<current->data<<endl;
current = current -> next;
}
}
template <class type>
void List<type>::makeEmpty(){
Node <type>* current = root;
Node <type>* temp;
while(current){
temp = current;
current = current -> next;
delete temp;
}
}
void main(){
List<int> obj;
char ans = 'y';
int data;
while(ans!='n'){
cout<<"Enter data to insert"<<endl;
cin>>data;
obj.insert(data);
cout<<endl;
cout<<"Press n to exit"<<endl;
cin>>ans;
}
cout<<"List:"<<endl;
obj.print();
ans='y';
while(ans!='n'){
cout<<"Enter data to delete"<<endl;
cin>>data;
obj.Delete(data);
cout<<endl;
cout<<"Press n to exit"<<endl;
cin>>ans;
cout<<"List:"<<endl;
obj.print();
}
system("PAUSE");
}
#include "stddef.h"
using namespace std;
template <class type>
class List;
template <class type>
class Node {
public:
type data;
Node * next;
Node (type d);
};
template <class type>
Node<type>::Node(type d){
data = d;
next = NULL;
}
template <class type>
class List{
Node<type> * root;
public:
List();
bool isFull();
bool isEmpty();
void print();
bool insert(type info);
void makeEmpty();
bool Delete(type info);
~List();
};
template <class type>
List<type>::List(){
root = NULL;
}
template <class type>
List<type>::~List(){
makeEmpty();
}
template <class type>
bool List<type>::Delete(type info){
if(!isEmpty()){
Node<type> * previous = NULL;
Node<type> * current;
current = root;
while(current && current->data != info){
previous = current;
current= current->next;
}
if(!previous){
root = current->next;
}
else
{
if(current){
previous->next = current->next;
}
else
return false;
}
delete current;
return true;
}
return false;
}
template <class type>
bool List<type>::insert(type info){
if(!isFull()){
Node<type> * newNode = new Node<type>(info);
Node<type> * previous = NULL;
Node<type> * current;
current = root;
while(current && current->data < info){
previous = current;
current= current->next;
}
if(!previous){
newNode->next = root;
root = newNode;
}
else
{
newNode -> next = current;
previous -> next = newNode;
}
return true;
}
return false;
}
template <class type>
bool List<type>::isFull(){
Node <type> * temp = new Node<type>(1);
if(temp){
delete temp;
return false;
}
else
return true;
}
template <class type>
bool List<type>::isEmpty(){
if(root)
return false;
else
return true;
}
template <class type>
void List<type>::print(){
Node <type>* current = root;
while(current){
cout<<current->data<<endl;
current = current -> next;
}
}
template <class type>
void List<type>::makeEmpty(){
Node <type>* current = root;
Node <type>* temp;
while(current){
temp = current;
current = current -> next;
delete temp;
}
}
void main(){
List<int> obj;
char ans = 'y';
int data;
while(ans!='n'){
cout<<"Enter data to insert"<<endl;
cin>>data;
obj.insert(data);
cout<<endl;
cout<<"Press n to exit"<<endl;
cin>>ans;
}
cout<<"List:"<<endl;
obj.print();
ans='y';
while(ans!='n'){
cout<<"Enter data to delete"<<endl;
cin>>data;
obj.Delete(data);
cout<<endl;
cout<<"Press n to exit"<<endl;
cin>>ans;
cout<<"List:"<<endl;
obj.print();
}
system("PAUSE");
}