?
Solved

doubt in linked list

Posted on 2003-03-17
6
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
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.
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…
Six Sigma Control Plans

764 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