DIGIT-ARC read-only archive · snapshot: July 23, 2026

C++ Program Q.

6 posts · originally from https://geek.digit.in/community/threads/c-program-q.5515/

S
shwetanshu Original post #1
Feb 8, 2005 at 10:56 PM
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

#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;
}
}
S
Feb 9, 2005 at 9:02 AM
Come on guys/gals help me out, its really urgent yaar, 2morrow is my boards practical yaar
S
Feb 9, 2005 at 10:24 AM
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
S
Feb 9, 2005 at 10:58 AM
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
S
Feb 9, 2005 at 11:36 AM
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..
S
Feb 9, 2005 at 1:50 PM
Thanx a lot man, it worked.