Last active
October 27, 2017 16:38
-
-
Save mirsahib/b34a2c7b8f4f200130b4201fb96f004f to your computer and use it in GitHub Desktop.
double linked list delete function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Mir Sahib | |
ID: 1510175 | |
Assignment_4 | |
*/ | |
#include <iostream> | |
using namespace std; | |
struct node{ | |
int data; | |
node* next; | |
node* prev; | |
}; | |
class List{ | |
private: | |
node* head; | |
node* temp; | |
node* curr; | |
public: | |
List(){ | |
head = NULL; | |
temp = NULL; | |
curr = NULL; | |
} | |
void add(int element){ | |
temp = new node; | |
temp->data = element; | |
temp->next = NULL; | |
temp->prev = NULL; | |
if(head==NULL){ | |
head = temp; | |
}else{ | |
curr = head; | |
while(curr->next!=NULL){ | |
curr=curr->next; | |
} | |
curr->next = temp; | |
temp->prev = curr; | |
} | |
} | |
void display(){ | |
curr=head; | |
while(curr!=NULL){ | |
if(curr->next==NULL){ | |
cout<<curr->data<<" ->NULL"; | |
}else{ | |
cout<<curr->data<<" -> "; | |
} | |
curr= curr->next; | |
} | |
} | |
void reverseDisplay(){ | |
curr=head; | |
while(curr->next!=NULL){ | |
curr=curr->next; | |
} | |
while(curr!=NULL){ | |
if(curr->prev==NULL){ | |
cout<<curr->data<<" ->NULL"; | |
}else{ | |
cout<<curr->data<<" -> "; | |
} | |
curr= curr->prev; | |
} | |
} | |
//frequency | |
int countOccurrent(int searchVal){ | |
int counter=0; | |
if(head == NULL){ | |
cout<<"List is empty"<<endl; | |
}else{ | |
curr = head; | |
while(curr!=NULL){ | |
if(curr->data==searchVal){ | |
counter++; | |
} | |
curr=curr->next; | |
} | |
} | |
return counter; | |
} | |
//insert after first occurrence | |
void insertAfter(int searchVal,int val){ | |
curr = head; | |
if(head==NULL){ | |
cout<<"List is empty"<<endl; | |
}else if(curr->data==searchVal){ | |
temp = new node; | |
temp->data = val; | |
temp->next = curr->next; | |
curr->next = temp; | |
temp->prev = curr; | |
}else{ | |
int counter=0; | |
while(curr!=NULL){ | |
if(curr->data==searchVal){ | |
temp = new node; | |
temp->data = val; | |
temp->next = curr->next; | |
curr->next->prev = temp; | |
curr->next = temp; | |
temp->prev = curr; | |
counter++; | |
break; | |
} | |
curr=curr->next; | |
} | |
if(counter==0)cout<<"Search value not found"<<endl; | |
} | |
} | |
//insert before first occurrence | |
void insertBefore(int searchVal, int Val){ | |
curr = head; | |
if(head==NULL){ | |
cout<<"List is empty"<<endl; | |
}else if(curr->data == searchVal){ | |
temp = new node; | |
temp->data = Val; | |
temp->next = curr; | |
curr->prev = temp; | |
temp->prev = NULL; | |
head = temp; | |
}else{ | |
int counter=0; | |
while(curr!=NULL){ | |
if(curr->data == searchVal){ | |
temp = new node; | |
temp->data = Val; | |
temp->next = curr; | |
temp->prev = curr->prev; | |
curr->prev->next = temp; | |
curr->prev = temp; | |
counter++; | |
break; | |
} | |
curr=curr->next; | |
} | |
if(counter==0)cout<<"\nSearch value not found"; | |
} | |
} | |
void deleteFirstOccurrence(int searchVal){ | |
curr=head; | |
if(head==NULL){ | |
cout<<"List is empty"<<endl; | |
}else if(curr->data == searchVal){ | |
curr->next->prev=NULL; | |
head = curr->next; | |
delete curr; | |
}else{ | |
int counter=0; | |
while(curr!=NULL){ | |
if(curr->data == searchVal){ | |
curr->prev->next = curr->next; | |
if(curr->next!=NULL){//if not last node | |
curr->next->prev = curr->prev; | |
} | |
delete curr; | |
counter++; | |
break; | |
} | |
curr=curr->next; | |
} | |
if(counter==0)cout<<"\nSearch value not found"<<endl; | |
} | |
} | |
void deleteLastOccurrence(int searchVal){ | |
curr=head; | |
if(head==NULL){ | |
cout<<"List is empty"<<endl; | |
}else{ | |
int counter=0; | |
while(curr!=NULL){ | |
if(curr->data==searchVal){ | |
temp = curr; | |
counter++; | |
} | |
curr=curr->next; | |
} | |
if(counter!=0){ | |
temp->prev->next=temp->next; | |
if(temp->next!=NULL){//if not last node | |
temp->next->prev = temp->prev; | |
} | |
delete temp; | |
}else{ | |
cout<<"\nSearch value not found"; | |
} | |
} | |
} | |
}; | |
int main() | |
{ | |
List myList; | |
for(int i=0;i<5;i++){ | |
myList.add(i+1); | |
} | |
// myList.insertBefore(3,43); | |
//myList.insertAfter(2,21); | |
cout<<"Initial List"<<endl; | |
myList.display(); | |
cout<<endl; | |
myList.reverseDisplay(); | |
cout<<"\nDelete first occurrence"; | |
myList.deleteFirstOccurrence(1); | |
cout<<endl; | |
myList.display(); | |
cout<<endl; | |
myList.reverseDisplay(); | |
cout<<"\nDelete Last occurrence"; | |
myList.deleteLastOccurrence(1); | |
cout<<endl; | |
myList.display(); | |
cout<<endl; | |
myList.reverseDisplay(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment