Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-08-2005, 06:29 PM   #1 (permalink)
Alpha Geek
 
Join Date: Mar 2005
Posts: 517
Default LInk List Problem , Immediate soln needed


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
mako_123 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 07-08-2005, 06:38 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default

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..
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by abhidev
- by Sarath
- by Krow

Advertisement




All times are GMT +5.5. The time now is 08:29 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2