HELLO!

There is no error and it runs, but it doesn't show my desired output. What is the problem?

#include<stdio.h>
#include<stdlib.h>

struct list
{
       int data;
       struct list *ptr;
       };
       
int main()
{
    struct list *clist;
    struct list *top;
    struct list *tmpptr;
    int i;
    
    clist=(struct list *)malloc(sizeof(struct list *));
    top=clist;
    for(i=1;i<=5;i++)
    {
                    clist->data = i;
                    tmpptr=(struct list *)malloc(sizeof(struct list *));
                    clist->ptr=tmpptr;
                    clist=tmpptr;
                    }
                    for(clist=top;clist->ptr;clist=clist->ptr)
                    {
                    printf("%d\n", clist->data);
                    }
                    getchar();
                    
                  
}

The problem is we have no idea
1) what the output is supposed to be
2) what output you actually got.

I just looked at your other threads. You certainly like to make us find out what's going wrong rather than simply telling us. Looks like you need to read the
[boilerplate_help_info]

Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]To [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

Look back on all your first posts and note which points are you consistently missing.

Well, the supposed output is

1
2
3
4
5

And the output i got is random numbers that won't stop like

56890
56789
56789
56778
56789
.....

Sit at your desk with the program.
Follow the program line by line with pencil and paper.
Write down the results of each statement.
Fill in variable contents as they change.
Draw another node for each malloc() .
Write the output in a separate place (acting as your screen) for each printf() Be very careful and do EXACTLY what each statement says to do, NOT what you meant it to do.

I think this is my problem.

for(clist=top;clist->ptr;clist=clist->ptr)
                    
                    printf("%d\n", clist->data);

It's looping that is why it doesn't stop from printing the output, and because i didn't put a NULL. I know the rest are fine. Is there an alternative code for this?

I think this is my problem.

for(clist=top;clist->ptr;clist=clist->ptr)
                    
                    printf("%d\n", clist->data);

It's looping that is why it doesn't stop from printing the output, and because i didn't put a NULL. I know the rest are fine. Is there an alternative code for this?

You were doing well with the code you originally posted in the other thread. Or was that somebody else's code you posted? Anyway, your loop for displaying the contents looks fine. The conditional expression for your for() loop (the expression in the middle) is equivalent to clist->ptr != NULL , other than if there should be only one node per value, then you don't want to quit when clist->ptr is NULL, you want to quit -after- you print that node:

for (clist = top; clist != NULL; clist = clist->ptr)
    printf("%d\n", clist->data);

The problems are (a) yes, you forgot to NULL-out the ptr for the last node in the list, (b) you're still allocating the data size incorrectly (this was pointed out in your previous thread), and (c) you're not thinking about when to allocate. For the last point, since you need one linked-list node per value of interest, then there's no need to allocate a node outside of your for-loop: loop over i, allocate a node, assign the data, set the ptr to NULL, "hook it on the end of your list" (your use of the clist pointer pretty much does this, just clean up the order in which you do things).

And don't do the print-loop inside the create-linked-list loop, you're potentially corrupting your clist variable that way. Build the list in one loop, then after that, print it out in another loop.

Well, i also arranged and changed the codes from my original post..

#include<stdlib.h>
#include<stdio.h>

struct node
{
int value;
struct node *next;
};

int main()
{
       int i;
       struct node *ptr, *tail, *list;
       
       
       for(i=1;i<=5;i++)
       {
                        
                        ptr=(struct node*)malloc(sizeof(struct node));
                        ptr->value=i;
                        
                        if(i>1)
                        {
                        tail->next=ptr;
                        ptr->next=NULL;
                        tail=ptr;
                        }
                        else
                        {
                        list=ptr;
                        list->next=NULL;
                        tail=list;
                        }          
                                         
                        printf("%d\n",list->value);
                        }                     
                                                                    
                                   getchar();
                                   
                                   }

The problem is, i think, the if else statement because it doesn't follow the i>1. Hence, it keeps printing
1
1
1
1
1

Please explain why?

When is i ever <= 0?

I assume you didn't bother with my post....

And as far as printing "1" each time, what are you printing? Why would you expect it to print anything else? If you can't answer this, do what WaltP recommended, and draw your list, one node at a time, as you build it. Not what you -think- it's doing, but following your own code, one line at a time. Then it should be obvious what's wrong.

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.