//Declaring things: Queue //element in queue struct qnode { char info; *** struct qnode *next;//Note must have struct before that }; typedef struct qnode qnode; //queue management record //it is this record that is passed to functions struct queuetype { int count; qnode *top; qnode *rear; }; typedef struct queuetype queuetype; Stack struct node { char info; //node *next; //CANNOT DO THIS IN THE STRUCT DEFINITION struct node *next; }; typedef struct node node; struct dstack { int count; node *top; }; typedef struct dstack dstack; //Recall how to create and destroy memory p = malloc(sizeof(qnode)); // request the space, put address in p free(p); //delete the node //Know the high level functions for a stack and queue Queue: ADD (to rear), REMOVE (front) Stack: PUSH (to front), POP (rear) ***double check //Factorial type stuff int factorial_for(int n) { int product=1; int x; for(x=1; x<=n; x++) { product=product*x; } return product; } //OR int factorial ( int n ) { if (n <= 1) { return 1; } else { return (n * factorial(n-1));} } //Structs for trees: //tree node struct tnode { char info; int count; *** struct tnode *child[2]; }; typedef struct tnode tnode; //tree management record: it is this record that is passed to functions struct tree { int count; //NEEDED? tnode *top; }; typedef struct tree tree; //Traversing a Tree void printtree(tree *t) { if(tempty(*t)==TRUE) { printf("tree is empty\n");} else { printnode(t->top);} } void printnode(tnode *n) { printf("%c occuring %d times\n", n->info, n->count); if(n->child[0] != NULL) { printnode(n->child[0] );} if(n->child[1] !=NULL) {printnode(n->child[1] );} } //Declaring a Hash Table (as a Struct) struct hashtable { int number_bins; struct tree hash[num_bins]; //num_bins a predefined constant }; typedef struct hashtable hashtable; // A useful bit to peruse: //Note in particular FILE OPENING AND CLOSING void main() { tree hash[100]; char hold; int index, i; FILE *fp; for(int i=0; i<100; i++) * { tinit(&hash[i]);} fp = fopen("datain.txt", "r"); * while ( !feof( fp ) ) { fscanf (fp, "%c", &hold); index=hashfunction(hold); if(index>-1 && index<100) {add(&hash[index], hold);} } fclose(fp); fp = fopen("hashtable.txt", "w+"); for(i=0; i<100; i++) { if(!tempty(hash[i])) { //fp = fopen("hashtable.txt", "w+"); fprintf(fp,"\nIn hash cell %d:\n", i); //fclose(fp); * fprinttree(fp, &hash[i]); } } printf("\n The End\n\n"); fclose(fp); } //How do we identify an email say? //NOTE: strstr(word, "blah") returns a character pointer or Null //ALSO try more traditional way 1 void findchar(char in[]); void main() { char word1[]="jleblanc@email.com"; findchar(word1); } void findchar(char in[]) { int scan=0; while(in[scan]!='\n') { * if(in[scan]=='@')//weird when it's this char { printf("\n%d\n\n", scan); } scan=scan+1; } } 2 int is_email(char word[]) { int is_it=-1; char *a, *d; a=strchr(word, '@'); if(a!=NULL) {d=strchr(a, '.');} if(a!=NULL && d!=NULL) { is_it=1; } return is_it; } 3 //This picks up html pages too int is_htm(char word[]) { int is_it=-1; char *h; h=strstr(word, ".htm"); if(h!=NULL) { is_it=1; } return is_it; } //RECALL HOW TO DECLARE AND USE ARRAYS!!! //RECALL also that arrays are like pointers and can be operated on by functions and not returned //Answer to the merging sorted arrays question: void main() { int a1[3]={1,4,56}; int a2[3]={2,4,7}; int J[6]; int i; int s1, s2, Ms; Ms=s1=s2=0; while(Ms<6) { if(s1>2) { J[Ms]=a2[s2]; Ms++; s2++; printf("e2 %d %d\n", s1, s2); } else if(s2>2) { J[Ms]=a1[s1]; Ms++; s1++; printf("e1 %d %d\n", s1, s2); } else { if(a1[s1] < a2[s2]) { J[Ms] = a1[s1]; Ms++; s1++; printf("i1 %d %d\n", s1, s2); } else //this also takes care of equalities { J[Ms] = a2[s2]; Ms++; s2++; printf("i2 %d %d\n", s1, s2); } } } for(i=0; i<6;i++) { printf("%d\n", J[i]); } }