Tag: 插入

插入已排序的数组

我想在正确的位置插入一个元素,该顺序在排序列表中维护。 我为arrays分配了2 * n的大小,并用999填充其余的,因为它们目前没有使用。 ordered_insert(int number,int array[],int size){ int i=0; int temp1,temp2,index; while(eleman>array[i]){ i++;} //push the rest to right by one index=i; if(i<size){ temp1=array[i]; temp2= array[i+1]; array[i+1]=temp1; array[i+2]=temp2; i++; } array[index]=number; } 我无法弄清楚如何覆盖999s或者有更好的方法吗?

将元素插入到数组C中

我有一个已经排序过的数字数组,所以不需要对它进行排序,我需要在数组中的有效位置插入一个给定的值,命名为val 。 我的程序适用于小于最后一个值的给定值,但对于值大于最后一个的情况,我的程序只是不想插入值。 例如,对于数组{1, 2, 3, 4, 6}和值5 ,数组应为{1, 2, 3, 4, 5, 6} ,但对于值7我的数组看起来像{1, 2, 7, 4, 6, 0} 。 #include void insert(int val, int *n, int v[]) { int index; index = n – 1; if (n == 0) { v[0] = val; // check if array is empty n = n + […]

二进制搜索树插入 – root始终为null

我有ds代码用于使用递归在二叉搜索树中插入值。 问题是root始终保持为null。 执行时,第一个printf()打印10,但第二个printf(在insertRec(10)之后)不打印任何内容,因为root为null。 #include #include struct llist { int data; struct llist *left; struct llist *right; }; typedef struct llist node; void insertRec(node *r, int num) { if(r==NULL) { r=(node*)malloc(sizeof(node)); r->data=num; r->left=NULL; r->right=NULL; printf(“%d “,r->data); //1st printf } else { if(num data) insertRec(r->left, num); else insertRec(r->right, num); } } void display(node *x) { if(x != […]

C在链接列表的开头插入元素

我用C编写了一个程序,用于按升序将结构插入到链表中。 问题是没有插入我的两个最低值(1和2)。 这是因为我目前没有工作处理程序来检查链表的第一个值是否已大于给定值。 这是我的function: struct PCB { struct PCB *Next_PCB ; int PID ; }; void insert_ordered (struct PCB *Head, struct PCB *Add) { tmp = Head; if (Head->PID == 0) { Head->PID = Add->PID; } else { if (Head->Next_PCB == NULL) { Head->Next_PCB = Add; } else { int count = 0; while (Head […]

有效地将字符串插入另一个字符串

我有 char aa[] = { “Hello, !” }; char bb[] = { “World” }; 如何使用cstring将bb最有效地插入aa?

最后是C链表插入节点

我的插入方法在C中的链接列表时遇到了一些问题。它似乎只在列表的开头添加。 我做的任何其他插入失败。 而这个CodeBlocks调试器很难理解我仍然没有得到它。 它永远不会给我价值,只有内存中的地址。 无论如何这是我的function。 你有没有看到它失败的原因? /* function to add a new node at the end of the list */ int addNodeBottom(int val, node *head){ //create new node node *newNode = (node*)malloc(sizeof(node)); if(newNode == NULL){ fprintf(stderr, “Unable to allocate memory for new node\n”); exit(-1); } newNode->value = val; //check for first insertion if(head->next == NULL){ […]