17.10.2014 Views

Pico: Scheme for mere mortals

Pico: Scheme for mere mortals

Pico: Scheme for mere mortals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A Linked List in C++<br />

template class List<br />

{<br />

public:<br />

! struct Node<br />

! {<br />

! ! Node(const T& data, Node* next=0):data(data),next(next) {}<br />

! ! Node* next;<br />

! ! T data; ! void swap(List& x)<br />

! };<br />

! {<br />

!<br />

! ! Node* tmp = head; head = x.head; x.head = tmp;<br />

List() : head(0) ! {}}<br />

! List(const List& ! L) List& : head(0) operator=(const List& x)<br />

! {<br />

! {<br />

! ! <strong>for</strong> ( const ! Node* ! List i = L.begin(); tmp(x); i!= L.end(); i=i->next )<br />

! ! ! push_front(i->data);<br />

! ! swap(tmp);<br />

! ! reverse(); ! ! return *this;<br />

! }<br />

! }<br />

C++ = 10 12<br />

! void reverse() ! ~List() { clear(); }<br />

! {<br />

! void clear() { while (!empty()) ! T& front() pop_front(); { return } head->data; }<br />

! ! Node* p = 0; Node* i = begin(); Node* n;<br />

! const T& front() const { return head->data; }<br />

! ! while (i)! bool empty() { return !head; }<br />

! ! {<br />

! Node* begin() { return head; }<br />

! ! ! n = i->next;<br />

! void push_front(const ! T& x) Node* { end() { return 0; }<br />

! ! ! i->next ! = ! p; Node* tmp = new Node(x,head); head = tmp;<br />

! ! ! p = i; ! i } = n;<br />

! const Node* begin() const { return head; }<br />

! ! }<br />

! const Node* end() const { return 0; }<br />

! ! head = p; ! void pop_front() {<br />

! }<br />

! ! if (head) { Node* private: tmp = head; head=head->next; delete tmp; }<br />

! }<br />

! Node* head;<br />

};

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!