int main() { Table table; InitTable(&table); AddTable(&table, 1, 0, 1); AddTable(&table, 1, 1, 2); AddTable(&table, 1, 2, 3); DisplayTable(&table); printf("\n"); int valid = 1; int dst_addr = 0; int out_link_id = 10; UpdateTable(&table, valid, dst_addr, out_link_id); DisplayTable(&table); /* int index = FindTableIndex(&table, dst_addr); if (index == ERROR) { printf("inserting new entry to table\n"); AddTable(&table, valid, dst_addr, out_link_id); } else { printf("updating entry %d\n", dst_addr); UpdateTableByIndex(&table, index, valid, out_link_id); } */ printf("OutLink for dst_addr = %d is %d\n", dst_addr, GetOutLink(&table, dst_addr)); return 0; }
int main(int argc, char * argv[]) { char *testFileName; FILE *fd; struct SymTab *theTable, *firstCopy, *secondCopy; struct SymEntry *anEntry; struct Attributes *anAttr; char buffer[16]; int val1, val2; if (argc != 2) { fprintf(stderr,"usage: SymTabDriver test-data-file\n"); exit(1); } testFileName = argv[1]; fd = fopen(testFileName,"r"); if (!fd) ErrorExit("Can't open input file.\n"); if (!(theTable = CreateSymTab(5))) ErrorExit("Failed to alloc first table.\n"); /* Read lines consisting of a name string and an integer from std input. If name already present increment value2, if new allocate storage for attribute structure and fill in fields. */ while (fscanf(fd,"%15s %d",buffer,&val1) != EOF) { printf("Find: %15s ",buffer); (FindName(theTable,buffer)) ? fprintf(stdout," Present -") : fprintf(stdout," Not Present -"); if (EnterName(theTable,buffer,&anEntry)) { fprintf(stdout," Enter: Present: %15s\n",GetName(anEntry)); anAttr = (struct Attributes *) GetAttr(anEntry); anAttr->value2++; anAttr->value1 = MAX(((struct Attributes *) GetAttr(anEntry))->value1,val1); } else { fprintf(stdout," Enter: Entered: %15s\n",GetName(anEntry)); anAttr = malloc(sizeof(struct Attributes)); anAttr->value1 = val1; anAttr->value2 = 1; SetAttr(anEntry,anAttr); } } fprintf(stdout,"\nContents of Original Table\n"); DisplayTable(theTable); DisplayStatistics(theTable); if (!(firstCopy = CopyTable(theTable,1))) ErrorExit("Failed to alloc first copy table.\n"); DestroySymTab(theTable); fprintf(stdout,"\nContents of First Copy Table\n"); DisplayTable(firstCopy); DisplayStatistics(firstCopy); if (!(secondCopy = CopyTable(firstCopy,100))) ErrorExit("Failed to alloc first copy table.\n"); DestroySymTab(firstCopy); fprintf(stdout,"\nContents of Second Copy Table\n"); DisplayTable(secondCopy); DisplayStatistics(secondCopy); VerifyCounts(secondCopy); FreeAllAttr(secondCopy); DestroySymTab(secondCopy); return 0; }