C 为单链表编写适当的push()函数。

C 为单链表编写适当的push()函数。,c,data-structures,linked-list,C,Data Structures,Linked List,请花些时间检查此链接列表基本文档中的错误推送功能。我只是遵循了这个函数的相同实现。因此,根据该文件,不应将数据添加到列表的开头。但我的工作方式与他们所说的必须实施的方式完全一致。我很困惑到底是什么问题 代码如下: #include<stdio.h> #include<stdlib.h> struct node{ int data; struct node * next; }; typedef struct node Node; /* Utilize a

请花些时间检查此链接列表基本文档中的错误推送功能。我只是遵循了这个函数的相同实现。因此,根据该文件,不应将数据添加到列表的开头。但我的工作方式与他们所说的必须实施的方式完全一致。我很困惑到底是什么问题

代码如下:

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

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

typedef struct node Node; /* Utilize a typedef for Node rather than typing
                             struct Node everytime we declare a node. */

Node* BuildOneTwoThree() {
    Node* head =NULL;   // pointer called head.
    Node* second =NULL; // pointer to second memory block
    Node* third = NULL; // pointer to third memory block

    head = (Node*)malloc(sizeof(Node));   //allocate a memory block of size node
                                          //in the heap and head pointer will 
                                          //point to this memory.
    second = (Node*)malloc(sizeof(Node)); //allocate a memory block of size node 
                                          //in the heap and second pointer will 
                                          //point to this memory
    third = (Node*)malloc(sizeof (Node)); //allocate a memory block of size node 
                                          //in the heap and third pointer will 
                                          //point to this memory

    head->data = 1;
    head->next = second; //the next pointer of node type will point to another 
                         //pointer of node type which is second!!

    second -> data = 2;
    second -> next = third;

    third -> data = 3;
    third -> next = NULL;
    return head;
}


Node* WrongPush(Node* head, int data) {

    Node* newNode = malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = head;
    head = newNode; // NO this line does not work!
    return head;
}

Node* WrongPushTest() {
    Node* head = BuildOneTwoThree();
    Node* current = WrongPush(head, 4);
    return current;
}

int main(){
    Node* ptr = WrongPushTest(); //My question is why does the wrong push 
                                 //test implementation that I setup work?
    while(ptr!=NULL) {
        printf("%d\n",ptr->data);
        ptr=ptr->next;
    }
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
类型定义结构节点;/*对节点使用typedef,而不是键入
每次我们声明一个节点时,结构节点*/
节点*buildonetwother(){
Node*head=NULL;//指针名为head。
Node*second=NULL;//指向第二个内存块的指针
Node*third=NULL;//指向第三个内存块的指针
head=(Node*)malloc(sizeof(Node));//分配一个大小为Node的内存块
//在堆中,头指针将
//指向这个记忆。
second=(Node*)malloc(sizeof(Node));//分配一个大小为Node的内存块
//在堆中,第二个指针将
//指向这个记忆
第三个=(Node*)malloc(sizeof(Node));//分配一个大小为Node的内存块
//在堆中,第三个指针将
//指向这个记忆
头部->数据=1;
head->next=second;//节点类型的下一个指针将指向另一个
//第二个节点类型的指针!!
第二->数据=2;
第二->下一个=第三个;
第三->数据=3;
第三->下一步=空;
回流头;
}
节点*错误推送(节点*头,整数数据){
Node*newNode=malloc(sizeof(Node));
新建节点->数据=数据;
新建节点->下一步=头部;
head=newNode;//不,这行不行!
回流头;
}
节点*test(){
Node*head=buildonetwother();
节点*电流=错误推送(头部,4);
回流;
}
int main(){
Node*ptr=ErrorPushTest();//我的问题是为什么错误的推送
//测试我设置的实现是否正常工作?
while(ptr!=NULL){
printf(“%d\n”,ptr->data);
ptr=ptr->next;
}
}

我注意到的第一件事是,您实际更改了您引用的文档中所写的实现。文档实现了如下
错误推送
(我对其进行了修改以使用您的结构定义):

您的实现的问题在于它不容易扩展。例如,使用您的代码,尝试如下测试:

Node * WrongPushTest() {
    Node * head = BuildOneTwoThree();
    Node * current = WrongPush(head, 4);
    current = WrongPush(head, 5);
    current = WrongPush(head, 6);
}
void Push(Node** headRef, int data) {
    Node * newNode = malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = *headRef;
    *headRef = newNode;
}
输出将不是程序员想要的东西。我们的目标是提供一个push函数,该函数利用原始的
头部
,而无需每次将另一个节点推到链接列表上时都创建一个新节点。他们在文件中使用的示例如下:

Node * WrongPushTest() {
    Node * head = BuildOneTwoThree();
    Node * current = WrongPush(head, 4);
    current = WrongPush(head, 5);
    current = WrongPush(head, 6);
}
void Push(Node** headRef, int data) {
    Node * newNode = malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = *headRef;
    *headRef = newNode;
}
请注意,我没有像在您的示例中那样返回指向新创建的头部的指针。通过直接操作原始的
指针,我们可以始终将新节点推送到原始链表的头上

Node * PushTest() {
    Node * head = BuildOneTwoThree();
    Push (&head, 4);
    Push (&head, 5);
    Push (&head, 6);
    return head;
}

希望这能帮你把事情弄清楚

调用
错误推送
后,
头插入
错误推送测试`错误。由于您在呼叫
错误推送后从未访问过
头部
,因此您可以侥幸逃脱处罚
谢谢!非常感谢。实际上,我无法扩展该程序!我正在寻找其他替代方案来扩大规模!!非常感谢!!当然。我很高兴这有帮助。