I’m having some trouble with this program. I think I have it almost right, besides the fact that it prints garbage to the screen 🙁
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strsize 30
typedef struct member
{
int number;
char fname[strsize];
struct member *next;
} RECORD;
RECORD* insert (RECORD *it);
RECORD* print(RECORD *it, int j);
int main (void)
{
int i, result;
RECORD *head, *p;
head=NULL;
printf("Enter the number of characters: ");
scanf("%d", &result);
for (i=1; i<=result; i++)
head=insert (head);
print (head, result);
return 0;
}
RECORD* insert (RECORD *it)
{
RECORD *cur, *q;
int num;
char junk;
char first[strsize];
printf("Enter a character:");
scanf("%c", &first);
cur=(RECORD *) malloc(sizeof(RECORD));
strcpy(cur->fname, first);
cur->next=NULL;
if (it==NULL)
it=cur;
else
{
q=it;
while (q->next!=NULL)
q=q->next;
q->next=cur;
}
return (it);
}
RECORD* print(RECORD *it, int j)
{
RECORD *cur;
cur=it;
int i;
for(i=1;i<=j;i++)
{
printf("%c \n", cur->fname);
cur=cur->next;
}
return;
}
Take a quick step back; I’d like to suggest some general programming guidelines based on what I see from your code:
An
insert()routine on records suitable for complex datastructures is not usually expected / allowed / desired to perform user interaction; you are mingling user interface with internal business logic. (While business logic is a high-falutin phrase, I don’t know a better way to say “the things your program has to do in order to justify its existence” or “the essential requirements the program must satisfy”. Replacement suggestions welcomed. 🙂Consider this pseudo-code as a replacement algorithm:
Separate all the code for the datastructure from the interaction with the human. (This separation of presentation from logic is often formalized as
Model View Controller, but it is important to recognize that it is not limited to user interfaces — you want your stack, list, or queue to be useful in your next programming project, so build generic routines that only operate on the stack, list, or queue, and you can reuse them in the next project.)
Update
Now this is more like it. While I appreciate what your teacher is trying to do, a linked list isn’t the datastructure I’d select for this problem. (I would pick an array if the problem size was bounded, a stack if the problem size was unbounded.) It’s solvable with a linked list, and there are three possible approaches that come to mind immediately:
Write a recursive output function that works like this:
This uses the call stack to reverse the output. Clever trick, but sometimes wastes memory compared to other approaches.
Write your lists with doubly-linked list elements. Use both
nextandprevpointers, and manage them all carefully to allow you to traverse the list in either direction. This requires subtle coding and carefully thinking things through. Or copying the correct order of operations from a published source such as Knuth or your favorite algorithms text. 🙂Actually reverse your singly-linked list. Also requires subtle, careful coding or thoughtful copying. 🙂