[英]Linked List Implementation in C
我是Linkstst的新手,我正在嘗試在C中實現Linked List。 在我的代碼下面:
#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");
}
當我運行此代碼時,輸出為:-
new
0
0
0
0
0
entered null
0
entered null
0
entered null
head始終為null,並且不更新null。 它不會進入insert中的else循環。 誰能幫我解決這個問題。 指出我正在做的錯誤。 謝謝
您的代碼中可能還存在其他錯誤,但是一個大問題是,您試圖在insert
設置頭節點,但這只會影響傳入的指針的本地副本,因此在調用者端無效:
void insert(struct node *head,int data){
....
head = malloc(sizeof(struct node)); // head is local, caller won't see this
您還需要確保在傳遞非NULL
的節點時,實際上將新節點附加到頭部。 您可以通過將指針傳遞給指針或返回設置的指針來解決第一個問題。 例如,
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;
}
}
然后,在main
,您需要將指針傳遞到頭指針,即使用地址運算符&
:
struct node *head = NULL;
insert(&head, 5);
注意問題的一部分是該函數嘗試執行過多操作。 這稱為insert
,但是如果傳入的指針為NULL
,它將嘗試創建一個新節點。 最好將這些職責分開:
// 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;
}
我已經修復了您的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){//傳遞引用而不是指針^您傳遞了一個指針,但這僅允許您更改其指向的數據。 為了更改指針本身,您必須傳遞一個引用。 不確定我的C語言是否有點生銹,但我只是在指出正確的方向。
問候,
安德烈
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.