Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <fstream>
- namespace str {
- class my_str {
- public:
- //O(n) =
- my_str(const char *s = " "){
- head = createNode(*s++);
- list_node *temp = head;
- while(*s != '\0'){
- temp->next = createNode(*s++);
- temp = temp->next;
- }
- }
- //O(n) =
- my_str(const my_str &s) {
- list_node *tempL1;
- list_node *tempL2 = s.head;
- head = new list_node;
- tempL1 = head;
- while(tempL2 != nullptr)
- {
- tempL1->info = tempL2->info;
- tempL1->next = new list_node;
- tempL1 = tempL1->next;
- tempL2 = tempL2->next;
- }
- delete tempL1;
- }
- //O(n) =
- my_str &operator=(const my_str &s) {
- destroyList();
- list_node *tempL1;
- list_node *tempL2 = s.head;
- head = new list_node;
- tempL1 = head;
- while(tempL2 != nullptr)
- {
- tempL1->info = tempL2->info;
- tempL1->next = new list_node;
- tempL1 = tempL1->next;
- tempL2 = tempL2->next;
- }
- return *this;
- }
- //O(n) =
- char &operator[](const int index) {
- list_node *temp = head;
- int searchKey = 0;
- while(searchKey !=index && temp != nullptr){
- searchKey++;
- temp = temp->next;
- }
- return temp->info;
- }
- //
- int length()const {
- return strlen(head);
- }
- //O(n) =
- int indexOf(char c)const {
- list_node *temp = head;
- int index = 0;
- while (temp->info != c && temp != nullptr){
- index++;
- temp = temp->next;
- }
- return index;
- }
- //O(n) =
- int indexOf(const my_str &pat)const {
- list_node *temp = head;
- }
- //O(n) =
- bool operator ==(const my_str &s)const {
- list_node *string1 = head;
- list_node *string2 = s.head;
- if(length() == s.length()){
- while(string1 != nullptr && string2 != nullptr){
- if(string1->info == string2->info )
- {
- return true;
- }
- string1 = string1->next;
- string2 = string2->next;
- }
- }
- return false;
- }
- //O(n) =
- my_str operator+(const my_str &s)const {
- my_str obj(*this);
- list_node *currChar = s.head;
- list_node *newChar;
- list_node *objTail = obj.getTail();
- while(currChar != nullptr){
- newChar = new list_node;
- newChar ->info = currChar->info;
- newChar ->next = nullptr;
- objTail ->next = newChar;
- objTail = objTail ->next;
- currChar = currChar->next;
- }
- return obj;
- }
- //O(n) =
- my_str &operator+=(const my_str &s) {
- }
- //O(n) =
- my_str reverse()const{
- my_str obj;
- list_node *tail;
- obj.head = new list_node;
- obj.head ->next = nullptr;
- obj.head->info = (*this)[length()-1];
- tail = obj.head;
- for(int i = length()-2; i >= 0; i--){
- char c = (*this)[i];
- list_node *temp = head;
- tail = new list_node;
- tail->next = nullptr;
- tail ->info = c;
- tail ->next = temp;
- tail = tail->next;
- }
- return obj;
- }
- //
- void print(std::ostream &out)const {
- list_node *temp = head;
- while(temp != nullptr)
- {
- out << temp->info;
- temp = temp->next;
- }
- }
- //O(n) =
- void read(std::istream &in) {
- destroyList();
- std::cout << "im here";
- list_node *temp = head;
- list_node *tail;
- head = new list_node;
- head->next = nullptr;
- std::cout << "im here 2";
- head ->info = in.get();
- std::cout << "im here 3";
- tail = head;
- char c = in.get();
- std::cout << "im here 4";
- while(!in.eof() && c != '\n' && c != '\0'){
- temp = new list_node;
- temp->next = nullptr;
- temp->info = c;
- c = in.get();
- tail->next = temp;
- tail = tail -> next;
- }
- std::cout << "im here 44";
- }
- //O(n) =
- ~my_str() {
- destroyList();
- }
- private:
- struct list_node
- {
- char info = '\0';
- list_node *next = nullptr;
- };
- list_node *createNode(char value)
- {
- return new list_node{value};
- }
- char &operator[](const int index)const {
- list_node *temp = head;
- int searchKey = 0;
- while(searchKey !=index && temp != nullptr){
- searchKey++;
- temp = temp->next;
- }
- return temp->info;
- }
- //returns the last index in the list
- list_node *getTail(){
- list_node *temp = head;
- while(temp->next != nullptr){
- temp = temp->next;
- }
- return temp;
- }
- void destroyList() {
- list_node *temp = head;
- list_node *ptr = temp;
- while(ptr != nullptr)
- {
- temp = temp->next;
- delete ptr;
- ptr = temp;
- }
- }
- static int strlen(list_node* ptr) {
- list_node *temp = ptr;
- int count = 0;
- while(temp != nullptr){
- count++;
- temp = temp->next;
- }
- return count;
- }
- list_node *head = nullptr;
- };
- inline std::ostream &operator <<(std::ostream &out, const my_str &str) {
- str.print(out);
- return out;
- }
- inline std::istream &operator >>(std::istream &in, my_str &str) {
- //str.read(in);
- return in;
- }
- /**********************************************************/
- //Testing Functions
- my_str copyConstructorTest(const my_str &l) {
- return l;
- }
- void testAssignment() {
- my_str a = "joji";
- my_str b;
- std::cout << "Before Assignment: " << a << std::endl;
- std::cout << "After Assignment: " << (b = a) << std::endl;
- }
- void testReverse() {
- std::ifstream ifs;
- ifs.open("input.txt");
- my_str l;
- l.read(ifs);
- std::cout << copyConstructorTest(l) << " " << l.length() << " " << l.reverse() << std::endl;
- ifs.close();
- }
- void testPrint() {
- my_str a = "hello";
- a.print(std::cout);
- }
- void testIndexOf() {
- my_str a = "jojithomas";
- std::cout << a.indexOf('a') << std::endl;
- }
- void testLength() {
- my_str j = "jojithomas";
- std::cout << j.length() << std::endl;
- }
- //test read
- void testRead() {
- my_str ar;
- ar.read(std::cin);
- std::cout << ar << std::endl;
- }
- void testAddEqual() {
- my_str a = "driver";
- my_str b = "racecar";
- std::cout << (b += a);
- }
- void testStrAdd() {
- my_str a = "joji";
- my_str b = "thomas";
- (a + b).print(std::cout);
- }
- void testEqual() {
- my_str a = "joji";
- my_str b = "joi";
- my_str c = "joji";
- std::cout << "A is = to B: ";
- std::cout << (a == b);
- std::cout << "\nA is = to C: ";
- std::cout << (a == c);
- }
- void testIndexOfMyStr() {
- my_str a = "joji";
- my_str b = "ojac";
- my_str c = "ji";
- std::cout << "a = " << a << std::endl;
- std::cout << "b = " << b << std::endl;
- std::cout << "c = " << c << std::endl;
- std::cout << "a.indexOf(b) = " << a.indexOf(b) << std::endl;
- std::cout << "a.indexOf(c) = " << a.indexOf(c) << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement