Hello everyone,

This is my first post, I'm having trouble on an assignment. I'm writing a function for a database program in class... I have to write the function that will tranverse a linked list, add a "node" and then write the user entered info into the "node".

The first printf is there just to prove to me that the next_id() function is working correctly, which it is, however when I get to the end the number that prints in the last printf statement is the first number in the linked list... This means I'm ovrwritting the first keyfile in the linked list...

This is the type definition that the proffesor gave us, and everything compiles, just doesn't function correct, I hope its something small and I appreciate any help!

Thanks,
Doug

/*   This is the type definition file for the data base management
system code.  Include it in source files that need it and contact
the project manager (Sean) if you want any definitions added to
the file.  */
#define null (KEYS)0
#define chnull (char *)0
#define L 19
#define MAXLINES 200
#define CHARS 132
  struct DATE {
      int month,day,year;
  };
  typedef struct DATE Date;

  typedef struct Key* KEYS;

  struct Key {
      char *sender;
      char *addressee;
      char *regarding;
      Date date;
      int id;
      char *fname;
      KEYS next;
  };
  typedef struct Key KEY;
#include <stdio.h>
#include "keytype.h"

void add(KEYS k)
{
int next_id(KEYS list);

int  nextID;
KEY* curr;

 curr = k;               /* set curr to head node */

 nextID = next_id(k);

 printf("%d\n", nextID);

 if( curr->next )                 /* loop through linked list to find last node */
      curr = curr->next;       /* bump pointer to next node */
 else {
      curr->next = (KEY*)malloc(sizeof(KEY)); /* allocate space for next node */
      curr = curr->next;       /* bump pointer to next node */
      curr->id = nextID;       /* put int from next_id() into curr->id */
      curr->next = NULL;      /* terminate linked list */
 }

 printf("Enter originators name:\n");
 scanf("%s", &curr->sender);

 printf("Enter addressee name:\n");
 scanf("%s", &curr->addressee);

 printf("Enter Subject:\n");
 scanf("%s\n", &curr->regarding);

 printf("Enter the date (mm dd yy):\n");
 scanf("%d %d &d\n", &curr->date.month, &curr->date.day, &curr->date.year);

 printf("Enter letter file name:\n");
 scanf("%s\n", &curr->fname);

 printf("The new letter id # is %d", curr->id);

}

Recommended Answers

All 4 Replies

You have a comment /* loop through linked list to find last node */ but there is no loop. Only a single IF statement.

I tired it with a while loop and got the same result.

#include <stdio.h>
#include "keytype.h"

void add(KEYS k)
{
int next_id(KEYS list);

int  nextID;
KEY* curr;

 curr = k;             /* set curr to head node */

 nextID = next_id(k);

 printf("%d\n", nextID);

 while(curr->next) {          /* loop through linked list to find last node */
      curr = curr->next;      /* bump pointer to next node */
 }

 curr->next = (KEY*)malloc(sizeof(KEY)); /* allocate space for next node */
 curr = curr->next;           /* bump pointer to next node */
 curr->id = nextID;           /* put int from next_id() into curr->id */
 curr->next = NULL;          /* terminate linked list */

 printf("Enter originators name:\n");
 scanf("%s", &curr->sender);
 
 printf("Enter addressee name:\n");
 scanf("%s", &curr->addressee);
 
 printf("Enter Subject:\n");
 scanf("%s\n", &curr->regarding);
 
 printf("Enter the date (mm dd yy):\n");
 scanf("%d %d &d\n", &curr->date.month, &curr->date.day, &curr->date.year);
 
 printf("Enter letter file name:\n");
 scanf("%s\n", &curr->fname);
 
 printf("The new letter id # is %d", curr->id);

}

Then it looks like your nodes are broken. Are you sure your malloc worked? You never tested it's return

Maybe you should print curr and curr->next as you look through the list.

And after you've filled the node, print the values so you know all of them are filled properly.

The following code fixed the problem but then i had to malloc space for each member of the structure and some other things...

curr = k->next;

I was setting the curr variable to the head of the list instead of the head next variable....

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.