-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
86 lines (71 loc) · 1.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct IntNode_struct {
int dataVal;
struct IntNode_struct* nextNodePtr;
} IntNode;
// Initialization
void InitializeIntNode(int dataInit, IntNode* thisNode) {
thisNode->dataVal = dataInit;
thisNode->nextNodePtr = NULL;
}
// Get node value
int GetNodeData(IntNode* thisNode) {
return thisNode->dataVal;
}
// Get pointer to next node
IntNode* GetNext(IntNode* thisNode) {
return thisNode->nextNodePtr;
}
/* Insert node after this node.
Before: this -- next
After: this -- node -- next
*/
void InsertAfter(IntNode* thisNode, IntNode* newNode) {
IntNode* tempNext = NULL;
tempNext = thisNode->nextNodePtr;
thisNode->nextNodePtr = newNode;
newNode->nextNodePtr = tempNext;
}
// Return true if list items are in asending order
bool IsSorted(IntNode* headNode) {
bool sorted = true;
IntNode* currNode = GetNext(headNode);
// Check for empty list
if (currNode == NULL) {
return true;
}
// Step through list one item at a time searching for target
while (GetNext(currNode) != NULL) {
if (GetNodeData(currNode) > GetNodeData(GetNext(currNode))) {
sorted = false;
}
// move forward one item
currNode = GetNext(currNode);
}
return sorted;
}
int main(void) {
IntNode* headNode;
IntNode* currNode;
IntNode* lastNode;
// Initiaize head node
headNode = (IntNode*)malloc(sizeof(IntNode));
InitializeIntNode(-1, headNode);
lastNode = headNode;
// Add nodes to the list
for (int i = 0; i < 20; ++i) {
currNode = (IntNode*)malloc(sizeof(IntNode));
InitializeIntNode(i, currNode);
InsertAfter(lastNode, currNode);
lastNode = currNode;
}
if (IsSorted(headNode)) {
printf("true\n");
}
else {
printf("false\n");
}
return 0;
}