Tag: insert

插入单链表C

我在C中链接列表fucntion前面的插入有问题 #define arrSIZE = 100; struct listNode { char data[arrSIZE]; struct listNode *nextPtr; }; typedef struct listNode ListNode; void insertHead(ListNode *sPtr, char value[arrSIZE]){ ListNode *newPtr = (ListNode *)malloc(sizeof(ListNode)); strncpy(newPtr->data, value, arrSIZE); if(sPtr ==NULL){ newPtr->nextPtr=NULL; sPtr = newPtr; }else{ newPtr->nextPtr=sPtr; sPtr =newPtr; } }

将数字插入已排序的链接列表,为什么数字每次都插入第二个位置?

我正在开发一个关于链表的项目,我无法将数字插入到已排序的链表中。 每次插入第二个位置的数字,我无法弄清楚问题出在哪里。这是我的代码: void insertSort(struct linkedList *n,int num,int *length){ //insert number to a sort linked list node *new = (node *) malloc(sizeof(node)); //create a new node new->next=NULL; new->data = num; while(n!=NULL&&n->data > new->data){ // find which position num should insert in sorted list n = n->next; } new->next = n->next; n->next= new; length += 1; } […]