+ struct node { + int x; + node * next; + }; + node *mylist; + node *mylist = null; head 121
TRANSCRIPT
+ struct Node {+ int x;+ Node * next;+ };
+ Node *mylist;+ Node *mylist = NULL;
head 1 2 1
+ Node *temp = new Node;+ temp->x = 1; // the new data.+ temp->next = mylist;+ mylist = temp;
+ Node *temp = mylist;+ while (temp) { // != NULL+ if (temp->x == target) + return temp; + temp = temp->next;+ }
+ Node *tail = mylist;+ while (tail) { + tail = tail->next;+ }+ Node *temp = new Node;+ temp->x = 1; // the new data.+ temp->next = NULL;+ tail->next= temp; //wrong! tail points to NULL
+ Node *tail = mylist;+ while (tail->next) { // empty list??+ tail = tail->next;+ }+ Node *temp = new Node;+ temp->x = 1; // the new data.+ temp->next = NULL;+ tail->next= temp;
head
1 2 1dummy
+ Node *mylist = NULL;+ Node *temp = new Node;+ temp->x = -1; // dummy+ temp->next = NULL;+ mylist = temp;
head
-1(dummy)
+ Node *temp = new Node;+ temp->x = 1; // the new data.+ temp->next = mylist->next;+ mylist->next = temp;
head
-1(dummy)
1
+ Node *tail = mylist;+ while (tail->next) {+ tail = tail->next;+ }+ Node *temp = new Node;+ temp->x = 1; // the new data.+ temp->next = NULL;+ tail->next= temp;
+ keep a pointer pointing to the last node of the list to speed up
+ Node *temp = mylist;+ while (temp->next) { // != NULL+ if (temp->next->x == target) {+ node *to_del = temp->next;+ temp->next = to_del->next;+ delete to_del;+ break;+ } + temp = temp->next;+ }
+ while (mylist != 0) {+ Node *t = mylist ->next;+ delete mylist ;+ mylist = t;+ }
+ struct Node {+ int x;+ Node *prev;+ Node * next;+ }; + http://en.wikipedia.org/wiki/Doubly-linked_list
+ http://en.wikipedia.org/wiki/Linked_list#Singly-.2C_doubly-.2C_and_multiply-linked_lists
+ http://en.wikipedia.org/wiki/Stack_%28data_structure%29
+ Use cpp library?
+ Can implement stack by array/linked list
+ #include <stack>+ #include <iostream>+ using namespace std;+ int main(){+ stack<int> mystack;+ int x;+ while (cin >> x)+ mystack.push(x); + while (!mystack.empty()){+ cout << mystack.top() << endl;+ mystack.pop();+ }+ }
+ http://en.wikipedia.org/wiki/Queue_%28data_structure%29
+ Use cpp library?
+ Can implement queue by array/linked list
+ #include <queue>+ #include <iostream>+ using namespace std;+ int main(){+ queue<int> myqueue; + int x;+ while (cin >> x)+ myqueue.push(x); + while (!myqueue.empty()){+ cout << myqueue.front() << endl;+ myqueue.pop();+ } + }
+ 514 Rails+ http://uva.onlinejudge.org/external/
5/514.html+ 120 Stacks of Flapjacks + http://uva.onlinejudge.org/external/
1/120.html+ 673 Parentheses Balance + http://uva.onlinejudge.org/external/
6/673.html
+ a+(b+c) abc+++ (a+b)+c ab+c++ a-b*c abc*-+ (a/b)*(c/d) ab/cd/*+ a/(b+c*d-e) abcd*+e-/+ a-b*c+d/e abc*-de/+
+ small exercise: Write a program to read in a postfix expression and calculate the result
+ See demo
+ 727 Equation + http://uva.onlinejudge.org/external/
7/727.html
+ Use a stack to store operators, left parentheses.
+ Read a char, check if cahr is….
+ 1 operand print it
+ 2 ( push onto stack.
+ 3 operator+ – pop and print the operators from the stack
one by one whenever they have a greater or equal precedence than char
+ –push the operator char onto the stack.
+ 4 ) + – pop and print the operators from the stack
one by one, stop when see a (+ – pop the (