954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

A linked list with a twist

By vegaseat on Dec 19th, 2004 12:20 am

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

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!

D_switch
Newbie Poster
15 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

ravishkkumar
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: