From 18a02f1dd2c45a2eaed476b8100eb4d01f166283 Mon Sep 17 00:00:00 2001 From: UMAR SALMAN <32339184+umarsalman@users.noreply.github.com> Date: Sat, 27 Oct 2018 11:00:44 +0530 Subject: [PATCH] Create linklistumarsalman.cpp --- Linkedlist/linklistumarsalman.cpp | 170 ++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Linkedlist/linklistumarsalman.cpp diff --git a/Linkedlist/linklistumarsalman.cpp b/Linkedlist/linklistumarsalman.cpp new file mode 100644 index 0000000..9a72c9c --- /dev/null +++ b/Linkedlist/linklistumarsalman.cpp @@ -0,0 +1,170 @@ +#include +using namespace std; +struct node +{ + int data; + node *next; +}; +class list +{ + private: + node *head, *tail; + public: + list() + { + head=NULL; + tail=NULL; + } + void createnode(int value) + { + node *temp=new node; + temp->data=value; + temp->next=NULL; + if(head==NULL) + { + head=temp; + tail=temp; + temp=NULL; + } + else + { + tail->next=temp; + tail=temp; + } + } + void display() + { + node *temp=new node; + temp=head; + while(temp!=NULL) + { + cout<data<<"\t"; + temp=temp->next; + } + } + void insert_start(int value) + { + node *temp=new node; + temp->data=value; + temp->next=head; + head=temp; + } + void insert_position(int pos, int value) + { + node *pre=new node; + node *cur=new node; + node *temp=new node; + cur=head; + for(int i=1;inext; + } + temp->data=value; + pre->next=temp; + temp->next=cur; + } + void delete_first() + { + node *temp=new node; + temp=head; + head=head->next; + delete temp; + } + void delete_last() + { + node *current=new node; + node *previous=new node; + current=head; + while(current->next!=NULL) + { + previous=current; + current=current->next; + } + tail=previous; + previous->next=NULL; + delete current; + } + void delete_position(int pos) + { + node *current=new node; + node *previous=new node; + current=head; + for(int i=1;inext; + } + previous->next=current->next; + } + + void search(int key) + { + int pos=1; + bool found=false; + node *temp = new node; + temp=head; + while(temp!=NULL) + { + if(temp->data==key) + { + cout<<"\nFound at position:"<<" "<next; + pos++; + + } + if(found!=true){cout<<"\nElement doesn't exist in LinkedList";} + } +}; +int main() +{ + list obj; + obj.createnode(25); + obj.createnode(50); + obj.createnode(90); + obj.createnode(40); + cout<<"\n--------------------------------------------------\n"; + cout<<"---------------Displaying All nodes---------------"; + cout<<"\n--------------------------------------------------\n"; + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"-----------------Inserting At End-----------------"; + cout<<"\n--------------------------------------------------\n"; + obj.createnode(55); + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"----------------Inserting At Start----------------"; + cout<<"\n--------------------------------------------------\n"; + obj.insert_start(50); + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"-------------Inserting At Particular--------------"; + cout<<"\n--------------------------------------------------\n"; + obj.insert_position(5,60); + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"----------------Deleting At Start-----------------"; + cout<<"\n--------------------------------------------------\n"; + obj.delete_first(); + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"-----------------Deleing At End-------------------"; + cout<<"\n--------------------------------------------------\n"; + obj.delete_last(); + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"--------------Deleting At Particular--------------"; + cout<<"\n--------------------------------------------------\n"; + obj.delete_position(4); + obj.display(); + cout<<"\n--------------------------------------------------\n"; + cout<<"--------------Searching for an element--------------"; + cout<<"\n--------------------------------------------------\n"; + obj.search(50); + system("pause"); + return 0; +}