DNWA/11/wywolania/Data/stide_v1.1/Utils/linklist.cc
Jakub Stefko ab1d7e2546 ...
2021-01-28 18:33:55 +01:00

133 lines
3.4 KiB
C++
Executable File

// linklist.cpp
#include "linklist.h"
// data structures:
// node for a linked list
class LLNode {
public:
int val; // the value at this node
LLNode *next; // pointer to the next node
};
//===========================================================================
LinkedList::LinkedList(void) {
root = NULL;
}
//===========================================================================
LinkedList::LinkedList(const LinkedList &llist) { // the copy constructor
root = NULL;
LLNode *temp_ptr = llist.root;
while (temp_ptr) {
Insert(temp_ptr->val);
temp_ptr = temp_ptr->next;
}
}
//===========================================================================
LinkedList &LinkedList::operator = (const LinkedList &llist) {
root = NULL;
LLNode *temp_ptr = llist.root;
while (temp_ptr) {
Insert(temp_ptr->val);
temp_ptr = temp_ptr->next;
}
return *this;
}
//============================================================================
LinkedList::~LinkedList(void) {
if (root) {
LLNode *temp_ptr = root->next, *next_temp_ptr;
delete root;
while (temp_ptr) {
next_temp_ptr = temp_ptr->next;
delete temp_ptr;
temp_ptr = next_temp_ptr;
}
}
}
//===========================================================================
// returns 1 if there was no copy, 0 otherwise
int LinkedList::Insert(int val) {
if (!root) {
root = new LLNode;
root->val = val;
root->next = NULL;
return 1;
} else {
if (!Search(val)) { // only put in if it is not already in - this is ineff.
LLNode temp_node;
temp_node.val = root->val;
temp_node.next = root->next;
root->val = val;
root->next = new LLNode;
root->next->val = temp_node.val;
root->next->next = temp_node.next;
return 1;
}
}
return 0;
}
//===========================================================================
int LinkedList::Search(int val) {
LLNode *curr_ptr = root;
while (curr_ptr) {
if (curr_ptr->val == val) return 1;
else curr_ptr = curr_ptr->next;
}
return 0;
}
//===========================================================================
void LinkedList::Write(ostream &s) {
LLNode *curr_ptr = root;
while (curr_ptr) {
s<<curr_ptr->val<<" ";
curr_ptr = curr_ptr->next;
}
}
//=============================================================================
ostream &operator<<(ostream &s, LinkedList &ll) {
ll.Write(s);
return s;
}
//===========================================================================
/*
// this is for testing the linked list
// something similar should be done for all data structures
// using the Test func on the base class, we can test all descendants with
// those methods
#include "arrays.h"
void Test(LinkedList &list) {
list.Insert(10);
list.Insert(5);
list.Insert(3);
list.Insert(5);
list.Insert(7);
list.Insert(26);
list.Insert(13);
list.Insert(26);
cout<<endl;
list.Write(cout);
cout<<endl<<list.Search(5);
cout<<endl<<list.Search(0);
cout<<endl<<list.Search(13);
}
void TestArray(Array<LinkedList> &larray) {
for (int i = 0; i < larray.Size(); i++)
Test(larray[i]);
}
#include <fstream.h>
void main(void) {
ifstream inf;
Array<LinkedList> ll(200);
TestArray(ll);
ll[0].Insert(999);
cout<<endl<<ll[0];
ll[3] = ll[0];
cout<<endl<<ll[3]<<" = "<<ll[0];
}
*/