[英]Linked List Implementation in C
I am new to Linked LIsts and I am trying to implement a Linked List in C . 我是Linkstst的新手,我正在尝试在C中实现Linked List。 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");
}
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 . head始终为null,并且不更新null。 It doesnt enter into the else loop in insert . 它不会进入insert中的else循环。 Can anyone help me fix this please . 谁能帮我解决这个问题。 Point out the mistake I am doing . 指出我正在做的错误。 thanks 谢谢
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: 您的代码中可能还存在其他错误,但是一个大问题是,您试图在insert
设置头节点,但这只会影响传入的指针的本地副本,因此在调用者端无效:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
You also need to ensure that when you pass a node that is not NULL
, you actually attatch the new node to the head. 您还需要确保在传递非NULL
的节点时,实际上将新节点附加到头部。 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, ie use the address-of operator &
: 然后,在main
,您需要将指针传递到头指针,即使用地址运算符&
:
struct node *head = NULL;
insert(&head, 5);
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
. 这称为insert
,但是如果传入的指针为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;
}
I have fixed your insert
function: 我已经修复了您的insert
功能:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
#define CREATENODE malloc(sizeof(struct node))
void insert (struct node *head, int data);
void print (struct node *head);
int main()
{
struct node *head ;
head = CREATENODE;
printf("new\n");
insert(head,5);
insert(head,4);
insert(head,6);
print(head);
print(head);
print(head);
}
void insert(struct node *head,int data){
struct node *temp, *nn;
for(temp=head;temp->next!=NULL;temp=temp->next);
nn=CREATENODE;
nn->data = data;
nn->next =temp->next;
temp->next = nn;
}
void print (struct node *head) {
struct node *tmp = head->next;
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");
}
void insert(struct node &head,int data){ // pass reference rather than pointer ^ You passed a pointer, but that will only allow you to change the data it is pointing to. void insert(struct node&head,int data){//传递引用而不是指针^您传递了一个指针,但这仅允许您更改其指向的数据。 In order to change the pointer itself, you have to pass a reference. 为了更改指针本身,您必须传递一个引用。 Not sure my C isn't a bit rusty, but I'm just pointing you in the right direction.... 不确定我的C语言是否有点生锈,但我只是在指出正确的方向。
Regards, 问候,
André 安德烈
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.