int main(int argc, char* argv[]) { int returned = -1; struct linkedList* q = createLinkedList(3); printf("Checking if the list is empty: %d\n", isEmptyList(q)); printf("Attempting to add to front \n"); addFrontList(q, 1); removeList(q, 1); printf("Checking if the list is empty: %d\n", isEmptyList(q)); addFrontList(q, 2); addFrontList(q, 3); _printList(q); printf("Attempting to add to back \n"); addBackList(q, 4); addBackList(q, 5); addBackList(q, 6); _printList(q); printf("Attempting to print front \n"); printf("%d\n", frontList(q)); printf("Attempting to print back \n"); printf("%d\n", backList(q)); printf("Attempting to remove front \n"); removeFrontList(q); _printList(q); printf("Attempting to remove back \n"); removeBackList(q); _printList(q); printf("Attempting to delete 4 \n"); removeList(q, 4); _printList(q); printf("Attempting to print list \n"); _printList(q); printf("Attempting to confirm contains 5 \n"); returned = containsList(q, 5); if (returned == 1) printf("true \n"); else printf("false \n"); return 0; }
/* Add an item to the bag param: lst pointer to the bag param: v value to be added pre: lst is not null post: a link storing val is added to the bag */ void addList(struct linkedList *lst, TYPE v) { /* FIXME: you must write this */ assert(lst!=NULL); addBackList(lst,v); }
/* Add an item to the bag param: lst pointer to the bag param: v value to be added pre: lst is not null post: a link storing val is added to the bag */ void addList(struct linkedList *lst, TYPE v) { /* FIXME: you must write this */ assert(lst != NULL); /* add to the back of the list instead of front since it is a bag */ addBackList(lst, v); }
int main(int argc, char* argv[]) { printf("implement testLinkedList.c!\n"); struct linkedList *lst; lst = createLinkedList(); printf("isEmptyList?%i\n", isEmptyList(lst)); printf("\nTesting add links (3),5,6,9,8\n"); addList(lst, 5); printf("addList 5:%i\n", frontList(lst)); addBackList(lst, 6); printf("backList 6:%i\n", backList(lst)); addBackList(lst, 9); printf("backList 9:%i\n", backList(lst)); addFrontList(lst,3); printf("frontList 3:%i\n", frontList(lst)); addBackList(lst, 8); printf("backList 8:%i\n\n", backList(lst)); printf("frontList 3:%i\n", frontList(lst)); printf("backList 8:%i\n\n", backList(lst)); printf("remove the front(3) and back(8)...\n"); removeFrontList(lst); printf("frontList should now be 5:%i\n", frontList(lst)); removeBackList(lst); printf("backList should now be 9:%i\n", backList(lst)); printf("contains 5?%i\n",containsList(lst,5)); printf("removing 5..."); removeFrontList(lst); printf("contains 5?%i\n",containsList(lst,5)); printf("removing 6..."); removeList(lst,6); printf("frontList? 9:%i", frontList(lst)); printf("isEmptyList?%i\n", isEmptyList(lst)); return 0; }
int main(){ struct bag* b = (struct bag*)malloc(sizeof(struct bag));/*Create new bag*/ initBag(b);/*Initialize*/ printf("size of list = %d \n", b->lst->size); printf("\n"); addFrontList(b->lst, 3); addFrontList(b->lst, 2); addBackList(b->lst, 4); addBackList(b->lst, 5); addBackList(b->lst, 6); addFrontList(b->lst, 1); int i; struct DLink *currentlink = b->lst->head->next; for(i = 1; i <= b->lst->size; i++){ printf("index %d = %d \n", i, currentlink->value); currentlink = currentlink->next; } printf("\n"); printf("front = %d \n", frontList(b->lst)); printf("back = %d \n", backList(b->lst)); printf("is list empty? = %d \n", isEmptyList(b->lst)); printf("size of list = %d \n", b->lst->size); printf("list contains = %d \n", listContains(b->lst, 12)); printf("\n"); addToBag(b, 10); removeFromBag(b, 1); struct DLink *link = b->lst->head->next; for(i = 1; i <= b->lst->size; i++){ printf("index %d = %d \n", i, link->value); link = link->next; } printf("list contains = %d \n", bagContains(b, 100)); return 0; }
void addList(struct linkedList *lst, TYPE v) { assert(lst != 0); addBackList(lst, v); }
/* Add an item to the bag param: lst pointer to the bag param: v value to be added pre: lst is not null post: a link storing val is added to the bag */ void addList(struct linkedList *lst, TYPE v) { assert(lst != 0); //make sure lst contains a valid address addBackList(lst, v); //add link to the bag (adds item to the back of the underlying list) }
/* Add an item to the bag param: lst pointer to the bag param: v value to be added pre: lst is not null post: a link storing val is added to the bag */ void addList(struct linkedList *lst, TYPE v){ // since this is a bag interface function, // it doesn't really matter where in the list // the link goes addBackList(lst, v); }
int main (int argc, char **argv) { /* init vars */ char opt; int numforks = 1; int msqid; int c; int i = 0; int j = 0; int bucket = 0; int found_word = 0; int msgLen; int msgTypes[MAXFORKS+1]; int EOM = 0; char s[MXSTRGLEN]; char t[MXSTRGLEN]; struct linkedList * master; struct linkedList * childlist[MAXFORKS+1]; struct mbuf m_toChild; struct mbuf m_toParent; /* sigaction for SIGINT */ struct sigaction si; struct sigaction ti; /* set up signal handling SIGINT */ si.sa_handler = sigi_handler; sigemptyset(&si.sa_mask); si.sa_flags = 0; /* initialize the actions */ sigaction(SIGINT, &si, &ti); /* use getopt to parse command line options pull in the options from the command line convert the numbers to longs using strtol acceptable options: -n: number of sort forks to create */ while ((opt = getopt (argc,argv,"n:"))!= -1) switch (opt){ case 'n': numforks = (int)strtol(argv[optind-1], (char**)NULL, 10); break; case '?': printf("******error******\n"); default: printf("bad input detected\n"); exit(EXIT_FAILURE); } /*generate a message queue*/ msqid = msgget(IPC_PRIVATE, S_IRUSR | S_IWUSR); /*make sure numforks is valid*/ if (numforks > MAXFORKS) numforks = MAXFORKS; else if (numforks < 1) numforks = 1; /*bucketize messages into MAXFORKS/numforks buckets (most efficient for when MAXFORKS % numforks = 0*/ bucket = 1; for (i = 1; i <= MAXFORKS; i++){ msgTypes[i] = bucket; if (i % ((MAXFORKS ) / numforks) == 0 && bucket < numforks) bucket++; } /*fork and exec the spec'd no. of times*/ for (i = 1; i <= numforks; i++){ switch(fork()){ case -1: perror("fork"); exit(EXIT_FAILURE); case 0: /*init linked list for this child*/ childlist[i]= createLinkedList(); /*recieve messages until EOM sent*/ while(!EOM){ msgrcv(msqid, &m_toChild, MXSTRGLEN, i, 0); memcpy(t, m_toChild.mtext, MXSTRGLEN); if(strcmp(t,"<EOM>\0") == 0){ EOM = 1; } else{ /*add received message to list*/ addSorted(childlist[i], t); } } /*send linked list as a message*/ /*printList(childlist[i]);*/ sendList(childlist[i], i, msqid, &m_toParent); strcpy(m_toParent.mtext,"<EOM>\0"); m_toParent.mtype = i; msgsnd(msqid, &m_toParent, MXSTRGLEN,0); /*printList(childlist[i]);*/ exit(EXIT_SUCCESS); default: break; } } master = createLinkedList(); /*parse the text, send it to sort processes*/ i = 0; while ((c =fgetc(stdin)) != EOF ) { /*if it's an alpha, convert it to lower case*/ if (isalpha(c)) { found_word = 1; c = tolower(c); /*fputc(c, stdout);*/ s[j] = c; j++; } else if (found_word) { /*fputc('\n', stdout);*/ s[j] = '\0'; msgLen = j+1; memcpy(m_toChild.mtext, s, msgLen); j=0; i=0; found_word=0; m_toChild.mtype = msgTypify(s[0],msgTypes); /*addSorted(master, s);*/ if (msgsnd(msqid, &m_toChild, msgLen, 0) == -1){ perror("msgsnd"); exit(EXIT_FAILURE); } } } /*send the sort processes the End Of Message*/ memcpy(m_toChild.mtext, "<EOM>\0", sizeof("<EOM>\0")); for (i = 1; i <= numforks; i++){ m_toChild.mtype = i; if (msgsnd(msqid, &m_toChild, msgLen, 0) == -1){ perror("msgsnd"); exit(EXIT_FAILURE); } } /*combine*/ EOM = 0; while(EOM < numforks){ msgrcv(msqid, &m_toParent, MXSTRGLEN, 0, 0); strcpy(s, m_toParent.mtext); if(strcmp(s,"<EOM>\0")==0) EOM++; else addBackList(master, s); } printList(master); /*kill the queue*/ msgctl(msqid, IPC_RMID, NULL); /*wait for children to finish*/ while (numforks > 1){ wait(NULL); --numforks; } exit(EXIT_SUCCESS); }