I have written the code below for inserting an element at the position specified by the user.
I have seen the code in multiple books, and they have always used two pointers in the function that inserts the node at specified position. However, in my code I have used only single pointer.
My code works fine, but I want to know if there any pitfalls to this design.
#include<stdio.h>
#include<stdlib.h>
struct node{
unsigned int data1;
unsigned int data2;
struct node *ptr;
}obj;
void enterData() // Here the EnterDAta fnnction uses the obj object to enter the data and note that this
{ // obj is used agauin and again in the every node of the list to enter the data
printf("\n Enter the data1 ");
scanf("%u",&obj.data1);
printf("\n Enter the data2 ");
scanf("%u",&obj.data2);
}
void append(struct node **start) // This is used to append the dara un the list or also used to add the first element in the list
{
enterData();
struct node *next_node=*start;
if(next_node==NULL)
{
printf("\nAdding first element in the list ......\n");
next_node=malloc(sizeof(struct node));
if(next_node==NULL)
{
printf("\n Out of Memory");
}
else{
next_node->data1=obj.data1;
next_node->data2=obj.data2;
next_node->ptr=NULL;
*start=next_node; //This line of code here is modifying the header pointer see the magic of the poiter :)
}
printf("\n The first element added successfully");
}
else
{
printf("\n Appending the data ......\n");
struct node *temp=next_node;
next_node=malloc(sizeof(struct node));
if(next_node==NULL)
printf("\n Out of Memory");
else
{
next_node->data1=obj.data1;
next_node->data2=obj.data2;
next_node->ptr=NULL;
while(temp->ptr!=NULL)
temp=temp->ptr;
}
temp->ptr=next_node;
temp=NULL;
printf("\n Data appended Successfully!!! ");
}
next_node=NULL;
}
void display(struct node *start) //Used to display the list
{
if(start==NULL)
printf("\n The list is empty!!. Please create a list firsr Using option 1 !!! ");
else
{
printf("\n The linked list is as shown below ");
while(start!=NULL)
{
printf("[%d | %d] ",start->data1,start->data2);
start=start->ptr;
if(start!=NULL)
printf("->");
}
}
start=NULL;
}
void insertmid(struct node *start) // This is the function inserting the nodes at the specified position enetered by the user
{
struct node *q=start,*new_node;
int k=1,po,total=0,i=1,j=1;
printf("\n Enter the position where u want to enter the new node ");
scanf("%d",&po);
while(k<po-1)
{
q=q->ptr;
++k;
}
new_node=malloc(sizeof(struct node));
enterData();
printf("\n Adding the node at the specified position !!!!! ");
new_node->data1=obj.data1;
new_node->data2=obj.data2;
new_node->ptr=q->ptr;
q->ptr=new_node;
}
int main(void)
{
struct node *head=NULL;
append(&head);
append(&head);
append(&head);
append(&head);
append(&head);
display(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
insertmid(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
insertmid(head);
display(head);
return 0;
}