'Add element at the end of linked list
How can i add an element at the end of a linked list?
This is my try but I know there are some incorrect things:
typedef struct node_struct{
int data;
struct node_struct *next;
}node;
node *end(node *p){
node *new = malloc(sizeof(node));
node *next;
int value;
printf("Insert new element: ");
scanf("%d",&value);
p->next = next;
new->data = value;
next = new->data;
return next;
return next;
}
Now this is my code but it's still doesn't work:
node *end(node *p){
node *new = malloc(sizeof(node));
node *next;
int value;
printf("Insert new element: ");
scanf("%d",&value);
p->next = next;
new->data = value;
while(next->next){
next = next->next;
}
next->next = new;
return new;
}
Solution 1:[1]
Problem solved, this code works well, at least I think so.
void end(node *head){
node *new = malloc(sizeof(node));
node *next;
int value;
next = head->next;
printf("Insert number to add at the end: ");
scanf("%d",&value);
new->data= value;
while(next->next){
next = next->next;
}
next->next = new;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
