This is an example of a singly linked list allowing you to enter a number of names and associated ages. The twist is that the names are inserted into the list in ascending order. When the list is displayed, it is already sorted by name.

This is not an exercise in safe data input. While precautions have been taken, any fool can stress the language easily.

// a list of data in memory where each data item contains the
// address of the next data item is called a singly linked list
// in this case we are creating a sorted singly linked list
// tested with Pelles C     vegaseat    18dec2004

#include <stdio.h>
#include <stdlib.h>   // malloc()
#include <string.h>   // strcmp()

struct list {
  char name[31];
  int  age;
  struct list *next;  // pointer to next data item in list
  } prsn;

struct list *top;     // establish start of list

// proto types
struct list *sort_store(struct list *new, struct list *top);
void display(struct list *start);

int main()
  struct list *person, *sort_store();

  printf("\nEnter a number of names and ages (q to exit loop)\n");
  for (;;) 
     person = (struct list *) malloc(sizeof(prsn));
    if (!person) 
      puts("\nOut of memory!"); 
    printf("\nEnter name : ");
    // use fgets() to allow for names with spaces
    fgets( person->name, sizeof(person->name), stdin);
    // remove frigging fgets() trailing '\n'
    if (person->name[strlen(person->name)-1] == '\n')
        person->name[strlen(person->name)-1] = '\0';     
    // mimic an AND situation		
    if (strlen(person->name) == 1)
      if (person->name[0] == 'q') break;
    printf("Enter age  : ");
    int e;
    e = scanf("%d",&person->age);
    // trap non-numeric entry
    if (e == 0)
      person->age = 0;
    // flush the input stream in case of bad input
    // store data and update top of list		
    top = sort_store(person,top);
  // display the sorted list from the top
  getchar();  // wait
  return 0;

// insert new data to the list in sorted order
struct list *sort_store(struct list *new, struct list *top)
  static struct list *last = NULL;
  struct list *old, *start;

  start = top;
  if (!last) 
    new->next = NULL;
    last      = new;
    return (new);
  old = NULL;
  while (top) 
    // sort by name in ascending order
    if (strcmp(top->name, new->name) < 0) 
      old = top;
      top = top->next;
      if (old) 
        old->next = new;
        new->next = top;
        return (start);
      new->next = top; 
      return (new);
  last->next = new;
  new->next  = NULL;
  last       = new;
  return (start);

// walk through the linked list and display the content
void display(struct list *start)
  while (start) 
    printf("\nName = %-30s  Age = %2d", start->name, start->age);
    start = start->next;
About the Author
Member Avatar vegaseat DaniWeb's Hypocrite


it has 5 errors when i tried it on BOrland C++

Note: It is impossible to write C code for every silly compiler there is, but you could at least list the errors you encountered!

This is a no-no:


And scanf with %s could be done better or avoided.


use fgets() instead of scanf() because it avoids buffer overflow problems if you type in more characters than name will hold. On the flip side of the coin you will have to remove the '\n' that fgets() will probably tack on to the end of the string. fgets( person->name, sizeof(person->name), stdin); person->name[strlen(person->name] = 0; Another reason to use fgets() is so that you can enter names that contain spaces. scanf() will not allow you to do that.

>use fgets() instead of scanf() because it avoids buffer overflow problems if you type in more characters than name will hold.
I'm not going to repeat what Tom Gunn said once, I'm just going to link you:

it has four problems while compiling on turbo c compiler..
please specify on which u have implemented this code??????

I got 1 problem with this line down here

top = sort_store(person,top);

as I tried to run this program, the error message as following "...extra parameter in call to sort_store() "

Recheck the function prototype at the top of the program to make sure it has two parameters. If you still can't find the problem then post the code you wrote. Please start a new thread for your question(s).