A linked list with a twist

Updated vegaseat 1 Tallied Votes 467 Views Share

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.

Note:
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!"); 
      break; 
    }
    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
    fflush(stdin);
    // store data and update top of list		
    top = sort_store(person,top);
  }
  // display the sorted list from the top
  display(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;
    }
    else 
    {
      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;
  }
	printf("\n");
}
D_switch 0 Newbie Poster

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!

Dave Sinkula 2,398 long time no c Team Colleague

This is a no-no:

fflush(stdin);

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

scanf("%s",person->name);

http://www.daniweb.com/tutorials/tutorial45806.html

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

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.

tux4life 2,072 Postaholic

>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: http://www.daniweb.com/forums/post956935.html#post956935

ravishkkumar 0 Newbie Poster

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

Shami80 0 Newbie Poster

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() "

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

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).

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.