ساختمان داده ها
كتابهاي الكترونيكي درس
#include <iostream.h> struct Node { int data; Node* link; }; void main1() { Node* tail = new Node; tail->data = 2; tail->link = 0; tail->link = new Node; tail->link->data = 7; tail->link->link = 0; tail->link->link = new Node; tail->link->link->data = 1; tail->link->link->link = 0; /* Node* t = tail; while( t != 0 ) { cout << t->data << endl; t = t->link; } ///*/ for(Node* t=tail; t != 0; t = t->link) cout << t->data << endl; } void PrintNodes(Node* tail) { for(Node* t=tail; t != 0; t = t->link) cout << t->data << ", "; cout << endl; } Node* AddNode(int d, Node* l) { Node* t = new Node; t->data = d; t->link = l; return t; } int Max(Node* tail) { if( !tail ) return -1000; //tail != 0 int h = tail->data; for(Node* t=tail; t; t=t->link) { if( h < t->data ) h = t->data; } return h; } void RemoveAll(Node* &tail) { Node* t = tail; while( t ) { Node* y = t->link; delete t; t = y; } tail = 0; } void AddToBegin(Node* &tail, int d) { tail = AddNode(d, tail); } void RemoveFromBegin(Node* &tail) { if( !tail ) return; Node* t = tail->link; delete tail; tail = t; } void main2() { Node* tail= AddNode(2, AddNode(7, AddNode(1, AddNode(5, 0) ) ) ); //tail->link = AddNode(7, 0); //tail->link->link = AddNode(1, 0); //PrintNodes( tail ); //cout << Max(tail) << endl; /* PrintNodes(tail ); AddToBegin(tail, 3); PrintNodes(tail ); //*/ while( tail ) { PrintNodes(tail ); RemoveFromBegin( tail ); } RemoveAll( tail ); } ///### STACK #### struct Stack { Node* tail; }; bool isEmpty(Stack st) { return !st.tail; } bool push(Stack& st, int d) { AddToBegin(st.tail, d); return true; } bool pop(Stack& st, int& d) { if( isEmpty(st) ) return false; else { d = st.tail->data; RemoveFromBegin( st.tail ); return true; } } void main() { Stack st; st.tail = 0; for(int i=1; i<=12; i++) push(st, i); int d = 0; while( !isEmpty(st) ) { pop(st, d); cout << d << endl; } }
#include <iostream.h> const int MAX = 100; struct Stack { int top; char items[MAX]; bool push(char ch) { if( top == MAX ) return false; else { items[ top ] = ch; top ++; return true; } } bool pop(char& ch) { if( top == 0 ) return false; else { top --; ch = items[ top ]; return true; } } bool isEmpty() { return top == 0; } }; int code(char ch) { switch( ch ) { case '(': return 1; case ')': return -1; case '<': return 2; case '>': return -2; case '[': return 3; case ']': return -3; case '{': return 4; case '}': return -4; default: return 0; } } bool isBalance(const char s[]) { Stack st = {0}; for(int i=0; s[i] != '\0'; i++) { if( code( s[i] ) > 0 ) st.push( s[i] ); else if( code(s[i]) < 0 ) { if( st.isEmpty() ) return false; char ch; st.pop( ch ); if( code(s[i])+code(ch) != 0 ) return false; } } return st.isEmpty(); } /* void main() { Stack st = {0}; for(char ch='A'; ch <='Z'; ch++) st.push(ch); while( !st.isEmpty() ) { st.pop(ch); cout << ch << endl; } } //*/ void main() { char s[100]; /* cin.getline( s, 100 ); while( s[0] != '\0' ) { if( isBalance( s ) ) cout << "Balance !!" << endl; else cout << "not Balance !?!?" << endl; cin.getline( s, 100 ); } //*/ do { cin.getline( s, 100 ); if( isBalance( s ) ) cout << "Balance !!" << endl; else cout << "not Balance !?!?" << endl; } while( s[0] != '\0' ); }
- امتحان میان ترم:
- 1389/02/14 - 10:30
- امتحان پایان ترم:
- 1389/03/25 - 10:30