1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct _Node 5 { 6 int data; 7 struct _Node *next; 8 }Node; 9 10 Node *newList(); 11 Node *insertNode(Node *head, int data); 12 void printList(Node *head); 13 Node* insert_at_tail(Node *tail, int data); 14 void deleteList(Node *head); 15 int getMax(Node *head); 16 17 int getMax(Node *head) 18 { 19 Node *temp = head; 20 int maxVal = head->data; 21 while(temp != NULL) 22 { 23 if(maxVal < temp->data) 24 { 25 maxVal = temp->data; 26 } 27 temp = temp->next; 28 } 29 return retVal; 30 } 31 32 Node *insertNode(Node *head, int data) 33 { 34 Node *newNode = malloc(sizeof(Node)); 35 if(newNode == NULL) 36 { 37 printf("newNode is NULL!\n"); 38 exit(-1); 39 } 40 Node *temp = head; 41 while( temp->next != NULL) 42 { 43 temp = temp->next; 44 } 45 newNode->data = data; 46 newNode->next = temp->next; 47 temp->next = newNode; 48 49 return newNode; 50 } 51 52 /* Make a new list. Create a dummy head node to represent an empty list, and it returns a head of a new empty list*/ 53 Node *newList() 54 { 55 Node *dummyNode = malloc(sizeof(Node)); 56 if(dummyNode == NULL) 57 { 58 printf("dummyNode is NULL! \n"); 59 exit(-1); 60 } 61 return dummyNode; 62 } 63 64 /* Insert at tail */ 65 Node *insert_at_tail(Node *tail, int data) 66 { 67 Node *newNode = malloc(sizeof(Node)); 68 if(newNode == NULL) 69 { 70 printf("newNode is NULL!\n"); 71 exit(-1); 72 } 73 newNode->data = data; 74 newNode->next = NULL; 75 tail->next = newNode; 76 return newNode; 77 } 78 79 /* Delete an entire list */ 80 void deleteList(Node *head) 81 { 82 ListNode *temp = head; 83 ListNode *delNode; 84 85 while(temp != NULL) 86 { 87 delNode = temp; 88 temp = temp->next; 89 delNode->next = NULL; 90 free(delNode); 91 } 92 free(temp); 93 } 94 95 void printList(Node *head) 96 { 97 Node *temp = head; 98 while(temp != NULL) 99 { 100 printf("%d \n",temp->data); 101 temp = temp->next; 102 } 103 }