TO DO
- exception handling
- merge with dynamic arrary
*SO Bug here--remove this line and Control K will not work
/*
Added in comments from Jerry, Ed and Loki
This code borrows from from http://cslibrary.stanford.edu/
C++ Version - lnode, lnode_reverse, lnode_push, lnode_print, lnode_count
*/
#include "c_arclib.cpp"
template <class T1> class sl_list
{
private:
class node_1
{
public:
T1 data;
node_1 *next;
node_1(T1 data, node_1 *next = NULL) : data(data), next(next) {}
};
node_1 *head;
int count;
public:
sl_list() : count(0), head(NULL){}
void push(int data)
{
node_1 *newNode = new node_1(data, head);
++count;
head = newNode;
}
void print()
{
node_1 *current=head;
while (current != NULL)
{
cout << current->data << endl;
current = current->next;
}
}
void reverse()
{
node_1 *previous, *current, *future;
previous=NULL;
current=head;
future=head;
while(current!=NULL)
{
future=current->next;
current->next=previous;
previous=current;
current=future;
}
head=previous;
}
void break_heap()
{
int i=0;
while(1)
{
i++;
push(1);
}
}
};
/*
C Version - lnode, lnode_reverse, lnode_push, lnode_print, lnode_count
*/
typedef struct node_type
{
int data;
struct node_type* next;
} lnode_type;
struct lnode
{
int data;
struct lnode* next;
};
void lnode_reverse(struct lnode*& head)
{
struct lnode *previous, *current, *future;
previous=NULL;
current=head;
future=head;
while(current!=NULL)
{
future=current->next;
current->next=previous;
previous=current;
current=future;
}
head=previous;
}
void lnode_push(struct lnode*& head, int data)
{
struct lnode* newNode = new lnode;
newNode->data = data;
newNode->next = head;
head = newNode;
}
void lnode_print(struct lnode* current)
{
while (current != NULL)
{
cout << current->data;
current = current->next;
}
}
int lnode_count(struct lnode* current)
{
int count = 0;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
void break_heap()
{
struct lnode* head = NULL;
int i=0;
while(1)
{
i++;
lnode_push(head,1);
}
}
int main()
{
sl_list<int> list1;
list1.push(10);
list1.push(20);
list1.push(30);
list1.push(40);
list1.push(50);
list1.push(60);
list1.print();
list1.reverse();
list1.print();
}
break_heap
method for? It sounds malicious and it's public too! – greatwolf Nov 3 '11 at 7:48