LInk List Problem , Immediate soln needed

Status
Not open for further replies.

mako_123

Ambassador of Buzz
Hi guys , plz see this code for me


The problem lies in the Inserting of node portion.



#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

struct linklist
{ int data;
linklist *next;
};
typedef struct linklist node;

void create(node *start);
void insert(node *start);
void display(node *start);
void inter(node *start);


void main()
{clrscr();
node *start;
int ch=0;
while(ch!=5)
{
cout<<"\n\n 1 . Create the linklist ";
cout<<"\n 2 . INsert in Link List";
cout<<"\n 3 . Display ";
cout<<"\n 4 . Interchange two nodes";
cout<<"\n 5 . Exit ";
cout<<"\n\n our Choice (1-5)";
cin>>ch;

switch(ch)
{
case 1:
create(start);
break;

case 2:
insert(start);
break;

case 3:
display(start);
break;

case 4:
inter(start);
break;

case 5:
exit(0);
break;

default:
cout<<"\n Enter valid choice";
break;
}
}
getch();

}

void create(node *start)
{
//start = new node;
int val=0;
while(val != -1)
{
cout<<"\n Enter the data to insert ( -1 to quit )";
cin>>val;
if(val == -1)
{
break;
}
start->data=val;
start->next=new node;
start=start->next;
}
start->next=NULL;
}

void insert ( node *start )
{ node *temp;
int val,pos;
temp=new node;
cout<<"\n Enter the Data you want to Insert \t";
cin>>val;

cout<<"\n Enter the position where you want to insert (0 to insert in the beginning \t";
cin>>pos;

if (pos==0)
{ temp->data=val;
temp->next=start;
start=temp;
}
else
{
for(int i=1;i<pos;i++)
{ start=start->next;
if(start->next==NULL)
{cout<<" INcorrect position ";
break;
}
}
temp->data=val;
temp->next=start->next;
start->next=temp;
}

}

void display ( node *start )
{ cout<<"\n The elements are \n";
while( start->next != NULL)
{cout<<"\t"<<start->data;
start=start->next;
}
}

void inter(node *start)
{ node *p,*q;
p=q=start;
int a,b,i,temp;
cout<<"\n Enter the position you want to interchange \n";
cin>>a;
cin>>b;

for(i=1;i<a;i++)
p=p->next;

for(i=1;i<b;i++)
q=q->next;

temp=p->data;
p->data=q->data;
q->data=temp;

}

The programs runs fine but i am unable to add a node to the beginning although the logic i have used is correct . Kindly help please
 

QwertyManiac

Commander in Chief
Hmm, i compiled it but i get some memory couldnt be read error that i've been gettin in the past 2 months,
will try it out in my home os soon..
 
Status
Not open for further replies.
Top Bottom