Posts

Showing posts from April 29, 2018

Circular Linked List

Source code: #include <iostream> using namespace std; struct node{ int num; node*next; };node*head=NULL; node*temp=NULL; void add(int n) { bool flag=false; node*newnode=new node; newnode->num=n; if(head==NULL) { head=newnode; newnode->next=head; flag=true; } else { temp=head; while(temp->next!=head) { temp=temp->next; } temp->next=newnode; newnode->next=head; flag=true; } if(flag==true) cout<<"value is entered \n"; else cout<<"Error \n"; } void Delete() { bool flag=false; node*temp2=NULL; if(head==NULL) { cout<<"list is empty \n"; } else if(head->next==head) { head->next=NULL; head=NULL; delete head; flag=true; } else { temp=head; while(temp->next!=head) { temp2=temp; temp=temp->next; } temp2->next=head; delete temp; flag=true; } if(flag==true) cout<<"value is Deleted \n"; else cout<<"E...

Data Structure Theory Assignment 2

Question#1 a)       What are the advantages of doubly linked list over singly linked list? Advantages over singly linked list 1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. In singly linked list, to delete a node, pointer to the previous node is needed. To get this  previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer. a)       What’s the difference between singly, doubly and circular linked list? Singly linked list A singly linked list is a linked list where the node contains some data and a pointer to the next node in the list It allows traversal only in one way It uses less memory per node (single pointer) Doubly A doubly linked list is complex type of linked list where the node contains some data and a pointer to the next as well as the...