-
Notifications
You must be signed in to change notification settings - Fork 5
ass00 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: selfie-master
Are you sure you want to change the base?
ass00 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4104,6 +4104,171 @@ int* copyC2CStarArguments(int argc, int *argv) { | |
| return cstar_argv; | ||
| } | ||
|
|
||
| ///////////////////////////////////////////////////////////////////// | ||
| //////------Assignment 0: Implementation of a Linked List------////// | ||
| ///////////////////////////////////////////////////////////////////// | ||
|
|
||
| // Initialization of the linked list (Creating the header with data -1) | ||
| // The header cannot be deleted!! | ||
| int* initializeList(int *list) { | ||
| int *header; | ||
| int data = -1; // no data inserted | ||
|
|
||
| header = malloc(4*2); | ||
| *header = 0; | ||
| *(header+1) = data; | ||
| return header; | ||
| } | ||
|
|
||
| // Add a node to the linked list at the bottom | ||
| int* addToList(int *list, int data) { | ||
|
|
||
| int *newNode; | ||
| newNode = malloc(4*2); | ||
| *newNode = list; | ||
| *(newNode+1) = data; | ||
| return newNode; | ||
| } | ||
|
|
||
| // Iterate through the linked list and get the nth node of the list | ||
| int* getNodeFromList(int* list, int nthNode) { | ||
|
|
||
| while(nthNode > 0) { | ||
| list = *list; | ||
| nthNode = nthNode - 1; | ||
| } | ||
|
|
||
| return list; | ||
| } | ||
|
|
||
| // Iterate through the linked list and get the data of the nth node of the list | ||
| int getDataFromList(int* list, int nthNode) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why duplicate the loop from getNodeFromList? One you've got the node, you got the data |
||
|
|
||
| while(nthNode > 0) { | ||
| list = *list; | ||
| nthNode = nthNode - 1; | ||
| } | ||
|
|
||
| return *(list+1); | ||
| } | ||
|
|
||
| // Iterate through the linked list and set the data of the nth node of the list | ||
| int alterDataInList(int* list, int nthNode, int newValue) { | ||
|
|
||
| while(nthNode > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment on getDataFromList. Same story here |
||
| list = *list; | ||
| nthNode = nthNode - 1; | ||
| } | ||
|
|
||
| *(list+1) = newValue; | ||
|
|
||
| return *(list+1); | ||
| } | ||
|
|
||
|
|
||
| // Get the size of the linked list | ||
| int sizeOfList(int* list) { | ||
|
|
||
| int *count; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is "count" allocated on the heap? |
||
| count = malloc(4); | ||
| *count = 0; | ||
|
|
||
| while(*list != 0) { | ||
| list = *list; | ||
| *count = *count + 1; | ||
| } | ||
| return *count; | ||
| } | ||
|
|
||
| // Delete a node from the linked list from the top | ||
| int* deleteFirstNodeFromList(int* list) { | ||
|
|
||
| int *prev; | ||
| int *next; | ||
| prev = malloc(2*4); | ||
| next = malloc(2*4); | ||
| *prev = 0; | ||
| *next = 0; | ||
| int size = sizeOfList(list); | ||
|
|
||
| prev = getNodeFromList(list, size-2); | ||
| next = getNodeFromList(list, size); | ||
| *prev = next; | ||
|
|
||
| return list; | ||
| } | ||
|
|
||
| // Sort the list with Bubble Sort | ||
| int* sortList(int* list) { | ||
|
|
||
| int size = sizeOfList(list); | ||
| while(size > 1){ | ||
| int i = 0; | ||
| while(i<size-1){ | ||
| int x = getDataFromList(list, i); | ||
| int y = getDataFromList(list, i+1); | ||
| if(x < y){ | ||
| alterDataInList(list, i, y); | ||
| alterDataInList(list, i+1, x); | ||
| } | ||
| i = i + 1; | ||
| } | ||
| size = size - 1; | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| // Print the linked list | ||
| void printList(int* list) { | ||
|
|
||
| int *counter; | ||
| counter = malloc(4); | ||
| *counter = sizeOfList(list); | ||
|
|
||
| while(*counter >= 0) { | ||
| int number = getDataFromList(list, *counter); | ||
| int *Buffer = (int*)malloc(4*10); | ||
| print(itoa( number, Buffer, 10, 0)); | ||
| putchar(CHAR_LF); | ||
| *counter = *counter - 1; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| int testList() { | ||
|
|
||
| int *list; | ||
| // Create new linked list (FIFO Linked List): | ||
| // top -> [9,7,8,2,4,1,5,3] -> bottom | ||
| printString('I','n','s','e','r','t',' ','i','n','t','o',' ','l','i','s','t',CHAR_LF,0,0,0); | ||
| list = initializeList(list); | ||
| list = addToList(list, 9); | ||
| list = addToList(list, 7); | ||
| list = addToList(list, 8); | ||
| list = addToList(list, 2); | ||
| list = addToList(list, 4); | ||
| list = addToList(list, 1); | ||
| list = addToList(list, 5); | ||
| list = addToList(list, 3); | ||
| printList(list); | ||
|
|
||
| // Delete the first node (FIFO Linked List): | ||
| // top -> [2,4,1,5,3] -> bottom | ||
| printString('D','e','l','e','t','e',' ','f','i','r','s','t',' ','n','o','d','e',CHAR_LF,0,0); | ||
| list = deleteFirstNodeFromList(list); | ||
| list = deleteFirstNodeFromList(list); | ||
| list = deleteFirstNodeFromList(list); | ||
| printList(list); | ||
|
|
||
| // Sorting the list | ||
| // top -> [5,4,3,2,1] -> bottom | ||
| printString('S','o','r','t','i','n','g',' ','t','h','e',' ','l','i','s','t',CHAR_LF,0,0,0); | ||
| list = sortList(list); | ||
| printList(list); | ||
|
|
||
| exit(0); | ||
| } | ||
|
|
||
| int main(int argc, int *argv) { | ||
| int *cstar_argv; | ||
| int *firstParameter; | ||
|
|
@@ -4136,5 +4301,6 @@ int main(int argc, int *argv) { | |
| } | ||
| } else | ||
| // default: compiler | ||
| main_compiler(); | ||
| } | ||
| //main_compiler(); | ||
| testList(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not alter the default behaviour of selfie. Add, for example, a new flag to trigger your test case. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See home work flow description again. Add your names! Do not replace previous authors. That's called plagiarism!