Download presentation
Presentation is loading. Please wait.
Published byAmy Cain Modified over 9 years ago
2
+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121
3
+ Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = mylist; + mylist = temp;
4
+ Node *temp = mylist; + while (temp) { // != NULL + if (temp->x == target) + return temp; + temp = temp->next; + }
5
+ 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
6
+ 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;
7
head 121 dummy
8
+ Node *mylist = NULL; + Node *temp = new Node; + temp->x = -1; // dummy + temp->next = NULL; + mylist = temp; head -1(dummy)
9
+ Node *temp = new Node; + temp->x = 1; // the new data. + temp->next = mylist->next; + mylist->next = temp; head -1(dummy) 1
10
+ 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;
11
+ keep a pointer pointing to the last node of the list to speed up
12
+ 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; + }
13
+ while (mylist != 0) { + Node *t = mylist ->next; + delete mylist ; + mylist = t; + }
14
+ struct Node { + int x; + Node *prev; + Node * next; + }; + http://en.wikipedia.org/wiki/Doubly- linked_list http://en.wikipedia.org/wiki/Doubly- linked_list + http://en.wikipedia.org/wiki/Linked_list#Singl y-.2C_doubly-.2C_and_multiply-linked_lists http://en.wikipedia.org/wiki/Linked_list#Singl y-.2C_doubly-.2C_and_multiply-linked_lists
15
+ http://en.wikipedia.org/wiki/Stack_%28data_s tructure%29 http://en.wikipedia.org/wiki/Stack_%28data_s tructure%29 + Use cpp library? + Can implement stack by array/linked list
16
+ #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(); + }
17
+ http://en.wikipedia.org/wiki/Queue_%28data _structure%29 http://en.wikipedia.org/wiki/Queue_%28data _structure%29 + Use cpp library? + Can implement queue by array/linked list
18
+ #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(); + }
19
+ 514 Rails + http://uva.onlinejudge.org/external/5/514.ht ml http://uva.onlinejudge.org/external/5/514.ht ml + 120 Stacks of Flapjacks + http://uva.onlinejudge.org/external/1/120.ht ml http://uva.onlinejudge.org/external/1/120.ht ml + 673 Parentheses Balance + http://uva.onlinejudge.org/external/6/673.ht ml http://uva.onlinejudge.org/external/6/673.ht ml
20
+ 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
21
+ 727 Equation + http://uva.onlinejudge.org/external/7/727.ht ml http://uva.onlinejudge.org/external/7/727.ht ml
22
+ Use a stack to store operators, left parentheses. + Read a char, check if cahr is…. + 1operand print it + 2 ( push onto stack.
23
+ 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 (
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.