#include<stdio.h>
typedef struct Node{
int id;
int value;
struct Node* next;
}Node;
Node* creatSingleList(int singleListLength);
Node* getSingleListTail(Node* singleListHead);
void printSingleList(Node* singleListHead);
void printNodeIDandValue(Node* singleListPointer);
int getSingleListLength(Node* singleListHead);
int insertNodeAfterId(Node* singleListHead, int id);
int insertNodeAfterValue(Node* singleListHead, int value);
int searchIdFromList(Node* singleListHead, int id);
int searchValueFromList(Node* singleListHead, int value);
int updateSingleListLength(Node* singleListHead, int* singleListLengthPointer);
int main(int argc, char** argv){
Node* singleListHead;
Node* singleListTail;
int singleListLength;
int i;
singleListLength = 20;
singleListHead = creatSingleList(singleListLength);
printSingleList(singleListHead);
singleListTail = getSingleListTail(singleListHead);
printNodeIDandValue(singleListTail);
singleListLength = getSingleListLength(singleListHead);
printf("List length is : %d \n", singleListLength);
updateSingleListLength(singleListHead, &singleListLength);
printf("List length is : %d \n", singleListLength);
i = searchIdFromList(singleListHead, 6);
if(i != -1){
printf("id position is : %d \n", i);
}
i = searchValueFromList(singleListHead, 6);
if(i != -1){
printf("value position is : %d \n", i);
}
return 0;
}
void printSingleList(Node* singleListHead){
Node* singleListPointer;
singleListPointer = singleListHead;
while(singleListPointer != NULL){
printf("Node ID: %d \tvalue: %d ",singleListPointer->id,singleListPointer->value);
printf("\n");
singleListPointer = singleListPointer->next;
}
}
Node* creatSingleList(int singleListLength){
Node* singleListHead;
Node* singleListPointer;
int i;
singleListHead = (Node*)malloc(sizeof(Node));
singleListPointer = singleListHead;
srand(time(NULL));
for(i=0;i<singleListLength-1;i++){
singleListPointer->id = rand()%singleListLength;
singleListPointer->value = 2*i;
singleListPointer->next = (Node*)malloc(sizeof(Node));
singleListPointer = singleListPointer->next;
}
singleListPointer->id = rand()%singleListLength;
singleListPointer->value = 2*i;
singleListPointer->next = NULL;
return singleListHead;
}
Node* getSingleListTail(Node* singleListHead){
Node* singleListTail;
Node* singleListPointer;
singleListPointer = singleListHead;
while(singleListPointer->next != NULL){
singleListPointer = singleListPointer->next;
}
singleListTail = singleListPointer;
return singleListTail;
}
void printNodeIDandValue(Node* singleListPointer){
printf("\n");
printf("Node ID: %d\tvalue: %d",singleListPointer->id,singleListPointer->value);
printf("\n");
}
int getSingleListLength(Node* singleListHead){
int i;
Node* singleListPointer;
singleListPointer = singleListHead;
i = 1;
while(singleListPointer->next != NULL){
i++;
singleListPointer = singleListPointer->next;
}
return i;
}
int insertNodeAfterId(Node* singleListHead, int id){
return 0;
}
int insertNodeAfterValue(Node* singleListHead, int value){
return 0;
}
int updateSingleListLength(Node* singleListHead, int* singleListLengthPointer){
int length;
length = getSingleListLength(singleListHead);
*singleListLengthPointer = length;
return 0;
}
int searchIdFromList(Node* singleListHead, int id){
Node* singleListPointer;
int position;
position = 0;
singleListPointer = singleListHead;
while(singleListPointer->id != id){
singleListPointer = singleListPointer->next;
position++;
if(position >= getSingleListLength(singleListHead)){
fprintf(stderr,"Error! can not find id in the list!\n");
position = -1;
break;
}
}
return position;
}
int searchValueFromList(Node* singleListHead, int value){
Node* singleListPointer;
int position;
position = 0;
singleListPointer = singleListHead;
while(singleListPointer->value != value){
singleListPointer = singleListPointer->next;
position++;
if(position >= getSingleListLength(singleListHead)){
fprintf(stderr,"Error! can not find id in the list!\n");
position = -1;
break;
}
}
return position;
return 0;
}