Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 08-02-2005, 10:56 PM   #1 (permalink)
Wise Old Owl
 
Join Date: Dec 2004
Location: New Delhi/Bangalore
Posts: 1,087
Default C++ Program Q.


Can anyone tell me the program for DELETION FROM THE END IN LINKED LIST DYNAMICALLY ???? I need it urgently, since day after tomorrow is my board practical. Here's the program for DELETION FROM THE BEGINING, but i m not able to modify it for DELETION from THE END

Quote:
#include<iostream.h>
#include<conio.h>
#include<stddef.h>
#include<process.h>

struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr,*rear;

node *create_new_node(int);
void delnode();
void insert(node*);
void display(node*);

void main()
{
clrscr();
start=NULL;
int inf;
char ch='y';
do
{
cout<<"\n\n Enter information for the new node : ";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout<<"\n\n Creating new node !!! Aborting....!!!!\n";
getch();
exit(0);
}
insert(newptr);
cout<<"\n\n Press Y to enter New Nodes, N to exit\n";
cin>>ch;
}while(ch=='Y'||ch=='y');
clrscr();
do
{
cout<<"\n\n The list now is : \n";
display(start);
getch();
cout<<"\n\n Want to delete first Node? (y/n) ...";
cin>>ch;
if(ch=='Y'||ch=='y')
delnode();
}while(ch=='y'||ch=='Y');
}

node *create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}

void insert(node *np)
{
if(start==NULL)
{
start=np;
rear=np;
}
else
{
rear->next=np;
rear=np;
}
}

void display(node *np)
{
while(np!=NULL)
{
cout<<np->info<<" -> ";
np=np->next;
}
cout<<"!!!\n";
}

void delnode()
{
if(start==NULL)
cout<<"\n\n UNDERFLOW";
else
{
ptr=start;
start=start->next;
delete ptr;
}
}
shwetanshu is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 09-02-2005, 09:02 AM   #2 (permalink)
Wise Old Owl
 
Join Date: Dec 2004
Location: New Delhi/Bangalore
Posts: 1,087
Default

Come on guys/gals help me out, its really urgent yaar, 2morrow is my boards practical yaar
shwetanshu is offline  
Old 09-02-2005, 10:24 AM   #3 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

Shwetanshu,
1. In order to delete from the end, you need to create a bidirectional list. In struct node, add another variable node *prev. Go through your code and make sure you incorporate the new pointer in all your initializations and operations. Then, you can delete nodes from the end. (I am not going to give you detailed code, coz that will defeat the purpose of the learning process...)
2. Please indent your code - so hard to read, i had to cut and paste and indent myself before I would read...
For example,
void main()
{
clrscr();
start=NULL;
int inf;
char ch='y';
do
{
cout<<"\n\n Enter information for the new node : ";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout<<"\n\n Creating new node !!! Aborting....!!!!\n";
getch();
exit(0);
}
insert(newptr);
cout<<"\n\n Press Y to enter New Nodes, N to exit\n";
cin>>ch;
}while(ch=='Y'||ch=='y');
clrscr();
do
{
cout<<"\n\n The list now is : \n";
display(start);
getch();
cout<<"\n\n Want to delete first Node? (y/n) ...";
cin>>ch;
if(ch=='Y'||ch=='y')
delnode();
}while(ch=='y'||ch=='Y');
}

Best of luck for your boards practical
Arun
sakumar79 is offline  
Old 09-02-2005, 10:58 AM   #4 (permalink)
Wise Old Owl
 
Join Date: Dec 2004
Location: New Delhi/Bangalore
Posts: 1,087
Default

well it was indented but like ur code upon posting it becomes very crappy as u can see. but man i want the whole code, i m tryin to make it 4 last 4 days, but of no use. Well anyway thanx for ur help
shwetanshu is offline  
Old 09-02-2005, 11:36 AM   #5 (permalink)
In The Zone
 
Join Date: Mar 2004
Location: Chennai
Posts: 331
Default

Quote:
void delnode()
{
if(start==NULL)

{
cout<<"\n\n UNDERFLOW";
exit(0);
}

else
{
ptr=start;

while((ptr->next)->next != NULL)
{

//just traverse;
ptr=ptr->next;

}

// once the above condition satisfies you are at last-//but-one node.now set ptr->next as null

ptr->next = NULL;
}
}
Im Not sure abt the code.. But thats the Logic.. Hope it helps..
SmoothCriminal is offline  
Old 09-02-2005, 01:50 PM   #6 (permalink)
Wise Old Owl
 
Join Date: Dec 2004
Location: New Delhi/Bangalore
Posts: 1,087
Default

Thanx a lot man, it worked.
shwetanshu is offline  
Closed Thread

Bookmarks

Thread Tools
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 Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:42 AM.


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

Search Engine Optimization by vBSEO 3.3.2