+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121
+ 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 121 dummy
+ 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; + }; + linked_list linked_list + y-.2C_doubly-.2C_and_multiply-linked_lists y-.2C_doubly-.2C_and_multiply-linked_lists
+ tructure%29 tructure%29 + Use cpp library? + Can implement stack by array/linked list
+ #include + using namespace std; + int main(){ + stack mystack; + int x; + while (cin >> x) + mystack.push(x); + while (!mystack.empty()){ + cout << mystack.top() << endl; + mystack.pop(); + }
+ _structure%29 _structure%29 + Use cpp library? + Can implement queue by array/linked list
+ #include + using namespace std; + int main(){ + queue myqueue; + int x; + while (cin >> x) + myqueue.push(x); + while (!myqueue.empty()){ + cout << myqueue.front() << endl; + myqueue.pop(); + }
+ 514 Rails + ml ml Stacks of Flapjacks + ml ml Parentheses Balance + ml ml
+ a+(b+c) abc++ + (a+b)+cab+c+ + a-b*cabc*- + (a/b)*(c/d)ab/cd/* + a/(b+c*d-e)abcd*+e-/ + a-b*c+d/eabc*-de/+ + small exercise: Write a program to read in a postfix expression and calculate the result + See demo
+ 727 Equation + ml ml
+ Use a stack to store operators, left parentheses. + Read a char, check if cahr is…. + 1operand print it + 2 ( push onto stack.
+ 3operator + – 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 (