Linked List

3011

3011

@3011-Ivk5C8 Oct 26, 2024
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<stdio.h>
#include<conio.h>
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Oct 15, 2010

    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

    @leo-ZJQlmh Oct 15, 2010

    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

    @3011-Ivk5C8 Oct 15, 2010

    Ya there were some errors during typing, but it's not working.

    #include<stdio.h>
    #include<conio.h>
    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

    @manish-r2Hoep Oct 15, 2010

    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