C Programming-Enter 3 Characters and print in reverse order
I'm having some trouble with this program. I think I have it almost right, besides the fact 开发者_JAVA百科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:
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));
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:
while we need more characters
prompt user for another character
store character in datastructure
print datastructure in reverse
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
Write a program that creates a linked list of 10 characters, then creates a copy of the list in reverse order. The user should be prompted to input the characters and the program should have a print function that prints out the original list and then prints out the list in reverse order
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:
void print_output(RECORD *r) { if this is the last RECORD in the chain print the data else print_output(next record in the chain) }
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
next
andprev
pointers, 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. :)
In your RECORD* print(RECORD *it, int j)
function, you tell printf that you want to print a character but you are passing a pointer to the first element in your fname
array, which is a memory address.
Use either:
printf("%s \n", cur->fname); /* print the string */
or
printf("%c \n", *cur->fname); /* print the first character */
I'm not sure what you want since your question is extremely vague.
精彩评论