//
// main.cpp
// CS32_Hwk2
//
// Created by Sowmya Sridharan on 7/21/17.
// Copyright © 2017 project. All rights reserved.
//
#include <iostream>
#include <iostream>
#include <string>
using namespace std;
typedef string ItemType;
struct Node {
ItemType value;
Node *next;
};
class LinkedList {
private:
Node *head;
public:
// default constructor
LinkedList() : head(nullptr) { }
// copy constructor
LinkedList(const LinkedList& rhs);
// Destroys all the dynamically allocated memory
// in the list.
~LinkedList();
// assignment operator
const LinkedList& operator=(const LinkedList& rhs);
// Inserts val at the front of the list
void insertToFront(const ItemType &val);
// Prints the LinkedList
void printList() const;
// Sets item to the value at position i in this
// LinkedList and return true, returns false if
// there is no element i
bool get(int i, ItemType& item) const;
// Reverses the LinkedList
void reverseList();
// Prints the LinkedList in reverse order
void printReverse() const;
// Appends the values of other onto the end of this
// LinkedList.
void append(const LinkedList &other);
// Exchange the contents of this LinkedList with the other one.
void swap(LinkedList &other);
// Returns the number of items in the Linked List.
int size() const;
};
LinkedList::~LinkedList(){
Node* p;
while (head!= nullptr) {
p = head;
head = head->next;
delete p;
}
}
void LinkedList::insertToFront(const ItemType &val){
Node* temp = new Node;
temp->value = val;
temp->next = head;
head = temp;
}
void LinkedList::printList() const {
Node* temp = head;
while (temp!=nullptr) {
cout << temp->value << " ";
temp = temp->next;
}
cout << endl;
}
// Sets item to the value at position i in this
// LinkedList and return true, returns false if
// there is no element i
bool LinkedList:: get(int i, ItemType& item) const{
Node* temp = head;
for (int j =0; j < i; j++) {
if (temp->next == nullptr) return false;
temp=temp->next;
}
item = temp->value;
return true;
}
// Reverses the LinkedList
void LinkedList::reverseList(){
Node* nxt = nullptr;
Node* current = head;
Node* prev = nullptr;
while (current!=nullptr){
nxt = current->next;
current->next = prev;
prev = current;
current = nxt;
}
head = prev;
}
// Prints the LinkedList in reverse order
void LinkedList:: printReverse() const {
Node* headtwo;
Node* tempptr = head;
while (tempptr != nullptr) {
Node* temp = new Node;
headtwo = temp;
temp->value = tempptr->value;
tempptr = tempptr->next;
//temp->next = headtwo;
//headtwo = temp;
headtwo = temp -> next;
}
Node* temp2 = headtwo;
while (temp2!=nullptr) {
cout << temp2->value << " ";
temp2 = temp2->next;
}
cout << endl;
}
// Appends the values of other onto the end of this
// LinkedList.
void LinkedList:: append(const LinkedList &other) {
Node* temp = head;
while (temp->next != nullptr) temp = temp->next;
temp->next = other.head;
}
// Exchange the contents of this LinkedList with the other one.
void LinkedList:: swap(LinkedList &other) {
Node* temp = new Node;
Node* temp2 = new Node;
temp2 = other.head;
Node* placeholder = head;
while (placeholder != nullptr) {
temp->value = temp2->value;
temp2->value = placeholder->value;
placeholder->value = temp->value;
placeholder = placeholder->next;
}
}
// Returns the number of items in the Linked List.
int LinkedList::size() const
{
Node* temp = head;
int count = 0;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}
int main () {
LinkedList ls;
ls.insertToFront("Jack");
ls.insertToFront("Germaine");
ls.insertToFront("Agatha");
ls.insertToFront("Agnes");
for (int k = 0; k < ls.size(); k++)
{
string x;
ls.get(k, x);
cout << x << endl;
}
LinkedList ls2;
ls2.insertToFront("George");
ls2.insertToFront("Loise");
ls2.insertToFront("Lionel");
ls2.insertToFront("Helen");
//ls2.printList();
ls2.printReverse();
}