Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Description: Make a program that uses link lists
- Ismaeel Varis
- 2/3/16
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct Node
- {
- char letter;
- Node * link;
- Node(char let = ' ', Node * next = nullptr)
- {
- letter = let; link = next;
- }
- };
- class List
- {
- public:
- List(Node * ptr = nullptr)
- {
- head = ptr;
- }
- void add2Beg(char let)
- {
- Node *ptr;
- if (head == nullptr)
- {
- head = new Node(let);
- }
- else
- {
- ptr = new Node(let, head);
- head = ptr;
- }
- }
- void print()
- {
- print(head);
- }
- void add2End(char let)
- {
- Node * ptr = head;
- if (head == nullptr)
- head = new Node(let);
- else
- {
- while (ptr->link != nullptr)
- ptr = ptr->link;
- ptr->link = new Node(let);
- }
- }
- void fromString(const string & word)
- {
- Node * ptr = head;
- if (head == nullptr)
- {
- head = new Node(word[0], nullptr);
- }
- else
- {
- while (ptr->link != nullptr)
- {
- ptr->link;
- }
- }
- for (int i = 0; i > word.length(); i++)
- {
- ptr->link = new Node(word[i]);
- ptr = ptr->link;
- }
- }
- List operator+ (const List & rhs) const
- {
- List list1;
- Node *ptr = head;
- Node *rtptr = rhs.head;
- while (ptr != nullptr)
- {
- list1.add2End(ptr->letter);
- ptr = ptr->link;
- }
- while (rtptr != nullptr)
- {
- list1.add2End(rtptr->letter);
- rtptr->link;
- }
- return list1;
- // add2End will add to the end of rtptr
- }
- friend ostream & operator<<(ostream & out, const List & rhs);
- void deleteList(Node * & ptr)
- {
- if (ptr != nullptr)
- {
- deleteList (ptr->link);
- delete ptr;
- }
- // deletes info stored in headPtr
- }
- ~List()
- {
- deleteList(head);
- //default destructor
- }
- private:
- Node * head;
- void print(Node * ptr)
- {
- if (ptr != nullptr)
- {
- cout << ptr->letter << " ";
- print(ptr->link);
- }
- }
- };
- ostream & operator<<(ostream & out, const List & rhs)
- {
- Node *ptr = rhs.head;
- while (ptr != nullptr)
- {
- out << ptr->letter;
- ptr = ptr->link;
- }
- return out;
- }
- int main()
- {
- List list1;
- List list2;
- List list3;
- string word1 = "cat";
- string word2 = "fish";
- list1.fromString(word1);
- list2.fromString(word2);
- cout << list1 << " + " << list2 << " = "; //cat + fish = catfish
- list3 = list1 + list2;
- cout << list3 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement