patterncMinor
Linked list insertion
Viewed 0 times
listinsertionlinked
Problem
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
#include
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 elem
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
#include
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 elem
Solution
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 listI don't know what you see, but I see a 304-character line with loads of spaces.
This is not good.
Here's something a little more approachable:
// This is used to append the dara un the list or
// also used to add the first element in the list
void append(struct node **start)Further, what is "dara un"? A typo for "data in"?
Code Snippets
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// This is used to append the dara un the list or
// also used to add the first element in the list
void append(struct node **start)Context
StackExchange Code Review Q#94765, answer score: 4
Revisions (0)
No revisions yet.