+ struct node { + int x; + node * next; + }; + node *mylist; + node *mylist = null; head 121

24

Upload: amy-cain

Post on 05-Jan-2016

329 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121
Page 2: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ struct Node {+ int x;+ Node * next;+ };

+ Node *mylist;+ Node *mylist = NULL;

head 1 2 1

Page 3: + 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;

Page 4: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ Node *temp = mylist;+ while (temp) { // != NULL+ if (temp->x == target) + return temp; + temp = temp->next;+ }

Page 5: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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

Page 6: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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;

Page 7: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

head

1 2 1dummy

Page 8: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ Node *mylist = NULL;+ Node *temp = new Node;+ temp->x = -1; // dummy+ temp->next = NULL;+ mylist = temp;

head

-1(dummy)

Page 9: + 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->next;+ mylist->next = temp;

head

-1(dummy)

1

Page 10: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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;

Page 11: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ keep a pointer pointing to the last node of the list to speed up

Page 12: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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;+ }

Page 13: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ while (mylist != 0) {+ Node *t = mylist ->next;+ delete mylist ;+ mylist = t;+ }

Page 14: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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

Page 15: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ http://en.wikipedia.org/wiki/Stack_%28data_structure%29

+ Use cpp library?

+ Can implement stack by array/linked list

Page 16: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ #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();+ }+ }

Page 17: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ http://en.wikipedia.org/wiki/Queue_%28data_structure%29

+ Use cpp library?

+ Can implement queue by array/linked list

Page 18: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ #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();+ } + }

Page 19: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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

Page 20: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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

Page 21: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 727 Equation + http://uva.onlinejudge.org/external/

7/727.html

Page 22: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ Use a stack to store operators, left parentheses.

+ Read a char, check if cahr is….

+ 1 operand print it

+ 2 ( push onto stack.

Page 23: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121

+ 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 (

Page 24: + struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121