Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:02:37+00:00 2026-05-23T01:02:37+00:00

I’m having some trouble with this program. I think I have it almost right,

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:02:37+00:00Added an answer on May 23, 2026 at 1:02 am

    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 and prev 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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.