Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 删除链接排序列表中的第一个元素_C_Linked List_Pass By Reference - Fatal编程技术网

C 删除链接排序列表中的第一个元素

C 删除链接排序列表中的第一个元素,c,linked-list,pass-by-reference,C,Linked List,Pass By Reference,我是新来的,这实际上是我的第一个问题 我写了这段代码,我不能删除列表中的第一个元素,我可以删除其他元素。 我对函数reverse_list也有一个问题,我相信这是因为我没有将参数作为引用传递 我可以在C编程的参数中使用&吗 #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node * next; }; struct Node * build_sorted_li

我是新来的,这实际上是我的第一个问题

我写了这段代码,我不能删除列表中的第一个元素,我可以删除其他元素。 我对函数reverse_list也有一个问题,我相信这是因为我没有将参数作为引用传递

我可以在C编程的参数中使用&吗

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node * next;
};


struct Node * build_sorted_list () {
    struct Node * head = NULL, * temp, * rear, * front;
    int num;
    printf ("please enter a number:");
    scanf ("%d", &num);
    while (num != 0) {
        temp = (struct Node *) malloc (sizeof (struct Node));
        if (temp == NULL) {
            printf ("god damn");
            break;
        }
        temp -> data = num;
        if (head == NULL) {
            temp -> next = NULL;
            head = temp;
        }
        else if (num <= head -> data) {
            temp -> next = head;
            head = temp;
        }
        else {
            rear = head;
            front = rear -> next;
            while (front != NULL && front -> data <= num) {
                rear = front;
                front = front -> next;
            }
            temp -> next = front;
            rear -> next = temp;
        }
        printf ("enter number please:\n");
        scanf ("%d", &num);
    }
    return head;
}


struct Node * reverse_list (struct Node * head) {
    struct Node * rear, * mid, * front;
    if (head != NULL || head -> next == NULL) {
        return head;
    }
    rear = head;
    mid = rear ->next;
    front = mid -> next;
    while (front != NULL) {
        mid -> next = rear;
        rear = mid;
        mid = front;
        front = front ->next;
    }
    mid -> next = rear;
    head -> next = NULL;
    head = mid;
    return head;
}


struct Node * delete_num (int wanted, struct Node * head) {
    struct Node * rear, * front;
    if (head == NULL) {
        return head;
    }
    if (head -> data == wanted) {
        struct Node * temp = head;
        head = head -> next;
        temp = NULL;
        return head;
    }
    rear = head;
    front = head -> next;
    while (front != NULL) {
        if (front -> data == wanted) {
            break;
        }
        rear = front;
        front = front -> next;
    }
    if (front != NULL) {
        rear -> next = front -> next;
    }
    free (front);
    return head;
}


int main() {
    struct Node * head;
    int wanted;
    head = build_sorted_list(); /* Please pretend this function exists */
    reverse_list (head);

    printf ("please enter a number to delete:");
    scanf ("%d", &wanted);
    delete_num (wanted, head);

    free_list (head);

    return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*生成排序列表(){
结构节点*head=NULL,*temp,*rear,*front;
int-num;
printf(“请输入一个数字:”);
scanf(“%d”和&num);
while(num!=0){
temp=(结构节点*)malloc(sizeof(结构节点));
if(temp==NULL){
printf(“该死的”);
打破
}
温度->数据=num;
if(head==NULL){
temp->next=NULL;
压头=温度;
}
else if(num数据){
温度->下一步=头部;
压头=温度;
}
否则{
后部=头部;
前=后->下一步;
while(front!=NULL&&front->data next;
}
温度->下一步=前;
后->下一步=温度;
}
printf(“请输入号码:\n”);
scanf(“%d”和&num);
}
回流头;
}
结构节点*反向列表(结构节点*头){
结构节点*后部、*中部、*前部;
if(head!=NULL | | head->next==NULL){
回流头;
}
后部=头部;
中=后->下一步;
前=中->下一步;
while(前面!=NULL){
中->下一步=后;
后部=中部;
中=前;
前=前->下一步;
}
中->下一步=后;
head->next=NULL;
头部=中部;
回流头;
}
结构节点*删除数量(需要整数,结构节点*头){
结构节点*后,*前;
if(head==NULL){
回流头;
}
如果(头部->数据==需要){
结构节点*温度=头部;
头部=头部->下一步;
温度=零;
回流头;
}
后部=头部;
前=头部->下一步;
while(前面!=NULL){
如果(前端->数据==需要){
打破
}
后=前;
前=前->下一步;
}
如果(前!=NULL){
后->下一步=前->下一步;
}
自由(正面);
回流头;
}
int main(){
结构节点*头部;
通缉非法入境者;
head=build_sorted_list();/*请假设此函数存在*/
反向清单(标题);
printf(“请输入要删除的数字:”);
scanf(“%d”,需要(&d);
删除_num(通缉犯,负责人);
免费名单(标题);
返回0;
}

作为管理数据结构的递归函数的经验法则。对结构的操作应返回结构

根据定义,链表是:

  • 空名单
  • 以值和链接列表作为尾部的节点
链表具有递归性质。你需要非常小心,递归函数非常复杂,尽管代码很短

在main函数中有一个head指针。假设head保持不变是不安全的。事实上,当删除第一个元素(head)时,问题就来了

您的删除应该如下所示:

struct Node * delete_num(int wanted, struct Node * head){
    if( head == NULL)
        return NULL;    //Reached the end of the line, nothing to do.

    if( head->data != wanted )
        head->next = delete_num(wanted, head->next);    //Haven't reached it yet, but 
                                                        //the element must be down 
                                                        //the line, I want to update
                                                        //my next in case it's the 
                                                        //next node.

    if(head->data == wanted){
         struct Node * aux = head;
         head = head->next;
         free(aux);
    }

    return head;
}
这是假设您只想删除一个元素,并且您的列表不允许重复值

您从main对此函数的调用应该是:

head = delete_num(wanted, head);

Andres

在C中搜索模拟通过引用传递。我觉得你的
delete_num
没问题,但是你需要执行
head=delete_num(通缉,head);
temp=NULL
应该是
free(temp)首先,谢谢你抽出时间来帮助我。我看了一些计算机科学教授的视频,可惜他教C++,所以他使用了删除函数,当我尝试学习C时,我把删除函数翻译成Free函数,但是有人告诉我如果你使用自由函数,它会删除信息in。ide但没有删除变量。谢谢Andres,head=delete_num(通缉,head);解决了它,但我仍然不明白为什么,我只是添加了“head=”我仍然对反向函数有问题。。。