Can someone help me create a function in a linked list program that replaces an element in (I) place with an element a user enters?
here is the sudo code I tried to implement but Im kinda lost.
this is in my header file -> typedef int el_t; // el_t is an alias for int
this is in my implementation file:
void replace(el_t Elem, int I)
{
go to the Ith node (I is between 1 and Count) and replace the element there with Elem. If I was an illegal value, call ListError.
}
I think that my deleteIth function can be used but im having trouble:
here is my deleteIth function:
// PURPOSE: to delete a node at the Ith place
// PARAMS: OewNum of type el_t set by refrence
// I sent by value
// ALGORITHM: checks if I is grater than count and less than 1
// Else, it checks two special cases.
// the first is when count is equal to 1. The last
// case takes care of all other cases greater than count
void llist::deleteIth(int I, el_t& OldNum)
{
if ( I < 1 || I > count) //checks if I is greater than count or less than 1.
{
QueueError("List is Empty, Exiting Program."); //empty message
exit(1);
}
else if(count==1) //case where count is 1
{
deleteFront(OldNum); //calls deleteFront function.
}
else if(I==1)
{
deleteFront(OldNum);
}
else //takes care of the rest of the cases
{
Node *P; //new pointer P
Node *Q; //new pointer Q
P=Front; //P points to Front
Q=Front;
//foor loop to deletes an element on I-1 place
for(int i=1; i < (I-1); i++)
{
P = P->Next; //P set equal to P's Next
}
Q=P->Next; //Q is set equal to P's Next
OldNum=Q->Elem;
P->Next= Q->Next;
delete Q;
count--;
}
}