问题
I have written a linked list code to insert a element in the node. But the problem is when i want to insert first element using function, the output is coming empty. But when i insert first element inside the main function (see comment line), it gives the correct output. How to solve it ? Here is my C code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int val;
struct node *next;
}node;
void print(node *head){
if(tem == NULL){
printf("List is Empty\n");
return;
}
node *tem= head;
while(tem != NULL){
printf("%d ", tem->val);
tem= tem->next;
}
}
void insert(node *head, int val){
if(head == NULL){
node *tem= malloc(sizeof(node*));
tem->val= val;
tem->next= NULL;
head= tem;
return;
}
node *tem= head;
while(tem->next != NULL){
tem= tem->next;
}
tem->next= malloc(sizeof(node*));
tem->next->val = val;
tem->next->next= NULL;
}
int main()
{
node *head= NULL;
/*
head = malloc(sizeof(node*));
head->val= 5;
head->next= NULL;
*/
insert(head, 15);
print(head);
return 0;
}
Thanks
回答1:
Try sending the address of the head
instead of head
as shown below:
insert(&head, 15);
void insert(node **head, int val){
if(*head == NULL){
node *tem= malloc(sizeof(node*));
tem->val= val;
tem->next= NULL;
*head= tem;
return;
}
This is because when you are sending the head, any changes made will be local to that function (insert in this case) and won't be reflected outside that function. Hence, you have to send the address of head (&head
) so that changes made to head are reflected outside the function as well. Cheers
回答2:
Try this completely implemented singly linked list:
#include <stdio.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
void insert(int data, int position)
{
struct node *newNode=malloc(sizeof(struct node));
newNode->data=data;
if(position<1)
{
printf("Invalid Insertion Position \n");
return;
}
if(head==NULL && position==1)
{
newNode->next=NULL;
head=newNode;
}
else if(head==NULL && position!=1)
{
printf("Invalid Insertion Position \n");
}
else if(position==1)
{
newNode->next=head;
head=newNode;
}
else
{
int i=0;
struct node *temp=head;
while(temp->next!=NULL && i<position-2)
{
i++;
temp=temp->next;
}
if(i<position-2)
{
printf("Invalid Insertion Position \n");
}
else
{
newNode->next=temp->next;
temp->next=newNode;
}
}
}
void delete(int position)
{
int i=0;
if(position<1)
{
printf("Invalid Position of Deletion \n");
return;
}
if(head==NULL)
{
return;
}
if(position==1)
{
head=head->next;
}
else
{
struct node *temp=head;
while(temp->next->next!=NULL && i<position-2)
{
i++;
temp=temp->next;
}
if(i<position-2)
{
printf("Invalid Position of Deletion \n");
return;
}
else
{
temp->next=temp->next->next;
}
}
}
void printlist()
{
if(head==NULL)
{
printf("Empty List!! \n");
return;
}
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
printf("\t");
temp=temp->next;
}
printf("\n");
}
int main()
{
int t;
printf("Enter number of Test Cases: \t");
scanf("%d", &t);
printf("\nEnter Queries in this format: \n");
printf("For Insertion: \t I data position \n");
printf("\tEx:\t I 25 5 \n");
printf("For Deletion: \t D position \n");
printf("\tEx:\t D 2 \n\n");
while(t--)
{
char c;
int a,b;
printf("Enter query: \t");
scanf("%c", &c);
scanf("%c", &c);
if(c=='I')
{
scanf("%d %d", &a,&b);
insert(a,b);
}
else if(c=='D')
{
scanf("%d", &a);
delete(a);
}
printlist();
}
}
来源:https://stackoverflow.com/questions/22918628/linked-list-not-working-for-insertion