Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int getLength(){
- int length = 0;
- ListNode* current = head;
- while (current) {
- length++;
- current = current->next;
- }
- return length;
- }
- int getPosition(int data){
- int position = 0;
- ListNode* current = head;
- while (current) {
- if(current->data < data){
- position++;
- }
- current = current->next;
- }
- return position;
- }
- void insert(int data){
- ListNode* tmp = (ListNode*) malloc(sizeof(100));
- ListNode* current = head;
- int position = getPosition(data),counter = 0, length = getLength();
- //Loop trough the List to check where we need to add it
- if(current != NULL){
- while (current) {
- if(position == counter){
- tmp->data = data;
- tmp->next = current;
- break;
- }
- current = current->next;
- counter++;
- }
- current->next = tmp;
- }else{
- //First element setting
- tmp->data = data;
- tmp->next = head;
- head = tmp;
- }
- free(tmp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement