Google Answers Logo
View Question
 
Q: C Programming Language ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: C Programming Language
Category: Computers > Programming
Asked by: teddy78-ga
List Price: $10.00
Posted: 04 Dec 2002 19:21 PST
Expires: 03 Jan 2003 19:21 PST
Question ID: 119509
I need help to write a C program that will read from a text file
(id.txt) which contains my full name and ID (which is Teddy Karthig
and w0117445)
My program should read my ID and generate a sorted dynamic linked
list(largest to smallest), where each node of the linked list would
contain a single integer

Request for Question Clarification by rbnn-ga on 04 Dec 2002 20:19 PST
In order to answer this question, it is necessary to specify the
format of id.txt. In addition, the contents of the linked list itself
does not appear to be specified: what are the "integers" it contains.

Normally in this sort of problem, id.txt would contain multiple names
and ids, and each node of the linked list would represent one entry in
the text file. I'm not sure where it is relevant that your id appears
in the file, but once you specify id.txt format perhaps this will be
more clear.

Clarification of Question by teddy78-ga on 05 Dec 2002 04:07 PST
Basically my program should take every single digits from my ID Number
(which is in my text fie)and produce a linkedlist by itself. Then  the
program should sort my IDNumber in to largest to smallest
Example: ID Number 0117445
Linked list

Clarification of Question by teddy78-ga on 05 Dec 2002 04:11 PST
Basically my program should take every single digits from my ID Number
(which is in my text fie)and produce a linked list by itself. Then 
the program should sort my IDNumber from largest to smallest
Example: ID Number   0117445
         Linked list 0->1->1->7->4->4->5
         Finallly the program should display theSorted Linked list as
follows
         7544110
Answer  
Subject: Re: C Programming Language
Answered By: maniac-ga on 05 Dec 2002 21:05 PST
Rated:4 out of 5 stars
 
Hello teddy78,

See the program listed at the end of the answer. With the following
input file (id.txt)
Teddy Karthig w0117445

Compiled the source file on a Unix system using
  cc -o id -g id.c
and run with
  ./id id.txt
to produce
head->0->1->1->7->4->4->5
sorted->7->5->4->4->1->1->0
which displays the list as entered and the new sorted list.

You may need to make changes to meet your specific needs - the
instructions are still a little unclear. There are a couple printf
calls commented out - enable them if you want to see the program run
as well. If you need help - request a clarification. Good luck.

--Maniac
id.c follows...
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct l;
struct l {
  struct l *next;
  char c;};

int main (int argc,
          char *argv[],
          char *envp[])
{
  FILE *idfile;
  const int maxstr = 20;
  char first[maxstr], last[maxstr];
  char id[maxstr];
  struct l *n = NULL;
  struct l *head = n;
  struct l *r, *s, *t;
  struct l *sorted = n;
  int i;

  idfile = fopen(argv[1], "r");
  if (idfile == NULL) {
    printf("%s: Failed to open input\n", argv[0]);
    perror(argv[0]);
    return 1;
  }

  if ((i=fscanf(idfile, "%s %s %s", first, last, id)) < 3) {
    printf("%s: Failed input at %d, quitting\n", argv[0], i);
    perror(argv[0]);
    return 1;
  }

  /* this code creates the list in order */

  s = n; /* no tail yet */
  for (i=0; i<strlen(id); i++) {
    if (isdigit(id[i])) {
      t = malloc (sizeof(struct l));
      t -> c = id[i];
      t -> next = n;
      if (head==n) {
        head = t;
      }
      else {
        s->next = t;
      }
      s = t;
      /* printf("%d %x %x %x %c\n", i, head, t, s, t->c); */
    }
  }

  /* print out in order to show done OK */
  printf("head");
  for (s=head; s!=n; s=s->next) {
    printf("->%c", s->c);
  }
  printf("\n");

  /* sort the list (insertion) */
  sorted = head;
  head = head->next;
  sorted->next = n; /* first item removed */
  for (s=head ; head!=n; s=head) {
    head = head->next; /* remove it */
    if (sorted->c<=s->c) { /* insert at head */
      s->next = sorted;
      sorted = s;
      /* printf("Insert %c at head before %c\n", sorted->c,
sorted->next->c); */
    }
    else { /* find proper spot */
      r = sorted;
      for (t=sorted->next; t!=n; t=t->next) {
        if (t->c<=s->c) { /* insert here */
          s->next = t;
          r->next = s;
          /* printf("Insert %c before %c\n", t->c, s->c); */
          break;
        }
        else
          r = r->next;
      }
    }
  }
  
  /* print the sorted output */
  printf("sorted");
  for (s=sorted; s!=n; s=s->next) {
    printf("->%c", s->c);
  }
  printf("\n");
  
  return 0;
}
teddy78-ga rated this answer:4 out of 5 stars
The answer given by the reasearcher named maniac-ga, was brilliant and
really helped me to successfully complete my programming task.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy