Spring 2020

PIC 10B: Intermediate Programming

Discussion Section 2A

Wonjun Lee



Week9

Tuesday

LinkedList Tutorial Part 1: Node

Link to the video

Enter password:

LinkedList Tutorial Part 2: Iterator

Link to the video

Enter password:

LinkedList Tutorial Part 3: Push Back

Link to the video

Enter password:

Solution
#include < iostream >
#include < cassert >

using namespace std;

class List;

class Node{
public:
  Node(int data):data(data), next(NULL), previous(NULL) {}
private:
  int data;
  Node* next;
  Node* previous;

friend class Iterator;
friend class List;
};

class Iterator{
public:
  Iterator(): position(NULL), container(NULL) {}

  int get() const{
    assert(position != NULL);
    return position->data;
  }

  void next(){
    assert(position != NULL);
    position = position->next;
  }

  bool equals(Iterator iter){
    return position == iter.position;
  }
private:
  Node* position;
  List* container;

friend class List;
};

class List{
public:
  List(): first(NULL), last(NULL) {}

  void push_back(int data){
    if(first == NULL) // when there is no node in the list
    {
      Node* new_node = new Node(data);
      first = new_node;
      last  = new_node;

      return;
    }

    Node* new_node = new Node(data);
    last->next = new_node;
    new_node->previous = last;
    last = new_node;
  }

  void insert(Iterator iter, int data){
    if(iter.position == NULL){
      push_back(data);
      return;
    }

    Node* new_node = new Node(data);

    if(iter.position == first){ 
      new_node->next = first;
      first->previous= new_node;
      first = new_node;
      return;
    }

    Node* after = iter.position;
    Node* before = iter.position->previous;

    new_node->next = after;
    new_node->previous = before;

    after->previous = new_node;
    before->next  = new_node;
  }

  void remove(Iterator iter){
    if(iter.position == NULL){
      return;
    }

    if(iter.position == first){
      Node* next_node = first->next;
      delete first;
      first = next_node;
      return;
    }

    if(iter.position == last){
      Node* prev_node = last->previous;
      delete last;
      last = prev_node;
      return;
    }

    Node* current= iter.position;
    Node* after = iter.position->next;
    Node* before= iter.position->previous;

    before->next = after;
    after->previous = before;

    delete current;
  }

  void reverse(){
    Node* tmp_first = first;
    Node* tmp_last  = last;

    Iterator iter;

    iter = begin();

    while(iter.equals(end()) == false){

      Node* some_node = iter.position;

      Node* tmp_next = some_node->next;
      some_node->next = some_node->previous;
      some_node->previous = tmp_next;

      iter.position = iter.position->previous;
    }

    first = tmp_last;
    last  = tmp_first;
  }

  Iterator begin(){
    Iterator iter;
    iter.position = first;
    iter.container= this;
    return iter;
  }

  Iterator end(){
    Iterator iter;
    iter.position = NULL;
    iter.container= this;
    return iter;
  }

private:
  Node* first;
  Node* last;

friend class Iterator;
};

int main(){

  List v;

  v.push_back(1);
  v.push_back(2);
  v.push_back(3);

  v.reverse();

  Iterator iter;
  
  for(iter = v.begin(); iter.equals(v.end()) == false; iter.next()){
    cout << iter.get() << endl;
  }
}

Thursday

Problems related to Linked List
Link to the video

Enter password:

Exercise P12.6.

Solution

class List{
public:

    // ... Member functions of List ...
    void reverse(){
        Node* tmp_first = first;
        Node* tmp_last  = last;

        Iterator iter;

        iter = begin();

        while(iter.equals(end()) == false){

            Node* some_node = iter.position;

            Node* tmp_next = some_node->next;
            some_node->next = some_node->previous;
            some_node->previous = tmp_next;

            iter.position = iter.position->previous;
        }

        first = tmp_last;
        last  = tmp_first;
    }

private:
  Node* first;
  Node* last;

friend class Iterator;
};

Exercise P12.7.

Solution

class List{
public:

    // ... Member functions of List ...
    
    void push_front(int data){

        size ++;

        // no item in the list
        if(first == NULL){
            Node* new_node = new Node(data);
            first = new_node;
            last  = new_node;

            return;
        }

        // at least one item in the list
        Node* new_node = new Node(data);

        first->previous = new_node;
        new_node->next  = first;
        first = new_node;
    }

private:
  Node* first;
  Node* last;

friend class Iterator;
};

Exercise P12.8.

Solution

class List{
public:

    // ... Member functions of List ...
     
    void swap(List& other){
        // TODO
        // swap first and first
        // swap last and last

        Node* original_first = first;
        Node* original_last = last;
        int orignal_size = size;

        first = other.first;
        last  = other.last;
        size  = other.size;

        other.first = original_first;
        other.last  = original_last;
        other.size  = orignal_size;
    }

private:
  Node* first;
  Node* last;

friend class Iterator;
};

Exercise P12.9.

Solution

class List{
public:

    // ... Member functions of List ...
     
    int get_size(){
        int count = 0;

        Iterator iter;

        for(iter = begin(); iter.equals(end()) == false; iter.next()){
            count ++;
        }

        return count;
    }

private:
  Node* first;
  Node* last;

friend class Iterator;
};