Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

doubt in linked list

Posted on 2003-03-17
6
Medium Priority
?
328 Views
Last Modified: 2010-08-05
this is the code for linked list but i can not insert values at the middle.the use of pointer is also there.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *nest;
};
struct node *head=NULL;

void insert(int);
void display();
void insertatposition(int, int);

void main()
{
int num,j,k,pos;
char ch='y';
while(ch=='y'||ch=='Y')
{
clrscr();
printf("Enter one number to insert    :");
scanf("%d",&num);
insert(num);
printf("Successes fully inserted");
printf("\n\n\nWould u like to insert more ");
ch=getch();
}
printf("\n\nWould u like to insert the element at position \nif so insert the number and position  ");
scanf("%d  %d",&num,&pos);
insertatposition(num,pos);

display();
}
void insert(int n)
{
struct node *ptr= (struct node*)malloc(sizeof(struct node*));
ptr->data=n;
ptr->nest=NULL;
if(head==NULL)
     head=ptr;
else
{
     struct node *trav;
     for(trav=head;trav->nest!=NULL;trav=trav->nest);
     trav->nest=ptr;
}
}
void display()
{
int i,j;
struct node *trav;
//printf("Address of head  =%u",head);
//getch();
for(trav=head,i=1;trav!=NULL;trav=trav->nest,i++)
printf("\n\nNumber at position %d  (%u)=  %d",i,trav,trav->data);
getch();
}
void insertatposition(int num, int position)
{
int j;
char ch;
struct node *ptr=(struct node*) malloc(sizeof(struct node*));
ptr->data=num;
ptr->nest=NULL;
if(head==NULL && position==1)
{
     head=ptr;
}
else if(position==1)
{
struct node *temp;
temp=head;
head=ptr;
ptr->nest=temp;
}
if(head!=NULL&&position!=1)
{
struct node *trav;
for(trav=head,j=1;trav!=NULL;trav=trav->nest,j++)
{
     if(j==position)
     break;
}
if(position>j)
{
     printf("\n\nSorry Your Position is out of range\n Would u like to add the number at the  end of list Y/N");
     ch=getch();
     if(ch=='y'||ch=='Y')
     trav->nest=ptr;
     else
     printf("Sorry we can't insert your number");
}
else
{
struct node *tmp1;
struct node *tmp2;
tmp2=trav;
trav->nest=ptr;
for(tmp1=head,j=1;j<=position+1;j++,tmp1=tmp1->nest);
tmp1->nest=tmp2;
}
}
}

0
Comment
Question by:raonv
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
prady_21 earned 100 total points
ID: 8157487
Hello
I would suggest you to do this

if(head!=NULL&&position!=1)
{
   struct node *trav,*prev;
   
  for(trav=head,j=1;trav->nest!=NULL,j++)
  {
    prev=trav;
    trav=trav->nest;
    if(j==position)
    break;
  }
 
.
.
.
.  ( and then replace the following with else )
else
{
struct node *tmp1;
struct node *tmp2;
tmp2=trav;
trav->nest=ptr;
for(tmp1=head,j=1;j<=position+1;j++,tmp1=tmp1->nest);
tmp1->nest=tmp2;
}

( replace the above else with -- )
else {
   prev->nest=ptr;
   ptr->nest=trav;
}



NOTE* This is untested code, just change according to your needs
If any doubts do ask me
0
 
LVL 3

Expert Comment

by:prady_21
ID: 8157489
Hello
I would suggest you to do this

if(head!=NULL&&position!=1)
{
   struct node *trav,*prev;
   
  for(trav=head,j=1;trav->nest!=NULL,j++)
  {
    prev=trav;
    trav=trav->nest;
    if(j==position)
    break;
  }
 
.
.
.
.  ( and then replace the following with else )
else
{
struct node *tmp1;
struct node *tmp2;
tmp2=trav;
trav->nest=ptr;
for(tmp1=head,j=1;j<=position+1;j++,tmp1=tmp1->nest);
tmp1->nest=tmp2;
}

( replace the above else with -- )
else {
   prev->nest=ptr;
   ptr->nest=trav;
}



NOTE* This is untested code, just change according to your needs
If any doubts do ask me
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 8157528
>> scanf("%d  %d",&num,&pos);

Make it:

scanf ( "%d%d", &num, &pos ) ; // notice there is no space

Also,

>> else
>> {
>> struct node *tmp1;
>> struct node *tmp2;
>> tmp2=trav;
>> trav->nest=ptr;
>> for(tmp1=head,j=1;j<=position+1;j++,tmp1=tmp1->nest);
>> tmp1->nest=tmp2;
>> }

Make it:

else
{
  ptr -> nest = trav -> nest ;
  trav -> nest = ptr ;

} // end else

Hope that helps!

Mayank.

PS: Please get into the habit of indenting your code so that it is more readable.

0
 

Expert Comment

by:CleanupPing
ID: 9447055
raonv:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9472004
Recommendation: Split points between prady_21 and mayankeagle.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question