Skip to content

Commit

Permalink
Deletion at middle of linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilatkan committed Feb 20, 2020
1 parent e15ace9 commit 4dbdff5
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions LLdeletionatmiddle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *next;

node(int d)
{
data=d;
next=NULL;
}
};




void insertathead(node*&head,int data)
{
node*n=new node(data);
n->next=head;
head=n;
}

void deletionatmiddle(node*&head,int key)
{
if(head==NULL)
{
return;
}

//node*temp=NULL;
node*prev=NULL;
node*cur=head;
while(cur->data!=key)
{
prev=cur;
cur=cur->next;
}
prev->next=cur->next;
delete cur;
}




void print(node*head)
{
while(head!=NULL)
{
cout<<head->data<<"->";
head=head->next;
}
}

int main()
{
node*head=NULL;
insertathead(head,8);
insertathead(head,17);
insertathead(head,5);
insertathead(head,3);
deletionatmiddle(head,5);
print(head);

return 0;


}

0 comments on commit 4dbdff5

Please sign in to comment.