Add after a node

One of the nice things about a linked list is that it's pretty easy to insert new nodes anywhere you'd like. If there's a new value you've read in, you might not necessarily want to add it to the front of the list or to the back. You might, for example, want to insert it in a list in a way that maintains sorted order.

Let's consider a function addafter(val, P) that will insert a node containing value val after p so that sorted order is maintained. Here's what we need to do in pictures:

We want to add a node with data after p. We are using the following struct definition:

struct Node
{
  double data;
  Node* next;
}; 
Create the new node we want, with the proper data and next values (drag your mouse to see the code below).
Node *T = new Node;
T->data = val;
T->next = p->next;
Splice our new node in after the node p points to (drag your mouse to see the code below).
p->next = T;
Done!
In summary, we can write the function as follows:

void addafter(double val, Node* p)  // When you call this function, make sure p is not NULL
{
  if( p == NULL )
  {
    cerr << "error!" << endl;
    exit(1);
  }
  Node *T = new Node;
  T->data = val;
  T->next = p->next;
  p->next = T                          // if p is NULL, p->next will make your program will crash!!!
}

Adding to the back of a list

Adding to the front of our bare-bones linked lists is very natural. You have two basic cases, which really need to be handled separately:
  1. the original list L is empty
    In this case, just use add2front().
  2. the original list L is non-empty
    In this case, you will have to traverse the list in order to get to the last node, and then apply addafter() on this last node.
Here's a nice little implementation of an add2back function that works in just this way.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

struct Node
{
  string data;
  Node *next;
};

void add2front(string d, Node*& L);
void addafter(string d, Node* p);
void add2back(string d, Node*& L);

int main()
{
  Node* L = NULL;
  string s;
  while(cin >> s && s != ";")
    add2back(s,L);
  
  for(Node* p = L; p != NULL; p = p->next)
    cout << p->data << ' ';
  cout << endl;

  return 0;
}



void add2front(string d, Node*& L)
{
  Node* T = new Node; 
  T->data = d; 
  T->next = L;
  L = T;
}

void addafter(string d, Node* p)
{
  if( p == NULL )
  {
    cerr << "error!" << endl;
    exit(1);
  }

  Node *T = new Node;
  T->data = d;
  T->next = p->next;
  p->next = T;
}

void add2back(string d, Node*& L)
{
  if (L == NULL)
  {
    add2front(d, L);
  }
  else
  { 
    // find the last node
    Node* last = L; 
    while(last->next != NULL)
      last = last->next;

    // insert d after the last node
    addafter(d, last); 
  }
}


Add in order

Suppose we want to maintain a sorted list. Using add2front and addafter, we can easily write a function addinorder(val,L) that will insert a new value val into L so that sorted order is maintained. There are two cases to consider when we write the function:

Case 1?

When is case 1 true? We have two sub-cases:
In conclusion, the following code handles case 1:

if( L == NULL || val < L->data )
  add2front(val, L);

Case 2?

In this case, we just need to traverse the list to find the right node and then apply the function addafter().
Traverse the list until you find the right node. Let p point to the currently-visited node.

   for(Node* p = L; p != NULL; p = p->next)
   {
      if( p->next == NULL || val < p->next->data )
      {
        addafter(data, p);
        break;
      }
   }

Final code


void addinorder(double val, Node*& L)
{
  if( L == NULL || val < L->data )
  {
    add2front(val, L);
  }
  else
  {
    for(Node* p = L; p != NULL; p = p->next)
    {
      if( p->next == NULL || val < p->next->data )
      {
        addafter(val, p); 
        break;
      }
    }
  }
}

Sorting

With our addinorder we can sort stuff pretty easily with linked lists. For example, if you want to read in a bunch of values and put them in sorted order, you can simply put them in a list as you read them using addinorder, and when you're done, you'll have a sorted list of values. Not bad, eh?

Suppose that you already have a list and it's not in sorted order. You could easily create a sorted copy of the list by moving through you list a node at a time and calling addinorder for that node's data.


Node* sortedcopy(Node* L)
{
  Node* S = NULL;   // a new empty list S

  for(Node* p = L; p != NULL; p = p->next)
    addinorder(p->data,S);    // insert each data to the new list S using addinorder()

  return S;         // return the list S
}
This function returns a copy of the original list in sorted order.

Problems

  1. Write a program that reads in commands from the user, which are quit, add x, and print. Each time the add x command is given, the actual number (as opposed to x) is stored. Each time the print command is given, all the numbers added thus far are printed out in sorted order. I'll leave it to you to guess what the quit command does!
  2. Modify the addinorder function from above so that duplicate values are not inserted, but rather simply ignored. Thus, if used on the example code in which words are entered, only unique words would appear — no duplicates.
  3. Write a program that reads in a collection of words (comma-separated, semicolon-terminated) and then prints out each word (order not important) along with the number of times that word was entered. Here's a sample run:
    ~/$ ./prog
    hi , bye , yo , ciao , hi , hallo , yo , sianara ;
    sinara 1
    hallo 1
    ciao 1
    yo 2
    bye 1
    hi 2