Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Bool Int: "Linked List Doesnot Exist"

The function del_anywhere deletes a node from a linked list given a node number. It first checks if the node number is 1, in which case it sets the head to the next node and deletes the current node. Otherwise, it iterates through the list with a previous and current pointer, deleting the current node and updating the pointers when the node number matches. It returns true if successful or false if not found.

Uploaded by

Hafsa Sheikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Bool Int: "Linked List Doesnot Exist"

The function del_anywhere deletes a node from a linked list given a node number. It first checks if the node number is 1, in which case it sets the head to the next node and deletes the current node. Otherwise, it iterates through the list with a previous and current pointer, deleting the current node and updating the pointers when the node number matches. It returns true if successful or false if not found.

Uploaded by

Hafsa Sheikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

bool List::del_anywhere(int node_no){

Node*current=NULL;
if(head){
current=head;
}
if(node_no==1){
head=head->Next;
current ->Next=NULL;
delete current;
return true;
}
else if(node_no>1){
Node*prev=NULL;
int node_count=1;

while(current!=NULL){
if(node_count==node_no&&current ==tail){
prev->Next=NULL;
tail=prev;
delete current;
}
else if(node_count==node_no&&current!=tail){
prev->Next=current->Next;
current->Next=NULL;
delete current;
return true;
}
node_count++;
prev=current;
current=current->Next;
}
}
return false;
}
void List::dipaly(){
if(!head){
cout<<"linked list doesnot exist"<<endl;
}
else{
int node=1;
Node *t=head;
while(t!=NULL){
cout<<"NODE: "<<node++<<endl;
cout<<"NAME: "<<t->name<<endl;
cout<<"AGE: "<<t->age<<endl;
cout<<"ID: "<<t->id<<endl;
cout<<"GPA: "<<t->gpa<<endl;
t=t->Next;
}
}
}
int _tmain(int argc, _TCHAR* argv[]){
List obj;
obj.create_node("Hania",14,18,3);
obj.create_node("Fatima",44,19,3);
obj.insert_anywhere("Tatheer",49,19,4,1);
obj.insert_at_beginning("Guriya",49,19,4);
obj.dipaly();
cout<<"ENTER NODE NUMBER FOR OPERATION"<<endl;
int node_no;
cin>>node_no;
obj.del_anywhere(node_no);
obj.dipaly();
system("pause");
return 0;
}

You might also like