Created
February 9, 2012 03:58
-
-
Save rctay/1777166 to your computer and use it in GitHub Desktop.
[c] extension of Week 4, Q5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "list.h" | |
int | |
list_head(list *the_list, node_val *ret) | |
{ | |
if (!the_list) | |
return 1; | |
*ret = the_list->val; | |
return 0; | |
} | |
int | |
list_rest(list* the_list, list **ret) | |
{ | |
if (!the_list || !the_list->next) | |
return 1; | |
*ret = the_list->next; | |
return 0; | |
} | |
void | |
list_free(list** the_list) | |
{ | |
list *prev; | |
while ((prev = *the_list)) { | |
*the_list = (*the_list)->next; | |
free(prev); | |
} | |
*the_list = NULL; | |
} | |
static | |
int list_item_prompt(int *val) | |
{ | |
printf("Enter next int: "); | |
return scanf("%d", val); | |
} | |
int | |
list_from_stdin(list** ret) | |
{ | |
int read_int; | |
struct node *curr, *head, *last; | |
head = last = NULL; | |
while (list_item_prompt(&read_int) == 1) { | |
curr = (struct node *) malloc(sizeof(curr)); | |
curr->val = read_int; | |
if (!head) { | |
head = curr; | |
head->next = NULL; | |
last = head; | |
} else { | |
curr->next = NULL; | |
last->next = curr; | |
last = curr; | |
} | |
} | |
*ret = head; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <malloc.h> | |
typedef int node_val; | |
struct node { | |
node_val val; | |
struct node *next; | |
}; | |
typedef struct node list; | |
int | |
list_head(list *the_list, node_val *ret); | |
int | |
list_rest(list* the_list, list **ret); | |
void | |
list_free(list** the_list); | |
int | |
list_from_stdin(list** ret); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "list.h" | |
int is_non_decreasing(list*); | |
int main(void) { | |
int ret; | |
list *the_list; | |
list_from_stdin(&the_list); | |
ret = is_non_decreasing(the_list); | |
if (ret == 0) | |
printf("the integers are in non-decreasing order\n"); | |
else if (ret == 1) | |
printf("the integers are not in non-decreasing order\n"); | |
else | |
/* if we reach here, an error occurred; assume the error has | |
* been reported and do nothing */ | |
; | |
list_free(&the_list); | |
return 0; | |
} | |
int is_non_decreasing(list* the_list) { | |
list *rest_of_list; | |
node_val a, b; | |
/* base case 1: list is empty -> is non-decreasing */ | |
if (!the_list) | |
return 0; | |
/* base case 2: list has one item -> is non-decreasing */ | |
if (list_rest(the_list, &rest_of_list)) | |
return 0; | |
/* paranoid check */ | |
if (list_head(the_list, &a) | |
|| list_head(rest_of_list, &b)) { | |
fprintf(stderr, "bad condition\n"); | |
return -1; | |
} | |
/*************************** | |
* The condition here is wrong, can you fix it? | |
***************************/ | |
if (a == b || | |
is_non_decreasing(rest_of_list) == 0) | |
return 0; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment