Linked List

Hi CEans,
There is my coding in C for insertion of a new node at desired location(using call by reference method). During compilation the program succeed, but its not showing any output. If you have any idea please correct me.
#include
#include
struct student
{
int data;
struct student *next;
};
void insert_pos(struct student **);
void main()
{
struct student *head;
head=create();
traverse(head);
insert_pos(&head);
traverse(head);
getch();
}
void insert_pos(struct student **head)
{
struct student *ptr, *loc=NULL,*n;
ptr=*head;
n=(struct student *)malloc(sizeof(struct student *));
printf("Enter value");
scanf("%d",&n->data);
n->next=NULL
printf("Enter value where u want to insert");
scanf("%d",&item);
while(ptr!=NULL)
{
if(item==ptr->data)
{
loc=ptr;
break;
}
ptr=ptr->next;
}
if(loc==NULL)
ptr=n;
else
{
n->next=ptr->next;
loc->next=n;
}
}
struct student *create()
{
struct student *head=NULL,*ptr;
char choice='y';
while(choice='='||choice=='Y')
{
if(head==NULL)
{
head=(struct student *)malloc(sizeof(struct student *));
ptr=head;
}
else
{
ptr->next=(struct student *)malloc(sizeof(struct student *));
ptr=ptr->next;
}
ptr->next=NULL;
printf("Enter ur data");
scanf("%d",&ptr->data);
printf("Do u want 2 create another node");
scanf("%c",&choice);
}
return(head);
}
void traverse(struct student *ptr)
{
while(ptr!NULL)
{
printf("%d",ptr->data);
ptr=ptr->next;
}
}

Replies

  • Manish Goyal
    Manish Goyal
    Your program is working perfectly in my pc now

    but i wanna know how can you say that iit compiles successfully there are so many errors in your program and the worst part all are basic

    Check your program thoroughly again

    For your easiness I will tell you one error

    Statement missing ;

    Try it if not able to solve it ,then i will help you 😀
  • Leo
    Leo
    Goayl, In fact i think this is the worst kind of programming practice i have ever seen. No comments, unnecessary heavy use of pointers, invalid use of void and return and last but not least no checking of allocated memory after using malloc function. The program is really very easy create, insert and transverse linked list and i don't think he may have needed any help if he had practiced programming a little bit.
  • 3011
    3011
    Ya there were some errors during typing, but it's not working.

    #include
    #include
    struct student
    {
    int data;
    struct student *next;
    };
    void insert_pos(struct student **);
    void main()
    {
    struct student *head;
    head=create();
    traverse(head);
    insert_pos(&head);
    traverse(head);
    getch();
    }
    void insert_pos(struct student **head)
    {
    struct student *ptr, *loc=NULL,*n;
    ptr=*head;
    n=(struct student *)malloc(sizeof(struct student *));
    printf("Enter value");
    scanf("%d",&n->data);
    n->next=NULL;
    printf("Enter value where u want to insert");
    scanf("%d",&item);
    while(ptr!=NULL)
    {
    if(item==ptr->data)
    {
    loc=ptr;
    break;
    }
    ptr=ptr->next;
    }
    if(loc==NULL)
    ptr=n;
    else
    {
    n->next=ptr->next;
    loc->next=n;
    }
    }
    struct student *create()
    {
    struct student *head=NULL,*ptr;
    char choice='y';
    while(choice=='y'||choice=='Y')
    {
    if(head==NULL)
    {
    head=(struct student *)malloc(sizeof(struct student *));
    ptr=head;
    }
    else
    {
    ptr->next=(struct student *)malloc(sizeof(struct student *));
    ptr=ptr->next;
    }
    ptr->next=NULL;
    printf("Enter ur data");
    scanf("%d",&ptr->data);
    printf("Do u want 2 create another node");
    scanf("%c",&choice);
    }
    return(head);
    }
    void traverse(struct student *ptr)
    {
    while(ptr!NULL)
    {
    printf("%d",ptr->data);
    ptr=ptr->next;
    }
    }
  • Manish Goyal
    Manish Goyal
    Still there is error
    you are using item variable but i wanna know where it is declared
    sorry to say but it seems that you have just copy paste the code from some place
    i am again saying check your program thoroughly again

You are reading an archived discussion.

Related Posts

i m in b.tech 3rd yr computer science branch. i wish to know when known it companies provide industrial training,i mean when the fornms are released and what is the...
brothers how to solve these problems https://projecteuler.net/index.php?section=problems&page=1because the range is wide .Is there anyway to solve these things I am able to solve two problems the first one and and...
I have a subject called "Object Oriented Methodologies". In this, I frequently come across these two terms: Generalization and Inheritance. But, the difference between the 2 is not clearly mentioned...
[FONT="] [/FONT] [FONT="]Ready for a job that lets you innovate and build cool new applications? Desire to win an ipad? [/FONT] [FONT="][/FONT] [FONT="] We are looking for research engineers who...
u all guys r genious........................ thanks 4 such a nice platform.................................................