用C链接列表实现 [英] Linked List Implementation in C
问题描述
我是新来的链表,我想实现在C链表。
下面我code: -
I am new to Linked LIsts and I am trying to implement a Linked List in C . Below in my code :-
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void insert (struct node *head, int data);
void print (struct node *head);
int main()
{
struct node *head ;
head= NULL;
printf("new\n");
insert(head,5);
printf("%d\n",head);
insert(head,4);
insert(head,6);
print(head);
print(head);
print(head);
}
void insert(struct node *head,int data){
printf("%d\n",head);
if(head == NULL){
head =malloc(sizeof(struct node));
head->next = NULL;
head->data = data;
}
else {
printf("entered else\n");
struct node *tmp = head;
if(tmp->next!=NULL){
tmp = tmp->next;
}
tmp->next = malloc(sizeof(struct node));
tmp->next->next = NULL;
tmp->next->data = data;
}
}
void print (struct node *head) {
printf("%d\n",head);
struct node *tmp = head;
if (head == NULL) {
printf("entered null\n");
return;
}
while (tmp != NULL) {
if (tmp->next == NULL) {
printf("%0d", tmp->data);
} else {
printf("%0d -> ", tmp->data);
}
tmp = tmp->next;
}
printf("\n");
}
当我运行这个code输出: -
When I run this code the output is :-
new
0
0
0
0
0
entered null
0
entered null
0
entered null
头总是空并且它不更新空。它不进入中插入其他循环。
谁能帮我解决这个问题吧。指出我做的错误。
谢谢
The head is always null and it doesnt update the null . It doesnt enter into the else loop in insert . Can anyone help me fix this please . Point out the mistake I am doing . thanks
推荐答案
有可能在你的code其他错误,但一个大问题是,你正试图设置头节点插入
,但只影响传入的指针的本地副本,所以它在主叫方没有任何影响:
There may be other errors in your code, but one big issue is that you are attempting to set a head node in insert
, but that only affects a local copy of the pointer passed in, so it has no effect in the caller side:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
您还需要确保当你通过这不是一个节点 NULL
,你居然attatch新节点的头部。
您可以通过传递一个指针的指针,或返回集指针解决的第一个问题。例如,
You also need to ensure that when you pass a node that is not NULL
, you actually attatch the new node to the head.
You can fix the first problem by passing a pointer to a pointer, or by returning the set pointer. For example,
void insert(struct node **head, int data) {
if(*head == NULL) {
// create the head node
...
*head = malloc(sizeof(struct node));
....
else {
// create a new node and attach it to the head
struct node* tmp = malloc(sizeof(struct node));
....
(*head)->next = tmp;
}
}
然后,在主
,你需要一个指针传递给头指针,即使用地址运算符&安培;
:
Then, in main
, you need to pass a pointer to the head pointer, i.e. use the address-of operator &
:
struct node *head = NULL;
insert(&head, 5);
注意的部分问题是该函数试图做太多。这就是所谓的插入
,但它试图创建一个新的节点,如果传入的指针 NULL
。这将是更好的分离这些职责:
Note part of the problem is that the function is trying to do too much. It is called insert
, but it attempts to create a new node if the pointer passed in is NULL
. It would be better to separate these responsibilities:
// allocate a node and set its data field
struct node* create_node(int data)
{
struct node* n = malloc(sizeof(struct node));
n->next = NULL;
n->data = data;
return n;
}
// create a node and add to end node.
// return the new end node.
// end has to point to a valid node object.
struct node* append_node(struct node* tail, int node_data)
{
struct node* new_tail = create_node(node_data);
tail->next = new_tail;
return new_tail;
}
这篇关于用C链接列表实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!