Untitled
unknown
plain_text
a year ago
1.3 kB
5
Indexable
Never
#include<iostream> using namespace std; class LinkedLL { public: int val; LinkedLL* next; LinkedLL(int val) { this->val=val; this->next=NULL; } }; LinkedLL* head=NULL; void insert_node(int position, int value) { // @params position, integer // @params value, integer LinkedLL* temp=new LinkedLL(value); LinkedLL* t=head; if(position==1) { temp->next = head; head=temp; return; } else { int k=1; while(k<(position-1) && t!=NULL) { k++; t=t->next; } if(t == NULL) return; temp->next=t->next; t->next=temp; } } void delete_node(int position) { // @params position, integer int k=1; LinkedLL* t=head; while(k<(position-1) && t!=NULL) { k++; t=t->next; } if(t == NULL || t->next == NULL) return; LinkedLL* s = t->next; t->next=t->next->next; s->next = NULL; } void print_ll() { // Output each element followed by a space LinkedLL* t=head; while(t!=NULL) { if(t->next==NULL) cout<<t->val; else cout<<t->val<<" "; t=t->next; } }