



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




#include
cout<<"6.Search Element"<<endl; cout<<"7.Display List"<<endl; cout<<"8.Reverse List"<<endl; cout<<"9.Exit"<<endl; cout<<"Enter your choice : "; cin>>c; switch(c) { case 1: cdl.insert_begin(); break; case 2: cdl.insert_end(); break; case 3: cdl.insert_pos(); break; case 4: cdl.delete_pos(); break; case 5: cdl.update(); break; case 6: cdl.search(); break; case 7: cdl.display(); break; case 8: cdl.reverse(); break; case 9: exit(1); default: cout<<"Wrong choice"<<endl; } } }
nod* circulardoublylist::create_node(int v) { count++; struct nod *t; t = new(struct nod); t->info = v; t->n = NULL; t->p = NULL; return t; }
if (pos == 1) { start = last = t; start->n = last->n = NULL; start->p = last->p = NULL; } else { cout<<"Position out of range"<<endl; count--; return; } } else { if (count < pos) { cout<<"Position out of range"<<endl; count--; return; } s = start; for (i = 1;i <= count;i++) { ptr = s; s = s->n; if (i == pos - 1) { ptr->n = t; t->p= ptr; t->n= s; s->p = t; cout<<"Element inserted"<<endl; break; } } } } void circulardoublylist::delete_pos() { int pos, i; nod *ptr, *s; if (start == last && start == NULL) { cout<<"List is empty, nothing to delete"<<endl; return; } cout<<endl<<"Enter the position of element to be deleted: "; cin>>pos; if (count < pos) { cout<<"Position out of range"<<endl; return; } s = start; if(pos == 1) { count--; last->n = s->n; s->n->p = last;
start = s->n; free(s); cout<<"Element Deleted"<<endl; return; } for (i = 0;i < pos - 1;i++ ) { s = s->n; ptr = s->p; } ptr->n = s->n; s->n->p = ptr; if (pos == count) { last = ptr; } count--; free(s); cout<<"Element Deleted"<<endl; } void circulardoublylist::update() { int v, i, pos; if (start == last && start == NULL) { cout<<"The List is empty, nothing to update"<<endl; return; } cout<<endl<<"Enter the position of node to be updated: "; cin>>pos; cout<<"Enter the new value: "; cin>>v; struct nod *s; if (count < pos) { cout<<"Position out of range"<<endl; return;} s = start; if (pos == 1) { s->info = v; cout<<"Node Updated"<<endl; return; } for (i=0;i < pos - 1;i++) { s = s->n; } s->info = v; cout<<"Node Updated"<<endl; } void circulardoublylist::search() { int pos = 0, v, i; bool flag = false; struct nod *s; if (start == last && start == NULL) {