Skip to content

Commit

Permalink
updated the linked list in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Sep 9, 2018
1 parent 4afe41d commit 187f3eb
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions C++/Data-Structures/LISTS/LINKED-LIST/SINGLE/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,48 @@ class list
// cout << size()-1-num <<endl;
return value_from_front(s-1-num);
}
void delete_given_node()
{

/***************************************/
/*===========> DELETE GIVEN NODE <=======*/
void delete_given_node(string val)
{
if(head==NULL)
{
return;
}
else if(head->next==NULL)
{
if(head->str==val)
{
head=NULL;
s--;
}
}
else if(head->str==val) //for deleting from head;
{
node* tmp = head;
head=head->next;
delete(tmp);
s--;
}
else
{
node* current=head;
node* prev=head;
while(current->next!=NULL)
{
if(current->str==val)
{
break;
}
prev=current;
current=current->next;
}
node* tmp=current;
prev->next=current->next;
delete(tmp);
s--;
}
}

/***************************************/
Expand Down Expand Up @@ -185,6 +224,8 @@ int main()
A.insert_tail("is");
A.insert_tail("insane");
A.insert_tail("rock");
A.insert_tail("clown");
A.insert_tail("stupid");
A.print_list();

A.delete_head();
Expand All @@ -202,4 +243,6 @@ int main()
A.value_from_last(100);

cout << A.size() << endl;
A.delete_given_node("insane");
A.print_list();
}

0 comments on commit 187f3eb

Please sign in to comment.