在C中添加到链接列表的最前面 [英] Adding to the front of a Linked List in C

查看:39
本文介绍了在C中添加到链接列表的最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般在理解链表时遇到麻烦.我了解它们是如何在纸上工作的,但是一旦开始对它们进行编码,我似乎再也无法完成任何工作.

这是我的代码:

头文件:

  #include< stdlib.h>#include< stdio.h>#include< string.h>typedef结构列表{整数数据;struct列表* next;} 列表; 

实现文件:

  #include"test.h"void addToFront(int data,List * head);int main(void){清单*清单;list = malloc(sizeof(List));list-> next = NULL;名单*头;头= NULL;addToFront(5,head);printf(%d",head-> data);//打印第一个元素printf(%d",list-> data);//打印第一个元素}void addToFront(int data,List * head){if(head == NULL){列表* newNode = malloc(sizeof(List));newNode-> data = data;头= newNode;}别的 {列表* newNode = malloc(sizeof(List));newNode-> data = data;newNode-> next = head;头= newNode;}} 

我知道要使链接列表为空,标头为 NULL ,所以我在那里进行了检查.当我收到一个段错误说该头未初始化时,问题就出现了.很显然,不是这样,如果我进行了初始化,就无法跟踪该列表是否为空,因此使用了头节点./p>

我现在该怎么办?我不想使用双指针,因为对于我的课,到目前为止,还没有人在使用双指针(请不要让我使用双指针),而我对如何继续此处完全迷失了.

我正在考虑在没有头节点的情况下尝试此操作.这样,我可以有一个计数器来跟踪列表中的项目,检查其是否为零,然后将基本元素添加到最前面,否则可以在else语句中做同样的事情?

解决方案

问题出在您的addFront函数中,您仅将指针传递给头部,为了更改头部指向的内容,您需要传递地址头:

  void addToFront(int data,List ** head) 

然后

  * head = newHead 

仅传递指针时,您只是将指针的副本传递给函数,因此一旦离开函数作用域,对函数内部所做的指针所做的任何更改都会丢失.

在概念上类似于:

  void f(int n){n = 53;} 

为避免出现双指针,您可以返回新的头部:

  List * addToFront(int data,List * head){...返回newNode;}...头= addToFront(数据,头);... 

I'm having trouble comprehending linked lists in general. I understand how they work on paper, but once I get to coding them, I never seem to accomplish anything.

Here's my code:

header file:

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

typedef struct List {

    int data;
    struct List * next;
} List;

implementation file:

#include "test.h"

void addToFront(int data, List * head);

int main(void) {
    List * list;
    list = malloc(sizeof(List));
    list->next = NULL;

    List * head;
    head = NULL;

    addToFront(5,head);

    printf("%d",head->data); //print first element
    printf("%d",list->data); //print first element

}

void addToFront(int data, List * head) {
    if(head == NULL) {
        List * newNode = malloc(sizeof(List));
        newNode->data = data;
        head = newNode;
    }
    else {
        List * newNode = malloc(sizeof(List));
        newNode->data = data;
        newNode->next = head;
        head = newNode;
    }

}

I know that for a linked list to be empty, the header is NULL, so I have that checked there. The issue arises as I get a segfault saying that the header is not initialized, well obviously it's not, if I do initializes, I can't keep track of if the list is empty or not, hence the use of a header node.

What can I do now? I don't want to use double pointers, as for my class no one else is using them at any point so far yet (Please don't make me use double pointers), and I'm completely lost on how to proceed here.

I was thinking of trying this without a header node. As such I could have a counter that keeps track of the items in the list, check if its zero and then just add the basic element to the front, otherwise do the same thing I'm doing in my else statement?

解决方案

The problem is in your addFront function, you only pass the pointer to the head, in order to change what the head points to you need to pass the address of the head:

void addToFront(int data, List ** head) 

then

*head = newHead

When you pass only the pointer you are just passing a copy of the pointer to the function so any changes to the pointer you do inside the function are lost once you leave the function scope.

Similar in concept to:

void f(int n)
{
  n = 53;
}

To avoid double pointer you can return the new head:

List* addToFront(int data, List* head)
{
  ...
  return newNode;
}

...

head = addToFront(data, head);

...

这篇关于在C中添加到链接列表的最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆