From 84a264e3e67e458ddfbb789c43ea2c67712a6e60 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gupta <113535692+Vaishnavi2445@users.noreply.github.com> Date: Thu, 6 Oct 2022 09:39:06 +0530 Subject: [PATCH] Create create linked list --- create linked list | 110 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 create linked list diff --git a/create linked list b/create linked list new file mode 100644 index 0000000..425d59b --- /dev/null +++ b/create linked list @@ -0,0 +1,110 @@ +#include +using namespace std; + +class Node{ +public: +int val; +Node *next; +Node(int val) +{ + this->val=val; + this->next=nullptr; +} +}; + +class customlinkedlist { +public: +Node *head , *tail; +int size=0; +customlinkedlist(){ + size=0; + head=nullptr; + tail=nullptr; +} + + +void addlast(int value){ +Node *node=new Node(value); +if(size==0){ + size++; + head=node; + tail=node; + return; +} + tail->next=node; + tail=node; + size++; +} + +void addfirst(int value){ + Node *node=new Node(value); + if(size==0){ + addlast(value); + return; + } + node->next=head; + head=node; + size++; +} + +void removelast(){ + if(size==0){ + cout<<"linked list is empty"<next!=tail) + { + p=p->next; + } + p->next=nullptr; + tail=p; + +} + +void removefirst(){ + if(size==0){ + removelast(); + return; + } + if(size==1){ + removelast(); + } + size--; + head=head->next; +} + +void display(){ + Node *p=head; + while (p!=nullptr) + { + cout<val<<" "; + p=p->next; + } + cout<addlast(10); + c1->addlast(20); + c1->addlast(30); + c1->addlast(50); + c1->display(); + c1->addfirst(80); + c1->display(); + c1->removelast(); + c1->display(); + c1->removefirst(); + c1->display(); + +}