⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insertsort.txt

📁 It is an ebook about data structures,mainly linked list
💻 TXT
字号:
Write a C program to insert nodes into a linked list in a sorted fashion? 

Discuss it!          



The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node. 

Note that we assume the memory for the new node has already been allocated and a pointer to that memory is being passed to this function. 



// Special case code for the head end 
void linkedListInsertSorted(struct node** headReference, struct node* newNode) 
{ 
  // Special case for the head end 
  if (*headReference == NULL || (*headReference)->data >= newNode->data) 
  { 
     newNode->next = *headReference; 
     *headReference = newNode; 
  } 
  else 
  { 
     // Locate the node before which the insertion is to happen! 
     struct node* current = *headReference; 
     while (current->next!=NULL && current->next->data < newNode->data) 
     { 
        current = current->next; 
     } 
     newNode->next = current->next; 
     current->next = newNode; 
   } 
} 




 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -