Consider following singly linked list implementation taken from some online reso
ID: 3801180 • Letter: C
Question
Consider following singly linked list implementation taken from some online resource. insert_node_pos(int pos,int val) adds a node with value as val at position pos in the singly linked list.
struct node
{
int value;
struct node *next;
};
typedef struct node snode;
//linked list operations
snode* create_node(int);
void insert_node_first(int value);
void insert_node_last(int value);
void insert_node_pos(int position, int value);
void delete_pos();
void search(int value);
void display();
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;
void insert_node_pos(int pos, int val)
{
int cnt = 0, i;
newnode = create_node(val);
ptr = first;
while (ptr != NULL) {
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf(" Inserted");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf(" ----INSERTED----");
}
else
{
printf("Position is out of range");
}
}
Understand the behavior of the method insert_node_pos. Write 3 unit tests for this method. Try to cover as much of code as possible.
Explanation / Answer
// this function takes a value that needs to be inserted and also a position where it needs to be inserted.
void insert_node_pos(int pos, int val)
{
int cnt = 0, i;
//Create node will create a new node in memory will the val value
newnode = create_node(val);
//Just a pointer initially pointing to first node in the linked list
ptr = first;
while (ptr != NULL) {
//Then we iterate till we reach linklist end, and every iteration we increment cnt
ptr = ptr->next;
cnt++;
}
// Here cnt will have the count of nodes in current link list
//Now we check where we need to insert the new node in current link list
if (pos == 1)
{
//insert in front
if (first == last && first == NULL)
{
//Scenario when linklist is Empty
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
//Else we store current first in temp pointer so we don't lose it
temp = first;
// Then we make the current first point to new created node
first = newnode;
//In the end we just link the new first to old first(temp)
first->next = temp;
}
printf(" Inserted");
}
else if (pos>1 && pos<=cnt)
{
//We are inserting in middle
ptr = first;
for (i = 1;i < pos;i++)
{
//So we iterate till we reach that position keeping track of both current and previous node
prev = ptr;
ptr = ptr->next;
}
//and then we link new node between previous and current
prev->next = newnode;
newnode->next = ptr;
printf(" ----INSERTED----");
}
else
{
// This case when we are trying to insert a node at a position which doesn't exist in linklist
printf("Position is out of range");
}
}
void main(){
//First create a demo list
insert_node_first( 5);
insert_node_last(10);
insert_node_last(15);
)
//Test case 1 insert in front
insert_node_pos(1, 3);
//TC 2: insert in mid
insert_node_pos(3,7);
//TC 3: insert in second last
insert_node_pos(12,5);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.