- Write a function called InsertList() which given a list, a position and a valu
ID: 3583893 • Letter: #
Question
- Write a function called InsertList() which given a list, a position and a value creates a node to store the given value and inserts this node to the given position.
- Write a function called DeleteList() which given a value finds the node in the list with that value and deletes that node from the list.
- Write a function called GetElementAtPosition() which given a list and a position value, finds the node at that position and returns the value stored in the node in that position.
- Write a function called GetPositionOfElement() which given a list and a value, finds the position of the node that stores that given value.
Explanation / Answer
Below are the rquired 4 functions. Post in comments section if require any help/explanations.
void InsertList(struct ListRecord *l, int pos, int val)
{
int ctr=0;
struct Node *p;
p = l->head;
int size=ListSize(struct ListRecord * l);
if (pos > size)
{
printf("exceeds boundary");
}
else
{
while (ctr != pos)
{
p = p->next;
ctr++;
}
struct Node *q;
q->value=val;
struct Node *temp=p->next;
p->next=q;
q->next=temp;
}
}
void DeleteList(struct ListRecord *l,int val)
{
struct Node *p;
p = l->head;
while (p->next != NULL)
{
q=p->next;
if(val==q->value)
{
p->next=q->next;
break;
}
else
{
p = p->next;
}
}
}
int GetElementAtPosition(struct ListRecord *l,int pos)
{
int ctr=0;
struct Node *p;
p = l->head;
int size=ListSize(struct ListRecord * l);
if (pos > size)
{
printf("exceeds boundary");
}
else
{
while (ctr != pos)
{
p = p->next;
ctr++;
}
int valu=p->value;
return valu;
}
int GetPositionOfElement(struct ListRecord *l,int val)
{
struct Node *p;
p = l->head;
int ctr=0;
while (p != NULL)
{
if(val==p->value)
{
return ctr;
break;
}
else
{
p = p->next;
ctr++;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.