Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
DoubleListInt
//g++ 7.4.0 //Double Linked List (int) // class code credit wikihut, internet #include <iostream> using namespace std; struct node { int data; node* next; node* prev; }; struct node* head; struct node* tail; void init() { head=NULL; tail=NULL; } class Double_List { public: void createlist(int n); void addbegin(int n); void display(); void reverse(); node* gethead() { return head;} node* gettail(){return tail;} void after(node *q, int v) { node* p = new node; p->data=v; p->next=q->next; q->next = p; p->prev= q; } void remove(); bool isempty() { if (head == NULL) return true; return false; } }; void Double_List::createlist(int n) { struct node *newnode; newnode=new node; newnode->data=n; if(head==NULL) { head=newnode; newnode->prev=NULL; newnode->next=NULL; tail=newnode; } else { newnode->prev=tail; tail->next=newnode; newnode->next=NULL; tail=newnode; } } void Double_List::addbegin(int n) { if(head==NULL) { cout<<" Empty list : \n"; return; } node *temp=new node; temp->data=n; temp->prev=NULL; temp->next=head; head->prev=temp; head=temp; } void Double_List::display() { node *q; if(head==NULL) { cout<<" List empty.. \n"; return; } q=head; cout<<"\nThe Double Linked list is \n"; while(q!=NULL) { cout<<q->data<<" <--> "; q=q->next; } } void Double_List::reverse() { node *r; if(tail==NULL) { cout<<"EMPTY list: \n"; return; } else { r=tail; while(r!=NULL) { cout<<r->data<<" <--> "; r=r->prev; } } } void Double_List:: remove() { if(isempty()) { cout<<"is empty"; } else if (head == tail){ delete head; head= tail =NULL; } else { node* temp, *temp2; temp2= tail; temp= head; while (temp->next!= NULL) { temp2= temp; temp=temp->next; } tail = temp2; tail->next=NULL; delete temp; while(temp->prev != NULL) { temp= temp2; temp2= temp2->prev;} head = temp; tail->prev= NULL; delete temp2; } } int main() { init(); Double_List dl; dl.createlist(10); dl.createlist(11); dl.createlist(12); dl.addbegin(9); dl.display(); cout<<endl; cout<<"The reverse is:\n"; dl.reverse(); dl.remove(); dl.after(dl.gethead()->next->next,11); dl.after(dl.gettail(),15); dl.display(); return 0; }
run
|
edit
|
history
|
help
0
single_digit
Newspaper
Synchro#2
prototype match
Empty C++ Script
ADVENTURE CODE CSCI 40
Wipro Problem 1: generate roll number
CutRod
threadpool01
MovConstrAssign