i've written a code for insertion in a linked list for all the 3 cases that is ...at the end,in the beginning & in between.....when i run this code then it runs successfully for insertion at end and in the beginning ,it is also showing no errors while compiling the program but when i run this code for insertion in between then it prints like this:
enter the size of list:5
enter list:
10
20
30
40
50
Enter 1.to insert at end
Enter 2.to insert in the beginning
Enter 3.to insert in the between
//so, when i enter here choice number 3.to insert in between
3.
enter the location after which the number has to be inserted:3
enter number to be inserted:100
then it doesn't print anything and comes back to the program...that is the screen where the source program was written.why?it's not printing anything.plz make necesaary changes in my code to make it properly run....plz help me out with my code...

#include<stdio.h>
#include<conio.h>
struct link
{
int data;
struct link*ptr;
};
typedef struct link node;
void main()
{
int size,i,choice;
char ch;
node*start,*temp,*p;
clrscr();
do
{
printf("enter the size of list:");
scanf("%d",&size);
printf("enter list\n");
start=NULL;
for(i=0;i<size;i++)
{
if(start==NULL)
{
start=(node*)malloc(sizeof(node));
scanf("%d",&start->data);
start->ptr=NULL;
}
else
{
for(p=start;p->ptr!=NULL;p=p->ptr);
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->ptr=NULL;
p->ptr=temp;
}
}
printf("Enter 1.To insert at the end\n");
printf("Enter 2.To insert at the beginning\n");
printf("Enter 3.To insert in between\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
end(start,p);
break;
case 2:
beg(start,p);
break;
case 3:
between(start,p,size);
break;
default:
printf("wrong number entered\n");
}
printf("Enter y to continue\n");
scanf("%c",&ch);
}
while(ch=='Y'||ch=='y');
getch();
}
end(node*start,node*p)
{
node*temp;
for(p=start;p->ptr!=NULL;p=p->ptr)
temp=(node*)malloc(sizeof(node));
printf("enter the number to be inserted\n");
scanf("%d",&temp->data);
temp->ptr=NULL;
p->ptr=temp;
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
return(0);
}
beg(node*start,node*p)
{
node*temp;
temp=(node*)malloc(sizeof(node));
printf("enter the number to be inserted\n");
scanf("%d",&temp->data);
temp->ptr=start;
start=temp;
printf("new linked list is:\n");
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
return(0);
}
between(node*start,node*p,int size)
{
node*temp;
int loc,count=0;
printf("enter the location after which the number has to be inserted\n");
scanf("%d",&loc);
if(loc<=size)
{
temp=(node*)malloc(sizeof(node));
printf("enter the number to be inserted\n");
scanf("%d",&temp->data);
for(p=start;p!=NULL;p=p->ptr)
{
count++;
if(count==loc)
{
temp->ptr=p->ptr;
p->ptr=temp;
printf("new linked list is:");
for(p=start;p!=NULL;p=p->ptr)
printf("%d",p->data);
return(0);
}
else
{
printf("location is not found\n");
return(0);
}
}
}
return(0);
}

It's been at least three months since I last looked at one of your posts, and you've learned nothing. Maybe if you actually tried to write good code, it would be easier to find your many mistakes.

>plz make necesaary changes in my code to make it properly run
This is incredibly rude. You're assuming that we care about you and your program and that you're entitled to a complete rewrite by us. This sentence alone is why I refuse to help you.

i am sorry....but i didn't mean to be rude at all....i said please if u could make some necessary changes that r reuired to make my program run successfuly because i know that you guys can only help me out here.....so i request you again to help me out...coz i have tried everything but i can't think of anything so that it can run properly...i have written the entire code but just can't able to run it properly.....so please help me out ....

plz reply.....to my code.....its not running still..there is some mistake in the betwen section iam sure...please help me out with my code...

Hello my friend.
This is exactly what you need. but it is written in C++

#include<iostream>
#include<conio.h>
#include<cassert>
using namespace std;

struct nodeType
{
       int info;
       nodeType *link;
};      
                         
int main()
{
    int choice, num;
    nodeType *first, *last, *newNode, *current;
         cout<<"Enter numbers and press -999 to quit-->";
         cin>>num;
         first = 0; last = 0; current = 0; newNode = 0;
         while(num!=-999)
         {
                         newNode=new nodeType;
                         assert(newNode!=NULL);
                         newNode->info=num;
                         newNode->link=NULL;
                         if(first==NULL)
                         {
                                        first=newNode;
                                        last=newNode;
                         }
                         else
                         {
                             last->link=newNode;
                             last=newNode;
                         }
                         cin>>num;
         }
                         
    cout<<"Enter 1.To insert at the end\n";
    cout<<"Enter 2.To insert at the beginning\n";
    cout<<"Enter 3.To insert in between\n";
    cin>>choice;
    if(choice==1)
    {
          cout<<"Enter number to insert at the end-->";
          cin>>num;
          newNode=new nodeType;
          assert(newNode!=NULL);
          newNode->info=num;
          newNode->link=NULL;
          if(first==NULL)
          {
                         first=newNode;
                         last=newNode;
          }
          else
          {
                         last->link=newNode;
                         last=newNode;
          }
    }
    
    else if(choice==2)
    {
         cout<<"Enter number to insert first-->";
          cin>>num;
          newNode=new nodeType;
          assert(newNode!=NULL);
          newNode->info=num;
          newNode->link=first;
          first=newNode;
    }
    else if(choice==3)
    {
          int loc, count=1;
          current=first;
          cout<<"Enter the location after which number has to be inserted-->";
          cin>>loc;
          cout<<"Enter the number-->";
          cin>>num;
          newNode=new nodeType;
          assert(newNode!=NULL);
          newNode->info=num;
          newNode->link=NULL;
          while(current!=NULL && count<loc)
          {
               current=current->link;
               count++;
          }
               if(count==loc)
               {
                             nodeType *temp;
                             temp=current->link;
                             current->link=newNode;
                             newNode->link=temp;
               }
    }
    else
    cerr<<"Enter correct input";
    
    
    current = first;
    while (current!=NULL)
    {
          cout<<current->info<<" ";
          current = current->link;
    }
    getch();
    return 0;
}

I'm sure it was worth waiting 2 YEARS for you to show up with an answer :rolleyes:

In the meantime, feel free to swing by the "how to use code tags" readme thread at the top of the forum on how to post code which isn't an unindented mess.

Hey first I am not interested in this forum.
Second I come across this on the web and wanted to help him,
with that purpose I registered and wasted my time writing the code.
and "Don't teach your Grandfather how to suck an egg k?"

>Hey first I am not interested in this forum.
Then why did you post?

>Second I come across this on the web and wanted to help him, with that purpose I registered and wasted my
>time writing the code.
Yes, you certainly did waste your time, because I don't think someone is going to wait 2 years for an answer. The OP has already given up, or solved the problem. Next time look at the date before posting.

And by being so hasty to reply, you missed the forum announcements which explain things such as how to use code tags (as already mentioned by Salem), rules, and such. Believe me, it's not a good idea to try to suddenly reply to a thread without familiarizing yourself with the fourm and the community first.

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.