Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Last active September 9, 2017 08:34
Show Gist options
  • Save mirsahib/3771eed7bba1b6f51a512f8bf608a20a to your computer and use it in GitHub Desktop.
Save mirsahib/3771eed7bba1b6f51a512f8bf608a20a to your computer and use it in GitHub Desktop.
//============================================================================
// Name : Doubly_LinkedList.cpp
// Author : Mir Sahib
// Version :
// Copyright : MIT License
// Description : Doubly Linked List implementation
//============================================================================
#include <iostream>
#include "List.h"
using namespace std;
List::List(){
head = NULL;
temp = NULL;
curr = NULL;
}
node *List::get(int index){
temp = new node;
if(index>size()){
temp = NULL;
}else{
curr = head;
for(int i=0;i<index-2;i++){
curr=curr->next;
}
temp = curr->next;
}
return temp;
}
node *List::getFirst(){
return head;
}
node *List::getLast(){
temp = head;
while(temp!=NULL){
temp = temp->next;
}
return temp;
}
//insert function
void List::add(int index,int newData){
if(index>size()){
cout<<"Invalid Index"<<endl;
}else{
temp = new node;
temp->data = newData;
temp->prev = NULL;
temp->next = NULL;
curr = head;
for(int i=0;i<index-2;i++){
curr = curr->next;
}
temp->next = curr->next;
temp->prev = curr;
curr->next = temp;
}
}
void List::addFirst(int newData){
temp = new node;
temp->data = newData;
temp->prev = NULL;
temp->next = NULL;
if(isEmpty()){
head = temp;
}else{
temp->next = head;
head->prev = temp;
head = temp;
}
}
void List::addLast(int newData){
temp = new node;
temp->data = newData;
temp->prev = NULL;
temp->next = NULL;
if(isEmpty()){
head = temp;
}else{
curr = head;
while(curr->next!=NULL){
curr = curr->next;
}
curr->next = temp;
temp->prev = curr;
}
}
//delete function
void List::remove(int index){
if(index>size()){
cout<<"Invalid Index"<<endl;
}else{
curr = head;
for(int i=0;i<index-2;i++){
curr = curr->next;
}
temp = curr->next;
curr->next = temp->next;
temp->next->prev = curr;
delete temp;
}
}
void List::removeFirst(){
if(!isEmpty()){
curr = head;
curr->next->prev=NULL;
head = curr->next;
delete curr;
}else{
cout<<"The List is empty"<<endl;
}
}
void List::removeLast(){
if(!isEmpty()){
curr = head;
while(curr->next!=NULL){
curr=curr->next;
}
curr->prev->next=NULL;
delete curr;
}else{
cout<<"The List is empty"<<endl;
}
}
//print function
void List::print(){
curr = head;
cout<<"List is: ";
while(curr!=NULL){
cout<<curr->data<<" ";
curr=curr->next;
}
cout<<endl;
}
//miscellaneous function
int List::size(){
temp = head;
int i=0;
while(temp!=NULL){
i++;
temp = temp->next;
}
return i;
}
bool List::isEmpty(){
if(head==NULL){
return true;
}
return false;
}
int main() {
List myList;
int limit,num;
cout<<"How many number to print"<<endl;
cin>>limit;
for(int i=0;i<limit;i++){
cout<<"Enter number"<<endl;
cin>>num;
myList.addFirst(num);
//myList.addLast(num);
myList.print();
}
myList.addLast(14);
myList.addLast(15);
myList.addLast(16);
myList.addLast(17);
myList.print();
cout<<"Adding element at index 4"<<endl;
myList.add(4,56);
myList.print();
cout<<"Number at index 5 : "<<myList.get(5)->data<<endl;
cout<<"Delete first"<<endl;
myList.removeFirst();
myList.print();
cout<<"Delete Last"<<endl;
myList.removeLast();
myList.print();
cout<<"Delete index 3: "<<endl;
myList.remove(3);
myList.print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment