Question:
this is a program of how to create a linked list and print the list in C language.?
perky_ashish
2011-12-24 23:34:21 UTC
when i run this program i didn't get the correct result and i am not able to find what is wrong with the program....pllllllzzzzzz help me out with ths program...what is wrong with the code?
#include
#include
#include
#define NULL 0
void print();
typedef struct list
{
int num;
struct list *next;
}l1;
l1 *head=NULL,*temp,*node;
void main()
{
char ch;
clrscr();
do
{
node=(l1 *)malloc(sizeof(l1));
scanf("%d",&node->num);
if(head==NULL)
{
head=node;
temp=node;
}
else
{
node->next=node;
temp=node;
}
printf("do you want to continue:");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
print();
getch();
}
void print()
{
temp=head;
while(temp!=NULL)
{
printf("%d ",temp->num);
temp=temp->next;
}
}
Three answers:
anonymous
2011-12-25 11:04:47 UTC
There is a lot wrong with it.



- NULL is defined in stdio and stdlib. Redefining it in your program is pointless and stupid.



- main's return type is required to be int. Get a copy of one of the standards. C89's the earliest, C99 has been the major standard for roughly 11 years, and C11 has just recently been published. All of them require you to define main with a return type of int.



- Avoid the use of conio.h, since it's incredibly old and unportable. Your program aims to build a linked list and then print it, you can accomplish that perfectly using only the standard libraries. Using conio.h does more harm than good.



- fflush requires its argument to point to an *OUTPUT* stream. You're passing a pointer to an input stream to it, which results in undefined behaviour.



Your use of scanf is incorrect; it has a return value and you should check it to ensure that scanf successfully read an element of data and that the stream hasn't reached EOF. I suggest you use a combination of fgets and sscanf or strtol instead. Below is an example of how you might implement a linked list, using scanf (in a somewhat limited way) for input.



#include

#include



struct node {

        int datum;

        struct node *next;

};



static void freelist(struct node *head)

{

        while (head) {

                struct node *tmp = head->next;



                free(head);

                head = tmp;

        }

}



static struct node *readlist(void)

{

        struct node head;

        struct node *np = &head;

        int num;



        while (scanf("%d", &num) == 1) {

                np->next = malloc(sizeof *np->next);

                if (!np->next) {

                        freelist(head.next);

                        fprintf(stderr, "Unable to allocate memory\n");

                        exit(EXIT_FAILURE);

                }

                np = np->next;

                np->datum = num;

        }

        np->next = 0;

        return head.next;

}



static void printlist(struct node *head)

{

        while (head) {

                printf("%d -> ", head->datum);

                head = head->next;

        }

        printf("(null)\n");

}



int main(void)

{

        struct node *head;



        head = readlist();

        printlist(head);

        freelist(head);

        return 0;

}



Given the input: 10 20 30 40<\n>

It will output: 10 -> 20 -> 30 -> 40 -> (null)
anonymous
2011-12-24 23:50:46 UTC
You are not inserting nodes into the list. You check to see if there is a head to the list, and if not, create the head, but that’s all you do with the head node. In the else clause you simply point the newly allocated node’s next point to itself (node->next = node). You should insert the new node into the list:



node->next = head->next;

head->next = node;



That will insert the new node at the top of the list. If you wish to insert it at the bottom of the list, iterate though the list until node->next == null.
hare
2016-11-14 16:12:01 UTC
Why do not you basically upload a non everlasting variable? ok, basically thinknig approximately it, this works: (this might all be pseudo code with the aid of fact i won't be able to keep in mind C) Variables would be talked approximately as var1 and var2. var1 = var1 + var2 var 2 = var1 - var2 var1 = var2 - var1 each and each step being finished after the previous one. desire it facilitates.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...