I have been practicing DS using C and feel sorry for my unreadable code.
i tried to implement linked list using C and i am beginner to learn data structures
Please help me why this is running slow.
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node* link;
}NODE;
int main(void){
NODE* A;
A = NULL;
NODE* temp = (NODE*)malloc(sizeof(NODE));
temp->data = 2;
temp->link = NULL;
A = temp;
temp = (NODE*)malloc(sizeof(NODE));
temp->data = 3;
temp->link = NULL;
A->link = temp;
temp = (NODE*)malloc(sizeof(NODE));
temp->data = 5;
temp->link = NULL;
NODE* temp1 = A;
while(temp1->link != NULL){
printf("%d\n",temp1->data);
temp1 = temp1->link;
printf("%d\n",temp1->data);
}
temp1->link = temp;
temp1 = temp1->link;
printf("%d\n",temp1->data);
temp1 = A;
printf("%d\n",temp1->data);
printf("after inserting 100 in to the second\n");
int count = 1;
NODE* new = (NODE*)malloc(sizeof(NODE));
new->data = 100;
new->link = NULL;
NODE* before = (NODE*)malloc(sizeof(NODE));
NODE* after = (NODE*)malloc(sizeof(NODE));
while(temp1->link != NULL){
if(count == 2){
new->link = temp1;
break;
}
if(count == 1){
before = temp1->link;
temp1->link = new;
temp1 = before;
count++;
continue;
}
temp1 = temp1->link;
count++;
}
temp1 = A;
while(temp1->link != NULL){
printf("%d\n",temp1->data);
temp1 = temp1->link;
}
printf("%d\n",temp1->data);
}