C++ insert and delete characters in a linked list
<p>My problem is that I'm trying to insert a letter into a linked list based on users input at a specific position given by the int h...However every time I run the program only the second character in the list changes regardless of the number the user puts into the program. </p>

<pre><code>Example: 
 ./h
 Name: koala
 a&lt;-l&lt;-a&lt;-o&lt;-k
 Change the position: 2
 To the character: 3
 a&lt;-l&lt;-3&lt;-o&lt;-k
 Insert the Character: F
 To the Postion: 3
 a&lt;-F&lt;-l&lt;-3&lt;-o&lt;-k
</code></pre>

<p>I want it to look like.</p>

<pre><code> ./h
 Name: koala
 a&lt;-l&lt;-a&lt;-o&lt;-k
 Change the position: 2
 To the character: 3
 a&lt;-l&lt;-3&lt;-o&lt;-k
 Insert the Character: F
 To the Postion: 3
 a&lt;-l&lt;-3&lt;-F&lt;-o&lt;-k
</code></pre>

<p>I know my problem is in the insert_char() function in my lists.cpp but just can't figure out what i'm doing wrong...</p>

<p>List.h</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
struct Node;
Node* new_list();
void insert_front(Node** plist,char x);
void insert_char(Node* plist, char x, int p);
void change_char(Node* plist, char x, int p);
void print_list(Node* list);
void delete_front(Node** plist);
void delete_list(Node** plist);
//void delete_char(Node* plist,int p);
struct Node {
  char x;
  Node *next;
};
</code></pre>

<p>main.cpp</p>

<pre><code>struct Node {
  char x;
  Node *next;
};

int main(){
  Node *list;
  list = new_list(); //new empty list
  cout&lt;&lt;"Name: ";
  string name;
  cin&gt;&gt; name;
  for (int i =0; i &lt; name.length(); i++)
  insert_front(&amp;list, name[i]);
  //---------print list-------------------------
  print_list(list);
  cout &lt;&lt;"Change the position: ";
  int z;
  cin&gt;&gt; z;
  cout&lt;&lt; "To the character: " ;
  char x;
  cin&gt;&gt; x;
  change_char(list, x, z);
  print_list(list);
  cout &lt;&lt;"Insert the Character: ";
  char y;
  cin&gt;&gt; y;
  cout&lt;&lt; "To the Postion: ";
  int h;
  cin&gt;&gt; h;
  insert_char(list, y, h);
  print_list(list);
  return 0;
}
</code></pre>

<p>lists.cpp</p>

<pre><code>Node* new_list()
{
  Node* list = 0; //in C++ it is better to use 0 than NULL
  return list;
}
void insert_front(Node** plist,char x){
  Node* t;
  t = new Node;
  t-&gt;x = x;
  t-&gt;next = *plist;
  *plist = t;
  return;
}
void change_char(Node* plist, char x, int p)
{
  Node* s = plist;
  for (int i=1; i&lt;p &amp;&amp; 0!=s;i++)
    s = s-&gt;next;
  if (0 != s)
    s-&gt;x = x;
  return;
}
void insert_char(Node* plist, char x, int p){
  Node* s = plist;
  Node* a = new Node();
  for (int i=1; i&lt;p &amp;&amp; s; i++)
    a-&gt;next=s-&gt;next;
    s-&gt;next=a;
  if (0 !=s)
    a-&gt;x=x;
  return;
}

//void delete_char(Node* plist,int p)
void print_list(Node* list){
  Node* p;
  p = list;
  if(p == 0)
    cout &lt;&lt; "--- empty list ---" &lt;&lt; endl;
  while(p !=0){
    cout &lt;&lt; p-&gt;x&lt;&lt;"&lt;-";
    p = p-&gt;next;
  }
  cout &lt;&lt; endl;
}
void delete_front(Node** plist){
  Node* t;
  if( *plist != 0){   // list is not empty
    t = (*plist);
    *plist = (*plist)-&gt;next;
    delete t;
  }
  }
void delete_list(Node** plist){
  while(*plist != 0) //while list not empty
    delete_front(plist);
}
bool is_empty(Node* list){
  return (list == 0);
}
</code></pre>
