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--;
}
}

I'm just creating the simple independent function to show how to it.
Since don't have seen the full program... and don't know what you have declare...etc..

Replace the element at the "n"th position of the link list..
Considering .. node as

typedef struct node *Nodeptr;
struct node
{
	int info;
	node *link;
};
Nodeptr front, rear;

now assuming that the link list already exits

void replace(int position, int info)
{
 Nodeptr traverse=front;
 for(int i=1; i<position; i++) 	traverse=traverse->link;
 traverse->info=info;
}

Hi FAZ3,

What exactly you want ? why are you using delete in your algorithm?
Do you want it to be at particular position as has been done by Rhohitman
or is your list like this

struct node 
{
  int i;
  char a;
  node *next;

};

and you want the character a at node i to be changed?
Also do you need to replace it or insert a new element after the desired one.

First please read the rules and add code tags. No one will take the effort to read the code without those.

Now here in your delete function

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

your Q points to the front. At the end of the for loop P points to the Ith element
when you do Q = P->next ... your front is now P->next and you have bypassed ALL the elements from the head to your Ith location .. and deleted none of them. Now you have a bunch of memory leaks, and you have probably bypassed more elements than what you wanted to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.