Docsity

Data Structure and Algorithm Lab Task 05, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Lab #5 Data structure & algorithm
1
Lab Task 5
Lab Task:
1. Modify the Doubly linked list to create a Doubly Circular Linked List. Update all the functions
accordingly for Insertion, Deletion and Display.
2. Write a function to display size of doubly linked list in task 1 and display all the elements in
reverse order
3. Add Search function to the Doubly Circular Linked List while will receive a parameter containing
the value that will be searched in the list. Do implement a stopping mechanism as there will be
no NULL link in the circular list.
#include<iostream>
using namespace std;
using namespace std;
struct nod {
int info;
struct nod *n;
struct nod *p;
}*start, *last;
int count = 0;
class circulardoublylist {
public:
nod *create_node(int);
void insert_begin();
void insert_end();
void insert_pos();
void delete_pos();
void search();
void update();
void display();
void reverse();
circulardoublylist() {
start = NULL;
last = NULL;
}
};
int main() {
int c;
circulardoublylist cdl;
while (1) {
cout<<"1.Insert at Beginning"<<endl;
cout<<"2.Insert at End"<<endl;
cout<<"3.Insert at Position"<<endl;
cout<<"4.Delete at Position"<<endl;
cout<<"5.Update Node"<<endl;
pf3
pf4
pf5

Partial preview of the text

Download Data Structure and Algorithm Lab Task 05 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Lab Task 5

Lab Task:

  1. Modify the Doubly linked list to create a Doubly Circular Linked List. Update all the functions accordingly for Insertion, Deletion and Display.
  2. Write a function to display size of doubly linked list in task 1 and display all the elements in reverse order
  3. Add Search function to the Doubly Circular Linked List while will receive a parameter containing the value that will be searched in the list. Do implement a stopping mechanism as there will be no NULL link in the circular list.

#include using namespace std; using namespace std; struct nod { int info; struct nod *n; struct nod p; }start, *last; int count = 0; class circulardoublylist { public: nod *create_node(int); void insert_begin(); void insert_end(); void insert_pos(); void delete_pos(); void search(); void update(); void display(); void reverse(); circulardoublylist() { start = NULL; last = NULL; } }; int main() { int c; circulardoublylist cdl; while (1) { cout<<"1.Insert at Beginning"<<endl; cout<<"2.Insert at End"<<endl; cout<<"3.Insert at Position"<<endl; cout<<"4.Delete at Position"<<endl; cout<<"5.Update Node"<<endl;

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) {