I have wrote a code to insert data in LinkedList. But there is problem outputting the data. Here is the code.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *name;
int age;
struct node *next;
};
struct node *linkeslist1head= NULL;
struct node *linkedlist1tail= NULL;
void llinsertend(const char *a,const int *b){
struct node *current = malloc(sizeof(struct node));
if(current == NULL){
printf("Current creation failed.\n");
}
current->name = malloc(strlen(a)+1);
if(current->name == NULL) {
printf("String allocation failed\n");
}
strcpy(current->name,a);
current->age = *b;
if(linkeslist1head == NULL){
linkeslist1head = current;
linkedlist1tail = current;
}else{
//If the list is not empty, append the new node to the end
linkedlist1tail->next = current;
// Update tail to point to the new last node
linkedlist1tail = current;
}
}
void llinsertbegin(const char *a, const int *b) {
struct node *newnode = malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Memory allocation failed\n");
return;
}
newnode->name = malloc(strlen(a) + 1);
if (newnode->name == NULL) {
printf("String allocation failed\n");
free(newnode);
return;
}
strcpy(newnode->name, a);
newnode->age = *b;
if (linkeslist1head == NULL) {
// If the list is empty
newnode->next = NULL;
linkeslist1head = newnode;
linkedlist1tail = newnode;
} else {
// If the list is not empty
newnode->next = linkeslist1head;
linkeslist1head = newnode;
}
}
void llinsertaftern(const char *a, const int *b, int n) {
struct node *current = linkeslist1head;
int i;
for (i = 1; current != NULL && i < n; i++){
current = current->next; // Iterate until the (n-1)th node or until current becomes NULL
}
if (current == NULL){
printf("LL short\n");
return; // Exit the function if current is NULL
}
printf("Reached node %d\n", i);
struct node *newnode = malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Memory allocation failed\n");
return;
}
newnode->name = malloc(strlen(a) + 1);
if (newnode->name == NULL) {
printf("String allocation failed\n");
free(newnode);
return;
}
strcpy(newnode->name, a);
newnode->age = *b;
if (current == NULL) {
printf("LL is shorter than %d\n", n);
free(newnode->name);
free(newnode);
return;
}
newnode->next = current->next;
current->next = newnode;
}
void outputLinkedList(struct node *head){
struct node *p = head;
while(p != NULL){
printf("Name:%s Age:%d\n",p->name,p->age);
p = p->next;
}
printf("\n");
}
int main() {
printf("How many persons' details you want to add\n");
int t;
scanf("%d",&t);
getchar();
for(int i=1;i<=t;i++){
int x;
char name[50];
scanf("%s",name);
getchar();
scanf("%d",&x);
llinsertend(name,&x);
}
int x=10,y=20,z=30;
llinsertbegin("facebook",&x );//(const int *) 10
llinsertbegin("instragram", &y);
llinsertbegin("whatsapp", &z);
outputLinkedList(linkeslist1head);
int code=1200,pos = 3;
llinsertaftern("Dhaka",&code,pos);
outputLinkedList(linkeslist1head);
}
In the second outputLinkedList not working in either CLion or Codeblocks but it works in Compiler Explorer. I mean after adding some elements through the llinsertend() function then the llinsertbegin() function, the output function works perfectly. But when I use the llinsertaftern() function it shows a segmentation fault in the debugger. Suppose that I add three elements through the llinsertend() function and then add 3 elements through the llinsertbegin() function, then what is the problem of llinsertaftern() with the value of pos = 3. It should work perfectly fine.
Your code contains meny bugs. For example let's consider function
llinsertendFirstly if a new node was not allocated you continue to deal with a null pointer
Similarly if a character array was not allocated you again continue to use a null pointer
And you forgot to set the data member
nextof the newly created node toNULL.So only this one function contains three bugs.
Pay attention to that the function
llinsertafterncan append a new node to the tail of the list. However the function does not change the pointerlinkedlist1tail. Consider for example a situation when the list already contains one node and the function is called with the parameternequal to1.